use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct BatchOperationResult {
pub total: usize,
pub successful: usize,
pub failed: usize,
pub succeeded_ids: Vec<Uuid>,
pub failed_ids: Vec<(Uuid, String)>,
}
impl BatchOperationResult {
pub fn new() -> Self {
Self {
total: 0,
successful: 0,
failed: 0,
succeeded_ids: Vec::new(),
failed_ids: Vec::new(),
}
}
pub fn record_success(&mut self, task_id: Uuid) {
self.total += 1;
self.successful += 1;
self.succeeded_ids.push(task_id);
}
pub fn record_failure(&mut self, task_id: Uuid, error: String) {
self.total += 1;
self.failed += 1;
self.failed_ids.push((task_id, error));
}
pub fn all_succeeded(&self) -> bool {
self.failed == 0 && self.total > 0
}
pub fn has_failures(&self) -> bool {
self.failed > 0
}
pub fn success_rate(&self) -> f64 {
if self.total == 0 {
0.0
} else {
self.successful as f64 / self.total as f64
}
}
pub fn failure_rate(&self) -> f64 {
1.0 - self.success_rate()
}
}
impl Default for BatchOperationResult {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for BatchOperationResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Batch Operation Result:")?;
writeln!(f, " Total: {}", self.total)?;
writeln!(
f,
" Successful: {} ({:.1}%)",
self.successful,
self.success_rate() * 100.0
)?;
writeln!(
f,
" Failed: {} ({:.1}%)",
self.failed,
self.failure_rate() * 100.0
)?;
if !self.failed_ids.is_empty() {
writeln!(f, " Failed IDs:")?;
for (id, error) in self.failed_ids.iter().take(5) {
writeln!(f, " {} - {}", &id.to_string()[..8], error)?;
}
if self.failed_ids.len() > 5 {
writeln!(f, " ... and {} more", self.failed_ids.len() - 5)?;
}
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct PoolStats {
pub backend_type: String,
pub connection_mode: String,
pub is_connected: bool,
}
impl std::fmt::Display for PoolStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"PoolStats: type={}, mode={}, connected={}",
self.backend_type, self.connection_mode, self.is_connected
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StateCount {
pub total: usize,
pub pending: usize,
pub started: usize,
pub success: usize,
pub failure: usize,
pub retry: usize,
pub revoked: usize,
pub not_found: usize,
}
impl StateCount {
pub fn percentages(&self) -> StatePercentages {
let total = self.total as f64;
if total == 0.0 {
return StatePercentages::default();
}
StatePercentages {
pending: (self.pending as f64 / total) * 100.0,
started: (self.started as f64 / total) * 100.0,
success: (self.success as f64 / total) * 100.0,
failure: (self.failure as f64 / total) * 100.0,
retry: (self.retry as f64 / total) * 100.0,
revoked: (self.revoked as f64 / total) * 100.0,
not_found: (self.not_found as f64 / total) * 100.0,
}
}
}
impl std::fmt::Display for StateCount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "StateCount (Total: {}):", self.total)?;
writeln!(f, " Pending: {}", self.pending)?;
writeln!(f, " Started: {}", self.started)?;
writeln!(f, " Success: {}", self.success)?;
writeln!(f, " Failure: {}", self.failure)?;
writeln!(f, " Retry: {}", self.retry)?;
writeln!(f, " Revoked: {}", self.revoked)?;
write!(f, " Not Found: {}", self.not_found)?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct StatePercentages {
pub pending: f64,
pub started: f64,
pub success: f64,
pub failure: f64,
pub retry: f64,
pub revoked: f64,
pub not_found: f64,
}
impl Default for StatePercentages {
fn default() -> Self {
Self {
pending: 0.0,
started: 0.0,
success: 0.0,
failure: 0.0,
retry: 0.0,
revoked: 0.0,
not_found: 0.0,
}
}
}
impl std::fmt::Display for StatePercentages {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "State Percentages:")?;
writeln!(f, " Pending: {:.1}%", self.pending)?;
writeln!(f, " Started: {:.1}%", self.started)?;
writeln!(f, " Success: {:.1}%", self.success)?;
writeln!(f, " Failure: {:.1}%", self.failure)?;
writeln!(f, " Retry: {:.1}%", self.retry)?;
writeln!(f, " Revoked: {:.1}%", self.revoked)?;
write!(f, " Not Found: {:.1}%", self.not_found)?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskSummary {
pub total: usize,
pub found: usize,
pub not_found: usize,
pub pending: usize,
pub started: usize,
pub success: usize,
pub failure: usize,
pub retry: usize,
pub revoked: usize,
}
impl TaskSummary {
pub fn completion_rate(&self) -> f64 {
if self.total == 0 {
0.0
} else {
(self.success + self.failure + self.revoked) as f64 / self.total as f64
}
}
pub fn success_rate(&self) -> f64 {
if self.total == 0 {
0.0
} else {
self.success as f64 / self.total as f64
}
}
pub fn failure_rate(&self) -> f64 {
if self.total == 0 {
0.0
} else {
self.failure as f64 / self.total as f64
}
}
pub fn all_complete(&self) -> bool {
self.pending == 0 && self.started == 0 && self.retry == 0
}
pub fn has_failures(&self) -> bool {
self.failure > 0
}
}
impl std::fmt::Display for TaskSummary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "TaskSummary:")?;
writeln!(f, " Total: {}", self.total)?;
writeln!(f, " Found: {} | Not Found: {}", self.found, self.not_found)?;
writeln!(
f,
" Pending: {} | Started: {} | Retry: {}",
self.pending, self.started, self.retry
)?;
writeln!(
f,
" Success: {} | Failure: {} | Revoked: {}",
self.success, self.failure, self.revoked
)?;
writeln!(
f,
" Completion Rate: {:.1}%",
self.completion_rate() * 100.0
)?;
writeln!(f, " Success Rate: {:.1}%", self.success_rate() * 100.0)?;
write!(f, " Failure Rate: {:.1}%", self.failure_rate() * 100.0)?;
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct BackendStats {
pub task_key_count: usize,
pub chord_key_count: usize,
pub total_keys: usize,
pub used_memory_bytes: u64,
}
impl std::fmt::Display for BackendStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"BackendStats: {} task keys, {} chord keys, {:.2} MB memory",
self.task_key_count,
self.chord_key_count,
self.used_memory_bytes as f64 / 1024.0 / 1024.0
)
}
}
pub mod ttl {
use std::time::Duration;
pub const TEMPORARY: Duration = Duration::from_secs(3600);
pub const SHORT: Duration = Duration::from_secs(6 * 3600);
pub const SUCCESS: Duration = Duration::from_secs(86400);
pub const MEDIUM: Duration = Duration::from_secs(3 * 86400);
pub const FAILURE: Duration = Duration::from_secs(7 * 86400);
pub const LONG: Duration = Duration::from_secs(30 * 86400);
pub const MAXIMUM: Duration = Duration::from_secs(90 * 86400);
}
pub mod batch_size {
pub const SMALL: usize = 10;
pub const MEDIUM: usize = 50;
pub const LARGE: usize = 100;
pub const EXTRA_LARGE: usize = 500;
pub const MAXIMUM: usize = 1000;
}