vecstore 1.0.0

The perfect vector database - 100/100 score, embeddable, high-performance, production-ready with RAG toolkit
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Vector Database Monitoring and Alerting
//!
//! Provides real-time monitoring of vector operations with configurable alerts
//! for data quality, performance, storage, and index health.

use crate::error::{Result, VecStoreError};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, SystemTime};

/// Monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringConfig {
    /// Maximum number of metric samples to keep in history
    pub max_history_size: usize,

    /// Interval for collecting metrics
    pub collection_interval: Duration,

    /// Enable alert notifications
    pub enable_alerts: bool,

    /// Alert cooldown period to prevent spam
    pub alert_cooldown: Duration,

    /// Enable metric aggregation
    pub enable_aggregation: bool,
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            max_history_size: 1000,
            collection_interval: Duration::from_secs(60),
            enable_alerts: true,
            alert_cooldown: Duration::from_secs(300),
            enable_aggregation: true,
        }
    }
}

/// Alert severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum AlertSeverity {
    Info,
    Warning,
    Error,
    Critical,
}

/// Alert categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AlertCategory {
    Performance,
    DataQuality,
    Storage,
    IndexHealth,
    Security,
}

/// Alert definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Alert {
    pub id: String,
    pub timestamp: SystemTime,
    pub severity: AlertSeverity,
    pub category: AlertCategory,
    pub message: String,
    pub metric_name: String,
    pub metric_value: f64,
    pub threshold: f64,
}

/// Alert rule for triggering notifications
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertRule {
    pub name: String,
    pub metric_name: String,
    pub category: AlertCategory,
    pub severity: AlertSeverity,
    pub condition: AlertCondition,
    pub threshold: f64,
    pub enabled: bool,
}

/// Alert condition types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AlertCondition {
    GreaterThan,
    LessThan,
    Equal,
    NotEqual,
    PercentageChange,
}

impl AlertCondition {
    fn evaluate(&self, value: f64, threshold: f64) -> bool {
        match self {
            AlertCondition::GreaterThan => value > threshold,
            AlertCondition::LessThan => value < threshold,
            AlertCondition::Equal => (value - threshold).abs() < 1e-6,
            AlertCondition::NotEqual => (value - threshold).abs() >= 1e-6,
            AlertCondition::PercentageChange => {
                (value / threshold - 1.0).abs() > 0.1 // 10% change
            }
        }
    }
}

/// Metric types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MetricType {
    // Performance metrics
    QueryLatency,
    InsertLatency,
    ThroughputQPS,

    // Data quality metrics
    VectorQuality,
    DuplicateRate,
    OutlierRate,

    // Storage metrics
    StorageUsed,
    IndexSize,
    MemoryUsage,

    // Index health metrics
    IndexFragmentation,
    CacheHitRate,
    ErrorRate,
}

impl MetricType {
    pub fn name(&self) -> &'static str {
        match self {
            MetricType::QueryLatency => "query_latency_ms",
            MetricType::InsertLatency => "insert_latency_ms",
            MetricType::ThroughputQPS => "throughput_qps",
            MetricType::VectorQuality => "vector_quality",
            MetricType::DuplicateRate => "duplicate_rate",
            MetricType::OutlierRate => "outlier_rate",
            MetricType::StorageUsed => "storage_used_mb",
            MetricType::IndexSize => "index_size_mb",
            MetricType::MemoryUsage => "memory_usage_mb",
            MetricType::IndexFragmentation => "index_fragmentation",
            MetricType::CacheHitRate => "cache_hit_rate",
            MetricType::ErrorRate => "error_rate",
        }
    }

    pub fn unit(&self) -> &'static str {
        match self {
            MetricType::QueryLatency | MetricType::InsertLatency => "ms",
            MetricType::ThroughputQPS => "qps",
            MetricType::VectorQuality
            | MetricType::DuplicateRate
            | MetricType::OutlierRate
            | MetricType::CacheHitRate
            | MetricType::ErrorRate => "ratio",
            MetricType::StorageUsed | MetricType::IndexSize | MetricType::MemoryUsage => "MB",
            MetricType::IndexFragmentation => "percent",
        }
    }
}

/// Metric data point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricPoint {
    pub timestamp: SystemTime,
    pub value: f64,
}

/// Metric history
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricHistory {
    pub metric_type: MetricType,
    pub points: VecDeque<MetricPoint>,
    pub max_size: usize,
}

impl MetricHistory {
    pub fn new(metric_type: MetricType, max_size: usize) -> Self {
        Self {
            metric_type,
            points: VecDeque::with_capacity(max_size),
            max_size,
        }
    }

    pub fn add(&mut self, value: f64) {
        if self.points.len() >= self.max_size {
            self.points.pop_front();
        }
        self.points.push_back(MetricPoint {
            timestamp: SystemTime::now(),
            value,
        });
    }

    pub fn latest(&self) -> Option<f64> {
        self.points.back().map(|p| p.value)
    }

    pub fn average(&self) -> Option<f64> {
        if self.points.is_empty() {
            return None;
        }
        let sum: f64 = self.points.iter().map(|p| p.value).sum();
        Some(sum / self.points.len() as f64)
    }

    pub fn percentile(&self, p: f64) -> Option<f64> {
        if self.points.is_empty() {
            return None;
        }
        let mut values: Vec<f64> = self.points.iter().map(|p| p.value).collect();
        values.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let idx = ((values.len() - 1) as f64 * p / 100.0) as usize;
        Some(values[idx])
    }
}

/// Monitoring statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringStats {
    pub total_alerts: usize,
    pub alerts_by_severity: HashMap<AlertSeverity, usize>,
    pub alerts_by_category: HashMap<AlertCategory, usize>,
    pub active_rules: usize,
    pub metrics_tracked: usize,
    pub uptime: Duration,
}

/// Monitoring report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringReport {
    pub timestamp: SystemTime,
    pub metrics: HashMap<String, f64>,
    pub recent_alerts: Vec<Alert>,
    pub stats: MonitoringStats,
}

/// Vector database monitor
pub struct Monitor {
    config: MonitoringConfig,
    metrics: HashMap<MetricType, MetricHistory>,
    alert_rules: Vec<AlertRule>,
    alerts: VecDeque<Alert>,
    last_alert_time: HashMap<String, SystemTime>,
    start_time: SystemTime,
}

impl Monitor {
    /// Create a new monitor
    pub fn new(config: MonitoringConfig) -> Self {
        Self {
            config,
            metrics: HashMap::new(),
            alert_rules: Vec::new(),
            alerts: VecDeque::new(),
            last_alert_time: HashMap::new(),
            start_time: SystemTime::now(),
        }
    }

    /// Create monitor with default configuration
    pub fn default() -> Self {
        Self::new(MonitoringConfig::default())
    }

    /// Add an alert rule
    pub fn add_rule(&mut self, rule: AlertRule) {
        self.alert_rules.push(rule);
    }

    /// Remove an alert rule by name
    pub fn remove_rule(&mut self, name: &str) -> bool {
        if let Some(pos) = self.alert_rules.iter().position(|r| r.name == name) {
            self.alert_rules.remove(pos);
            true
        } else {
            false
        }
    }

    /// Record a metric value
    pub fn record(&mut self, metric_type: MetricType, value: f64) {
        // Get or create metric history
        let history = self
            .metrics
            .entry(metric_type)
            .or_insert_with(|| MetricHistory::new(metric_type, self.config.max_history_size));

        history.add(value);

        // Check alert rules if enabled
        if self.config.enable_alerts {
            self.check_alerts(metric_type, value);
        }
    }

    /// Check alert rules for a metric
    fn check_alerts(&mut self, metric_type: MetricType, value: f64) {
        let metric_name = metric_type.name();

        for rule in &self.alert_rules {
            if !rule.enabled || rule.metric_name != metric_name {
                continue;
            }

            // Check cooldown period
            if let Some(last_time) = self.last_alert_time.get(&rule.name) {
                if let Ok(elapsed) = SystemTime::now().duration_since(*last_time) {
                    if elapsed < self.config.alert_cooldown {
                        continue;
                    }
                }
            }

            // Evaluate condition
            if rule.condition.evaluate(value, rule.threshold) {
                let alert = Alert {
                    id: format!(
                        "{}-{}",
                        rule.name,
                        SystemTime::now()
                            .duration_since(SystemTime::UNIX_EPOCH)
                            .unwrap()
                            .as_secs()
                    ),
                    timestamp: SystemTime::now(),
                    severity: rule.severity,
                    category: rule.category,
                    message: format!(
                        "{}: {} = {:.3} {} (threshold: {:.3})",
                        rule.name,
                        metric_name,
                        value,
                        metric_type.unit(),
                        rule.threshold
                    ),
                    metric_name: metric_name.to_string(),
                    metric_value: value,
                    threshold: rule.threshold,
                };

                self.alerts.push_back(alert);
                self.last_alert_time
                    .insert(rule.name.clone(), SystemTime::now());

                // Limit alert history
                while self.alerts.len() > self.config.max_history_size {
                    self.alerts.pop_front();
                }
            }
        }
    }

    /// Get recent alerts
    pub fn get_alerts(&self, count: usize) -> Vec<Alert> {
        self.alerts.iter().rev().take(count).cloned().collect()
    }

    /// Get alerts by severity
    pub fn get_alerts_by_severity(&self, severity: AlertSeverity) -> Vec<Alert> {
        self.alerts
            .iter()
            .filter(|a| a.severity == severity)
            .cloned()
            .collect()
    }

    /// Get alerts by category
    pub fn get_alerts_by_category(&self, category: AlertCategory) -> Vec<Alert> {
        self.alerts
            .iter()
            .filter(|a| a.category == category)
            .cloned()
            .collect()
    }

    /// Clear all alerts
    pub fn clear_alerts(&mut self) {
        self.alerts.clear();
        self.last_alert_time.clear();
    }

    /// Get metric history
    pub fn get_metric(&self, metric_type: MetricType) -> Option<&MetricHistory> {
        self.metrics.get(&metric_type)
    }

    /// Get all metrics
    pub fn get_all_metrics(&self) -> &HashMap<MetricType, MetricHistory> {
        &self.metrics
    }

    /// Generate monitoring report
    pub fn generate_report(&self) -> MonitoringReport {
        // Collect current metric values
        let mut metrics = HashMap::new();
        for (metric_type, history) in &self.metrics {
            if let Some(value) = history.latest() {
                metrics.insert(metric_type.name().to_string(), value);
            }
        }

        // Get recent alerts
        let recent_alerts = self.get_alerts(10);

        // Calculate statistics
        let mut alerts_by_severity = HashMap::new();
        let mut alerts_by_category = HashMap::new();

        for alert in &self.alerts {
            *alerts_by_severity.entry(alert.severity).or_insert(0) += 1;
            *alerts_by_category.entry(alert.category).or_insert(0) += 1;
        }

        let uptime = SystemTime::now()
            .duration_since(self.start_time)
            .unwrap_or(Duration::from_secs(0));

        let stats = MonitoringStats {
            total_alerts: self.alerts.len(),
            alerts_by_severity,
            alerts_by_category,
            active_rules: self.alert_rules.iter().filter(|r| r.enabled).count(),
            metrics_tracked: self.metrics.len(),
            uptime,
        };

        MonitoringReport {
            timestamp: SystemTime::now(),
            metrics,
            recent_alerts,
            stats,
        }
    }

    /// Get monitoring statistics
    pub fn get_stats(&self) -> MonitoringStats {
        let mut alerts_by_severity = HashMap::new();
        let mut alerts_by_category = HashMap::new();

        for alert in &self.alerts {
            *alerts_by_severity.entry(alert.severity).or_insert(0) += 1;
            *alerts_by_category.entry(alert.category).or_insert(0) += 1;
        }

        let uptime = SystemTime::now()
            .duration_since(self.start_time)
            .unwrap_or(Duration::from_secs(0));

        MonitoringStats {
            total_alerts: self.alerts.len(),
            alerts_by_severity,
            alerts_by_category,
            active_rules: self.alert_rules.iter().filter(|r| r.enabled).count(),
            metrics_tracked: self.metrics.len(),
            uptime,
        }
    }

    /// Export metrics as Prometheus format
    pub fn export_prometheus(&self) -> String {
        let mut output = String::new();

        for (metric_type, history) in &self.metrics {
            if let Some(value) = history.latest() {
                output.push_str(&format!("vecstore_{} {}\n", metric_type.name(), value));
            }
        }

        output
    }
}

/// Preset alert rules for common scenarios
pub struct AlertPresets;

impl AlertPresets {
    /// High query latency alert
    pub fn high_query_latency(threshold_ms: f64) -> AlertRule {
        AlertRule {
            name: "high_query_latency".to_string(),
            metric_name: "query_latency_ms".to_string(),
            category: AlertCategory::Performance,
            severity: AlertSeverity::Warning,
            condition: AlertCondition::GreaterThan,
            threshold: threshold_ms,
            enabled: true,
        }
    }

    /// Low cache hit rate alert
    pub fn low_cache_hit_rate(threshold: f64) -> AlertRule {
        AlertRule {
            name: "low_cache_hit_rate".to_string(),
            metric_name: "cache_hit_rate".to_string(),
            category: AlertCategory::Performance,
            severity: AlertSeverity::Warning,
            condition: AlertCondition::LessThan,
            threshold,
            enabled: true,
        }
    }

    /// High error rate alert
    pub fn high_error_rate(threshold: f64) -> AlertRule {
        AlertRule {
            name: "high_error_rate".to_string(),
            metric_name: "error_rate".to_string(),
            category: AlertCategory::Performance,
            severity: AlertSeverity::Error,
            condition: AlertCondition::GreaterThan,
            threshold,
            enabled: true,
        }
    }

    /// Low vector quality alert
    pub fn low_vector_quality(threshold: f64) -> AlertRule {
        AlertRule {
            name: "low_vector_quality".to_string(),
            metric_name: "vector_quality".to_string(),
            category: AlertCategory::DataQuality,
            severity: AlertSeverity::Warning,
            condition: AlertCondition::LessThan,
            threshold,
            enabled: true,
        }
    }

    /// High storage usage alert
    pub fn high_storage_usage(threshold_mb: f64) -> AlertRule {
        AlertRule {
            name: "high_storage_usage".to_string(),
            metric_name: "storage_used_mb".to_string(),
            category: AlertCategory::Storage,
            severity: AlertSeverity::Warning,
            condition: AlertCondition::GreaterThan,
            threshold: threshold_mb,
            enabled: true,
        }
    }

    /// High memory usage alert
    pub fn high_memory_usage(threshold_mb: f64) -> AlertRule {
        AlertRule {
            name: "high_memory_usage".to_string(),
            metric_name: "memory_usage_mb".to_string(),
            category: AlertCategory::Storage,
            severity: AlertSeverity::Error,
            condition: AlertCondition::GreaterThan,
            threshold: threshold_mb,
            enabled: true,
        }
    }

    /// High index fragmentation alert
    pub fn high_index_fragmentation(threshold_pct: f64) -> AlertRule {
        AlertRule {
            name: "high_index_fragmentation".to_string(),
            metric_name: "index_fragmentation".to_string(),
            category: AlertCategory::IndexHealth,
            severity: AlertSeverity::Warning,
            condition: AlertCondition::GreaterThan,
            threshold: threshold_pct,
            enabled: true,
        }
    }

    /// Get all default rules
    pub fn default_rules() -> Vec<AlertRule> {
        vec![
            Self::high_query_latency(100.0),
            Self::low_cache_hit_rate(0.7),
            Self::high_error_rate(0.05),
            Self::low_vector_quality(0.6),
            Self::high_storage_usage(1000.0),
            Self::high_memory_usage(2000.0),
            Self::high_index_fragmentation(30.0),
        ]
    }
}

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

    #[test]
    fn test_monitor_creation() {
        let monitor = Monitor::default();
        assert_eq!(monitor.metrics.len(), 0);
        assert_eq!(monitor.alert_rules.len(), 0);
    }

    #[test]
    fn test_record_metrics() {
        let mut monitor = Monitor::default();

        monitor.record(MetricType::QueryLatency, 50.0);
        monitor.record(MetricType::QueryLatency, 75.0);
        monitor.record(MetricType::QueryLatency, 100.0);

        let history = monitor.get_metric(MetricType::QueryLatency).unwrap();
        assert_eq!(history.points.len(), 3);
        assert_eq!(history.latest(), Some(100.0));
    }

    #[test]
    fn test_metric_statistics() {
        let mut history = MetricHistory::new(MetricType::QueryLatency, 100);

        history.add(50.0);
        history.add(100.0);
        history.add(150.0);

        assert_eq!(history.latest(), Some(150.0));
        assert_eq!(history.average(), Some(100.0));

        // Test percentile
        let p50 = history.percentile(50.0);
        assert!(p50.is_some());
        assert!((p50.unwrap() - 100.0).abs() < 1.0);
    }

    #[test]
    fn test_alert_rules() {
        let mut monitor = Monitor::default();

        // Add high latency alert rule
        monitor.add_rule(AlertPresets::high_query_latency(100.0));

        // Record values below threshold - no alerts
        monitor.record(MetricType::QueryLatency, 50.0);
        monitor.record(MetricType::QueryLatency, 75.0);
        assert_eq!(monitor.alerts.len(), 0);

        // Record value above threshold - should trigger alert
        monitor.record(MetricType::QueryLatency, 150.0);
        assert_eq!(monitor.alerts.len(), 1);

        let alert = &monitor.alerts[0];
        assert_eq!(alert.severity, AlertSeverity::Warning);
        assert_eq!(alert.category, AlertCategory::Performance);
    }

    #[test]
    fn test_alert_cooldown() {
        let config = MonitoringConfig {
            alert_cooldown: Duration::from_millis(100),
            ..Default::default()
        };

        let mut monitor = Monitor::new(config);
        monitor.add_rule(AlertPresets::high_query_latency(100.0));

        // First trigger
        monitor.record(MetricType::QueryLatency, 150.0);
        assert_eq!(monitor.alerts.len(), 1);

        // Immediate second trigger - should be suppressed
        monitor.record(MetricType::QueryLatency, 200.0);
        assert_eq!(monitor.alerts.len(), 1);

        // Wait for cooldown
        thread::sleep(Duration::from_millis(150));

        // Third trigger after cooldown - should create new alert
        monitor.record(MetricType::QueryLatency, 250.0);
        assert_eq!(monitor.alerts.len(), 2);
    }

    #[test]
    fn test_alert_filtering() {
        let mut monitor = Monitor::default();

        monitor.add_rule(AlertPresets::high_query_latency(100.0));
        monitor.add_rule(AlertPresets::high_error_rate(0.05));

        monitor.record(MetricType::QueryLatency, 150.0);
        monitor.record(MetricType::ErrorRate, 0.1);

        assert_eq!(monitor.alerts.len(), 2);

        let warnings = monitor.get_alerts_by_severity(AlertSeverity::Warning);
        assert_eq!(warnings.len(), 1);

        let errors = monitor.get_alerts_by_severity(AlertSeverity::Error);
        assert_eq!(errors.len(), 1);

        let perf_alerts = monitor.get_alerts_by_category(AlertCategory::Performance);
        assert_eq!(perf_alerts.len(), 2);
    }

    #[test]
    fn test_monitoring_report() {
        let mut monitor = Monitor::default();

        monitor.record(MetricType::QueryLatency, 50.0);
        monitor.record(MetricType::MemoryUsage, 512.0);

        monitor.add_rule(AlertPresets::high_memory_usage(1000.0));

        let report = monitor.generate_report();

        assert_eq!(report.metrics.len(), 2);
        assert!(report.metrics.contains_key("query_latency_ms"));
        assert!(report.metrics.contains_key("memory_usage_mb"));

        assert_eq!(report.stats.metrics_tracked, 2);
        assert_eq!(report.stats.active_rules, 1);
    }

    #[test]
    fn test_prometheus_export() {
        let mut monitor = Monitor::default();

        monitor.record(MetricType::QueryLatency, 50.0);
        monitor.record(MetricType::ThroughputQPS, 1000.0);

        let output = monitor.export_prometheus();

        assert!(output.contains("vecstore_query_latency_ms"));
        assert!(output.contains("vecstore_throughput_qps"));
        assert!(output.contains("50"));
        assert!(output.contains("1000"));
    }

    #[test]
    fn test_rule_management() {
        let mut monitor = Monitor::default();

        let rule = AlertPresets::high_query_latency(100.0);
        monitor.add_rule(rule);

        assert_eq!(monitor.alert_rules.len(), 1);

        let removed = monitor.remove_rule("high_query_latency");
        assert!(removed);
        assert_eq!(monitor.alert_rules.len(), 0);

        let removed_again = monitor.remove_rule("nonexistent");
        assert!(!removed_again);
    }

    #[test]
    fn test_default_presets() {
        let rules = AlertPresets::default_rules();
        assert_eq!(rules.len(), 7);

        // Verify all rules are enabled
        for rule in &rules {
            assert!(rule.enabled);
        }
    }
}