oximedia-batch 0.1.8

Comprehensive batch processing engine for OxiMedia
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//! Batch analytics — run-time trend analysis, failure rates, throughput
//! metrics, and SLA monitoring for batch processing workloads.
//!
//! [`BatchAnalytics`] ingests [`JobSample`] records as jobs complete and
//! maintains rolling time-series windows at configurable granularities.
//! Callers can query:
//!
//! * **Throughput** — jobs completed per minute/hour/day across a window.
//! * **Failure rate** — fraction of jobs that failed in a time window.
//! * **Latency percentiles** — P50/P95/P99 job wall-clock durations.
//! * **SLA compliance** — fraction of jobs completing within a target
//!   duration threshold.
//! * **Trend direction** — whether throughput is improving, degrading, or
//!   stable compared to the previous window of equal length.
//!
//! # Design
//!
//! All ingested samples are stored in a bounded ring-buffer ordered by
//! completion time.  Aggregations walk the buffer each time; no background
//! threads are required.  The module is `no_std`-compatible (aside from
//! `std::time`) and does not perform any I/O.

use std::collections::VecDeque;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

use crate::types::JobState;

// ---------------------------------------------------------------------------
// JobSample
// ---------------------------------------------------------------------------

/// A single completed-job record contributed to the analytics engine.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobSample {
    /// Human-readable job category (e.g. `"transcode:h264"`, `"ingest"`).
    pub category: String,
    /// Final state of the job.
    pub state: JobState,
    /// Wall-clock duration of the job in seconds (from queued to terminal).
    pub duration_secs: f64,
    /// Unix timestamp (seconds) when the job reached a terminal state.
    pub completed_at: u64,
    /// Optional priority level encoded as an integer (0 = low, 1 = normal,
    /// 2 = high).
    pub priority: u8,
    /// Bytes of output produced (0 if unknown / not applicable).
    pub output_bytes: u64,
}

impl JobSample {
    /// Construct a new sample with the current time as `completed_at`.
    #[must_use]
    pub fn now(category: impl Into<String>, state: JobState, duration_secs: f64) -> Self {
        let completed_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_secs();
        Self {
            category: category.into(),
            state,
            duration_secs,
            completed_at,
            priority: 1,
            output_bytes: 0,
        }
    }

    /// Returns `true` if the job terminated successfully.
    #[must_use]
    pub fn is_success(&self) -> bool {
        matches!(self.state, JobState::Completed)
    }

    /// Returns `true` if the job failed.
    #[must_use]
    pub fn is_failure(&self) -> bool {
        matches!(self.state, JobState::Failed)
    }
}

// ---------------------------------------------------------------------------
// Time window helpers
// ---------------------------------------------------------------------------

/// A named time-aggregation window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Window {
    /// Last 60 seconds.
    LastMinute,
    /// Last 3 600 seconds.
    LastHour,
    /// Last 86 400 seconds.
    LastDay,
    /// Custom width in seconds.
    Custom(u64),
}

impl Window {
    /// Width of the window in seconds.
    #[must_use]
    pub fn secs(self) -> u64 {
        match self {
            Self::LastMinute => 60,
            Self::LastHour => 3_600,
            Self::LastDay => 86_400,
            Self::Custom(s) => s,
        }
    }
}

// ---------------------------------------------------------------------------
// Aggregated metrics
// ---------------------------------------------------------------------------

/// Aggregated metrics for a time window.
#[derive(Debug, Clone, Default)]
pub struct WindowMetrics {
    /// Number of samples in this window (all terminal states).
    pub total_jobs: usize,
    /// Number of successfully completed jobs.
    pub completed: usize,
    /// Number of failed jobs.
    pub failed: usize,
    /// Number of cancelled jobs.
    pub cancelled: usize,
    /// Mean duration in seconds (0.0 if `total_jobs` == 0).
    pub mean_duration_secs: f64,
    /// P50 duration in seconds (0.0 if insufficient data).
    pub p50_duration_secs: f64,
    /// P95 duration in seconds (0.0 if insufficient data).
    pub p95_duration_secs: f64,
    /// P99 duration in seconds (0.0 if insufficient data).
    pub p99_duration_secs: f64,
    /// Maximum duration in seconds.
    pub max_duration_secs: f64,
    /// Failure rate (0.0 ..= 1.0).
    pub failure_rate: f64,
    /// Total output bytes across all jobs in the window.
    pub total_output_bytes: u64,
    /// Effective throughput: completed jobs per second (averaged over window).
    pub throughput_per_sec: f64,
}

/// Trend direction comparing two equal-length windows.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TrendDirection {
    /// Throughput is at least 5% higher in the current window.
    Improving,
    /// Throughput is within ±5% of the previous window.
    Stable,
    /// Throughput is at least 5% lower in the current window.
    Degrading,
    /// Insufficient data for comparison.
    Unknown,
}

/// SLA compliance result for a window.
#[derive(Debug, Clone)]
pub struct SlaReport {
    /// Jobs that completed within the SLA threshold.
    pub within_sla: usize,
    /// Jobs that exceeded the SLA threshold (including failures).
    pub breached_sla: usize,
    /// Compliance fraction (0.0 ..= 1.0).
    pub compliance_rate: f64,
    /// The threshold used.
    pub threshold_secs: f64,
}

// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------

/// Configuration for [`BatchAnalytics`].
#[derive(Debug, Clone)]
pub struct AnalyticsConfig {
    /// Maximum number of samples retained in the ring-buffer.
    pub max_samples: usize,
    /// Optional SLA target in seconds; used by [`BatchAnalytics::sla_report`].
    pub sla_target_secs: Option<f64>,
}

impl Default for AnalyticsConfig {
    fn default() -> Self {
        Self {
            max_samples: 100_000,
            sla_target_secs: None,
        }
    }
}

// ---------------------------------------------------------------------------
// BatchAnalytics
// ---------------------------------------------------------------------------

/// Analytics engine for batch processing workloads.
///
/// # Thread safety
///
/// `BatchAnalytics` wraps its ring-buffer in a `std::sync::Mutex` internally
/// via [`parking_lot::Mutex`].  All public methods take `&self` and are safe
/// to call concurrently.
///
/// # Example
///
/// ```
/// use oximedia_batch::batch_analytics::{BatchAnalytics, AnalyticsConfig, JobSample, Window};
/// use oximedia_batch::types::JobState;
///
/// let analytics = BatchAnalytics::new(AnalyticsConfig::default());
/// analytics.ingest(JobSample::now("transcode", JobState::Completed, 12.5));
/// analytics.ingest(JobSample::now("transcode", JobState::Failed, 0.5));
///
/// let metrics = analytics.metrics(Window::LastHour);
/// assert_eq!(metrics.total_jobs, 2);
/// assert!((metrics.failure_rate - 0.5).abs() < 1e-4);
/// ```
pub struct BatchAnalytics {
    samples: parking_lot::Mutex<VecDeque<JobSample>>,
    config: AnalyticsConfig,
}

impl BatchAnalytics {
    /// Create a new analytics engine.
    #[must_use]
    pub fn new(config: AnalyticsConfig) -> Self {
        Self {
            samples: parking_lot::Mutex::new(VecDeque::new()),
            config,
        }
    }

    // -----------------------------------------------------------------------
    // Ingestion
    // -----------------------------------------------------------------------

    /// Ingest a completed job sample.
    pub fn ingest(&self, sample: JobSample) {
        let mut ring = self.samples.lock();
        ring.push_back(sample);
        while ring.len() > self.config.max_samples {
            ring.pop_front();
        }
    }

    /// Ingest multiple samples at once.
    pub fn ingest_batch(&self, samples: impl IntoIterator<Item = JobSample>) {
        let mut ring = self.samples.lock();
        for s in samples {
            ring.push_back(s);
        }
        while ring.len() > self.config.max_samples {
            ring.pop_front();
        }
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    fn now_secs() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_secs()
    }

    /// Extract all samples whose `completed_at` falls in `[since, now]`.
    fn samples_in_window(&self, window: Window) -> Vec<JobSample> {
        let now = Self::now_secs();
        let since = now.saturating_sub(window.secs());
        let ring = self.samples.lock();
        ring.iter()
            .filter(|s| s.completed_at >= since)
            .cloned()
            .collect()
    }

    /// Extract samples in the previous equal-length window (for trend calc).
    fn samples_in_prev_window(&self, window: Window) -> Vec<JobSample> {
        let now = Self::now_secs();
        let width = window.secs();
        let end = now.saturating_sub(width);
        let start = end.saturating_sub(width);
        let ring = self.samples.lock();
        ring.iter()
            .filter(|s| s.completed_at >= start && s.completed_at < end)
            .cloned()
            .collect()
    }

    // -----------------------------------------------------------------------
    // Aggregate metrics
    // -----------------------------------------------------------------------

    /// Compute aggregated metrics for `window`.
    #[must_use]
    pub fn metrics(&self, window: Window) -> WindowMetrics {
        let samples = self.samples_in_window(window);
        Self::compute_metrics(&samples, window.secs())
    }

    fn compute_metrics(samples: &[JobSample], window_secs: u64) -> WindowMetrics {
        if samples.is_empty() {
            return WindowMetrics::default();
        }

        let total_jobs = samples.len();
        let completed = samples.iter().filter(|s| s.is_success()).count();
        let failed = samples.iter().filter(|s| s.is_failure()).count();
        let cancelled = samples
            .iter()
            .filter(|s| matches!(s.state, JobState::Cancelled))
            .count();
        let total_output_bytes: u64 = samples.iter().map(|s| s.output_bytes).sum();

        // Duration stats.
        let mut durations: Vec<f64> = samples.iter().map(|s| s.duration_secs).collect();
        durations.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let mean_duration_secs = durations.iter().sum::<f64>() / total_jobs as f64;
        let p50 = percentile(&durations, 0.50);
        let p95 = percentile(&durations, 0.95);
        let p99 = percentile(&durations, 0.99);
        let max_duration_secs = durations.last().copied().unwrap_or(0.0);

        let failure_rate = if total_jobs > 0 {
            failed as f64 / total_jobs as f64
        } else {
            0.0
        };

        let throughput_per_sec = if window_secs > 0 {
            completed as f64 / window_secs as f64
        } else {
            0.0
        };

        WindowMetrics {
            total_jobs,
            completed,
            failed,
            cancelled,
            mean_duration_secs,
            p50_duration_secs: p50,
            p95_duration_secs: p95,
            p99_duration_secs: p99,
            max_duration_secs,
            failure_rate,
            total_output_bytes,
            throughput_per_sec,
        }
    }

    // -----------------------------------------------------------------------
    // SLA
    // -----------------------------------------------------------------------

    /// Compute SLA compliance within `window` against `threshold_secs`.
    ///
    /// A job "breaches" SLA if its `duration_secs` exceeds `threshold_secs`
    /// OR if it failed or was cancelled.
    #[must_use]
    pub fn sla_report(&self, window: Window, threshold_secs: f64) -> SlaReport {
        let samples = self.samples_in_window(window);
        let total = samples.len();

        if total == 0 {
            return SlaReport {
                within_sla: 0,
                breached_sla: 0,
                compliance_rate: 1.0,
                threshold_secs,
            };
        }

        let within_sla = samples
            .iter()
            .filter(|s| s.is_success() && s.duration_secs <= threshold_secs)
            .count();
        let breached_sla = total - within_sla;
        let compliance_rate = within_sla as f64 / total as f64;

        SlaReport {
            within_sla,
            breached_sla,
            compliance_rate,
            threshold_secs,
        }
    }

    // -----------------------------------------------------------------------
    // Trend
    // -----------------------------------------------------------------------

    /// Compute the throughput trend direction by comparing the current `window`
    /// to the previous window of the same length.
    #[must_use]
    pub fn throughput_trend(&self, window: Window) -> TrendDirection {
        let current = self.metrics(window);
        let prev_samples = self.samples_in_prev_window(window);

        if prev_samples.is_empty() {
            return TrendDirection::Unknown;
        }

        let prev = Self::compute_metrics(&prev_samples, window.secs());

        let curr_tp = current.throughput_per_sec;
        let prev_tp = prev.throughput_per_sec;

        if prev_tp == 0.0 {
            if curr_tp > 0.0 {
                return TrendDirection::Improving;
            }
            return TrendDirection::Unknown;
        }

        let ratio = curr_tp / prev_tp;
        if ratio >= 1.05 {
            TrendDirection::Improving
        } else if ratio <= 0.95 {
            TrendDirection::Degrading
        } else {
            TrendDirection::Stable
        }
    }

    // -----------------------------------------------------------------------
    // Failure breakdown
    // -----------------------------------------------------------------------

    /// Return per-category failure rates within `window`.
    ///
    /// The map key is the category; the value is `(failed, total)`.
    #[must_use]
    pub fn failure_breakdown(
        &self,
        window: Window,
    ) -> std::collections::HashMap<String, (usize, usize)> {
        let samples = self.samples_in_window(window);
        let mut map: std::collections::HashMap<String, (usize, usize)> =
            std::collections::HashMap::new();

        for s in &samples {
            let entry = map.entry(s.category.clone()).or_insert((0, 0));
            entry.1 += 1; // total
            if s.is_failure() {
                entry.0 += 1; // failed
            }
        }
        map
    }

    // -----------------------------------------------------------------------
    // Top categories
    // -----------------------------------------------------------------------

    /// Return the `n` categories with the highest job count in `window`.
    ///
    /// Returns a `Vec<(category, count)>` sorted descending by count.
    #[must_use]
    pub fn top_categories(&self, window: Window, n: usize) -> Vec<(String, usize)> {
        let samples = self.samples_in_window(window);
        let mut map: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
        for s in &samples {
            *map.entry(s.category.clone()).or_insert(0) += 1;
        }
        let mut v: Vec<_> = map.into_iter().collect();
        v.sort_by(|a, b| b.1.cmp(&a.1));
        v.truncate(n);
        v
    }

    // -----------------------------------------------------------------------
    // Capacity
    // -----------------------------------------------------------------------

    /// Number of samples currently retained.
    #[must_use]
    pub fn sample_count(&self) -> usize {
        self.samples.lock().len()
    }

    /// Discard all retained samples.
    pub fn clear(&self) {
        self.samples.lock().clear();
    }
}

// ---------------------------------------------------------------------------
// Percentile helper
// ---------------------------------------------------------------------------

/// Compute the `p`-th percentile (0.0 ..= 1.0) of a *sorted* slice.
/// Returns 0.0 for empty slices.
fn percentile(sorted: &[f64], p: f64) -> f64 {
    if sorted.is_empty() {
        return 0.0;
    }
    if sorted.len() == 1 {
        return sorted[0];
    }
    let idx = p * (sorted.len() - 1) as f64;
    let lo = idx.floor() as usize;
    let hi = idx.ceil() as usize;
    let frac = idx - lo as f64;
    sorted[lo] + frac * (sorted[hi] - sorted[lo])
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::JobState;

    fn make_sample(state: JobState, duration: f64) -> JobSample {
        JobSample::now("test", state, duration)
    }

    // -----------------------------------------------------------------------
    // Percentile helper
    // -----------------------------------------------------------------------

    #[test]
    fn test_percentile_single_element() {
        assert!((percentile(&[5.0], 0.5) - 5.0).abs() < 1e-9);
    }

    #[test]
    fn test_percentile_sorted_range() {
        let data: Vec<f64> = (1..=100).map(|i| i as f64).collect();
        // P50 of 1..100 should be near 50.
        let p50 = percentile(&data, 0.50);
        assert!(p50 >= 49.0 && p50 <= 51.0, "p50={p50}");
        // P99 should be near 99.
        let p99 = percentile(&data, 0.99);
        assert!(p99 >= 97.0 && p99 <= 100.0, "p99={p99}");
    }

    #[test]
    fn test_percentile_empty_returns_zero() {
        assert_eq!(percentile(&[], 0.5), 0.0);
    }

    // -----------------------------------------------------------------------
    // BatchAnalytics — ingestion
    // -----------------------------------------------------------------------

    #[test]
    fn test_ingest_and_sample_count() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        a.ingest(make_sample(JobState::Completed, 1.0));
        a.ingest(make_sample(JobState::Failed, 0.5));
        assert_eq!(a.sample_count(), 2);
    }

    #[test]
    fn test_ingest_batch() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        let samples: Vec<_> = (0..10)
            .map(|_| make_sample(JobState::Completed, 2.0))
            .collect();
        a.ingest_batch(samples);
        assert_eq!(a.sample_count(), 10);
    }

    #[test]
    fn test_ring_buffer_max_capacity() {
        let cfg = AnalyticsConfig {
            max_samples: 3,
            sla_target_secs: None,
        };
        let a = BatchAnalytics::new(cfg);
        for _ in 0..10 {
            a.ingest(make_sample(JobState::Completed, 1.0));
        }
        assert_eq!(a.sample_count(), 3);
    }

    // -----------------------------------------------------------------------
    // Metrics
    // -----------------------------------------------------------------------

    #[test]
    fn test_metrics_all_successful() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        a.ingest(make_sample(JobState::Completed, 10.0));
        a.ingest(make_sample(JobState::Completed, 20.0));
        let m = a.metrics(Window::LastHour);
        assert_eq!(m.total_jobs, 2);
        assert_eq!(m.completed, 2);
        assert_eq!(m.failed, 0);
        assert!((m.failure_rate).abs() < 1e-9);
    }

    #[test]
    fn test_metrics_failure_rate() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        a.ingest(make_sample(JobState::Completed, 5.0));
        a.ingest(make_sample(JobState::Failed, 1.0));
        let m = a.metrics(Window::LastHour);
        assert!((m.failure_rate - 0.5).abs() < 1e-4);
    }

    #[test]
    fn test_metrics_empty_window() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        let m = a.metrics(Window::LastMinute);
        assert_eq!(m.total_jobs, 0);
        assert!((m.failure_rate).abs() < 1e-9);
    }

    #[test]
    fn test_metrics_duration_percentiles() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        for i in 1..=100 {
            a.ingest(make_sample(JobState::Completed, i as f64));
        }
        let m = a.metrics(Window::LastHour);
        assert!(m.p50_duration_secs >= 49.0 && m.p50_duration_secs <= 51.0);
        assert!(m.max_duration_secs >= 99.0 && m.max_duration_secs <= 101.0);
    }

    // -----------------------------------------------------------------------
    // SLA
    // -----------------------------------------------------------------------

    #[test]
    fn test_sla_all_within() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        a.ingest(make_sample(JobState::Completed, 5.0));
        a.ingest(make_sample(JobState::Completed, 3.0));
        let report = a.sla_report(Window::LastHour, 10.0);
        assert_eq!(report.within_sla, 2);
        assert!((report.compliance_rate - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_sla_some_breached() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        a.ingest(make_sample(JobState::Completed, 5.0));
        a.ingest(make_sample(JobState::Completed, 15.0)); // breaches 10s threshold
        a.ingest(make_sample(JobState::Failed, 1.0)); // failure always breaches
        let report = a.sla_report(Window::LastHour, 10.0);
        assert_eq!(report.within_sla, 1);
        assert_eq!(report.breached_sla, 2);
        assert!((report.compliance_rate - 1.0 / 3.0).abs() < 1e-4);
    }

    #[test]
    fn test_sla_empty_window_full_compliance() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        let report = a.sla_report(Window::LastHour, 30.0);
        assert!((report.compliance_rate - 1.0).abs() < 1e-9);
    }

    // -----------------------------------------------------------------------
    // Failure breakdown
    // -----------------------------------------------------------------------

    #[test]
    fn test_failure_breakdown_per_category() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());

        let mut s1 = make_sample(JobState::Completed, 5.0);
        s1.category = "encode".into();
        let mut s2 = make_sample(JobState::Failed, 1.0);
        s2.category = "encode".into();
        let mut s3 = make_sample(JobState::Failed, 2.0);
        s3.category = "ingest".into();

        a.ingest(s1);
        a.ingest(s2);
        a.ingest(s3);

        let breakdown = a.failure_breakdown(Window::LastHour);
        let (enc_fail, enc_total) = breakdown["encode"];
        assert_eq!(enc_total, 2);
        assert_eq!(enc_fail, 1);
        let (ing_fail, ing_total) = breakdown["ingest"];
        assert_eq!(ing_total, 1);
        assert_eq!(ing_fail, 1);
    }

    // -----------------------------------------------------------------------
    // Top categories
    // -----------------------------------------------------------------------

    #[test]
    fn test_top_categories_sorted_descending() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        for _ in 0..5 {
            let mut s = make_sample(JobState::Completed, 1.0);
            s.category = "encode".into();
            a.ingest(s);
        }
        for _ in 0..2 {
            let mut s = make_sample(JobState::Completed, 1.0);
            s.category = "ingest".into();
            a.ingest(s);
        }
        let top = a.top_categories(Window::LastHour, 2);
        assert_eq!(top[0].0, "encode");
        assert_eq!(top[0].1, 5);
        assert_eq!(top[1].0, "ingest");
    }

    // -----------------------------------------------------------------------
    // Trend
    // -----------------------------------------------------------------------

    #[test]
    fn test_trend_unknown_when_no_prior_data() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        a.ingest(make_sample(JobState::Completed, 1.0));
        // No data in the previous window → Unknown.
        assert_eq!(
            a.throughput_trend(Window::LastMinute),
            TrendDirection::Unknown
        );
    }

    // -----------------------------------------------------------------------
    // Clear
    // -----------------------------------------------------------------------

    #[test]
    fn test_clear_empties_buffer() {
        let a = BatchAnalytics::new(AnalyticsConfig::default());
        a.ingest(make_sample(JobState::Completed, 1.0));
        a.clear();
        assert_eq!(a.sample_count(), 0);
        let m = a.metrics(Window::LastHour);
        assert_eq!(m.total_jobs, 0);
    }

    // -----------------------------------------------------------------------
    // JobSample helpers
    // -----------------------------------------------------------------------

    #[test]
    fn test_job_sample_is_success_failure() {
        let s_ok = make_sample(JobState::Completed, 1.0);
        let s_fail = make_sample(JobState::Failed, 0.5);
        let s_cancel = make_sample(JobState::Cancelled, 0.1);
        assert!(s_ok.is_success());
        assert!(!s_ok.is_failure());
        assert!(s_fail.is_failure());
        assert!(!s_cancel.is_success());
        assert!(!s_cancel.is_failure());
    }
}