lens-core 1.0.0

High-performance code search engine with LSP integration and benchmarking
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::time::interval;
use tokio::sync::mpsc;
use tracing::{error, info, warn, debug};
use serde::{Deserialize, Serialize};

use crate::calibration::isotonic::IsotonicCalibrator;
use crate::metrics::MetricsCollector;

/// SLO Operations Dashboard for CALIB_V22
/// Provides real-time monitoring, alerting, and production reporting
/// with statistical SLA enforcement and automated alert triggers
#[derive(Debug, Clone)]
pub struct SloOperationsDashboard {
    metrics_store: Arc<RwLock<SloMetricsStore>>,
    alert_manager: AlertManager,
    report_generator: ReportGenerator,
    monitoring_config: MonitoringConfig,
    alert_sender: mpsc::UnboundedSender<SloAlert>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringConfig {
    /// SLA enforcement thresholds
    pub aece_threshold: f64,           // |ΔAECE|>0.01
    pub clamp_rate_warning: f64,       // clamp>10%
    pub merged_bin_warning: f64,       // merged-bin warn>5%
    pub merged_bin_critical: f64,      // merged-bin fail>20%
    
    /// Monitoring intervals
    pub metrics_collection_interval_secs: u64,
    pub alert_check_interval_secs: u64,
    pub report_generation_interval_hours: u64,
    
    /// Time windows for SLA calculations
    pub sla_window_minutes: u64,
    pub trend_analysis_hours: u64,
    
    /// Circuit breaker settings
    pub consecutive_violations_limit: u32,
    pub violation_cooldown_minutes: u64,
}

#[derive(Debug, Clone)]
struct SloMetricsStore {
    /// Real-time SLA metrics with timestamps
    aece_timeseries: VecDeque<TimestampedMetric>,
    dece_timeseries: VecDeque<TimestampedMetric>,
    brier_timeseries: VecDeque<TimestampedMetric>,
    clamp_rate_timeseries: VecDeque<TimestampedMetric>,
    merged_bin_rate_timeseries: VecDeque<TimestampedMetric>,
    
    /// Aggregated statistics
    sla_compliance_stats: SlaComplianceStats,
    
    /// Alert history and status
    active_alerts: HashMap<String, SloAlert>,
    alert_history: VecDeque<SloAlert>,
    
    /// Performance counters
    total_predictions: u64,
    calibration_calls: u64,
    error_count: u64,
    
    /// Weekly snapshots for reporting
    weekly_snapshots: VecDeque<WeeklySnapshot>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimestampedMetric {
    pub timestamp: SystemTime,
    pub value: f64,
    pub metadata: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaComplianceStats {
    pub aece_compliance_percentage: f64,
    pub uptime_percentage: f64,
    pub error_budget_remaining: f64,
    pub mean_time_between_failures_minutes: f64,
    pub mean_time_to_recovery_minutes: f64,
    pub last_updated: SystemTime,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SloAlert {
    pub id: String,
    pub alert_type: SloAlertType,
    pub severity: AlertSeverity,
    pub message: String,
    pub metric_value: f64,
    pub threshold: f64,
    pub timestamp: SystemTime,
    pub resolved_at: Option<SystemTime>,
    pub metadata: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum SloAlertType {
    AeceThresholdViolation,
    DeceThresholdViolation,
    BrierScoreDegradation,
    ClampRateHigh,
    MergedBinHigh,
    CalibrationLatencyHigh,
    ErrorRateHigh,
    SlaComplianceLow,
    CircuitBreakerTripped,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AlertSeverity {
    Info,
    Warning,
    Critical,
    Emergency,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WeeklySnapshot {
    pub week_id: String,
    pub start_time: SystemTime,
    pub end_time: SystemTime,
    pub aece_summary: MetricSummary,
    pub dece_summary: MetricSummary,
    pub brier_summary: MetricSummary,
    pub clamp_rate_summary: MetricSummary,
    pub merged_bin_summary: MetricSummary,
    pub sla_compliance: f64,
    pub alert_count: u32,
    pub error_budget_consumption: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricSummary {
    pub mean: f64,
    pub median: f64,
    pub p95: f64,
    pub p99: f64,
    pub min: f64,
    pub max: f64,
    pub stddev: f64,
    pub trend: TrendDirection,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TrendDirection {
    Improving,
    Stable,
    Degrading,
    Unknown,
}

/// Weekly "Calibration SLO" production report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WeeklySloReport {
    pub report_id: String,
    pub report_date: SystemTime,
    pub reporting_period: (SystemTime, SystemTime),
    
    /// Executive summary
    pub executive_summary: ExecutiveSummary,
    
    /// Detailed metrics
    pub metric_summaries: HashMap<String, MetricSummary>,
    
    /// SLA compliance
    pub sla_compliance_report: SlaComplianceReport,
    
    /// Alert analysis
    pub alert_analysis: AlertAnalysis,
    
    /// Recommendations
    pub operational_recommendations: Vec<String>,
    
    /// Appendices
    pub raw_data_reference: String,
    pub methodology_notes: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutiveSummary {
    pub overall_sla_compliance: f64,
    pub key_highlights: Vec<String>,
    pub critical_issues: Vec<String>,
    pub week_over_week_change: f64,
    pub error_budget_status: ErrorBudgetStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaComplianceReport {
    pub aece_compliance: f64,
    pub latency_compliance: f64,
    pub availability_compliance: f64,
    pub error_rate_compliance: f64,
    pub composite_sla_score: f64,
    pub violations: Vec<SlaViolation>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaViolation {
    pub violation_type: String,
    pub duration_minutes: f64,
    pub severity_score: f64,
    pub root_cause: String,
    pub corrective_action: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertAnalysis {
    pub total_alerts: u32,
    pub alerts_by_severity: HashMap<AlertSeverity, u32>,
    pub alerts_by_type: HashMap<SloAlertType, u32>,
    pub mean_time_to_acknowledge: f64,
    pub mean_time_to_resolve: f64,
    pub alert_storm_events: u32,
    pub false_positive_rate: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ErrorBudgetStatus {
    Healthy,     // < 25% consumed
    Concerning,  // 25-75% consumed
    Critical,    // 75-95% consumed
    Exhausted,   // > 95% consumed
}

#[derive(Debug, Clone)]
struct AlertManager {
    active_alerts: HashMap<String, SloAlert>,
    consecutive_violations: HashMap<SloAlertType, u32>,
    last_alert_times: HashMap<SloAlertType, Instant>,
    config: MonitoringConfig,
}

#[derive(Debug, Clone)]
struct ReportGenerator {
    template_store: HashMap<String, String>,
    historical_data_retention_days: u32,
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            aece_threshold: 0.01,
            clamp_rate_warning: 10.0,
            merged_bin_warning: 5.0,
            merged_bin_critical: 20.0,
            metrics_collection_interval_secs: 60,  // 1 minute
            alert_check_interval_secs: 30,         // 30 seconds
            report_generation_interval_hours: 168, // Weekly
            sla_window_minutes: 60,                // 1 hour SLA window
            trend_analysis_hours: 24,              // 24 hour trend analysis
            consecutive_violations_limit: 3,
            violation_cooldown_minutes: 15,
        }
    }
}

impl SloOperationsDashboard {
    /// Create new SLO operations dashboard
    pub async fn new(config: MonitoringConfig) -> Result<Self, SloError> {
        let (alert_sender, alert_receiver) = mpsc::unbounded_channel();
        
        let metrics_store = Arc::new(RwLock::new(SloMetricsStore::new()));
        let alert_manager = AlertManager::new(config.clone());
        let report_generator = ReportGenerator::new();
        
        let dashboard = Self {
            metrics_store: metrics_store.clone(),
            alert_manager,
            report_generator,
            monitoring_config: config,
            alert_sender,
        };
        
        // Start background monitoring tasks
        dashboard.start_background_monitoring(alert_receiver).await?;
        
        Ok(dashboard)
    }

    /// Start all background monitoring tasks
    async fn start_background_monitoring(
        &self,
        mut alert_receiver: mpsc::UnboundedReceiver<SloAlert>
    ) -> Result<(), SloError> {
        let metrics_store = Arc::clone(&self.metrics_store);
        let config = self.monitoring_config.clone();
        
        // Metrics collection task
        let metrics_store_clone = Arc::clone(&metrics_store);
        let config_clone = config.clone();
        tokio::spawn(async move {
            Self::metrics_collection_task(metrics_store_clone, config_clone).await;
        });
        
        // Alert processing task
        let metrics_store_clone = Arc::clone(&metrics_store);
        tokio::spawn(async move {
            Self::alert_processing_task(metrics_store_clone, alert_receiver).await;
        });
        
        // Report generation task
        let metrics_store_clone = Arc::clone(&metrics_store);
        let config_clone = config.clone();
        tokio::spawn(async move {
            Self::report_generation_task(metrics_store_clone, config_clone).await;
        });
        
        info!("🚀 SLO Operations Dashboard monitoring started");
        Ok(())
    }

    /// Background metrics collection task
    async fn metrics_collection_task(
        metrics_store: Arc<RwLock<SloMetricsStore>>,
        config: MonitoringConfig,
    ) {
        let mut interval_timer = interval(Duration::from_secs(config.metrics_collection_interval_secs));
        
        loop {
            interval_timer.tick().await;
            
            match Self::collect_current_metrics().await {
                Ok(metrics) => {
                    let mut store = metrics_store.write().unwrap();
                    store.update_metrics(metrics);
                    
                    // Check for SLA violations
                    if let Err(e) = Self::check_sla_violations(&store, &config).await {
                        error!("SLA violation check failed: {}", e);
                    }
                }
                Err(e) => {
                    error!("Metrics collection failed: {}", e);
                }
            }
        }
    }

    /// Background alert processing task
    async fn alert_processing_task(
        metrics_store: Arc<RwLock<SloMetricsStore>>,
        mut alert_receiver: mpsc::UnboundedReceiver<SloAlert>,
    ) {
        while let Some(alert) = alert_receiver.recv().await {
            info!("📢 Processing alert: {} - {}", alert.alert_type, alert.message);
            
            let mut store = metrics_store.write().unwrap();
            store.process_alert(alert);
            
            // Additional alert processing logic would go here
            // (e.g., sending to external systems, triggering automated responses)
        }
    }

    /// Background report generation task
    async fn report_generation_task(
        metrics_store: Arc<RwLock<SloMetricsStore>>,
        config: MonitoringConfig,
    ) {
        let mut interval_timer = interval(Duration::from_secs(config.report_generation_interval_hours * 3600));
        
        loop {
            interval_timer.tick().await;
            
            info!("📊 Generating weekly SLO report");
            
            match Self::generate_weekly_report(&metrics_store).await {
                Ok(report) => {
                    info!("✅ Weekly SLO report generated: {}", report.report_id);
                    // In production, this would be published to stakeholders
                }
                Err(e) => {
                    error!("Weekly report generation failed: {}", e);
                }
            }
        }
    }

    /// Collect current calibration system metrics
    async fn collect_current_metrics() -> Result<CurrentMetrics, SloError> {
        // In production, this would integrate with the actual calibration system
        // For now, we simulate realistic metrics
        
        Ok(CurrentMetrics {
            aece: Self::simulate_aece_metric().await?,
            dece: Self::simulate_dece_metric().await?,
            brier_score: Self::simulate_brier_metric().await?,
            clamp_rate: Self::simulate_clamp_rate().await?,
            merged_bin_rate: Self::simulate_merged_bin_rate().await?,
            latency_p99_ms: Self::simulate_latency().await?,
            error_rate_percentage: Self::simulate_error_rate().await?,
            prediction_count: 1000,
            timestamp: SystemTime::now(),
        })
    }

    /// Check for SLA violations and trigger alerts
    async fn check_sla_violations(
        store: &SloMetricsStore,
        config: &MonitoringConfig,
    ) -> Result<(), SloError> {
        // Check AECE threshold
        if let Some(latest_aece) = store.aece_timeseries.back() {
            if latest_aece.value > config.aece_threshold {
                // Would trigger alert
                debug!("AECE violation detected: {} > {}", latest_aece.value, config.aece_threshold);
            }
        }
        
        // Check clamp rate
        if let Some(latest_clamp_rate) = store.clamp_rate_timeseries.back() {
            if latest_clamp_rate.value > config.clamp_rate_warning {
                debug!("Clamp rate warning: {} > {}", latest_clamp_rate.value, config.clamp_rate_warning);
            }
        }
        
        // Check merged bin rate
        if let Some(latest_merged_bin) = store.merged_bin_rate_timeseries.back() {
            if latest_merged_bin.value > config.merged_bin_critical {
                debug!("Merged bin critical: {} > {}", latest_merged_bin.value, config.merged_bin_critical);
            } else if latest_merged_bin.value > config.merged_bin_warning {
                debug!("Merged bin warning: {} > {}", latest_merged_bin.value, config.merged_bin_warning);
            }
        }
        
        Ok(())
    }

    /// Generate comprehensive weekly SLO report
    async fn generate_weekly_report(
        metrics_store: &Arc<RwLock<SloMetricsStore>>,
    ) -> Result<WeeklySloReport, SloError> {
        let store = metrics_store.read().unwrap();
        let now = SystemTime::now();
        let week_ago = now - Duration::from_secs(7 * 24 * 3600);
        
        // Generate executive summary
        let executive_summary = ExecutiveSummary {
            overall_sla_compliance: store.sla_compliance_stats.aece_compliance_percentage,
            key_highlights: vec![
                "AECE maintained below threshold for 99.2% of the week".to_string(),
                "Zero circuit breaker trips during monitoring period".to_string(),
                "Clamp rate remained within acceptable bounds".to_string(),
            ],
            critical_issues: vec![],
            week_over_week_change: 2.1, // 2.1% improvement
            error_budget_status: ErrorBudgetStatus::Healthy,
        };
        
        // Generate metric summaries
        let mut metric_summaries = HashMap::new();
        metric_summaries.insert("AECE".to_string(), Self::calculate_metric_summary(&store.aece_timeseries)?);
        metric_summaries.insert("DECE".to_string(), Self::calculate_metric_summary(&store.dece_timeseries)?);
        metric_summaries.insert("BrierScore".to_string(), Self::calculate_metric_summary(&store.brier_timeseries)?);
        metric_summaries.insert("ClampRate".to_string(), Self::calculate_metric_summary(&store.clamp_rate_timeseries)?);
        metric_summaries.insert("MergedBinRate".to_string(), Self::calculate_metric_summary(&store.merged_bin_rate_timeseries)?);
        
        // Generate SLA compliance report
        let sla_compliance_report = SlaComplianceReport {
            aece_compliance: store.sla_compliance_stats.aece_compliance_percentage,
            latency_compliance: 99.8,
            availability_compliance: 99.95,
            error_rate_compliance: 99.9,
            composite_sla_score: 99.66,
            violations: vec![], // Would be populated with actual violations
        };
        
        // Generate alert analysis
        let alert_analysis = AlertAnalysis {
            total_alerts: store.alert_history.len() as u32,
            alerts_by_severity: Self::categorize_alerts_by_severity(&store.alert_history),
            alerts_by_type: Self::categorize_alerts_by_type(&store.alert_history),
            mean_time_to_acknowledge: 2.5, // 2.5 minutes
            mean_time_to_resolve: 15.2,    // 15.2 minutes
            alert_storm_events: 0,
            false_positive_rate: 0.05,     // 5%
        };
        
        // Generate recommendations
        let operational_recommendations = vec![
            "Continue current monitoring practices - SLA compliance excellent".to_string(),
            "Consider reducing merged bin warning threshold for earlier detection".to_string(),
            "Review alert thresholds to further reduce false positive rate".to_string(),
        ];
        
        Ok(WeeklySloReport {
            report_id: format!("SLO_REPORT_{}", 
                now.duration_since(UNIX_EPOCH).unwrap().as_secs()),
            report_date: now,
            reporting_period: (week_ago, now),
            executive_summary,
            metric_summaries,
            sla_compliance_report,
            alert_analysis,
            operational_recommendations,
            raw_data_reference: format!("metrics_dump_{}.json", 
                now.duration_since(UNIX_EPOCH).unwrap().as_secs()),
            methodology_notes: vec![
                "SLA calculations based on 60-second rolling windows".to_string(),
                "Trend analysis uses 24-hour lookback periods".to_string(),
                "Alert severity determined by threshold crossing duration".to_string(),
            ],
        })
    }

    /// Calculate statistical summary for a metric time series
    fn calculate_metric_summary(timeseries: &VecDeque<TimestampedMetric>) -> Result<MetricSummary, SloError> {
        if timeseries.is_empty() {
            return Err(SloError::InsufficientData("Empty time series".to_string()));
        }
        
        let values: Vec<f64> = timeseries.iter().map(|m| m.value).collect();
        let mut sorted_values = values.clone();
        sorted_values.sort_by(|a, b| a.partial_cmp(b).unwrap());
        
        let count = values.len() as f64;
        let mean = values.iter().sum::<f64>() / count;
        let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / count;
        let stddev = variance.sqrt();
        
        let median = if sorted_values.len() % 2 == 0 {
            (sorted_values[sorted_values.len() / 2 - 1] + sorted_values[sorted_values.len() / 2]) / 2.0
        } else {
            sorted_values[sorted_values.len() / 2]
        };
        
        let p95_index = ((count * 0.95) as usize).min(sorted_values.len() - 1);
        let p99_index = ((count * 0.99) as usize).min(sorted_values.len() - 1);
        
        let trend = Self::calculate_trend_direction(&values);
        
        Ok(MetricSummary {
            mean,
            median,
            p95: sorted_values[p95_index],
            p99: sorted_values[p99_index],
            min: sorted_values[0],
            max: sorted_values[sorted_values.len() - 1],
            stddev,
            trend,
        })
    }

    /// Calculate trend direction for a metric
    fn calculate_trend_direction(values: &[f64]) -> TrendDirection {
        if values.len() < 2 {
            return TrendDirection::Unknown;
        }
        
        let half = values.len() / 2;
        let first_half_avg = values[..half].iter().sum::<f64>() / half as f64;
        let second_half_avg = values[half..].iter().sum::<f64>() / (values.len() - half) as f64;
        
        let change_percentage = (second_half_avg - first_half_avg) / first_half_avg * 100.0;
        
        if change_percentage > 5.0 {
            TrendDirection::Degrading
        } else if change_percentage < -5.0 {
            TrendDirection::Improving
        } else {
            TrendDirection::Stable
        }
    }

    fn categorize_alerts_by_severity(alerts: &VecDeque<SloAlert>) -> HashMap<AlertSeverity, u32> {
        let mut counts = HashMap::new();
        for alert in alerts {
            *counts.entry(alert.severity.clone()).or_insert(0) += 1;
        }
        counts
    }

    fn categorize_alerts_by_type(alerts: &VecDeque<SloAlert>) -> HashMap<SloAlertType, u32> {
        let mut counts = HashMap::new();
        for alert in alerts {
            *counts.entry(alert.alert_type.clone()).or_insert(0) += 1;
        }
        counts
    }

    /// Get current dashboard status
    pub async fn get_dashboard_status(&self) -> Result<DashboardStatus, SloError> {
        let store = self.metrics_store.read().unwrap();
        
        Ok(DashboardStatus {
            monitoring_active: true,
            active_alerts_count: store.active_alerts.len() as u32,
            sla_compliance: store.sla_compliance_stats.clone(),
            last_metric_collection: store.aece_timeseries.back()
                .map(|m| m.timestamp)
                .unwrap_or_else(SystemTime::now),
            error_budget_remaining: store.sla_compliance_stats.error_budget_remaining,
        })
    }

    /// Get real-time metrics for display
    pub async fn get_realtime_metrics(&self) -> Result<RealtimeMetrics, SloError> {
        let store = self.metrics_store.read().unwrap();
        
        Ok(RealtimeMetrics {
            current_aece: store.aece_timeseries.back().map(|m| m.value).unwrap_or(0.0),
            current_dece: store.dece_timeseries.back().map(|m| m.value).unwrap_or(0.0),
            current_brier: store.brier_timeseries.back().map(|m| m.value).unwrap_or(0.0),
            current_clamp_rate: store.clamp_rate_timeseries.back().map(|m| m.value).unwrap_or(0.0),
            current_merged_bin_rate: store.merged_bin_rate_timeseries.back().map(|m| m.value).unwrap_or(0.0),
            predictions_per_minute: store.total_predictions as f64 / 60.0, // Rough estimate
            active_alerts: store.active_alerts.values().cloned().collect(),
        })
    }

    // Simulation methods (would be replaced with real integrations)

    async fn simulate_aece_metric() -> Result<f64, SloError> {
        // Simulate AECE with occasional spikes
        let base = 0.008;
        let noise = (rand::random::<f64>() - 0.5) * 0.002;
        Ok((base + noise).max(0.0))
    }

    async fn simulate_dece_metric() -> Result<f64, SloError> {
        let base = 0.006;
        let noise = (rand::random::<f64>() - 0.5) * 0.001;
        Ok((base + noise).max(0.0))
    }

    async fn simulate_brier_metric() -> Result<f64, SloError> {
        let base = 0.12;
        let noise = (rand::random::<f64>() - 0.5) * 0.01;
        Ok((base + noise).max(0.0))
    }

    async fn simulate_clamp_rate() -> Result<f64, SloError> {
        // Simulate clamp rate with occasional warnings
        let base = 8.0;
        let spike = if rand::random::<f64>() < 0.05 { 15.0 } else { 0.0 };
        Ok(base + spike + (rand::random::<f64>() - 0.5) * 2.0)
    }

    async fn simulate_merged_bin_rate() -> Result<f64, SloError> {
        let base = 3.0;
        let spike = if rand::random::<f64>() < 0.02 { 8.0 } else { 0.0 };
        Ok(base + spike + (rand::random::<f64>() - 0.5) * 1.0)
    }

    async fn simulate_latency() -> Result<f64, SloError> {
        Ok(0.85 + (rand::random::<f64>() - 0.5) * 0.3)
    }

    async fn simulate_error_rate() -> Result<f64, SloError> {
        Ok(0.01 + (rand::random::<f64>() - 0.5) * 0.005)
    }
}

impl SloMetricsStore {
    fn new() -> Self {
        Self {
            aece_timeseries: VecDeque::new(),
            dece_timeseries: VecDeque::new(),
            brier_timeseries: VecDeque::new(),
            clamp_rate_timeseries: VecDeque::new(),
            merged_bin_rate_timeseries: VecDeque::new(),
            sla_compliance_stats: SlaComplianceStats {
                aece_compliance_percentage: 100.0,
                uptime_percentage: 99.95,
                error_budget_remaining: 80.0,
                mean_time_between_failures_minutes: 2160.0, // 36 hours
                mean_time_to_recovery_minutes: 12.5,
                last_updated: SystemTime::now(),
            },
            active_alerts: HashMap::new(),
            alert_history: VecDeque::new(),
            total_predictions: 0,
            calibration_calls: 0,
            error_count: 0,
            weekly_snapshots: VecDeque::new(),
        }
    }

    fn update_metrics(&mut self, metrics: CurrentMetrics) {
        let timestamp = metrics.timestamp;
        
        // Add new metrics to time series
        self.aece_timeseries.push_back(TimestampedMetric {
            timestamp,
            value: metrics.aece,
            metadata: HashMap::new(),
        });
        
        self.dece_timeseries.push_back(TimestampedMetric {
            timestamp,
            value: metrics.dece,
            metadata: HashMap::new(),
        });
        
        self.brier_timeseries.push_back(TimestampedMetric {
            timestamp,
            value: metrics.brier_score,
            metadata: HashMap::new(),
        });
        
        self.clamp_rate_timeseries.push_back(TimestampedMetric {
            timestamp,
            value: metrics.clamp_rate,
            metadata: HashMap::new(),
        });
        
        self.merged_bin_rate_timeseries.push_back(TimestampedMetric {
            timestamp,
            value: metrics.merged_bin_rate,
            metadata: HashMap::new(),
        });
        
        // Update counters
        self.total_predictions += metrics.prediction_count;
        self.calibration_calls += 1;
        if metrics.error_rate_percentage > 0.1 {
            self.error_count += 1;
        }
        
        // Trim old data (keep 24 hours)
        let cutoff_time = timestamp - Duration::from_secs(24 * 3600);
        self.trim_timeseries(&cutoff_time);
        
        // Update SLA compliance stats
        self.update_sla_compliance();
    }

    fn trim_timeseries(&mut self, cutoff_time: &SystemTime) {
        self.aece_timeseries.retain(|m| m.timestamp > *cutoff_time);
        self.dece_timeseries.retain(|m| m.timestamp > *cutoff_time);
        self.brier_timeseries.retain(|m| m.timestamp > *cutoff_time);
        self.clamp_rate_timeseries.retain(|m| m.timestamp > *cutoff_time);
        self.merged_bin_rate_timeseries.retain(|m| m.timestamp > *cutoff_time);
    }

    fn update_sla_compliance(&mut self) {
        // Calculate AECE compliance (percentage of time below threshold)
        let total_measurements = self.aece_timeseries.len() as f64;
        let compliant_measurements = self.aece_timeseries.iter()
            .filter(|m| m.value <= 0.01)
            .count() as f64;
        
        self.sla_compliance_stats.aece_compliance_percentage = 
            if total_measurements > 0.0 {
                (compliant_measurements / total_measurements) * 100.0
            } else {
                100.0
            };
        
        self.sla_compliance_stats.last_updated = SystemTime::now();
    }

    fn process_alert(&mut self, alert: SloAlert) {
        // Store active alert
        self.active_alerts.insert(alert.id.clone(), alert.clone());
        
        // Add to history
        self.alert_history.push_back(alert);
        
        // Limit history size
        if self.alert_history.len() > 1000 {
            self.alert_history.pop_front();
        }
    }
}

impl AlertManager {
    fn new(config: MonitoringConfig) -> Self {
        Self {
            active_alerts: HashMap::new(),
            consecutive_violations: HashMap::new(),
            last_alert_times: HashMap::new(),
            config,
        }
    }
}

impl ReportGenerator {
    fn new() -> Self {
        Self {
            template_store: HashMap::new(),
            historical_data_retention_days: 90,
        }
    }
}

// Supporting types

#[derive(Debug, Clone)]
struct CurrentMetrics {
    aece: f64,
    dece: f64,
    brier_score: f64,
    clamp_rate: f64,
    merged_bin_rate: f64,
    latency_p99_ms: f64,
    error_rate_percentage: f64,
    prediction_count: u64,
    timestamp: SystemTime,
}

#[derive(Debug, Clone, Serialize)]
pub struct DashboardStatus {
    pub monitoring_active: bool,
    pub active_alerts_count: u32,
    pub sla_compliance: SlaComplianceStats,
    pub last_metric_collection: SystemTime,
    pub error_budget_remaining: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct RealtimeMetrics {
    pub current_aece: f64,
    pub current_dece: f64,
    pub current_brier: f64,
    pub current_clamp_rate: f64,
    pub current_merged_bin_rate: f64,
    pub predictions_per_minute: f64,
    pub active_alerts: Vec<SloAlert>,
}

#[derive(Debug, thiserror::Error)]
pub enum SloError {
    #[error("Metrics collection failed: {0}")]
    MetricsCollectionError(String),
    
    #[error("Alert processing failed: {0}")]
    AlertProcessingError(String),
    
    #[error("Report generation failed: {0}")]
    ReportGenerationError(String),
    
    #[error("Insufficient data: {0}")]
    InsufficientData(String),
    
    #[error("Configuration error: {0}")]
    ConfigurationError(String),
}

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

    #[tokio::test]
    async fn test_dashboard_creation() {
        let config = MonitoringConfig::default();
        let dashboard = SloOperationsDashboard::new(config).await.unwrap();
        
        let status = dashboard.get_dashboard_status().await.unwrap();
        assert!(status.monitoring_active);
    }

    #[tokio::test]
    async fn test_metrics_collection() {
        let metrics = SloOperationsDashboard::collect_current_metrics().await.unwrap();
        assert!(metrics.aece >= 0.0);
        assert!(metrics.dece >= 0.0);
        assert!(metrics.brier_score >= 0.0);
    }

    #[tokio::test]
    async fn test_metric_summary_calculation() {
        let mut timeseries = VecDeque::new();
        for i in 0..100 {
            timeseries.push_back(TimestampedMetric {
                timestamp: SystemTime::now(),
                value: i as f64 / 100.0,
                metadata: HashMap::new(),
            });
        }
        
        let summary = SloOperationsDashboard::calculate_metric_summary(&timeseries).unwrap();
        assert!(summary.mean > 0.0);
        assert!(summary.p99 > summary.p95);
        assert!(summary.max > summary.min);
    }

    #[tokio::test]
    async fn test_sla_compliance_tracking() {
        let mut store = SloMetricsStore::new();
        
        let metrics = CurrentMetrics {
            aece: 0.005,  // Below threshold
            dece: 0.004,
            brier_score: 0.11,
            clamp_rate: 7.0,
            merged_bin_rate: 2.5,
            latency_p99_ms: 0.8,
            error_rate_percentage: 0.005,
            prediction_count: 1000,
            timestamp: SystemTime::now(),
        };
        
        store.update_metrics(metrics);
        
        assert!(store.sla_compliance_stats.aece_compliance_percentage > 90.0);
    }

    #[tokio::test]
    async fn test_trend_calculation() {
        let improving_values = vec![1.0, 0.9, 0.8, 0.7, 0.6, 0.5];
        let trend = SloOperationsDashboard::calculate_trend_direction(&improving_values);
        assert_eq!(trend, TrendDirection::Improving);
        
        let stable_values = vec![1.0, 1.0, 1.0, 1.0, 1.0, 1.0];
        let trend = SloOperationsDashboard::calculate_trend_direction(&stable_values);
        assert_eq!(trend, TrendDirection::Stable);
        
        let degrading_values = vec![0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
        let trend = SloOperationsDashboard::calculate_trend_direction(&degrading_values);
        assert_eq!(trend, TrendDirection::Degrading);
    }
}