oxidite_queue/
stats.rs

1use std::sync::Arc;
2use tokio::sync::RwLock;
3use serde::{Deserialize, Serialize};
4
5/// Job queue statistics
6#[derive(Debug, Clone, Serialize, Deserialize, Default)]
7pub struct QueueStats {
8    pub total_enqueued: u64,
9    pub total_processed: u64,
10    pub total_failed: u64,
11    pub total_retried: u64,
12    pub pending_count: u64,
13    pub running_count: u64,
14    pub dead_letter_count: u64,
15}
16
17/// Thread-safe statistics tracker
18#[derive(Clone)]
19pub struct StatsTracker {
20    stats: Arc<RwLock<QueueStats>>,
21}
22
23impl StatsTracker {
24    pub fn new() -> Self {
25        Self {
26            stats: Arc::new(RwLock::new(QueueStats::default())),
27        }
28    }
29
30    pub async fn increment_enqueued(&self) {
31        let mut stats = self.stats.write().await;
32        stats.total_enqueued += 1;
33        stats.pending_count += 1;
34    }
35
36    pub async fn increment_processed(&self) {
37        let mut stats = self.stats.write().await;
38        stats.total_processed += 1;
39        if stats.running_count > 0 {
40            stats.running_count -= 1;
41        }
42    }
43
44    pub async fn increment_failed(&self) {
45        let mut stats = self.stats.write().await;
46        stats.total_failed += 1;
47        if stats.running_count > 0 {
48            stats.running_count -= 1;
49        }
50    }
51
52    pub async fn increment_retried(&self) {
53        let mut stats = self.stats.write().await;
54        stats.total_retried += 1;
55    }
56
57    pub async fn increment_dead_letter(&self) {
58        let mut stats = self.stats.write().await;
59        stats.dead_letter_count += 1;
60    }
61
62    pub async fn mark_running(&self) {
63        let mut stats = self.stats.write().await;
64        if stats.pending_count > 0 {
65            stats.pending_count -= 1;
66        }
67        stats.running_count += 1;
68    }
69
70    pub async fn get_stats(&self) -> QueueStats {
71        self.stats.read().await.clone()
72    }
73
74    pub async fn reset(&self) {
75        let mut stats = self.stats.write().await;
76        *stats = QueueStats::default();
77    }
78}
79
80impl Default for StatsTracker {
81    fn default() -> Self {
82        Self::new()
83    }
84}