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
//! Monitoring System
//!
//! This module provides comprehensive monitoring capabilities including:
//! - Metrics collection and aggregation
//! - Alert management and notification
//! - Health reporting and status monitoring
//! - Performance tracking and analysis

pub mod metrics;
pub mod alerts;
pub mod health;
pub mod config;

// Re-export main types for convenience
pub use metrics::{
    Metric, TimeRange, AggregationType, AggregatedMetric, MetricsCollector, MetricsConfig,
};
pub use alerts::{
    AlertRule, AlertCondition, ComparisonOperator, AlertSeverity, Alert, AlertManager,
    AlertStats, AlertConfig,
};
pub use health::{
    HealthCheck, HealthStatus, SystemStatus, HealthCheckResult, HealthReporter, HealthStats,
    HealthConfig,
};
pub use config::{
    MonitorConfig, MonitoringStats, PerformanceConfig, ResourceConfig, ExtendedMonitorConfig,
};

use std::sync::Arc;
use tokio::sync::RwLock;

/// Reliability monitoring system
#[derive(Debug, Clone)]
pub struct ReliabilityMonitor {
    /// Metrics collector
    pub metrics_collector: MetricsCollector,
    /// Alert manager
    pub alert_manager: AlertManager,
    /// Health reporter
    pub health_reporter: HealthReporter,
    /// Monitoring statistics
    pub stats: Arc<RwLock<MonitoringStats>>,
    /// Whether the system is initialized
    pub initialized: bool,
}

impl ReliabilityMonitor {
    /// Create a new reliability monitor
    pub fn new() -> Self {
        Self {
            metrics_collector: MetricsCollector::new(),
            alert_manager: AlertManager::new(),
            health_reporter: HealthReporter::new(),
            stats: Arc::new(RwLock::new(MonitoringStats::new())),
            initialized: false,
        }
    }
    
    /// Create a new reliability monitor with configuration
    pub fn with_config(config: MonitorConfig) -> Self {
        Self {
            metrics_collector: MetricsCollector::with_config(config.metrics_config),
            alert_manager: AlertManager::with_config(config.alert_config),
            health_reporter: HealthReporter::with_config(config.health_config),
            stats: Arc::new(RwLock::new(MonitoringStats::new())),
            initialized: false,
        }
    }

    /// Initialize the monitoring system
    pub async fn initialize(&mut self) -> Result<(), String> {
        if self.initialized {
            return Err("Monitoring system is already initialized".to_string());
        }

        // Initialize components
        self.metrics_collector = MetricsCollector::new();
        self.alert_manager = AlertManager::new();
        self.health_reporter = HealthReporter::new();

        self.initialized = true;
        Ok(())
    }

    /// Record a metric
    pub fn record_metric(&mut self, metric: Metric) {
        if !self.initialized {
            return;
        }

        self.metrics_collector.record(metric.clone());
        
        // Check if this metric should trigger any alerts
        let alerts = self.alert_manager.check_metric(&metric.name, metric.value);
        
        // Update statistics
        if let Ok(mut stats) = self.stats.try_write() {
            stats.increment_metrics_collected();
            if !alerts.is_empty() {
                stats.increment_alerts_triggered();
            }
        }
    }

    /// Add an alert rule
    pub fn add_alert_rule(&mut self, rule: AlertRule) {
        if !self.initialized {
            return;
        }
        self.alert_manager.add_rule(rule);
    }

    /// Add a health check
    pub fn add_health_check(&mut self, health_check: HealthCheck) {
        if !self.initialized {
            return;
        }
        self.health_reporter.add_health_check(health_check);
    }

    /// Perform all health checks
    pub fn perform_health_checks(&mut self) -> Vec<HealthCheckResult> {
        if !self.initialized {
            return Vec::new();
        }

        let results = self.health_reporter.perform_all_health_checks();
        
        // Update statistics
        if let Ok(mut stats) = self.stats.try_write() {
            stats.increment_health_checks();
        }

        results
    }

    /// Get the current system status
    pub fn get_system_status(&mut self) -> SystemStatus {
        if !self.initialized {
            return SystemStatus::new();
        }
        self.health_reporter.get_current_system_status()
    }

    /// Get monitoring statistics
    pub async fn get_stats(&self) -> Result<MonitoringStats, String> {
        if !self.initialized {
            return Err("Monitoring system is not initialized".to_string());
        }

        let stats = self.stats.read().await;
        Ok(stats.clone())
    }

    /// Get alert statistics
    pub fn get_alert_stats(&self) -> AlertStats {
        if !self.initialized {
            return AlertStats {
                total_rules: 0,
                active_alerts: 0,
                critical_alerts: 0,
                high_alerts: 0,
                medium_alerts: 0,
                low_alerts: 0,
                total_history: 0,
            };
        }
        self.alert_manager.get_stats()
    }

    /// Get health statistics
    pub fn get_health_stats(&self) -> HealthStats {
        if !self.initialized {
            return HealthStats {
                total_checks: 0,
                healthy_checks: 0,
                degraded_checks: 0,
                unhealthy_checks: 0,
                unknown_checks: 0,
                overall_status: HealthStatus::Unknown,
                uptime_seconds: 0,
            };
        }
        self.health_reporter.get_health_stats()
    }

    /// Get metrics for a specific name and time range
    pub fn get_metrics(&self, name: &str, time_range: &TimeRange) -> Vec<&Metric> {
        if !self.initialized {
            return Vec::new();
        }
        self.metrics_collector.get_metrics(name, time_range)
    }

    /// Get aggregated metrics
    pub fn get_aggregated_metrics(
        &self,
        name: &str,
        time_range: &TimeRange,
        aggregation_type: AggregationType,
    ) -> Option<AggregatedMetric> {
        if !self.initialized {
            return None;
        }
        self.metrics_collector.aggregate_metrics(name, time_range, aggregation_type)
    }

    /// Get all active alerts
    pub fn get_active_alerts(&self) -> Vec<&Alert> {
        if !self.initialized {
            return Vec::new();
        }
        self.alert_manager.get_active_alerts()
    }

    /// Resolve an alert
    pub fn resolve_alert(&mut self, alert_id: &str) -> bool {
        if !self.initialized {
            return false;
        }
        self.alert_manager.resolve_alert(alert_id)
    }

    /// Get all metric names
    pub fn get_metric_names(&self) -> Vec<String> {
        if !self.initialized {
            return Vec::new();
        }
        self.metrics_collector.get_metric_names()
    }

    /// Clear all metrics
    pub fn clear_metrics(&mut self) {
        if !self.initialized {
            return;
        }
        self.metrics_collector.clear();
    }

    /// Clear alert history
    pub fn clear_alert_history(&mut self) {
        if !self.initialized {
            return;
        }
        self.alert_manager.clear_history();
    }

    /// Shutdown the monitoring system
    pub async fn shutdown(&mut self) -> Result<(), String> {
        if !self.initialized {
            return Err("Monitoring system is not initialized".to_string());
        }

        // Perform final health checks
        self.perform_health_checks();

        // Clear all data
        self.clear_metrics();
        self.clear_alert_history();

        self.initialized = false;
        Ok(())
    }
}

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

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

    #[test]
    fn test_reliability_monitor_creation() {
        let monitor = ReliabilityMonitor::new();
        assert!(!monitor.initialized);
    }

    #[test]
    fn test_reliability_monitor_with_config() {
        let config = MonitorConfig::new();
        let monitor = ReliabilityMonitor::with_config(config);
        assert!(!monitor.initialized);
    }

    #[tokio::test]
    async fn test_reliability_monitor_initialization() {
        let mut monitor = ReliabilityMonitor::new();
        
        // Should not be initialized initially
        assert!(!monitor.initialized);
        
        // Initialize the monitor
        let result = monitor.initialize().await;
        assert!(result.is_ok());
        assert!(monitor.initialized);
        
        // Try to initialize again (should fail)
        let result = monitor.initialize().await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_reliability_monitor_operations() {
        let mut monitor = ReliabilityMonitor::new();
        
        // Operations should not work when not initialized
        let metric = Metric::new("test_metric".to_string(), 42.0);
        monitor.record_metric(metric);
        
        let metrics = monitor.get_metrics("test_metric", &TimeRange::last_seconds(3600));
        assert!(metrics.is_empty());
        
        // Initialize the monitor
        let result = monitor.initialize();
        assert!(result.await.is_ok());
        
        // Now operations should work
        let metric = Metric::new("test_metric".to_string(), 42.0);
        monitor.record_metric(metric);
        
        let metrics = monitor.get_metrics("test_metric", &TimeRange::last_seconds(3600));
        assert_eq!(metrics.len(), 1);
        assert_eq!(metrics[0].value, 42.0);
    }

    #[tokio::test]
    async fn test_reliability_monitor_alert_integration() {
        let mut monitor = ReliabilityMonitor::new();
        monitor.initialize().await.unwrap();
        
        // Add an alert 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,
        );
        monitor.add_alert_rule(rule);
        
        // Record a metric that should trigger the alert
        let metric = Metric::new("cpu_usage".to_string(), 85.0);
        monitor.record_metric(metric);
        
        // Check that we have an active alert
        let active_alerts = monitor.get_active_alerts();
        assert_eq!(active_alerts.len(), 1);
        assert_eq!(active_alerts[0].severity, AlertSeverity::High);
    }

    #[tokio::test]
    async fn test_reliability_monitor_health_integration() {
        let mut monitor = ReliabilityMonitor::new();
        monitor.initialize().await.unwrap();
        
        // Add a health check
        let health_check = HealthCheck::new(
            "database".to_string(),
            "Database Health".to_string(),
            "Checks database connectivity".to_string(),
            "check_database".to_string(),
        );
        monitor.add_health_check(health_check);
        
        // Perform health checks
        let results = monitor.perform_health_checks();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].check_id, "database");
        
        // Get system status
        let status = monitor.get_system_status();
        assert!(status.uptime_seconds > 0);
    }

    #[tokio::test]
    async fn test_reliability_monitor_statistics() {
        let mut monitor = ReliabilityMonitor::new();
        monitor.initialize().await.unwrap();
        
        // Record some metrics
        monitor.record_metric(Metric::new("metric1".to_string(), 10.0));
        monitor.record_metric(Metric::new("metric2".to_string(), 20.0));
        
        // Get statistics
        let alert_stats = monitor.get_alert_stats();
        let health_stats = monitor.get_health_stats();
        
        assert_eq!(alert_stats.total_rules, 0);
        assert_eq!(health_stats.total_checks, 0);
        
        // Get metric names
        let metric_names = monitor.get_metric_names();
        assert_eq!(metric_names.len(), 2);
        assert!(metric_names.contains(&"metric1".to_string()));
        assert!(metric_names.contains(&"metric2".to_string()));
    }

    #[tokio::test]
    async fn test_reliability_monitor_shutdown() {
        let mut monitor = ReliabilityMonitor::new();
        monitor.initialize().await.unwrap();
        
        assert!(monitor.initialized);
        
        // Shutdown the monitor
        let result = monitor.shutdown().await;
        assert!(result.is_ok());
        assert!(!monitor.initialized);
        
        // Try to shutdown again (should fail)
        let result = monitor.shutdown().await;
        assert!(result.is_err());
    }
}