scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Alerting and notification system for performance monitoring

use super::types::{
    AlertCondition, AlertEvent, AlertEventType, AlertRule, AlertSeverity, ComparisonOperator,
    ComprehensivePerformanceMetrics, NotificationChannel, PerformanceAlert,
};
use crate::error::{CoreError, CoreResult, ErrorContext};
use std::collections::VecDeque;
use std::time::{Duration, Instant, SystemTime};

/// Comprehensive alerting system for performance monitoring
#[allow(dead_code)]
#[derive(Debug)]
pub struct AlertingSystem {
    active_alerts: Vec<PerformanceAlert>,
    alert_rules: Vec<AlertRule>,
    alert_history: VecDeque<AlertEvent>,
    notification_channels: Vec<NotificationChannel>,
}

impl AlertingSystem {
    pub fn new() -> CoreResult<Self> {
        Ok(Self {
            active_alerts: Vec::new(),
            alert_rules: Self::default_alert_rules(),
            alert_history: VecDeque::with_capacity(1000),
            notification_channels: Vec::new(),
        })
    }

    fn default_alert_rules() -> Vec<AlertRule> {
        vec![
            AlertRule {
                name: "High CPU Usage".to_string(),
                condition: AlertCondition::Threshold {
                    metric: "cpu_utilization".to_string(),
                    operator: ComparisonOperator::GreaterThan,
                    value: 0.9,
                },
                severity: AlertSeverity::Warning,
                duration: Duration::from_secs(60),
            },
            AlertRule {
                name: "Critical CPU Usage".to_string(),
                condition: AlertCondition::Threshold {
                    metric: "cpu_utilization".to_string(),
                    operator: ComparisonOperator::GreaterThan,
                    value: 0.95,
                },
                severity: AlertSeverity::Critical,
                duration: Duration::from_secs(30),
            },
            AlertRule {
                name: "High Memory Usage".to_string(),
                condition: AlertCondition::Threshold {
                    metric: "memory_utilization".to_string(),
                    operator: ComparisonOperator::GreaterThan,
                    value: 0.9,
                },
                severity: AlertSeverity::Warning,
                duration: Duration::from_secs(120),
            },
            AlertRule {
                name: "Low Throughput".to_string(),
                condition: AlertCondition::Threshold {
                    metric: "operations_per_second".to_string(),
                    operator: ComparisonOperator::LessThan,
                    value: 100.0,
                },
                severity: AlertSeverity::Warning,
                duration: Duration::from_secs(180),
            },
        ]
    }

    pub fn check_and_trigger_alerts(
        &mut self,
        metrics: &ComprehensivePerformanceMetrics,
    ) -> CoreResult<()> {
        // Collect rules that need to trigger alerts to avoid borrowing conflicts
        let mut rules_to_trigger = Vec::new();
        for rule in &self.alert_rules {
            if self.evaluate_rule(rule, metrics)? {
                rules_to_trigger.push(rule.clone());
            }
        }

        // Trigger alerts for collected rules
        for rule in rules_to_trigger {
            let alert = PerformanceAlert {
                id: format!(
                    "alert_{}",
                    SystemTime::now()
                        .duration_since(SystemTime::UNIX_EPOCH)
                        .expect("Operation failed")
                        .as_millis()
                ),
                rule_name: rule.name.clone(),
                severity: rule.severity,
                message: format!("Alert triggered for rule: {}", rule.name),
                triggered_at: Instant::now(),
                acknowledged: false,
                resolved: false,
            };

            // Add to active alerts
            self.active_alerts.push(alert.clone());

            // Add to history
            self.alert_history.push_back(AlertEvent {
                alert,
                event_type: AlertEventType::Triggered,
                timestamp: Instant::now(),
            });
        }

        // Clean up resolved alerts
        self.clean_up_resolved_alerts(metrics)?;

        Ok(())
    }

    fn evaluate_rule(
        &self,
        rule: &AlertRule,
        metrics: &ComprehensivePerformanceMetrics,
    ) -> CoreResult<bool> {
        match &rule.condition {
            AlertCondition::Threshold {
                metric,
                operator,
                value,
            } => {
                let metric_value = self.get_metric_value(metric, metrics)?;

                let condition_met = match operator {
                    ComparisonOperator::GreaterThan => metric_value > *value,
                    ComparisonOperator::LessThan => metric_value < *value,
                    ComparisonOperator::Equal => (metric_value - value).abs() < 0.001,
                };

                Ok(condition_met)
            }
            AlertCondition::RateOfChange {
                metric,
                threshold,
                timeframe: _,
            } => {
                // Simplified rate of change calculation
                let current_value = self.get_metric_value(metric, metrics)?;
                // Would need historical data for proper rate calculation
                Ok(current_value.abs() > *threshold)
            }
        }
    }

    fn get_metric_value(
        &self,
        metric: &str,
        metrics: &ComprehensivePerformanceMetrics,
    ) -> CoreResult<f64> {
        match metric {
            "cpu_utilization" => Ok(metrics.cpu_utilization),
            "memory_utilization" => Ok(metrics.memory_utilization),
            "operations_per_second" => Ok(metrics.operations_per_second),
            "average_latency_ms" => Ok(metrics.average_latency_ms),
            "cache_miss_rate" => Ok(metrics.cache_miss_rate),
            _ => Err(CoreError::ValidationError(ErrorContext {
                message: format!("Unknown metric: {metric}"),
                location: None,
                cause: None,
            })),
        }
    }

    fn check_alerts(&mut self, metrics: &ComprehensivePerformanceMetrics) -> CoreResult<()> {
        // Collect rules that need to trigger alerts to avoid borrowing conflicts
        let mut rules_to_trigger = Vec::new();
        for rule in &self.alert_rules {
            if self.evaluate_rule(rule, metrics)? {
                // Check if alert is already active
                if !self
                    .active_alerts
                    .iter()
                    .any(|alert| alert.rule_name == rule.name)
                {
                    rules_to_trigger.push(rule.clone());
                }
            }
        }

        // Trigger alerts for rules that are not already active
        for rule in rules_to_trigger {
            let alert = PerformanceAlert {
                id: format!(
                    "alert_{}",
                    SystemTime::now()
                        .duration_since(SystemTime::UNIX_EPOCH)
                        .expect("Operation failed")
                        .as_millis()
                ),
                rule_name: rule.name.clone(),
                severity: rule.severity,
                message: format!("Alert triggered for rule: {}", rule.name),
                triggered_at: Instant::now(),
                acknowledged: false,
                resolved: false,
            };

            // Add to active alerts
            self.active_alerts.push(alert.clone());

            // Add to history
            self.alert_history.push_back(AlertEvent {
                alert,
                event_type: AlertEventType::Triggered,
                timestamp: Instant::now(),
            });

            // Implement notification sending
            self.send_notifications(&rule.name, rule.severity)?;
        }

        Ok(())
    }

    fn clean_up_resolved_alerts(
        &mut self,
        metrics: &ComprehensivePerformanceMetrics,
    ) -> CoreResult<()> {
        let mut resolved_alerts = Vec::new();

        // Collect rules and alert info to avoid borrowing conflicts
        let rule_evaluations: Vec<(usize, bool)> = self
            .active_alerts
            .iter()
            .enumerate()
            .map(|(index, alert)| {
                if let Some(rule) = self.alert_rules.iter().find(|r| r.name == alert.rule_name) {
                    let is_resolved = match self.evaluate_rule(rule, metrics) {
                        Ok(condition_met) => !condition_met,
                        Err(_) => false,
                    };
                    (index, is_resolved)
                } else {
                    (index, false)
                }
            })
            .collect();

        // Mark alerts as resolved based on evaluations
        for (index, is_resolved) in rule_evaluations {
            if is_resolved {
                if let Some(alert) = self.active_alerts.get_mut(index) {
                    alert.resolved = true;
                    resolved_alerts.push(alert.clone());
                }
            }
        }

        // Remove resolved alerts from active list
        self.active_alerts.retain(|alert| !alert.resolved);

        // Add resolved events to history
        for alert in resolved_alerts {
            self.alert_history.push_back(AlertEvent {
                alert,
                event_type: AlertEventType::Resolved,
                timestamp: Instant::now(),
            });
        }

        Ok(())
    }

    fn send_alert(&self, alertname: &str, severity: AlertSeverity) -> CoreResult<()> {
        for channel in &self.notification_channels {
            channel.send_notification(alertname, severity)?;
        }
        Ok(())
    }

    pub fn get_active_alerts(&self) -> CoreResult<Vec<PerformanceAlert>> {
        Ok(self.active_alerts.clone())
    }

    pub fn acknowledge_alert(&mut self, alertid: &str) -> CoreResult<()> {
        if let Some(alert) = self.active_alerts.iter_mut().find(|a| a.id == alertid) {
            alert.acknowledged = true;
        }
        Ok(())
    }

    /// Send notifications through all enabled channels
    fn send_notifications(&self, alert_name: &str, severity: AlertSeverity) -> CoreResult<()> {
        // Check if notifications should be sent based on severity
        let should_notify = match severity {
            AlertSeverity::Critical => true,
            AlertSeverity::Warning => {
                // For warning severity, check if there are multiple active alerts
                self.active_alerts.len() > 3
            }
            AlertSeverity::Info => false, // Don't notify for info severity
        };

        if !should_notify {
            return Ok(());
        }

        // Send notification through each enabled channel
        let mut errors = Vec::new();
        for channel in &self.notification_channels {
            if let Err(e) = channel.send_notification(alert_name, severity) {
                // Collect errors but don't fail the entire operation
                errors.push(format!("Failed to send notification: {}", e));
            }
        }

        // Log any notification errors (in production, this would use proper logging)
        if !errors.is_empty() {
            #[cfg(feature = "logging")]
            log::warn!("Notification errors: {:?}", errors);
        }

        Ok(())
    }

    /// Add a new alert rule to the system
    pub fn add_alert_rule(&mut self, rule: AlertRule) -> CoreResult<()> {
        // Check if rule with same name already exists
        if self.alert_rules.iter().any(|r| r.name == rule.name) {
            return Err(CoreError::ValidationError(ErrorContext {
                message: format!("Alert rule with name '{}' already exists", rule.name),
                location: None,
                cause: None,
            }));
        }

        self.alert_rules.push(rule);
        Ok(())
    }

    /// Remove an alert rule by name
    pub fn remove_alert_rule(&mut self, rule_name: &str) -> CoreResult<bool> {
        let initial_len = self.alert_rules.len();
        self.alert_rules.retain(|rule| rule.name != rule_name);
        Ok(self.alert_rules.len() < initial_len)
    }

    /// Add a notification channel
    pub fn add_notification_channel(&mut self, channel: NotificationChannel) -> CoreResult<()> {
        self.notification_channels.push(channel);
        Ok(())
    }

    /// Get alert history (last N events)
    pub fn get_alert_history(&self, limit: Option<usize>) -> CoreResult<Vec<AlertEvent>> {
        let history: Vec<AlertEvent> = if let Some(limit) = limit {
            self.alert_history
                .iter()
                .rev()
                .take(limit)
                .cloned()
                .collect()
        } else {
            self.alert_history.iter().cloned().collect()
        };

        Ok(history)
    }

    /// Get statistics about alerting system
    pub fn get_alerting_stats(&self) -> CoreResult<AlertingStats> {
        let total_rules = self.alert_rules.len();
        let active_alerts_count = self.active_alerts.len();
        let total_events = self.alert_history.len();

        // Count events by type
        let mut triggered_count = 0;
        let mut resolved_count = 0;
        let mut acknowledged_count = 0;

        for event in &self.alert_history {
            match event.event_type {
                AlertEventType::Triggered => triggered_count += 1,
                AlertEventType::Resolved => resolved_count += 1,
                AlertEventType::Acknowledged => acknowledged_count += 1,
            }
        }

        // Count alerts by severity
        let mut critical_count = 0;
        let mut warning_count = 0;
        let mut info_count = 0;

        for alert in &self.active_alerts {
            match alert.severity {
                AlertSeverity::Critical => critical_count += 1,
                AlertSeverity::Warning => warning_count += 1,
                AlertSeverity::Info => info_count += 1,
            }
        }

        Ok(AlertingStats {
            total_rules,
            active_alerts_count,
            total_events,
            triggered_count,
            resolved_count,
            acknowledged_count,
            critical_count,
            warning_count,
            info_count,
            notification_channels_count: self.notification_channels.len(),
        })
    }

    /// Resolve all alerts for a specific rule
    pub fn resolve_alerts_for_rule(&mut self, rule_name: &str) -> CoreResult<usize> {
        let mut resolved_count = 0;
        let mut resolved_alerts = Vec::new();

        // Mark matching alerts as resolved
        for alert in &mut self.active_alerts {
            if alert.rule_name == rule_name && !alert.resolved {
                alert.resolved = true;
                resolved_alerts.push(alert.clone());
                resolved_count += 1;
            }
        }

        // Remove resolved alerts from active list
        self.active_alerts.retain(|alert| !alert.resolved);

        // Add resolved events to history
        for alert in resolved_alerts {
            self.alert_history.push_back(AlertEvent {
                alert,
                event_type: AlertEventType::Resolved,
                timestamp: Instant::now(),
            });
        }

        Ok(resolved_count)
    }

    /// Clear old alert history to prevent memory buildup
    pub fn cleanup_alert_history(&mut self, max_age: Duration) -> CoreResult<usize> {
        let cutoff_time = Instant::now() - max_age;
        let initial_count = self.alert_history.len();

        // Remove events older than cutoff time
        self.alert_history.retain(|event| event.timestamp > cutoff_time);

        let removed_count = initial_count - self.alert_history.len();
        Ok(removed_count)
    }
}

/// Statistics about the alerting system
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct AlertingStats {
    pub total_rules: usize,
    pub active_alerts_count: usize,
    pub total_events: usize,
    pub triggered_count: usize,
    pub resolved_count: usize,
    pub acknowledged_count: usize,
    pub critical_count: usize,
    pub warning_count: usize,
    pub info_count: usize,
    pub notification_channels_count: usize,
}