oxirs-vec 0.3.2

Vector index abstractions for semantic similarity and AI-augmented querying
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Performance monitoring and alerting for the real-time embedding pipeline

use parking_lot::Mutex;
use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::RwLock;
use uuid::Uuid;

use super::config::MonitoringConfig;
use super::traits::{
    Alert, AlertCategory, AlertConfig, AlertHandler, AlertSeverity, AlertThrottling, HealthStatus,
    MetricPoint, MetricsStorage,
};
use super::types::PerformanceMetrics;
use super::PipelineError;

// Display impls needed for formatting in throttle key
impl fmt::Display for AlertCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AlertCategory::Performance => write!(f, "Performance"),
            AlertCategory::Quality => write!(f, "Quality"),
            AlertCategory::Health => write!(f, "Health"),
            AlertCategory::Security => write!(f, "Security"),
            AlertCategory::Configuration => write!(f, "Configuration"),
        }
    }
}

impl fmt::Display for AlertSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AlertSeverity::Info => write!(f, "Info"),
            AlertSeverity::Warning => write!(f, "Warning"),
            AlertSeverity::Error => write!(f, "Error"),
            AlertSeverity::Critical => write!(f, "Critical"),
        }
    }
}

/// In-memory metrics storage implementation
pub struct InMemoryMetricsStorage {
    metrics: Vec<(String, MetricPoint)>,
    max_metrics: usize,
}

impl InMemoryMetricsStorage {
    pub fn new(max_metrics: usize) -> Self {
        Self {
            metrics: Vec::new(),
            max_metrics,
        }
    }
}

impl MetricsStorage for InMemoryMetricsStorage {
    fn store_metric(
        &mut self,
        name: &str,
        value: f64,
        timestamp: SystemTime,
        tags: HashMap<String, String>,
    ) -> anyhow::Result<()> {
        self.metrics.push((
            name.to_string(),
            MetricPoint {
                value,
                timestamp,
                tags,
            },
        ));
        while self.metrics.len() > self.max_metrics {
            self.metrics.remove(0);
        }
        Ok(())
    }

    fn get_metrics(
        &self,
        name: &str,
        start: SystemTime,
        end: SystemTime,
    ) -> anyhow::Result<Vec<MetricPoint>> {
        let filtered = self
            .metrics
            .iter()
            .filter(|(n, m)| n == name && m.timestamp >= start && m.timestamp <= end)
            .map(|(_, m)| m.clone())
            .collect();
        Ok(filtered)
    }

    fn get_metric_names(&self) -> anyhow::Result<Vec<String>> {
        let mut names: Vec<String> = self.metrics.iter().map(|(n, _)| n.clone()).collect();
        names.dedup();
        Ok(names)
    }

    fn cleanup_old_metrics(&mut self, cutoff: SystemTime) -> anyhow::Result<usize> {
        let before = self.metrics.len();
        self.metrics.retain(|(_, m)| m.timestamp >= cutoff);
        Ok(before - self.metrics.len())
    }
}

/// Console-based alert handler implementation
pub struct ConsoleAlertHandler;

impl AlertHandler for ConsoleAlertHandler {
    fn handle_alert(&self, alert: &Alert) -> anyhow::Result<()> {
        println!(
            "[ALERT] {} - {} - {} - {}",
            alert.severity, alert.category, alert.source, alert.message
        );
        Ok(())
    }

    fn get_config(&self) -> AlertConfig {
        AlertConfig {
            min_severity: AlertSeverity::Warning,
            throttling: AlertThrottling {
                enabled: false,
                window_duration: Duration::from_secs(60),
                max_alerts_per_window: 100,
            },
            enable_notifications: true,
        }
    }

    fn is_enabled(&self) -> bool {
        true
    }
}

/// Alert manager for handling pipeline alerts
pub struct AlertManager {
    alert_handler: Arc<dyn AlertHandler>,
    active_alerts: Arc<RwLock<HashMap<Uuid, Alert>>>,
    alert_history: Arc<tokio::sync::Mutex<VecDeque<Alert>>>,
    throttling_state: Arc<RwLock<HashMap<String, Instant>>>,
}

impl AlertManager {
    pub fn new(alert_handler: Arc<dyn AlertHandler>) -> Self {
        Self {
            alert_handler,
            active_alerts: Arc::new(RwLock::new(HashMap::new())),
            alert_history: Arc::new(tokio::sync::Mutex::new(VecDeque::new())),
            throttling_state: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn register_alert(&self, alert: Alert) -> Result<(), PipelineError> {
        if self.should_throttle_alert(&alert).await {
            return Ok(());
        }

        {
            let mut active_alerts = self.active_alerts.write().await;
            active_alerts.insert(alert.id, alert.clone());
        }

        {
            let mut history = self.alert_history.lock().await;
            history.push_back(alert.clone());
            while history.len() > 1000 {
                history.pop_front();
            }
        }

        self.alert_handler
            .handle_alert(&alert)
            .map_err(|e| PipelineError::MonitoringError {
                message: format!("Alert handling failed: {}", e),
            })
    }

    pub async fn process_alerts(&self) -> Result<(), PipelineError> {
        let alerts = {
            self.active_alerts
                .read()
                .await
                .values()
                .cloned()
                .collect::<Vec<_>>()
        };
        for alert in alerts {
            self.check_alert_status(alert).await?;
        }
        Ok(())
    }

    pub async fn get_active_alerts(&self) -> Vec<Alert> {
        self.active_alerts.read().await.values().cloned().collect()
    }

    async fn should_throttle_alert(&self, alert: &Alert) -> bool {
        let throttle_key = format!("{}:{}", alert.category, alert.source);
        let throttling_state = self.throttling_state.read().await;

        if let Some(last_sent) = throttling_state.get(&throttle_key) {
            let throttle_duration = match alert.severity {
                AlertSeverity::Critical => Duration::from_secs(60),
                AlertSeverity::Error => Duration::from_secs(300),
                AlertSeverity::Warning => Duration::from_secs(600),
                AlertSeverity::Info => Duration::from_secs(1200),
            };
            last_sent.elapsed() < throttle_duration
        } else {
            false
        }
    }

    async fn check_alert_status(&self, _alert: Alert) -> Result<(), PipelineError> {
        Ok(())
    }
}

/// Health checker for monitoring system health
pub struct HealthChecker {
    component_health: Arc<RwLock<HashMap<String, HealthStatus>>>,
}

impl HealthChecker {
    pub fn new() -> Self {
        Self {
            component_health: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn check_health(&self) -> Result<HealthStatus, PipelineError> {
        let component_health = self.component_health.read().await;

        if component_health.is_empty() {
            return Ok(HealthStatus::Healthy);
        }

        for status in component_health.values() {
            if matches!(status, HealthStatus::Unhealthy { .. }) {
                return Ok(HealthStatus::Unhealthy {
                    message: "Component unhealthy".to_string(),
                });
            }
        }

        for status in component_health.values() {
            if matches!(status, HealthStatus::Warning { .. }) {
                return Ok(HealthStatus::Warning {
                    message: "Component warning".to_string(),
                });
            }
        }

        Ok(HealthStatus::Healthy)
    }

    pub async fn get_overall_health(&self) -> Result<HealthStatus, PipelineError> {
        self.check_health().await
    }

    pub async fn update_component_health(&self, component: String, status: HealthStatus) {
        let mut component_health = self.component_health.write().await;
        component_health.insert(component, status);
    }
}

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

/// Metrics collector for gathering performance data
pub struct MetricsCollector {
    current_metrics: Arc<RwLock<PerformanceMetrics>>,
}

impl MetricsCollector {
    pub fn new() -> Self {
        Self {
            current_metrics: Arc::new(RwLock::new(PerformanceMetrics::default())),
        }
    }

    /// Returns `(name, value, tags)` tuples — MetricPoint has no name field,
    /// so callers are responsible for associating the name when storing.
    pub async fn collect_metrics(
        &self,
    ) -> Result<Vec<(String, f64, HashMap<String, String>)>, PipelineError> {
        let metrics = vec![
            ("cpu_usage".to_string(), 50.0_f64, HashMap::new()),
            (
                "memory_usage".to_string(),
                (1024.0 * 1024.0 * 512.0_f64),
                HashMap::new(),
            ),
            (
                "embedding_throughput".to_string(),
                100.0_f64,
                HashMap::new(),
            ),
        ];
        Ok(metrics)
    }

    pub async fn get_current_metrics(&self) -> Result<PerformanceMetrics, PipelineError> {
        let metrics = self.current_metrics.read().await;
        Ok(metrics.clone())
    }
}

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

/// Performance monitoring manager for the pipeline
pub struct PipelinePerformanceMonitor {
    config: MonitoringConfig,
    metrics_storage: Arc<Mutex<InMemoryMetricsStorage>>,
    alert_manager: Arc<AlertManager>,
    health_checker: Arc<HealthChecker>,
    metrics_collector: Arc<MetricsCollector>,
    is_running: Arc<tokio::sync::Mutex<bool>>,
}

impl PipelinePerformanceMonitor {
    /// Create a new performance monitor
    pub fn new(config: MonitoringConfig, alert_handler: Arc<dyn AlertHandler>) -> Self {
        let alert_manager = Arc::new(AlertManager::new(alert_handler));
        let health_checker = Arc::new(HealthChecker::new());
        let metrics_collector = Arc::new(MetricsCollector::new());
        let metrics_storage = Arc::new(Mutex::new(InMemoryMetricsStorage::new(10_000)));

        Self {
            config,
            metrics_storage,
            alert_manager,
            health_checker,
            metrics_collector,
            is_running: Arc::new(tokio::sync::Mutex::new(false)),
        }
    }

    /// Start the monitoring system
    pub async fn start(&self) -> Result<(), PipelineError> {
        let mut running = self.is_running.lock().await;
        if *running {
            return Err(PipelineError::AlreadyRunning);
        }
        *running = true;
        drop(running);

        self.start_metrics_collection().await;
        self.start_health_monitoring().await;
        self.start_alert_processing().await;

        Ok(())
    }

    /// Stop the monitoring system
    pub async fn stop(&self) -> Result<(), PipelineError> {
        let mut running = self.is_running.lock().await;
        if !*running {
            return Err(PipelineError::NotRunning);
        }
        *running = false;
        Ok(())
    }

    /// Record a performance metric by name
    pub fn record_metric(
        &self,
        name: &str,
        value: f64,
        tags: HashMap<String, String>,
    ) -> Result<(), PipelineError> {
        self.metrics_storage
            .lock()
            .store_metric(name, value, SystemTime::now(), tags)
            .map_err(|e| PipelineError::MonitoringError {
                message: format!("Failed to store metric: {}", e),
            })
    }

    /// Get current performance metrics
    pub async fn get_current_metrics(&self) -> Result<PerformanceMetrics, PipelineError> {
        self.metrics_collector.get_current_metrics().await
    }

    /// Get health status
    pub async fn get_health_status(&self) -> Result<HealthStatus, PipelineError> {
        self.health_checker.get_overall_health().await
    }

    /// Register an alert
    pub async fn register_alert(&self, alert: Alert) -> Result<(), PipelineError> {
        self.alert_manager.register_alert(alert).await
    }

    async fn start_metrics_collection(&self) {
        let metrics_collector = Arc::clone(&self.metrics_collector);
        let metrics_storage = Arc::clone(&self.metrics_storage);
        let is_running = Arc::clone(&self.is_running);
        let collection_interval = Duration::from_millis(self.config.metrics_interval_ms);

        tokio::spawn(async move {
            loop {
                {
                    let running = is_running.lock().await;
                    if !*running {
                        break;
                    }
                }
                if let Ok(metrics) = metrics_collector.collect_metrics().await {
                    let timestamp = SystemTime::now();
                    let mut storage = metrics_storage.lock();
                    for (name, value, tags) in metrics {
                        let _ = storage.store_metric(&name, value, timestamp, tags);
                    }
                }
                tokio::time::sleep(collection_interval).await;
            }
        });
    }

    async fn start_health_monitoring(&self) {
        let health_checker = Arc::clone(&self.health_checker);
        let alert_manager = Arc::clone(&self.alert_manager);
        let is_running = Arc::clone(&self.is_running);

        tokio::spawn(async move {
            loop {
                {
                    let running = is_running.lock().await;
                    if !*running {
                        break;
                    }
                }
                if let Ok(health_status) = health_checker.check_health().await {
                    if !matches!(health_status, HealthStatus::Healthy) {
                        let alert = Alert {
                            id: Uuid::new_v4(),
                            category: AlertCategory::Health,
                            severity: AlertSeverity::Warning,
                            message: format!("Health check failed: {:?}", health_status),
                            timestamp: SystemTime::now(),
                            source: "health_monitor".to_string(),
                            details: HashMap::new(),
                        };
                        let _ = alert_manager.register_alert(alert).await;
                    }
                }
                tokio::time::sleep(Duration::from_secs(30)).await;
            }
        });
    }

    async fn start_alert_processing(&self) {
        let alert_manager = Arc::clone(&self.alert_manager);
        let is_running = Arc::clone(&self.is_running);

        tokio::spawn(async move {
            loop {
                {
                    let running = is_running.lock().await;
                    if !*running {
                        break;
                    }
                }
                let _ = alert_manager.process_alerts().await;
                tokio::time::sleep(Duration::from_secs(1)).await;
            }
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::real_time_embedding_pipeline::config::MonitoringConfig;

    #[tokio::test]
    async fn test_metrics_collector_creation() {
        let collector = MetricsCollector::new();
        let metrics = collector.collect_metrics().await.expect("should collect");
        assert!(!metrics.is_empty());
    }

    #[tokio::test]
    async fn test_health_checker() {
        let checker = HealthChecker::new();
        let health = checker.get_overall_health().await.expect("should check");
        assert!(matches!(health, HealthStatus::Healthy));
    }

    #[tokio::test]
    async fn test_alert_manager() {
        let handler = Arc::new(ConsoleAlertHandler);
        let manager = AlertManager::new(handler);

        let alert = Alert {
            id: Uuid::new_v4(),
            category: AlertCategory::Performance,
            severity: AlertSeverity::Warning,
            message: "Test alert".to_string(),
            timestamp: SystemTime::now(),
            source: "test".to_string(),
            details: HashMap::new(),
        };

        let result = manager.register_alert(alert).await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_in_memory_metrics_storage() {
        let mut storage = InMemoryMetricsStorage::new(100);
        let result = storage.store_metric("test_metric", 42.0, SystemTime::now(), HashMap::new());
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_monitor_creation() {
        let config = MonitoringConfig::default();
        let handler = Arc::new(ConsoleAlertHandler);
        let monitor = PipelinePerformanceMonitor::new(config, handler);
        let health = monitor
            .get_health_status()
            .await
            .expect("should check health");
        assert!(matches!(health, HealthStatus::Healthy));
    }

    #[test]
    fn test_console_alert_handler_config() {
        let handler = ConsoleAlertHandler;
        assert!(handler.is_enabled());
        let config = handler.get_config();
        assert!(config.enable_notifications);
    }

    #[test]
    fn test_alert_severity_display() {
        assert_eq!(format!("{}", AlertSeverity::Critical), "Critical");
        assert_eq!(format!("{}", AlertSeverity::Warning), "Warning");
    }

    #[test]
    fn test_alert_category_display() {
        assert_eq!(format!("{}", AlertCategory::Performance), "Performance");
        assert_eq!(format!("{}", AlertCategory::Health), "Health");
    }

    #[tokio::test]
    async fn test_health_checker_with_component() {
        let checker = HealthChecker::new();
        checker
            .update_component_health(
                "test_component".to_string(),
                HealthStatus::Warning {
                    message: "test warning".to_string(),
                },
            )
            .await;
        let health = checker.check_health().await.expect("should check");
        assert!(matches!(health, HealthStatus::Warning { .. }));
    }

    #[test]
    fn test_in_memory_metrics_storage_cleanup() {
        let mut storage = InMemoryMetricsStorage::new(100);
        let past = SystemTime::now()
            .checked_sub(Duration::from_secs(3600))
            .unwrap_or(SystemTime::UNIX_EPOCH);
        let _ = storage.store_metric("old_metric", 1.0, past, HashMap::new());
        let _ = storage.store_metric("new_metric", 2.0, SystemTime::now(), HashMap::new());

        let cutoff = SystemTime::now()
            .checked_sub(Duration::from_secs(1800))
            .unwrap_or(SystemTime::UNIX_EPOCH);
        let removed = storage.cleanup_old_metrics(cutoff).expect("cleanup ok");
        assert_eq!(removed, 1);
    }

    #[test]
    fn test_in_memory_metrics_storage_get() {
        let mut storage = InMemoryMetricsStorage::new(100);
        let now = SystemTime::now();
        let _ = storage.store_metric("cpu", 75.0, now, HashMap::new());

        let start = now.checked_sub(Duration::from_secs(1)).unwrap_or(now);
        let end = now
            .checked_add(Duration::from_secs(1))
            .unwrap_or_else(SystemTime::now);
        let results = storage.get_metrics("cpu", start, end).expect("should get");
        assert_eq!(results.len(), 1);
        assert!((results[0].value - 75.0).abs() < f64::EPSILON);
    }
}