use crate::{Slot, TaskPhase};
pub const DEFAULT_LIMIT: usize = 100;
pub const MAX_LIMIT: usize = 1000;
#[derive(Debug, Clone)]
pub struct TaskQuery {
status: Vec<TaskPhase>,
slot: Option<Slot>,
offset: usize,
limit: usize,
}
impl Default for TaskQuery {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct TaskPage<T> {
pub items: Vec<T>,
pub total: usize,
}
impl TaskQuery {
#[inline]
pub fn new() -> Self {
Self {
limit: DEFAULT_LIMIT,
status: Vec::new(),
slot: None,
offset: 0,
}
}
#[inline]
pub fn with_slot(mut self, slot: impl Into<Slot>) -> Self {
self.slot = Some(slot.into());
self
}
#[inline]
pub fn with_status(mut self, status: TaskPhase) -> Self {
if !self.status.contains(&status) {
self.status.push(status);
}
self
}
#[inline]
pub fn with_active(self) -> Self {
self.with_status(TaskPhase::Pending)
.with_status(TaskPhase::Running)
}
#[inline]
pub fn with_terminal(self) -> Self {
self.with_status(TaskPhase::Succeeded)
.with_status(TaskPhase::Exhausted)
.with_status(TaskPhase::Canceled)
.with_status(TaskPhase::Timeout)
.with_status(TaskPhase::Failed)
}
#[inline]
pub fn with_limit(mut self, limit: usize) -> Self {
self.limit = limit.min(MAX_LIMIT);
self
}
#[inline]
pub fn with_offset(mut self, offset: usize) -> Self {
self.offset = offset;
self
}
#[inline]
pub fn matches_phase(&self, phase: &TaskPhase) -> bool {
self.status.is_empty() || self.status.contains(phase)
}
#[inline]
pub fn limit(&self) -> usize {
self.limit
}
#[inline]
pub fn offset(&self) -> usize {
self.offset
}
#[inline]
pub fn slot(&self) -> Option<&Slot> {
self.slot.as_ref()
}
#[inline]
pub fn status_filters(&self) -> &[TaskPhase] {
&self.status
}
}