celers-backend-redis 0.2.0

Redis result backend for CeleRS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Statistics, batch operation results, and constant configurations
//!
//! This module contains:
//! - [`BatchOperationResult`] for detailed success/failure tracking
//! - [`PoolStats`] for connection pool statistics
//! - [`StateCount`] and [`StatePercentages`] for task state distribution
//! - [`TaskSummary`] for collection-level task statistics
//! - [`BackendStats`] for backend-level statistics
//! - [`ttl`] module with recommended TTL durations
//! - [`batch_size`] module with recommended batch sizes

use uuid::Uuid;

/// Batch operation result with detailed success/failure tracking
#[derive(Debug, Clone)]
pub struct BatchOperationResult {
    /// Total number of operations attempted
    pub total: usize,
    /// Number of successful operations
    pub successful: usize,
    /// Number of failed operations
    pub failed: usize,
    /// Task IDs that succeeded
    pub succeeded_ids: Vec<Uuid>,
    /// Task IDs that failed with error messages
    pub failed_ids: Vec<(Uuid, String)>,
}

impl BatchOperationResult {
    /// Create a new empty batch result
    pub fn new() -> Self {
        Self {
            total: 0,
            successful: 0,
            failed: 0,
            succeeded_ids: Vec::new(),
            failed_ids: Vec::new(),
        }
    }

    /// Record a successful operation
    pub fn record_success(&mut self, task_id: Uuid) {
        self.total += 1;
        self.successful += 1;
        self.succeeded_ids.push(task_id);
    }

    /// Record a failed operation
    pub fn record_failure(&mut self, task_id: Uuid, error: String) {
        self.total += 1;
        self.failed += 1;
        self.failed_ids.push((task_id, error));
    }

    /// Check if all operations succeeded
    pub fn all_succeeded(&self) -> bool {
        self.failed == 0 && self.total > 0
    }

    /// Check if any operations failed
    pub fn has_failures(&self) -> bool {
        self.failed > 0
    }

    /// Get success rate (0.0 to 1.0)
    pub fn success_rate(&self) -> f64 {
        if self.total == 0 {
            0.0
        } else {
            self.successful as f64 / self.total as f64
        }
    }

    /// Get failure rate (0.0 to 1.0)
    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(())
    }
}

/// Connection pool statistics
#[derive(Debug, Clone)]
pub struct PoolStats {
    /// Backend type
    pub backend_type: String,
    /// Connection mode (multiplexed, pooled, etc.)
    pub connection_mode: String,
    /// Whether connected to Redis
    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
        )
    }
}

/// Count of tasks by state
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StateCount {
    /// Total number of tasks queried
    pub total: usize,
    /// Number of pending tasks
    pub pending: usize,
    /// Number of started tasks
    pub started: usize,
    /// Number of successful tasks
    pub success: usize,
    /// Number of failed tasks
    pub failure: usize,
    /// Number of tasks being retried
    pub retry: usize,
    /// Number of revoked tasks
    pub revoked: usize,
    /// Number of tasks not found
    pub not_found: usize,
}

impl StateCount {
    /// Get the percentage of tasks in each state
    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(())
    }
}

/// Percentage breakdown of task states
#[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(())
    }
}

/// Summary statistics for a collection of tasks
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskSummary {
    /// Total number of tasks queried
    pub total: usize,
    /// Number of tasks found in backend
    pub found: usize,
    /// Number of tasks not found
    pub not_found: usize,
    /// Number of pending tasks
    pub pending: usize,
    /// Number of started tasks
    pub started: usize,
    /// Number of successful tasks
    pub success: usize,
    /// Number of failed tasks
    pub failure: usize,
    /// Number of tasks being retried
    pub retry: usize,
    /// Number of revoked tasks
    pub revoked: usize,
}

impl TaskSummary {
    /// Get the completion rate (0.0 to 1.0)
    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
        }
    }

    /// Get the success rate (0.0 to 1.0)
    pub fn success_rate(&self) -> f64 {
        if self.total == 0 {
            0.0
        } else {
            self.success as f64 / self.total as f64
        }
    }

    /// Get the failure rate (0.0 to 1.0)
    pub fn failure_rate(&self) -> f64 {
        if self.total == 0 {
            0.0
        } else {
            self.failure as f64 / self.total as f64
        }
    }

    /// Check if all tasks are complete
    pub fn all_complete(&self) -> bool {
        self.pending == 0 && self.started == 0 && self.retry == 0
    }

    /// Check if any tasks have failed
    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(())
    }
}

/// Backend statistics
#[derive(Debug, Clone)]
pub struct BackendStats {
    /// Number of task result keys in Redis
    pub task_key_count: usize,

    /// Number of chord state keys in Redis
    pub chord_key_count: usize,

    /// Total number of backend keys
    pub total_keys: usize,

    /// Memory used by Redis (bytes)
    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
        )
    }
}

/// Common TTL (Time-To-Live) durations for task results
///
/// These constants provide recommended TTL values for different use cases.
///
/// # Example
/// ```no_run
/// use celers_backend_redis::{RedisResultBackend, ResultBackend, TaskMeta, ttl};
/// use uuid::Uuid;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut backend = RedisResultBackend::new("redis://localhost")?;
/// let task_id = Uuid::new_v4();
/// let meta = TaskMeta::new(task_id, "my_task".to_string());
///
/// backend.store_result(task_id, &meta).await?;
///
/// // Use recommended TTL for success results
/// backend.set_expiration(task_id, ttl::SUCCESS).await?;
/// # Ok(())
/// # }
/// ```
pub mod ttl {
    use std::time::Duration;

    /// 1 hour - for temporary/transient results
    pub const TEMPORARY: Duration = Duration::from_secs(3600);

    /// 6 hours - for short-lived task results
    pub const SHORT: Duration = Duration::from_secs(6 * 3600);

    /// 24 hours - recommended for successful task results
    pub const SUCCESS: Duration = Duration::from_secs(86400);

    /// 3 days - for important results that need to be kept longer
    pub const MEDIUM: Duration = Duration::from_secs(3 * 86400);

    /// 7 days - recommended for failed tasks (useful for debugging)
    pub const FAILURE: Duration = Duration::from_secs(7 * 86400);

    /// 30 days - for archival/long-term storage
    pub const LONG: Duration = Duration::from_secs(30 * 86400);

    /// 90 days - maximum recommended retention
    pub const MAXIMUM: Duration = Duration::from_secs(90 * 86400);
}

/// Recommended batch sizes for optimal performance
///
/// These constants provide recommended batch sizes for different operations.
///
/// # Example
/// ```no_run
/// use celers_backend_redis::{RedisResultBackend, ResultBackend, batch_size};
/// use uuid::Uuid;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut backend = RedisResultBackend::new("redis://localhost")?;
///
/// // Use recommended batch size for operations
/// let task_ids: Vec<Uuid> = (0..batch_size::SMALL).map(|_| Uuid::new_v4()).collect();
/// let results = backend.get_results_batch(&task_ids).await?;
/// # Ok(())
/// # }
/// ```
pub mod batch_size {
    /// Small batch (10 items) - for low-latency requirements
    pub const SMALL: usize = 10;

    /// Medium batch (50 items) - recommended default for most operations
    pub const MEDIUM: usize = 50;

    /// Large batch (100 items) - for high-throughput scenarios
    pub const LARGE: usize = 100;

    /// Extra large batch (500 items) - for bulk operations with relaxed latency
    pub const EXTRA_LARGE: usize = 500;

    /// Maximum recommended batch (1000 items) - use with caution
    ///
    /// Batches larger than this may cause Redis to block or consume
    /// excessive memory. Consider using streaming instead.
    pub const MAXIMUM: usize = 1000;
}