scirs2_metrics/optimization/distributed_advanced/
monitoring.rs1use crate::error::{MetricsError, Result};
6use std::collections::HashMap;
7use std::time::{Duration, Instant};
8
9#[derive(Debug, Clone)]
11pub struct MonitoringManager {
12 node_id: String,
13 metrics: HashMap<String, MetricValue>,
14 alerts: Vec<Alert>,
15 thresholds: HashMap<String, Threshold>,
16}
17
18#[derive(Debug, Clone)]
19pub enum MetricValue {
20 Counter(u64),
21 Gauge(f64),
22 Histogram(Vec<f64>),
23 Timer(Duration),
24}
25
26#[derive(Debug, Clone)]
27pub struct Alert {
28 pub metric_name: String,
29 pub level: AlertLevel,
30 pub message: String,
31 pub timestamp: Instant,
32}
33
34#[derive(Debug, Clone)]
35pub enum AlertLevel {
36 Info,
37 Warning,
38 Critical,
39}
40
41#[derive(Debug, Clone)]
42pub struct Threshold {
43 pub warning_level: f64,
44 pub critical_level: f64,
45}
46
47impl MonitoringManager {
48 pub fn new(node_id: String) -> Self {
49 Self {
50 node_id,
51 metrics: HashMap::new(),
52 alerts: Vec::new(),
53 thresholds: HashMap::new(),
54 }
55 }
56
57 pub fn record_metric(&mut self, name: String, value: MetricValue) -> Result<()> {
58 self.metrics.insert(name.clone(), value);
59 self.check_thresholds(&name)?;
60 Ok(())
61 }
62
63 pub fn set_threshold(&mut self, metric_name: String, threshold: Threshold) {
64 self.thresholds.insert(metric_name, threshold);
65 }
66
67 pub fn get_metric(&self, name: &str) -> Option<&MetricValue> {
68 self.metrics.get(name)
69 }
70
71 pub fn get_alerts(&self) -> &[Alert] {
72 &self.alerts
73 }
74
75 fn check_thresholds(&mut self, metric_name: &str) -> Result<()> {
76 if let Some(threshold) = self.thresholds.get(metric_name) {
77 if let Some(metric) = self.metrics.get(metric_name) {
78 let value = match metric {
79 MetricValue::Gauge(v) => *v,
80 MetricValue::Counter(v) => *v as f64,
81 MetricValue::Timer(d) => d.as_secs_f64(),
82 _ => return Ok(()),
83 };
84
85 if value >= threshold.critical_level {
86 self.alerts.push(Alert {
87 metric_name: metric_name.to_string(),
88 level: AlertLevel::Critical,
89 message: format!("Critical threshold exceeded: {}", value),
90 timestamp: Instant::now(),
91 });
92 } else if value >= threshold.warning_level {
93 self.alerts.push(Alert {
94 metric_name: metric_name.to_string(),
95 level: AlertLevel::Warning,
96 message: format!("Warning threshold exceeded: {}", value),
97 timestamp: Instant::now(),
98 });
99 }
100 }
101 }
102 Ok(())
103 }
104
105 pub fn clear_alerts(&mut self) {
106 self.alerts.clear();
107 }
108}