diff --git a/contracts/warp-controller/src/query/job.rs b/contracts/warp-controller/src/query/job.rs index fdf01458..3c8be7db 100644 --- a/contracts/warp-controller/src/query/job.rs +++ b/contracts/warp-controller/src/query/job.rs @@ -35,6 +35,23 @@ pub fn query_jobs(deps: Deps, env: Env, data: QueryJobsMsg) -> StdResult query_jobs_by_ids(deps, env, ids, job_status), + QueryJobsMsg { + active: _, + name, + owner: Some(owner), + job_status, + start_after, + limit: _, + .. + } => query_jobs_by_owner( + deps, + env, + name, + owner, + job_status, + start_after.map(|i| (i._1.u64())), + page_size as usize, + ), QueryJobsMsg { active: _, name, @@ -130,3 +147,46 @@ pub fn query_jobs_by_reward( total_count: infos.len() as u32, }) } + +pub fn query_jobs_by_owner( + deps: Deps, + env: Env, + name: Option, + owner: Addr, + job_status: Option, + start_after: Option, + limit: usize, +) -> StdResult { + let start = start_after.map(Bound::exclusive); + let map = if job_status.is_some() && job_status.clone().unwrap() != JobStatus::Pending { + FINISHED_JOBS() + } else { + PENDING_JOBS() + }; + let infos = map + .idx + .owner + .prefix(owner.to_string()) + .range(deps.storage, start, None, Order::Ascending) + .filter(|h| { + resolve_filters( + deps, + env.clone(), + h.as_ref().unwrap().clone().1, + name.clone(), + None, + job_status.clone(), + ) + }) + .take(limit) + .collect::>>()?; + + let mut jobs = vec![]; + for info in infos.clone() { + jobs.push(info.1); + } + Ok(JobsResponse { + jobs, + total_count: infos.len(), + }) +} diff --git a/contracts/warp-controller/src/state.rs b/contracts/warp-controller/src/state.rs index abeab1f2..8ba9018d 100644 --- a/contracts/warp-controller/src/state.rs +++ b/contracts/warp-controller/src/state.rs @@ -11,11 +11,12 @@ use crate::ContractError; pub struct JobIndexes<'a> { pub reward: UniqueIndex<'a, (u128, u64), Job>, pub publish_time: MultiIndex<'a, u64, Job, u64>, + pub owner: MultiIndex<'a, String, Job, u64>, } impl IndexList for JobIndexes<'_> { fn get_indexes(&'_ self) -> Box> + '_> { - let v: Vec<&dyn Index> = vec![&self.reward, &self.publish_time]; + let v: Vec<&dyn Index> = vec![&self.reward, &self.publish_time, &self.owner]; Box::new(v.into_iter()) } } @@ -32,6 +33,11 @@ pub fn PENDING_JOBS<'a>() -> IndexedMap<'a, u64, Job, JobIndexes<'a>> { "pending_jobs_v6", "pending_jobs__publish_timestamp_v6", ), + owner: MultiIndex::new( + |_pk, job| job.owner.to_string(), + "pending_jobs_v6", + "pending_jobs__owner_v6", + ), }; IndexedMap::new("pending_jobs_v6", indexes) } @@ -48,6 +54,11 @@ pub fn FINISHED_JOBS<'a>() -> IndexedMap<'a, u64, Job, JobIndexes<'a>> { "finished_jobs_v6", "finished_jobs__publish_timestamp_v6", ), + owner: MultiIndex::new( + |_pk, job| job.owner.to_string(), + "finished_jobs_v6", + "finished_jobs__owner_v6", + ), }; IndexedMap::new("finished_jobs_v6", indexes) }