leptos-sync-core 0.9.0

Core synchronization library for Leptos applications
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
//! Alert management and notification system

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// Alert rule definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertRule {
    /// Unique identifier for the rule
    pub id: String,
    /// Human-readable name for the rule
    pub name: String,
    /// Description of what this rule monitors
    pub description: String,
    /// Metric name to monitor
    pub metric_name: String,
    /// Condition that triggers the alert
    pub condition: AlertCondition,
    /// Severity level of the alert
    pub severity: AlertSeverity,
    /// Whether the rule is enabled
    pub enabled: bool,
    /// Cooldown period between alerts (in seconds)
    pub cooldown_seconds: u64,
}

impl AlertRule {
    /// Create a new alert rule
    pub fn new(
        id: String,
        name: String,
        metric_name: String,
        condition: AlertCondition,
        severity: AlertSeverity,
    ) -> Self {
        Self {
            id,
            name,
            description: String::new(),
            metric_name,
            condition,
            severity,
            enabled: true,
            cooldown_seconds: 300, // 5 minutes default
        }
    }

    /// Set the description
    pub fn with_description(mut self, description: String) -> Self {
        self.description = description;
        self
    }

    /// Set the cooldown period
    pub fn with_cooldown(mut self, cooldown_seconds: u64) -> Self {
        self.cooldown_seconds = cooldown_seconds;
        self
    }

    /// Enable or disable the rule
    pub fn set_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }
}

/// Alert condition for triggering alerts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertCondition {
    /// Comparison operator
    pub operator: ComparisonOperator,
    /// Threshold value
    pub threshold: f64,
    /// Duration the condition must be true before triggering (in seconds)
    pub duration_seconds: u64,
}

impl AlertCondition {
    /// Create a new alert condition
    pub fn new(operator: ComparisonOperator, threshold: f64, duration_seconds: u64) -> Self {
        Self {
            operator,
            threshold,
            duration_seconds,
        }
    }

    /// Check if a value satisfies this condition
    pub fn is_satisfied(&self, value: f64) -> bool {
        match self.operator {
            ComparisonOperator::GreaterThan => value > self.threshold,
            ComparisonOperator::GreaterThanOrEqual => value >= self.threshold,
            ComparisonOperator::LessThan => value < self.threshold,
            ComparisonOperator::LessThanOrEqual => value <= self.threshold,
            ComparisonOperator::Equal => (value - self.threshold).abs() < f64::EPSILON,
            ComparisonOperator::NotEqual => (value - self.threshold).abs() >= f64::EPSILON,
        }
    }
}

/// Comparison operators for alert conditions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ComparisonOperator {
    /// Greater than
    GreaterThan,
    /// Greater than or equal
    GreaterThanOrEqual,
    /// Less than
    LessThan,
    /// Less than or equal
    LessThanOrEqual,
    /// Equal to
    Equal,
    /// Not equal to
    NotEqual,
}

/// Alert severity levels
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum AlertSeverity {
    /// Low severity - informational
    Low,
    /// Medium severity - warning
    Medium,
    /// High severity - error
    High,
    /// Critical severity - system failure
    Critical,
}

/// An active alert
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Alert {
    /// Unique identifier for the alert
    pub id: String,
    /// Rule that triggered this alert
    pub rule_id: String,
    /// Alert severity
    pub severity: AlertSeverity,
    /// Alert message
    pub message: String,
    /// Timestamp when the alert was triggered
    pub triggered_at: u64,
    /// Timestamp when the alert was resolved (if resolved)
    pub resolved_at: Option<u64>,
    /// Whether the alert is currently active
    pub is_active: bool,
    /// Additional context data
    pub context: HashMap<String, String>,
}

impl Alert {
    /// Create a new alert
    pub fn new(
        id: String,
        rule_id: String,
        severity: AlertSeverity,
        message: String,
    ) -> Self {
        Self {
            id,
            rule_id,
            severity,
            message,
            triggered_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            resolved_at: None,
            is_active: true,
            context: HashMap::new(),
        }
    }

    /// Add context data to the alert
    pub fn with_context(mut self, context: HashMap<String, String>) -> Self {
        self.context = context;
        self
    }

    /// Resolve the alert
    pub fn resolve(&mut self) {
        self.is_active = false;
        self.resolved_at = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        );
    }

    /// Get the duration the alert has been active
    pub fn duration(&self) -> Duration {
        let end_time = self.resolved_at.unwrap_or_else(|| {
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs()
        });
        Duration::from_secs(end_time - self.triggered_at)
    }
}

/// Alert manager for handling alert rules and notifications
#[derive(Debug, Clone)]
pub struct AlertManager {
    /// Alert rules
    rules: HashMap<String, AlertRule>,
    /// Active alerts
    active_alerts: HashMap<String, Alert>,
    /// Alert history
    alert_history: Vec<Alert>,
    /// Maximum number of alerts to keep in history
    max_history_size: usize,
    /// Last trigger times for cooldown tracking
    last_trigger_times: HashMap<String, u64>,
}

impl AlertManager {
    /// Create a new alert manager
    pub fn new() -> Self {
        Self {
            rules: HashMap::new(),
            active_alerts: HashMap::new(),
            alert_history: Vec::new(),
            max_history_size: 1000,
            last_trigger_times: HashMap::new(),
        }
    }

    /// Create an alert manager with configuration
    pub fn with_config(config: AlertConfig) -> Self {
        Self {
            rules: HashMap::new(),
            active_alerts: HashMap::new(),
            alert_history: Vec::new(),
            max_history_size: config.max_history_size,
            last_trigger_times: HashMap::new(),
        }
    }

    /// Add an alert rule
    pub fn add_rule(&mut self, rule: AlertRule) {
        self.rules.insert(rule.id.clone(), rule);
    }

    /// Remove an alert rule
    pub fn remove_rule(&mut self, rule_id: &str) {
        self.rules.remove(rule_id);
        // Also remove any active alerts for this rule
        self.active_alerts.retain(|_, alert| alert.rule_id != rule_id);
    }

    /// Update an alert rule
    pub fn update_rule(&mut self, rule: AlertRule) {
        self.rules.insert(rule.id.clone(), rule);
    }

    /// Get all alert rules
    pub fn get_rules(&self) -> Vec<&AlertRule> {
        self.rules.values().collect()
    }

    /// Get a specific alert rule
    pub fn get_rule(&self, rule_id: &str) -> Option<&AlertRule> {
        self.rules.get(rule_id)
    }

    /// Check if a metric value should trigger an alert
    pub fn check_metric(&mut self, metric_name: &str, value: f64) -> Vec<Alert> {
        let mut new_alerts = Vec::new();
        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Find rules that match this metric
        let rule_ids: Vec<String> = self.rules.keys().cloned().collect();
        let mut rules_to_resolve = Vec::new();
        
        for rule_id in rule_ids {
            if let Some(rule) = self.rules.get(&rule_id) {
                if !rule.enabled || rule.metric_name != metric_name {
                    continue;
                }

                // Check cooldown
                if let Some(last_trigger) = self.last_trigger_times.get(&rule.id) {
                    if current_time - last_trigger < rule.cooldown_seconds {
                        continue;
                    }
                }

                // Check if condition is satisfied
                if rule.condition.is_satisfied(value) {
                    // Check if there's already an active alert for this rule
                    let has_active_alert = self.active_alerts.values()
                        .any(|alert| alert.rule_id == rule.id && alert.is_active);

                    if !has_active_alert {
                        // Create new alert
                        let alert_id = format!("{}_{}", rule.id, current_time);
                        let message = format!(
                            "Alert triggered: {} (value: {}, threshold: {})",
                            rule.name, value, rule.condition.threshold
                        );

                        let mut alert = Alert::new(
                            alert_id,
                            rule.id.clone(),
                            rule.severity.clone(),
                            message,
                        );

                        // Add context
                        let mut context = HashMap::new();
                        context.insert("metric_name".to_string(), metric_name.to_string());
                        context.insert("value".to_string(), value.to_string());
                        context.insert("threshold".to_string(), rule.condition.threshold.to_string());
                        alert.context = context;

                        self.active_alerts.insert(alert.id.clone(), alert.clone());
                        self.alert_history.push(alert.clone());
                        self.last_trigger_times.insert(rule.id.clone(), current_time);
                        new_alerts.push(alert);
                    }
                } else {
                    // Condition not satisfied, mark for resolution
                    rules_to_resolve.push(rule.id.clone());
                }
            }
        }
        
        // Resolve alerts for rules that no longer match
        for rule_id in rules_to_resolve {
            self.resolve_alerts_for_rule(&rule_id);
        }

        // Clean up old history
        self.cleanup_history();

        new_alerts
    }

    /// Get all active alerts
    pub fn get_active_alerts(&self) -> Vec<&Alert> {
        self.active_alerts.values().filter(|alert| alert.is_active).collect()
    }

    /// Get alerts by severity
    pub fn get_alerts_by_severity(&self, severity: &AlertSeverity) -> Vec<&Alert> {
        self.active_alerts
            .values()
            .filter(|alert| alert.is_active && &alert.severity == severity)
            .collect()
    }

    /// Resolve an alert by ID
    pub fn resolve_alert(&mut self, alert_id: &str) -> bool {
        if let Some(alert) = self.active_alerts.get_mut(alert_id) {
            alert.resolve();
            true
        } else {
            false
        }
    }

    /// Resolve all alerts for a specific rule
    fn resolve_alerts_for_rule(&mut self, rule_id: &str) {
        for alert in self.active_alerts.values_mut() {
            if alert.rule_id == rule_id && alert.is_active {
                alert.resolve();
            }
        }
    }

    /// Get alert history
    pub fn get_alert_history(&self, limit: Option<usize>) -> Vec<&Alert> {
        let limit = limit.unwrap_or(self.alert_history.len());
        self.alert_history
            .iter()
            .rev()
            .take(limit)
            .collect()
    }

    /// Clear alert history
    pub fn clear_history(&mut self) {
        self.alert_history.clear();
    }

    /// Clean up old history entries
    fn cleanup_history(&mut self) {
        if self.alert_history.len() > self.max_history_size {
            let excess = self.alert_history.len() - self.max_history_size;
            self.alert_history.drain(0..excess);
        }
    }

    /// Get alert statistics
    pub fn get_stats(&self) -> AlertStats {
        let active_count = self.active_alerts.values().filter(|a| a.is_active).count();
        let critical_count = self.get_alerts_by_severity(&AlertSeverity::Critical).len();
        let high_count = self.get_alerts_by_severity(&AlertSeverity::High).len();
        let medium_count = self.get_alerts_by_severity(&AlertSeverity::Medium).len();
        let low_count = self.get_alerts_by_severity(&AlertSeverity::Low).len();

        AlertStats {
            total_rules: self.rules.len(),
            active_alerts: active_count,
            critical_alerts: critical_count,
            high_alerts: high_count,
            medium_alerts: medium_count,
            low_alerts: low_count,
            total_history: self.alert_history.len(),
        }
    }
}

impl Default for AlertManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Alert statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertStats {
    /// Total number of alert rules
    pub total_rules: usize,
    /// Number of active alerts
    pub active_alerts: usize,
    /// Number of critical alerts
    pub critical_alerts: usize,
    /// Number of high severity alerts
    pub high_alerts: usize,
    /// Number of medium severity alerts
    pub medium_alerts: usize,
    /// Number of low severity alerts
    pub low_alerts: usize,
    /// Total number of alerts in history
    pub total_history: usize,
}

/// Configuration for alert management
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertConfig {
    /// Maximum number of alerts to keep in history
    pub max_history_size: usize,
}

impl Default for AlertConfig {
    fn default() -> Self {
        Self {
            max_history_size: 1000,
        }
    }
}

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

    #[test]
    fn test_alert_rule_creation() {
        let condition = AlertCondition::new(ComparisonOperator::GreaterThan, 80.0, 60);
        let rule = AlertRule::new(
            "cpu_high".to_string(),
            "High CPU Usage".to_string(),
            "cpu_usage".to_string(),
            condition,
            AlertSeverity::High,
        );

        assert_eq!(rule.id, "cpu_high");
        assert_eq!(rule.metric_name, "cpu_usage");
        assert!(rule.enabled);
    }

    #[test]
    fn test_alert_condition() {
        let condition = AlertCondition::new(ComparisonOperator::GreaterThan, 80.0, 60);
        
        assert!(condition.is_satisfied(85.0));
        assert!(!condition.is_satisfied(75.0));
        assert!(!condition.is_satisfied(80.0));
    }

    #[test]
    fn test_alert_creation() {
        let alert = Alert::new(
            "alert_1".to_string(),
            "rule_1".to_string(),
            AlertSeverity::High,
            "Test alert".to_string(),
        );

        assert_eq!(alert.id, "alert_1");
        assert!(alert.is_active);
        assert!(alert.resolved_at.is_none());
    }

    #[test]
    fn test_alert_resolution() {
        let mut alert = Alert::new(
            "alert_1".to_string(),
            "rule_1".to_string(),
            AlertSeverity::High,
            "Test alert".to_string(),
        );

        assert!(alert.is_active);
        alert.resolve();
        assert!(!alert.is_active);
        assert!(alert.resolved_at.is_some());
    }

    #[test]
    fn test_alert_manager() {
        let mut manager = AlertManager::new();
        
        // Add a rule
        let condition = AlertCondition::new(ComparisonOperator::GreaterThan, 80.0, 60);
        let rule = AlertRule::new(
            "cpu_high".to_string(),
            "High CPU Usage".to_string(),
            "cpu_usage".to_string(),
            condition,
            AlertSeverity::High,
        );
        manager.add_rule(rule);

        // Check metric that should trigger alert
        let alerts = manager.check_metric("cpu_usage", 85.0);
        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].severity, AlertSeverity::High);

        // Check that we have an active alert
        let active_alerts = manager.get_active_alerts();
        assert_eq!(active_alerts.len(), 1);

        // Check metric that should not trigger alert
        let alerts = manager.check_metric("cpu_usage", 75.0);
        assert_eq!(alerts.len(), 0);

        // Check that the alert was resolved
        let active_alerts = manager.get_active_alerts();
        assert_eq!(active_alerts.len(), 0);
    }

    #[test]
    fn test_alert_cooldown() {
        let mut manager = AlertManager::new();
        
        let condition = AlertCondition::new(ComparisonOperator::GreaterThan, 80.0, 60);
        let rule = AlertRule::new(
            "cpu_high".to_string(),
            "High CPU Usage".to_string(),
            "cpu_usage".to_string(),
            condition,
            AlertSeverity::High,
        ).with_cooldown(300); // 5 minutes
        manager.add_rule(rule);

        // First trigger should create alert
        let alerts = manager.check_metric("cpu_usage", 85.0);
        assert_eq!(alerts.len(), 1);

        // Second trigger within cooldown should not create alert
        let alerts = manager.check_metric("cpu_usage", 90.0);
        assert_eq!(alerts.len(), 0);
    }
}