oxirs-gql 0.2.4

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
//! Prometheus Metric Cardinality Optimization
//!
//! Provides tools to manage and optimize Prometheus metric cardinality to prevent
//! memory issues and performance degradation.
//!
//! # Features
//!
//! - Label cardinality tracking and analysis
//! - Automatic label pruning and aggregation
//! - Configurable cardinality limits
//! - Dynamic label bucketing
//! - Label value allowlisting and denylisting
//! - Cardinality reporting and alerts
//! - Label normalization strategies

use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// Type alias for nested label values map: metric -> label -> value -> count
type LabelValuesMap = HashMap<String, HashMap<String, HashMap<String, usize>>>;

/// Configuration for cardinality optimization
#[derive(Debug, Clone)]
pub struct CardinalityConfig {
    /// Maximum unique label combinations per metric
    pub max_cardinality: usize,
    /// Maximum unique values per label
    pub max_label_values: usize,
    /// Enable automatic label pruning
    pub auto_prune: bool,
    /// Labels to exclude from cardinality limits
    pub exempt_labels: HashSet<String>,
    /// Enable label bucketing
    pub enable_bucketing: bool,
    /// Bucket size for numeric labels
    pub bucket_size: f64,
}

impl Default for CardinalityConfig {
    fn default() -> Self {
        Self {
            max_cardinality: 10_000,
            max_label_values: 1_000,
            auto_prune: true,
            exempt_labels: HashSet::new(),
            enable_bucketing: true,
            bucket_size: 100.0,
        }
    }
}

/// Label cardinality information
#[derive(Debug, Clone)]
pub struct LabelCardinality {
    pub label_name: String,
    pub unique_values: usize,
    pub total_occurrences: usize,
    pub sample_values: Vec<String>,
}

/// Metric cardinality information
#[derive(Debug, Clone)]
pub struct MetricCardinality {
    pub metric_name: String,
    pub total_series: usize,
    pub label_cardinalities: Vec<LabelCardinality>,
    pub exceeds_limit: bool,
}

/// Label value normalization strategy
#[derive(Debug, Clone, PartialEq)]
pub enum NormalizationStrategy {
    /// Keep original value
    None,
    /// Bucket numeric values
    Bucket { size: f64 },
    /// Truncate to prefix
    Prefix { length: usize },
    /// Hash long values
    Hash,
    /// Map to predefined categories
    Category { categories: Vec<String> },
    /// Custom transformation function name
    Custom(String),
}

/// Label allowlist/denylist
#[derive(Debug, Clone)]
pub struct LabelFilter {
    /// Allowed label values (if set, only these are allowed)
    pub allowlist: Option<HashSet<String>>,
    /// Denied label values
    pub denylist: HashSet<String>,
    /// Normalization strategy
    pub normalization: NormalizationStrategy,
}

/// Cardinality optimizer for Prometheus metrics
pub struct CardinalityOptimizer {
    config: CardinalityConfig,
    /// Track label value occurrences per metric and label
    label_values: Arc<RwLock<LabelValuesMap>>,
    /// Track total series count per metric
    series_counts: Arc<RwLock<HashMap<String, usize>>>,
    /// Label filters per metric/label combination
    filters: Arc<RwLock<HashMap<String, HashMap<String, LabelFilter>>>>,
    /// Cardinality warnings
    warnings: Arc<RwLock<Vec<String>>>,
    /// Last reset time
    last_reset: Arc<RwLock<Instant>>,
    /// Reset interval
    reset_interval: Duration,
}

impl CardinalityOptimizer {
    /// Create a new cardinality optimizer
    pub fn new(config: CardinalityConfig) -> Self {
        Self {
            config,
            label_values: Arc::new(RwLock::new(HashMap::new())),
            series_counts: Arc::new(RwLock::new(HashMap::new())),
            filters: Arc::new(RwLock::new(HashMap::new())),
            warnings: Arc::new(RwLock::new(Vec::new())),
            last_reset: Arc::new(RwLock::new(Instant::now())),
            reset_interval: Duration::from_secs(3600), // Reset hourly
        }
    }

    /// Record a metric with labels
    pub fn record_metric(
        &self,
        metric: &str,
        labels: &HashMap<String, String>,
    ) -> HashMap<String, String> {
        // Normalize labels
        let normalized_labels = self.normalize_labels(metric, labels);

        // Track cardinality - explicitly drop locks to avoid deadlock
        {
            let mut label_values = self.label_values.write().expect("lock poisoned");
            let metric_labels = label_values.entry(metric.to_string()).or_default();

            for (label_name, label_value) in &normalized_labels {
                let label_map = metric_labels.entry(label_name.clone()).or_default();
                *label_map.entry(label_value.clone()).or_insert(0) += 1;
            }
        } // Drop label_values lock

        // Update series count
        {
            let mut series_counts = self.series_counts.write().expect("lock poisoned");
            *series_counts.entry(metric.to_string()).or_insert(0) += 1;
        } // Drop series_counts lock

        // Check cardinality limits (acquires read locks internally)
        self.check_cardinality_limits(metric);

        // Auto-reset if needed
        self.check_auto_reset();

        normalized_labels
    }

    /// Normalize labels according to configured strategies
    pub fn normalize_labels(
        &self,
        metric: &str,
        labels: &HashMap<String, String>,
    ) -> HashMap<String, String> {
        let filters = self.filters.read().expect("lock poisoned");
        let metric_filters = filters.get(metric);

        let mut normalized = HashMap::new();

        for (label_name, label_value) in labels {
            // Skip exempt labels
            if self.config.exempt_labels.contains(label_name) {
                normalized.insert(label_name.clone(), label_value.clone());
                continue;
            }

            // Apply filter if exists
            let filter = metric_filters.and_then(|f| f.get(label_name));

            let normalized_value = if let Some(filter) = filter {
                // Check denylist
                if filter.denylist.contains(label_value) {
                    continue; // Skip denied labels
                }

                // Check allowlist
                if let Some(ref allowlist) = filter.allowlist {
                    if !allowlist.contains(label_value) {
                        continue; // Skip non-allowed labels
                    }
                }

                // Apply normalization
                self.apply_normalization(label_value, &filter.normalization)
            } else {
                // Default normalization
                if self.config.enable_bucketing && Self::is_numeric(label_value) {
                    self.bucket_numeric(label_value, self.config.bucket_size)
                } else {
                    label_value.clone()
                }
            };

            normalized.insert(label_name.clone(), normalized_value);
        }

        normalized
    }

    /// Apply normalization strategy
    fn apply_normalization(&self, value: &str, strategy: &NormalizationStrategy) -> String {
        match strategy {
            NormalizationStrategy::None => value.to_string(),
            NormalizationStrategy::Bucket { size } => self.bucket_numeric(value, *size),
            NormalizationStrategy::Prefix { length } => value.chars().take(*length).collect(),
            NormalizationStrategy::Hash => {
                if value.len() > 32 {
                    format!("hash_{:x}", Self::simple_hash(value))
                } else {
                    value.to_string()
                }
            }
            NormalizationStrategy::Category { categories } => {
                // Find best matching category
                categories
                    .iter()
                    .find(|c| value.contains(c.as_str()))
                    .cloned()
                    .unwrap_or_else(|| "other".to_string())
            }
            NormalizationStrategy::Custom(_name) => {
                // Custom normalization would be implemented via callback
                value.to_string()
            }
        }
    }

    /// Bucket numeric values
    fn bucket_numeric(&self, value: &str, bucket_size: f64) -> String {
        if let Ok(num) = value.parse::<f64>() {
            let bucket = (num / bucket_size).floor() * bucket_size;
            format!("{}-{}", bucket, bucket + bucket_size)
        } else {
            value.to_string()
        }
    }

    /// Register a label filter for a metric
    pub fn register_filter(&self, metric: &str, label: &str, filter: LabelFilter) {
        let mut filters = self.filters.write().expect("lock poisoned");
        filters
            .entry(metric.to_string())
            .or_default()
            .insert(label.to_string(), filter);
    }

    /// Get cardinality report for a metric
    pub fn get_metric_cardinality(&self, metric: &str) -> Option<MetricCardinality> {
        let label_values = self.label_values.read().expect("lock poisoned");
        let series_counts = self.series_counts.read().expect("lock poisoned");

        let metric_labels = label_values.get(metric)?;
        let total_series = *series_counts.get(metric).unwrap_or(&0);

        let mut label_cardinalities = Vec::new();

        for (label_name, values) in metric_labels {
            let unique_values = values.len();
            let total_occurrences: usize = values.values().sum();

            // Get sample values (top 5)
            let mut sample_values: Vec<_> = values.iter().collect();
            sample_values.sort_by(|a, b| b.1.cmp(a.1));
            let sample_values: Vec<String> = sample_values
                .iter()
                .take(5)
                .map(|(k, _)| k.to_string())
                .collect();

            label_cardinalities.push(LabelCardinality {
                label_name: label_name.clone(),
                unique_values,
                total_occurrences,
                sample_values,
            });
        }

        // Sort by cardinality (highest first)
        label_cardinalities.sort_by_key(|l| Reverse(l.unique_values));

        let exceeds_limit = total_series > self.config.max_cardinality;

        Some(MetricCardinality {
            metric_name: metric.to_string(),
            total_series,
            label_cardinalities,
            exceeds_limit,
        })
    }

    /// Get all metrics with cardinality information
    pub fn get_all_cardinalities(&self) -> Vec<MetricCardinality> {
        let series_counts = self.series_counts.read().expect("lock poisoned");
        let metrics: Vec<_> = series_counts.keys().cloned().collect();
        drop(series_counts);

        metrics
            .iter()
            .filter_map(|m| self.get_metric_cardinality(m))
            .collect()
    }

    /// Get metrics exceeding cardinality limits
    pub fn get_high_cardinality_metrics(&self) -> Vec<MetricCardinality> {
        self.get_all_cardinalities()
            .into_iter()
            .filter(|m| m.exceeds_limit)
            .collect()
    }

    /// Check cardinality limits and generate warnings
    fn check_cardinality_limits(&self, metric: &str) {
        let series_counts = self.series_counts.read().expect("lock poisoned");
        let series_count = *series_counts.get(metric).unwrap_or(&0);
        drop(series_counts);

        if series_count > self.config.max_cardinality {
            let warning = format!(
                "Metric '{}' has {} series, exceeding limit of {}",
                metric, series_count, self.config.max_cardinality
            );
            let mut warnings = self.warnings.write().expect("lock poisoned");
            warnings.push(warning);

            // Auto-prune if enabled
            if self.config.auto_prune {
                self.prune_metric(metric);
            }
        }

        // Check per-label cardinality
        let should_prune_labels = {
            let label_values = self.label_values.read().expect("lock poisoned");
            let mut should_prune = false;
            if let Some(metric_labels) = label_values.get(metric) {
                for (label_name, values) in metric_labels {
                    if values.len() > self.config.max_label_values {
                        let warning = format!(
                            "Label '{}' on metric '{}' has {} unique values, exceeding limit of {}",
                            label_name,
                            metric,
                            values.len(),
                            self.config.max_label_values
                        );
                        let mut warnings = self.warnings.write().expect("lock poisoned");
                        warnings.push(warning);
                        should_prune = true;
                    }
                }
            }
            should_prune
        }; // Drop read lock before pruning

        // Auto-prune labels if enabled and needed
        if should_prune_labels && self.config.auto_prune {
            self.prune_metric(metric);
        }
    }

    /// Prune least frequently used label values
    fn prune_metric(&self, metric: &str) {
        let mut label_values = self.label_values.write().expect("lock poisoned");
        if let Some(metric_labels) = label_values.get_mut(metric) {
            for values in metric_labels.values_mut() {
                if values.len() > self.config.max_label_values {
                    // Keep only top N most frequent values
                    let mut sorted: Vec<_> = values.iter().collect();
                    sorted.sort_by(|a, b| b.1.cmp(a.1));

                    let to_keep: HashSet<_> = sorted
                        .iter()
                        .take(self.config.max_label_values)
                        .map(|(k, _)| k.to_string())
                        .collect();

                    values.retain(|k, _| to_keep.contains(k));
                }
            }
        }
    }

    /// Get all warnings
    pub fn get_warnings(&self) -> Vec<String> {
        self.warnings.read().expect("lock poisoned").clone()
    }

    /// Clear warnings
    pub fn clear_warnings(&self) {
        self.warnings.write().expect("lock poisoned").clear();
    }

    /// Reset all cardinality data
    pub fn reset(&self) {
        self.label_values.write().expect("lock poisoned").clear();
        self.series_counts.write().expect("lock poisoned").clear();
        self.warnings.write().expect("lock poisoned").clear();
        *self.last_reset.write().expect("lock poisoned") = Instant::now();
    }

    /// Check if auto-reset is needed
    fn check_auto_reset(&self) {
        let last_reset = self.last_reset.read().expect("lock poisoned");
        if last_reset.elapsed() >= self.reset_interval {
            drop(last_reset);
            self.reset();
        }
    }

    /// Get total unique series across all metrics
    pub fn total_series(&self) -> usize {
        self.series_counts
            .read()
            .expect("lock poisoned")
            .values()
            .sum()
    }

    /// Get number of tracked metrics
    pub fn metric_count(&self) -> usize {
        self.series_counts.read().expect("lock poisoned").len()
    }

    /// Generate cardinality report as text
    pub fn generate_report(&self) -> String {
        let mut report = String::new();
        report.push_str("=== Prometheus Cardinality Report ===\n\n");

        let cardinalities = self.get_all_cardinalities();
        report.push_str(&format!("Total metrics: {}\n", cardinalities.len()));
        report.push_str(&format!("Total series: {}\n", self.total_series()));
        report.push_str(&format!(
            "Cardinality limit: {}\n\n",
            self.config.max_cardinality
        ));

        // High cardinality metrics
        let high_card = self.get_high_cardinality_metrics();
        if !high_card.is_empty() {
            report.push_str("⚠️  High Cardinality Metrics:\n");
            for metric in &high_card {
                report.push_str(&format!(
                    "  - {} ({} series)\n",
                    metric.metric_name, metric.total_series
                ));
            }
            report.push('\n');
        }

        // Top metrics by cardinality
        let mut sorted_metrics = cardinalities;
        sorted_metrics.sort_by_key(|m| Reverse(m.total_series));

        report.push_str("Top 10 Metrics by Cardinality:\n");
        for metric in sorted_metrics.iter().take(10) {
            report.push_str(&format!(
                "  {} - {} series\n",
                metric.metric_name, metric.total_series
            ));

            for label in metric.label_cardinalities.iter().take(3) {
                report.push_str(&format!(
                    "    {} - {} unique values\n",
                    label.label_name, label.unique_values
                ));
            }
        }

        // Warnings
        let warnings = self.get_warnings();
        if !warnings.is_empty() {
            report.push_str("\n⚠️  Warnings:\n");
            for warning in &warnings {
                report.push_str(&format!("  - {}\n", warning));
            }
        }

        report
    }

    // Helper methods

    fn is_numeric(s: &str) -> bool {
        s.parse::<f64>().is_ok()
    }

    fn simple_hash(s: &str) -> u64 {
        let mut hash: u64 = 5381;
        for byte in s.bytes() {
            hash = hash.wrapping_mul(33).wrapping_add(byte as u64);
        }
        hash
    }
}

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

    #[test]
    fn test_record_metric_basic() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        let mut labels = HashMap::new();
        labels.insert("endpoint".to_string(), "/api/v1".to_string());

        optimizer.record_metric("http_requests_total", &labels);

        assert_eq!(optimizer.metric_count(), 1);
        assert_eq!(optimizer.total_series(), 1);
    }

    #[test]
    fn test_label_cardinality_tracking() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        for i in 0..10 {
            let mut labels = HashMap::new();
            labels.insert("endpoint".to_string(), format!("/api/v{}", i));
            optimizer.record_metric("http_requests_total", &labels);
        }

        let cardinality = optimizer.get_metric_cardinality("http_requests_total");
        assert!(cardinality.is_some());

        let card = cardinality.expect("should succeed");
        assert_eq!(card.total_series, 10);
        assert_eq!(card.label_cardinalities.len(), 1);
        assert_eq!(card.label_cardinalities[0].unique_values, 10);
    }

    #[test]
    fn test_cardinality_limit_warning() {
        let config = CardinalityConfig {
            max_cardinality: 5,
            ..Default::default()
        };
        let optimizer = CardinalityOptimizer::new(config);

        for i in 0..10 {
            let mut labels = HashMap::new();
            labels.insert("id".to_string(), i.to_string());
            optimizer.record_metric("test_metric", &labels);
        }

        let warnings = optimizer.get_warnings();
        assert!(!warnings.is_empty());
        assert!(warnings[0].contains("exceeding limit"));
    }

    #[test]
    fn test_numeric_bucketing() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig {
            enable_bucketing: true,
            bucket_size: 100.0,
            ..Default::default()
        });

        let mut labels1 = HashMap::new();
        labels1.insert("response_time".to_string(), "150".to_string());

        let mut labels2 = HashMap::new();
        labels2.insert("response_time".to_string(), "175".to_string());

        let normalized1 = optimizer.normalize_labels("test", &labels1);
        let normalized2 = optimizer.normalize_labels("test", &labels2);

        // Both should be bucketed to same range
        assert_eq!(
            normalized1.get("response_time"),
            normalized2.get("response_time")
        );
        assert_eq!(
            normalized1.get("response_time").expect("should succeed"),
            "100-200"
        );
    }

    #[test]
    fn test_label_allowlist() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        let mut allowlist = HashSet::new();
        allowlist.insert("production".to_string());
        allowlist.insert("staging".to_string());

        optimizer.register_filter(
            "test_metric",
            "environment",
            LabelFilter {
                allowlist: Some(allowlist),
                denylist: HashSet::new(),
                normalization: NormalizationStrategy::None,
            },
        );

        let mut labels1 = HashMap::new();
        labels1.insert("environment".to_string(), "production".to_string());
        let normalized1 = optimizer.normalize_labels("test_metric", &labels1);
        assert!(normalized1.contains_key("environment"));

        let mut labels2 = HashMap::new();
        labels2.insert("environment".to_string(), "development".to_string());
        let normalized2 = optimizer.normalize_labels("test_metric", &labels2);
        assert!(!normalized2.contains_key("environment")); // Filtered out
    }

    #[test]
    fn test_label_denylist() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        let mut denylist = HashSet::new();
        denylist.insert("sensitive_value".to_string());

        optimizer.register_filter(
            "test_metric",
            "user_id",
            LabelFilter {
                allowlist: None,
                denylist,
                normalization: NormalizationStrategy::None,
            },
        );

        let mut labels = HashMap::new();
        labels.insert("user_id".to_string(), "sensitive_value".to_string());
        let normalized = optimizer.normalize_labels("test_metric", &labels);
        assert!(!normalized.contains_key("user_id")); // Denied
    }

    #[test]
    fn test_prefix_normalization() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        optimizer.register_filter(
            "test_metric",
            "transaction_id",
            LabelFilter {
                allowlist: None,
                denylist: HashSet::new(),
                normalization: NormalizationStrategy::Prefix { length: 8 },
            },
        );

        let mut labels = HashMap::new();
        labels.insert("transaction_id".to_string(), "1234567890abcdef".to_string());

        let normalized = optimizer.normalize_labels("test_metric", &labels);
        assert_eq!(
            normalized.get("transaction_id").expect("should succeed"),
            "12345678"
        );
    }

    #[test]
    fn test_hash_normalization() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        optimizer.register_filter(
            "test_metric",
            "long_value",
            LabelFilter {
                allowlist: None,
                denylist: HashSet::new(),
                normalization: NormalizationStrategy::Hash,
            },
        );

        let mut labels = HashMap::new();
        labels.insert(
            "long_value".to_string(),
            "this_is_a_very_long_value_that_should_be_hashed".to_string(),
        );

        let normalized = optimizer.normalize_labels("test_metric", &labels);
        let value = normalized.get("long_value").expect("should succeed");
        assert!(value.starts_with("hash_"));
    }

    #[test]
    fn test_category_normalization() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        optimizer.register_filter(
            "test_metric",
            "endpoint",
            LabelFilter {
                allowlist: None,
                denylist: HashSet::new(),
                normalization: NormalizationStrategy::Category {
                    categories: vec!["api".to_string(), "web".to_string(), "admin".to_string()],
                },
            },
        );

        let mut labels1 = HashMap::new();
        labels1.insert("endpoint".to_string(), "/api/users/123".to_string());
        let normalized1 = optimizer.normalize_labels("test_metric", &labels1);
        assert_eq!(normalized1.get("endpoint").expect("should succeed"), "api");

        let mut labels2 = HashMap::new();
        labels2.insert("endpoint".to_string(), "/web/dashboard".to_string());
        let normalized2 = optimizer.normalize_labels("test_metric", &labels2);
        assert_eq!(normalized2.get("endpoint").expect("should succeed"), "web");

        let mut labels3 = HashMap::new();
        labels3.insert("endpoint".to_string(), "/unknown/path".to_string());
        let normalized3 = optimizer.normalize_labels("test_metric", &labels3);
        assert_eq!(
            normalized3.get("endpoint").expect("should succeed"),
            "other"
        );
    }

    #[test]
    fn test_exempt_labels() {
        let mut exempt = HashSet::new();
        exempt.insert("critical_label".to_string());

        let config = CardinalityConfig {
            exempt_labels: exempt,
            enable_bucketing: true,
            ..Default::default()
        };
        let optimizer = CardinalityOptimizer::new(config);

        let mut labels = HashMap::new();
        labels.insert("critical_label".to_string(), "123".to_string());

        let normalized = optimizer.normalize_labels("test", &labels);

        // Exempt labels should not be bucketed
        assert_eq!(
            normalized.get("critical_label").expect("should succeed"),
            "123"
        );
    }

    #[test]
    fn test_high_cardinality_detection() {
        let config = CardinalityConfig {
            max_cardinality: 5,
            auto_prune: false,
            ..Default::default()
        };
        let optimizer = CardinalityOptimizer::new(config);

        for i in 0..10 {
            let mut labels = HashMap::new();
            labels.insert("id".to_string(), i.to_string());
            optimizer.record_metric("test_metric", &labels);
        }

        let high_card = optimizer.get_high_cardinality_metrics();
        assert_eq!(high_card.len(), 1);
        assert_eq!(high_card[0].metric_name, "test_metric");
        assert!(high_card[0].exceeds_limit);
    }

    #[test]
    fn test_auto_pruning() {
        let config = CardinalityConfig {
            max_label_values: 3,
            auto_prune: true,
            ..Default::default()
        };
        let optimizer = CardinalityOptimizer::new(config);

        // Record same labels multiple times to establish frequency
        for _ in 0..10 {
            let mut labels = HashMap::new();
            labels.insert("frequent".to_string(), "value1".to_string());
            optimizer.record_metric("test", &labels);
        }

        for _ in 0..5 {
            let mut labels = HashMap::new();
            labels.insert("frequent".to_string(), "value2".to_string());
            optimizer.record_metric("test", &labels);
        }

        for _ in 0..2 {
            let mut labels = HashMap::new();
            labels.insert("frequent".to_string(), "value3".to_string());
            optimizer.record_metric("test", &labels);
        }

        // This should trigger pruning
        for i in 4..10 {
            let mut labels = HashMap::new();
            labels.insert("frequent".to_string(), format!("value{}", i));
            optimizer.record_metric("test", &labels);
        }

        let cardinality = optimizer
            .get_metric_cardinality("test")
            .expect("should succeed");
        let label_card = &cardinality.label_cardinalities[0];

        // Should have pruned to top 3 values
        assert!(label_card.unique_values <= 3);
    }

    #[test]
    fn test_generate_report() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        for i in 0..5 {
            let mut labels = HashMap::new();
            labels.insert("endpoint".to_string(), format!("/api/v{}", i));
            optimizer.record_metric("http_requests", &labels);
        }

        let report = optimizer.generate_report();
        assert!(report.contains("Prometheus Cardinality Report"));
        assert!(report.contains("Total metrics: 1"));
        assert!(report.contains("http_requests"));
    }

    #[test]
    fn test_clear_warnings() {
        let config = CardinalityConfig {
            max_cardinality: 2,
            ..Default::default()
        };
        let optimizer = CardinalityOptimizer::new(config);

        for i in 0..5 {
            let mut labels = HashMap::new();
            labels.insert("id".to_string(), i.to_string());
            optimizer.record_metric("test", &labels);
        }

        assert!(!optimizer.get_warnings().is_empty());

        optimizer.clear_warnings();
        assert!(optimizer.get_warnings().is_empty());
    }

    #[test]
    fn test_reset_clears_all_data() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        let mut labels = HashMap::new();
        labels.insert("test".to_string(), "value".to_string());
        optimizer.record_metric("metric", &labels);

        assert_eq!(optimizer.total_series(), 1);

        optimizer.reset();

        assert_eq!(optimizer.total_series(), 0);
        assert_eq!(optimizer.metric_count(), 0);
    }

    #[test]
    fn test_sample_values_in_cardinality_report() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        for i in 0..10 {
            let mut labels = HashMap::new();
            labels.insert("id".to_string(), format!("value{}", i));
            optimizer.record_metric("test", &labels);
        }

        let cardinality = optimizer
            .get_metric_cardinality("test")
            .expect("should succeed");
        assert_eq!(cardinality.label_cardinalities.len(), 1);

        let label_card = &cardinality.label_cardinalities[0];
        assert_eq!(label_card.sample_values.len(), 5); // Top 5 samples
    }

    #[test]
    fn test_multiple_metrics_tracking() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        let mut labels1 = HashMap::new();
        labels1.insert("type".to_string(), "a".to_string());
        optimizer.record_metric("metric1", &labels1);

        let mut labels2 = HashMap::new();
        labels2.insert("type".to_string(), "b".to_string());
        optimizer.record_metric("metric2", &labels2);

        assert_eq!(optimizer.metric_count(), 2);

        let all_cards = optimizer.get_all_cardinalities();
        assert_eq!(all_cards.len(), 2);
    }

    #[test]
    fn test_label_value_frequency_tracking() {
        let optimizer = CardinalityOptimizer::new(CardinalityConfig::default());

        // Record same label value multiple times
        for _ in 0..5 {
            let mut labels = HashMap::new();
            labels.insert("status".to_string(), "200".to_string());
            optimizer.record_metric("http_requests", &labels);
        }

        let cardinality = optimizer
            .get_metric_cardinality("http_requests")
            .expect("should succeed");
        let label_card = &cardinality.label_cardinalities[0];

        assert_eq!(label_card.total_occurrences, 5);
        assert_eq!(label_card.unique_values, 1);
    }
}