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
//! Configuration types for monitoring system

use serde::{Deserialize, Serialize};
use super::alerts::AlertConfig;
use super::health::HealthConfig;
use super::metrics::MetricsConfig;

/// Main configuration for the monitoring system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitorConfig {
    /// Metrics collection configuration
    pub metrics_config: MetricsConfig,
    /// Alert management configuration
    pub alert_config: AlertConfig,
    /// Health reporting configuration
    pub health_config: HealthConfig,
    /// Whether the monitoring system is enabled
    pub enabled: bool,
    /// Global monitoring interval (in seconds)
    pub monitoring_interval_seconds: u64,
    /// Maximum number of monitoring statistics to keep
    pub max_stats_history: usize,
}

impl MonitorConfig {
    /// Create a new monitoring configuration
    pub fn new() -> Self {
        Self {
            metrics_config: MetricsConfig::default(),
            alert_config: AlertConfig::default(),
            health_config: HealthConfig::default(),
            enabled: true,
            monitoring_interval_seconds: 60,
            max_stats_history: 1000,
        }
    }

    /// Create a configuration with custom settings
    pub fn with_settings(
        metrics_config: MetricsConfig,
        alert_config: AlertConfig,
        health_config: HealthConfig,
    ) -> Self {
        Self {
            metrics_config,
            alert_config,
            health_config,
            enabled: true,
            monitoring_interval_seconds: 60,
            max_stats_history: 1000,
        }
    }

    /// Set the monitoring interval
    pub fn with_monitoring_interval(mut self, interval_seconds: u64) -> Self {
        self.monitoring_interval_seconds = interval_seconds;
        self
    }

    /// Set the maximum stats history
    pub fn with_max_stats_history(mut self, max_history: usize) -> Self {
        self.max_stats_history = max_history;
        self
    }

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

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

/// Monitoring statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringStats {
    /// Total number of metrics collected
    pub total_metrics_collected: u64,
    /// Total number of alerts triggered
    pub total_alerts_triggered: u64,
    /// Total number of health checks performed
    pub total_health_checks: u64,
    /// System uptime in seconds
    pub system_uptime_seconds: u64,
    /// Last monitoring update timestamp
    pub last_update_timestamp: u64,
    /// Monitoring system start time
    pub start_time: u64,
}

impl MonitoringStats {
    /// Create new monitoring statistics
    pub fn new() -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        Self {
            total_metrics_collected: 0,
            total_alerts_triggered: 0,
            total_health_checks: 0,
            system_uptime_seconds: 0,
            last_update_timestamp: now,
            start_time: now,
        }
    }

    /// Update the statistics
    pub fn update(&mut self) {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        
        self.system_uptime_seconds = now - self.start_time;
        self.last_update_timestamp = now;
    }

    /// Increment metrics collected count
    pub fn increment_metrics_collected(&mut self) {
        self.total_metrics_collected += 1;
        self.update();
    }

    /// Increment alerts triggered count
    pub fn increment_alerts_triggered(&mut self) {
        self.total_alerts_triggered += 1;
        self.update();
    }

    /// Increment health checks count
    pub fn increment_health_checks(&mut self) {
        self.total_health_checks += 1;
        self.update();
    }

    /// Get the monitoring system uptime
    pub fn get_uptime(&self) -> std::time::Duration {
        std::time::Duration::from_secs(self.system_uptime_seconds)
    }

    /// Get metrics collection rate (metrics per minute)
    pub fn get_metrics_rate(&self) -> f64 {
        if self.system_uptime_seconds == 0 {
            return 0.0;
        }
        (self.total_metrics_collected as f64) / (self.system_uptime_seconds as f64 / 60.0)
    }

    /// Get alert rate (alerts per hour)
    pub fn get_alert_rate(&self) -> f64 {
        if self.system_uptime_seconds == 0 {
            return 0.0;
        }
        (self.total_alerts_triggered as f64) / (self.system_uptime_seconds as f64 / 3600.0)
    }

    /// Get health check rate (checks per minute)
    pub fn get_health_check_rate(&self) -> f64 {
        if self.system_uptime_seconds == 0 {
            return 0.0;
        }
        (self.total_health_checks as f64) / (self.system_uptime_seconds as f64 / 60.0)
    }
}

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

/// Performance monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Whether to enable performance monitoring
    pub enabled: bool,
    /// Sampling rate for performance metrics (0.0 to 1.0)
    pub sampling_rate: f64,
    /// Maximum number of performance samples to keep
    pub max_samples: usize,
    /// Performance threshold for warnings (in milliseconds)
    pub warning_threshold_ms: u64,
    /// Performance threshold for errors (in milliseconds)
    pub error_threshold_ms: u64,
}

impl PerformanceConfig {
    /// Create a new performance configuration
    pub fn new() -> Self {
        Self {
            enabled: true,
            sampling_rate: 1.0,
            max_samples: 10000,
            warning_threshold_ms: 1000,
            error_threshold_ms: 5000,
        }
    }

    /// Set the sampling rate
    pub fn with_sampling_rate(mut self, rate: f64) -> Self {
        self.sampling_rate = rate.clamp(0.0, 1.0);
        self
    }

    /// Set the maximum samples
    pub fn with_max_samples(mut self, max_samples: usize) -> Self {
        self.max_samples = max_samples;
        self
    }

    /// Set the performance thresholds
    pub fn with_thresholds(mut self, warning_ms: u64, error_ms: u64) -> Self {
        self.warning_threshold_ms = warning_ms;
        self.error_threshold_ms = error_ms;
        self
    }
}

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

/// Resource monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceConfig {
    /// Whether to monitor CPU usage
    pub monitor_cpu: bool,
    /// Whether to monitor memory usage
    pub monitor_memory: bool,
    /// Whether to monitor disk usage
    pub monitor_disk: bool,
    /// Whether to monitor network usage
    pub monitor_network: bool,
    /// CPU usage warning threshold (percentage)
    pub cpu_warning_threshold: f64,
    /// CPU usage error threshold (percentage)
    pub cpu_error_threshold: f64,
    /// Memory usage warning threshold (percentage)
    pub memory_warning_threshold: f64,
    /// Memory usage error threshold (percentage)
    pub memory_error_threshold: f64,
    /// Disk usage warning threshold (percentage)
    pub disk_warning_threshold: f64,
    /// Disk usage error threshold (percentage)
    pub disk_error_threshold: f64,
}

impl ResourceConfig {
    /// Create a new resource configuration
    pub fn new() -> Self {
        Self {
            monitor_cpu: true,
            monitor_memory: true,
            monitor_disk: true,
            monitor_network: false,
            cpu_warning_threshold: 80.0,
            cpu_error_threshold: 95.0,
            memory_warning_threshold: 85.0,
            memory_error_threshold: 95.0,
            disk_warning_threshold: 80.0,
            disk_error_threshold: 90.0,
        }
    }

    /// Enable or disable CPU monitoring
    pub fn with_cpu_monitoring(mut self, enabled: bool) -> Self {
        self.monitor_cpu = enabled;
        self
    }

    /// Enable or disable memory monitoring
    pub fn with_memory_monitoring(mut self, enabled: bool) -> Self {
        self.monitor_memory = enabled;
        self
    }

    /// Enable or disable disk monitoring
    pub fn with_disk_monitoring(mut self, enabled: bool) -> Self {
        self.monitor_disk = enabled;
        self
    }

    /// Enable or disable network monitoring
    pub fn with_network_monitoring(mut self, enabled: bool) -> Self {
        self.monitor_network = enabled;
        self
    }

    /// Set CPU thresholds
    pub fn with_cpu_thresholds(mut self, warning: f64, error: f64) -> Self {
        self.cpu_warning_threshold = warning.clamp(0.0, 100.0);
        self.cpu_error_threshold = error.clamp(0.0, 100.0);
        self
    }

    /// Set memory thresholds
    pub fn with_memory_thresholds(mut self, warning: f64, error: f64) -> Self {
        self.memory_warning_threshold = warning.clamp(0.0, 100.0);
        self.memory_error_threshold = error.clamp(0.0, 100.0);
        self
    }

    /// Set disk thresholds
    pub fn with_disk_thresholds(mut self, warning: f64, error: f64) -> Self {
        self.disk_warning_threshold = warning.clamp(0.0, 100.0);
        self.disk_error_threshold = error.clamp(0.0, 100.0);
        self
    }
}

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

/// Extended monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtendedMonitorConfig {
    /// Base monitoring configuration
    pub base_config: MonitorConfig,
    /// Performance monitoring configuration
    pub performance_config: PerformanceConfig,
    /// Resource monitoring configuration
    pub resource_config: ResourceConfig,
    /// Whether to enable detailed logging
    pub detailed_logging: bool,
    /// Log level for monitoring (debug, info, warn, error)
    pub log_level: String,
}

impl ExtendedMonitorConfig {
    /// Create a new extended monitoring configuration
    pub fn new() -> Self {
        Self {
            base_config: MonitorConfig::new(),
            performance_config: PerformanceConfig::new(),
            resource_config: ResourceConfig::new(),
            detailed_logging: false,
            log_level: "info".to_string(),
        }
    }

    /// Enable detailed logging
    pub fn with_detailed_logging(mut self, enabled: bool) -> Self {
        self.detailed_logging = enabled;
        self
    }

    /// Set the log level
    pub fn with_log_level(mut self, level: String) -> Self {
        self.log_level = level;
        self
    }
}

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

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

    #[test]
    fn test_monitor_config_creation() {
        let config = MonitorConfig::new();
        assert!(config.enabled);
        assert_eq!(config.monitoring_interval_seconds, 60);
        assert_eq!(config.max_stats_history, 1000);
    }

    #[test]
    fn test_monitor_config_customization() {
        let config = MonitorConfig::new()
            .with_monitoring_interval(30)
            .with_max_stats_history(500)
            .set_enabled(false);

        assert!(!config.enabled);
        assert_eq!(config.monitoring_interval_seconds, 30);
        assert_eq!(config.max_stats_history, 500);
    }

    #[test]
    fn test_monitoring_stats() {
        let mut stats = MonitoringStats::new();
        
        assert_eq!(stats.total_metrics_collected, 0);
        assert_eq!(stats.total_alerts_triggered, 0);
        assert_eq!(stats.total_health_checks, 0);

        // Increment counters
        stats.increment_metrics_collected();
        stats.increment_alerts_triggered();
        stats.increment_health_checks();

        assert_eq!(stats.total_metrics_collected, 1);
        assert_eq!(stats.total_alerts_triggered, 1);
        assert_eq!(stats.total_health_checks, 1);

        // Test rates
        let metrics_rate = stats.get_metrics_rate();
        let alert_rate = stats.get_alert_rate();
        let health_check_rate = stats.get_health_check_rate();

        assert!(metrics_rate >= 0.0);
        assert!(alert_rate >= 0.0);
        assert!(health_check_rate >= 0.0);
    }

    #[test]
    fn test_performance_config() {
        let config = PerformanceConfig::new()
            .with_sampling_rate(0.5)
            .with_max_samples(5000)
            .with_thresholds(500, 2000);

        assert_eq!(config.sampling_rate, 0.5);
        assert_eq!(config.max_samples, 5000);
        assert_eq!(config.warning_threshold_ms, 500);
        assert_eq!(config.error_threshold_ms, 2000);
    }

    #[test]
    fn test_resource_config() {
        let config = ResourceConfig::new()
            .with_cpu_monitoring(true)
            .with_memory_monitoring(false)
            .with_cpu_thresholds(70.0, 90.0)
            .with_memory_thresholds(80.0, 95.0);

        assert!(config.monitor_cpu);
        assert!(!config.monitor_memory);
        assert_eq!(config.cpu_warning_threshold, 70.0);
        assert_eq!(config.cpu_error_threshold, 90.0);
        assert_eq!(config.memory_warning_threshold, 80.0);
        assert_eq!(config.memory_error_threshold, 95.0);
    }

    #[test]
    fn test_extended_monitor_config() {
        let config = ExtendedMonitorConfig::new()
            .with_detailed_logging(true)
            .with_log_level("debug".to_string());

        assert!(config.detailed_logging);
        assert_eq!(config.log_level, "debug");
        assert!(config.base_config.enabled);
        assert!(config.performance_config.enabled);
        assert!(config.resource_config.monitor_cpu);
    }

    #[test]
    fn test_config_serialization() {
        let config = MonitorConfig::new();
        let serialized = serde_json::to_string(&config).unwrap();
        let deserialized: MonitorConfig = serde_json::from_str(&serialized).unwrap();
        
        assert_eq!(config.enabled, deserialized.enabled);
        assert_eq!(config.monitoring_interval_seconds, deserialized.monitoring_interval_seconds);
    }
}