oxirs-gql 0.2.2

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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
//! Custom Business Metrics Integration
//!
//! Provides a flexible framework for defining and tracking custom business metrics
//! that are specific to GraphQL operations and RDF data.
//!
//! # Features
//!
//! - Custom metric definitions (counters, gauges, histograms, summaries)
//! - Metric registration and management
//! - Automatic Prometheus export
//! - Metric aggregation and computation
//! - Context-aware metric recording
//! - Tag-based metric organization
//! - Metric validation and constraints

use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant, SystemTime};

/// Custom metric types
#[derive(Debug, Clone, PartialEq)]
pub enum MetricType {
    /// Monotonically increasing counter
    Counter,
    /// Value that can go up or down
    Gauge,
    /// Distribution of values with buckets
    Histogram { buckets: Vec<f64> },
    /// Statistical summary with quantiles
    Summary { quantiles: Vec<f64> },
}

/// Metric value
#[derive(Debug, Clone)]
pub enum MetricValue {
    Counter(f64),
    Gauge(f64),
    Histogram(Vec<f64>),
    Summary {
        count: u64,
        sum: f64,
        quantiles: HashMap<u64, f64>, // quantile (0-100) -> value
    },
}

/// Metric metadata
#[derive(Debug, Clone)]
pub struct MetricMetadata {
    pub name: String,
    pub description: String,
    pub metric_type: MetricType,
    pub unit: Option<String>,
    pub tags: HashMap<String, String>,
}

/// A recorded metric data point
#[derive(Debug, Clone)]
pub struct MetricDataPoint {
    pub metadata: MetricMetadata,
    pub value: MetricValue,
    pub timestamp: SystemTime,
}

/// Metric aggregation strategy
#[derive(Debug, Clone, PartialEq)]
pub enum AggregationStrategy {
    Sum,
    Average,
    Min,
    Max,
    Count,
    P50,
    P95,
    P99,
}

/// Computed metric definition
#[derive(Debug, Clone)]
pub struct ComputedMetric {
    pub name: String,
    pub description: String,
    pub source_metrics: Vec<String>,
    pub aggregation: AggregationStrategy,
    pub filter: Option<MetricFilter>,
}

/// Filter for metrics
#[derive(Debug, Clone)]
pub struct MetricFilter {
    pub tags: HashMap<String, String>,
    pub min_value: Option<f64>,
    pub max_value: Option<f64>,
    pub time_range: Option<(SystemTime, SystemTime)>,
}

/// Custom business metrics registry
pub struct CustomMetricsRegistry {
    metrics: Arc<RwLock<HashMap<String, Vec<MetricDataPoint>>>>,
    metadata: Arc<RwLock<HashMap<String, MetricMetadata>>>,
    computed: Arc<RwLock<Vec<ComputedMetric>>>,
    retention_period: Duration,
    last_cleanup: Arc<Mutex<Instant>>,
}

impl CustomMetricsRegistry {
    /// Create a new metrics registry
    pub fn new(retention_period: Duration) -> Self {
        Self {
            metrics: Arc::new(RwLock::new(HashMap::new())),
            metadata: Arc::new(RwLock::new(HashMap::new())),
            computed: Arc::new(RwLock::new(Vec::new())),
            retention_period,
            last_cleanup: Arc::new(Mutex::new(Instant::now())),
        }
    }

    /// Register a new metric
    pub fn register_metric(&self, metadata: MetricMetadata) -> Result<(), String> {
        let mut meta_map = self.metadata.write().expect("lock poisoned");

        if meta_map.contains_key(&metadata.name) {
            return Err(format!("Metric '{}' already registered", metadata.name));
        }

        // Validate metric name
        if !Self::is_valid_metric_name(&metadata.name) {
            return Err(format!("Invalid metric name: {}", metadata.name));
        }

        meta_map.insert(metadata.name.clone(), metadata);
        Ok(())
    }

    /// Register a computed metric
    pub fn register_computed_metric(&self, computed: ComputedMetric) -> Result<(), String> {
        // Validate source metrics exist
        let meta_map = self.metadata.read().expect("lock poisoned");
        for source in &computed.source_metrics {
            if !meta_map.contains_key(source) {
                return Err(format!("Source metric '{}' not found", source));
            }
        }
        drop(meta_map);

        let mut computed_metrics = self.computed.write().expect("lock poisoned");
        computed_metrics.push(computed);
        Ok(())
    }

    /// Record a metric value
    pub fn record(
        &self,
        name: &str,
        value: MetricValue,
        tags: HashMap<String, String>,
    ) -> Result<(), String> {
        let meta_map = self.metadata.read().expect("lock poisoned");
        let metadata = meta_map
            .get(name)
            .ok_or_else(|| format!("Metric '{}' not registered", name))?
            .clone();
        drop(meta_map);

        // Validate metric type matches value type
        self.validate_metric_value(&metadata.metric_type, &value)?;

        let data_point = MetricDataPoint {
            metadata: MetricMetadata { tags, ..metadata },
            value,
            timestamp: SystemTime::now(),
        };

        let mut metrics = self.metrics.write().expect("lock poisoned");
        metrics
            .entry(name.to_string())
            .or_default()
            .push(data_point);

        Ok(())
    }

    /// Increment a counter
    pub fn increment_counter(
        &self,
        name: &str,
        value: f64,
        tags: HashMap<String, String>,
    ) -> Result<(), String> {
        self.record(name, MetricValue::Counter(value), tags)
    }

    /// Set a gauge value
    pub fn set_gauge(
        &self,
        name: &str,
        value: f64,
        tags: HashMap<String, String>,
    ) -> Result<(), String> {
        self.record(name, MetricValue::Gauge(value), tags)
    }

    /// Record a histogram observation
    pub fn observe_histogram(
        &self,
        name: &str,
        value: f64,
        tags: HashMap<String, String>,
    ) -> Result<(), String> {
        let meta_map = self.metadata.read().expect("lock poisoned");
        let metadata = meta_map
            .get(name)
            .ok_or_else(|| format!("Metric '{}' not registered", name))?;

        let buckets = if let MetricType::Histogram { buckets } = &metadata.metric_type {
            buckets.clone()
        } else {
            return Err(format!("Metric '{}' is not a histogram", name));
        };
        drop(meta_map);

        // Determine which bucket this value falls into
        let mut bucket_values = vec![0.0; buckets.len()];
        for (i, &bucket) in buckets.iter().enumerate() {
            if value <= bucket {
                bucket_values[i] = 1.0;
                break;
            }
        }

        self.record(name, MetricValue::Histogram(bucket_values), tags)
    }

    /// Get all data points for a metric
    pub fn get_metric_data(
        &self,
        name: &str,
        filter: Option<&MetricFilter>,
    ) -> Vec<MetricDataPoint> {
        let metrics = self.metrics.read().expect("lock poisoned");
        let data_points = metrics.get(name).cloned().unwrap_or_default();

        if let Some(filter) = filter {
            data_points
                .into_iter()
                .filter(|dp| self.matches_filter(dp, filter))
                .collect()
        } else {
            data_points
        }
    }

    /// Compute aggregated value for a metric
    pub fn compute_aggregation(
        &self,
        name: &str,
        strategy: AggregationStrategy,
        filter: Option<&MetricFilter>,
    ) -> Option<f64> {
        let data_points = self.get_metric_data(name, filter);

        if data_points.is_empty() {
            return None;
        }

        let values: Vec<f64> = data_points
            .iter()
            .filter_map(|dp| match &dp.value {
                MetricValue::Counter(v) | MetricValue::Gauge(v) => Some(*v),
                _ => None,
            })
            .collect();

        if values.is_empty() {
            return None;
        }

        match strategy {
            AggregationStrategy::Sum => Some(values.iter().sum()),
            AggregationStrategy::Average => Some(values.iter().sum::<f64>() / values.len() as f64),
            AggregationStrategy::Min => values.iter().cloned().fold(f64::INFINITY, f64::min).into(),
            AggregationStrategy::Max => values
                .iter()
                .cloned()
                .fold(f64::NEG_INFINITY, f64::max)
                .into(),
            AggregationStrategy::Count => Some(values.len() as f64),
            AggregationStrategy::P50 => Self::percentile(&values, 50.0),
            AggregationStrategy::P95 => Self::percentile(&values, 95.0),
            AggregationStrategy::P99 => Self::percentile(&values, 99.0),
        }
    }

    /// Evaluate computed metrics
    pub fn evaluate_computed_metric(&self, name: &str) -> Option<f64> {
        let computed_metrics = self.computed.read().expect("lock poisoned");
        let computed = computed_metrics.iter().find(|c| c.name == name)?;

        // Collect values from source metrics
        let mut all_values = Vec::new();
        for source_name in &computed.source_metrics {
            let data_points = self.get_metric_data(source_name, computed.filter.as_ref());
            for dp in data_points {
                if let MetricValue::Counter(v) | MetricValue::Gauge(v) = dp.value {
                    all_values.push(v);
                }
            }
        }

        if all_values.is_empty() {
            return None;
        }

        match computed.aggregation {
            AggregationStrategy::Sum => Some(all_values.iter().sum()),
            AggregationStrategy::Average => {
                Some(all_values.iter().sum::<f64>() / all_values.len() as f64)
            }
            AggregationStrategy::Min => all_values
                .iter()
                .cloned()
                .fold(f64::INFINITY, f64::min)
                .into(),
            AggregationStrategy::Max => all_values
                .iter()
                .cloned()
                .fold(f64::NEG_INFINITY, f64::max)
                .into(),
            AggregationStrategy::Count => Some(all_values.len() as f64),
            AggregationStrategy::P50 => Self::percentile(&all_values, 50.0),
            AggregationStrategy::P95 => Self::percentile(&all_values, 95.0),
            AggregationStrategy::P99 => Self::percentile(&all_values, 99.0),
        }
    }

    /// Export metrics in Prometheus format
    pub fn export_prometheus(&self) -> String {
        let mut output = String::new();
        let metrics_map = self.metrics.read().expect("lock poisoned");
        let metadata_map = self.metadata.read().expect("lock poisoned");

        for (name, metadata) in metadata_map.iter() {
            // Write metric help and type
            output.push_str(&format!("# HELP {} {}\n", name, metadata.description));
            output.push_str(&format!(
                "# TYPE {} {}\n",
                name,
                Self::prometheus_type(&metadata.metric_type)
            ));

            // Write metric values
            if let Some(data_points) = metrics_map.get(name) {
                for dp in data_points {
                    let tags_str = Self::format_prometheus_tags(&dp.metadata.tags);
                    match &dp.value {
                        MetricValue::Counter(v) | MetricValue::Gauge(v) => {
                            output.push_str(&format!("{}{} {}\n", name, tags_str, v));
                        }
                        MetricValue::Histogram(buckets) => {
                            if let MetricType::Histogram {
                                buckets: bucket_defs,
                            } = &metadata.metric_type
                            {
                                for (i, &bucket_value) in buckets.iter().enumerate() {
                                    if i < bucket_defs.len() {
                                        let mut bucket_tags = dp.metadata.tags.clone();
                                        bucket_tags
                                            .insert("le".to_string(), bucket_defs[i].to_string());
                                        let bucket_tags_str =
                                            Self::format_prometheus_tags(&bucket_tags);
                                        output.push_str(&format!(
                                            "{}_bucket{} {}\n",
                                            name, bucket_tags_str, bucket_value
                                        ));
                                    }
                                }
                            }
                        }
                        MetricValue::Summary {
                            count,
                            sum,
                            quantiles,
                        } => {
                            output.push_str(&format!("{}_count{} {}\n", name, tags_str, count));
                            output.push_str(&format!("{}_sum{} {}\n", name, tags_str, sum));
                            for (q, v) in quantiles {
                                let mut q_tags = dp.metadata.tags.clone();
                                q_tags.insert("quantile".to_string(), format!("0.{:02}", q));
                                let q_tags_str = Self::format_prometheus_tags(&q_tags);
                                output.push_str(&format!("{}{} {}\n", name, q_tags_str, v));
                            }
                        }
                    }
                }
            }
            output.push('\n');
        }

        output
    }

    /// Get all registered metric names
    pub fn list_metrics(&self) -> Vec<String> {
        self.metadata
            .read()
            .expect("lock poisoned")
            .keys()
            .cloned()
            .collect()
    }

    /// Get metric metadata
    pub fn get_metadata(&self, name: &str) -> Option<MetricMetadata> {
        self.metadata
            .read()
            .expect("lock poisoned")
            .get(name)
            .cloned()
    }

    /// Clean up old metric data based on retention period
    pub fn cleanup_old_data(&self) {
        let mut last_cleanup = self.last_cleanup.lock().expect("lock poisoned");
        if last_cleanup.elapsed() < Duration::from_secs(60) {
            return; // Only cleanup every minute
        }

        *last_cleanup = Instant::now();
        drop(last_cleanup);

        let cutoff = SystemTime::now() - self.retention_period;
        let mut metrics = self.metrics.write().expect("lock poisoned");

        for data_points in metrics.values_mut() {
            data_points.retain(|dp| dp.timestamp >= cutoff);
        }
    }

    /// Get total number of data points
    pub fn total_data_points(&self) -> usize {
        self.metrics
            .read()
            .expect("lock poisoned")
            .values()
            .map(|v| v.len())
            .sum()
    }

    // Helper methods

    fn is_valid_metric_name(name: &str) -> bool {
        !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_')
    }

    fn validate_metric_value(
        &self,
        metric_type: &MetricType,
        value: &MetricValue,
    ) -> Result<(), String> {
        match (metric_type, value) {
            (MetricType::Counter, MetricValue::Counter(_)) => Ok(()),
            (MetricType::Gauge, MetricValue::Gauge(_)) => Ok(()),
            (MetricType::Histogram { .. }, MetricValue::Histogram(_)) => Ok(()),
            (MetricType::Summary { .. }, MetricValue::Summary { .. }) => Ok(()),
            _ => Err("Metric type and value type mismatch".to_string()),
        }
    }

    fn matches_filter(&self, data_point: &MetricDataPoint, filter: &MetricFilter) -> bool {
        // Check tags
        for (key, value) in &filter.tags {
            if data_point.metadata.tags.get(key) != Some(value) {
                return false;
            }
        }

        // Check value range
        if let Some(min) = filter.min_value {
            if let MetricValue::Counter(v) | MetricValue::Gauge(v) = data_point.value {
                if v < min {
                    return false;
                }
            }
        }

        if let Some(max) = filter.max_value {
            if let MetricValue::Counter(v) | MetricValue::Gauge(v) = data_point.value {
                if v > max {
                    return false;
                }
            }
        }

        // Check time range
        if let Some((start, end)) = filter.time_range {
            if data_point.timestamp < start || data_point.timestamp > end {
                return false;
            }
        }

        true
    }

    fn percentile(values: &[f64], p: f64) -> Option<f64> {
        if values.is_empty() {
            return None;
        }

        let mut sorted = values.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        // Use floor to match the lower value for percentiles (nearest-rank method)
        let index = (p / 100.0 * (sorted.len() - 1) as f64).floor() as usize;
        Some(sorted[index])
    }

    fn prometheus_type(metric_type: &MetricType) -> &str {
        match metric_type {
            MetricType::Counter => "counter",
            MetricType::Gauge => "gauge",
            MetricType::Histogram { .. } => "histogram",
            MetricType::Summary { .. } => "summary",
        }
    }

    fn format_prometheus_tags(tags: &HashMap<String, String>) -> String {
        if tags.is_empty() {
            return String::new();
        }

        let tags_str: Vec<String> = tags
            .iter()
            .map(|(k, v)| format!("{}=\"{}\"", k, v))
            .collect();
        format!("{{{}}}", tags_str.join(","))
    }
}

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

    #[test]
    fn test_register_counter_metric() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        let metadata = MetricMetadata {
            name: "api_requests_total".to_string(),
            description: "Total API requests".to_string(),
            metric_type: MetricType::Counter,
            unit: Some("requests".to_string()),
            tags: HashMap::new(),
        };

        assert!(registry.register_metric(metadata).is_ok());
        assert_eq!(registry.list_metrics(), vec!["api_requests_total"]);
    }

    #[test]
    fn test_register_duplicate_metric_fails() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        let metadata = MetricMetadata {
            name: "test_metric".to_string(),
            description: "Test".to_string(),
            metric_type: MetricType::Counter,
            unit: None,
            tags: HashMap::new(),
        };

        assert!(registry.register_metric(metadata.clone()).is_ok());
        assert!(registry.register_metric(metadata).is_err());
    }

    #[test]
    fn test_increment_counter() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "requests".to_string(),
                description: "Request count".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

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

        assert!(registry.increment_counter("requests", 1.0, tags).is_ok());

        let data = registry.get_metric_data("requests", None);
        assert_eq!(data.len(), 1);
    }

    #[test]
    fn test_set_gauge() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "active_connections".to_string(),
                description: "Active connections".to_string(),
                metric_type: MetricType::Gauge,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        assert!(registry
            .set_gauge("active_connections", 42.0, HashMap::new())
            .is_ok());

        let data = registry.get_metric_data("active_connections", None);
        assert_eq!(data.len(), 1);

        if let MetricValue::Gauge(v) = data[0].value {
            assert_eq!(v, 42.0);
        } else {
            panic!("Expected gauge value");
        }
    }

    #[test]
    fn test_observe_histogram() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "request_duration".to_string(),
                description: "Request duration".to_string(),
                metric_type: MetricType::Histogram {
                    buckets: vec![0.1, 0.5, 1.0, 5.0],
                },
                unit: Some("seconds".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        assert!(registry
            .observe_histogram("request_duration", 0.3, HashMap::new())
            .is_ok());

        let data = registry.get_metric_data("request_duration", None);
        assert_eq!(data.len(), 1);
    }

    #[test]
    fn test_compute_sum_aggregation() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "sales".to_string(),
                description: "Sales amount".to_string(),
                metric_type: MetricType::Counter,
                unit: Some("dollars".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .increment_counter("sales", 100.0, HashMap::new())
            .expect("should succeed");
        registry
            .increment_counter("sales", 200.0, HashMap::new())
            .expect("should succeed");
        registry
            .increment_counter("sales", 150.0, HashMap::new())
            .expect("should succeed");

        let sum = registry.compute_aggregation("sales", AggregationStrategy::Sum, None);
        assert_eq!(sum, Some(450.0));
    }

    #[test]
    fn test_compute_average_aggregation() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "response_time".to_string(),
                description: "Response time".to_string(),
                metric_type: MetricType::Gauge,
                unit: Some("ms".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .set_gauge("response_time", 100.0, HashMap::new())
            .expect("should succeed");
        registry
            .set_gauge("response_time", 200.0, HashMap::new())
            .expect("should succeed");
        registry
            .set_gauge("response_time", 150.0, HashMap::new())
            .expect("should succeed");

        let avg = registry.compute_aggregation("response_time", AggregationStrategy::Average, None);
        assert_eq!(avg, Some(150.0));
    }

    #[test]
    fn test_compute_percentile_aggregation() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "latency".to_string(),
                description: "Latency".to_string(),
                metric_type: MetricType::Gauge,
                unit: Some("ms".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        for i in 1..=100 {
            registry
                .set_gauge("latency", i as f64, HashMap::new())
                .expect("should succeed");
        }

        let p50 = registry.compute_aggregation("latency", AggregationStrategy::P50, None);
        assert!(p50.is_some());
        assert!((p50.expect("should succeed") - 50.0).abs() < 2.0); // Allow small error

        let p95 = registry.compute_aggregation("latency", AggregationStrategy::P95, None);
        assert!(p95.is_some());
        assert!((p95.expect("should succeed") - 95.0).abs() < 2.0);
    }

    #[test]
    fn test_metric_filtering_by_tags() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "requests".to_string(),
                description: "Requests".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        let mut tags1 = HashMap::new();
        tags1.insert("endpoint".to_string(), "/api/v1".to_string());
        registry
            .increment_counter("requests", 10.0, tags1)
            .expect("should succeed");

        let mut tags2 = HashMap::new();
        tags2.insert("endpoint".to_string(), "/api/v2".to_string());
        registry
            .increment_counter("requests", 20.0, tags2)
            .expect("should succeed");

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

        let filter = MetricFilter {
            tags: filter_tags,
            min_value: None,
            max_value: None,
            time_range: None,
        };

        let filtered = registry.get_metric_data("requests", Some(&filter));
        assert_eq!(filtered.len(), 1);
    }

    #[test]
    fn test_metric_filtering_by_value_range() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "temperature".to_string(),
                description: "Temperature".to_string(),
                metric_type: MetricType::Gauge,
                unit: Some("celsius".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .set_gauge("temperature", 10.0, HashMap::new())
            .expect("should succeed");
        registry
            .set_gauge("temperature", 25.0, HashMap::new())
            .expect("should succeed");
        registry
            .set_gauge("temperature", 40.0, HashMap::new())
            .expect("should succeed");

        let filter = MetricFilter {
            tags: HashMap::new(),
            min_value: Some(20.0),
            max_value: Some(35.0),
            time_range: None,
        };

        let filtered = registry.get_metric_data("temperature", Some(&filter));
        assert_eq!(filtered.len(), 1);

        if let MetricValue::Gauge(v) = filtered[0].value {
            assert_eq!(v, 25.0);
        }
    }

    #[test]
    fn test_register_computed_metric() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "sales_region_a".to_string(),
                description: "Sales in region A".to_string(),
                metric_type: MetricType::Counter,
                unit: Some("dollars".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .register_metric(MetricMetadata {
                name: "sales_region_b".to_string(),
                description: "Sales in region B".to_string(),
                metric_type: MetricType::Counter,
                unit: Some("dollars".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        let computed = ComputedMetric {
            name: "total_sales".to_string(),
            description: "Total sales across all regions".to_string(),
            source_metrics: vec!["sales_region_a".to_string(), "sales_region_b".to_string()],
            aggregation: AggregationStrategy::Sum,
            filter: None,
        };

        assert!(registry.register_computed_metric(computed).is_ok());
    }

    #[test]
    fn test_evaluate_computed_metric() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "metric_a".to_string(),
                description: "Metric A".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .register_metric(MetricMetadata {
                name: "metric_b".to_string(),
                description: "Metric B".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .increment_counter("metric_a", 100.0, HashMap::new())
            .expect("should succeed");
        registry
            .increment_counter("metric_b", 200.0, HashMap::new())
            .expect("should succeed");

        let computed = ComputedMetric {
            name: "total".to_string(),
            description: "Total".to_string(),
            source_metrics: vec!["metric_a".to_string(), "metric_b".to_string()],
            aggregation: AggregationStrategy::Sum,
            filter: None,
        };

        registry
            .register_computed_metric(computed)
            .expect("should succeed");

        let total = registry.evaluate_computed_metric("total");
        assert_eq!(total, Some(300.0));
    }

    #[test]
    fn test_prometheus_export_counter() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "http_requests_total".to_string(),
                description: "Total HTTP requests".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        let mut tags = HashMap::new();
        tags.insert("method".to_string(), "GET".to_string());
        registry
            .increment_counter("http_requests_total", 42.0, tags)
            .expect("should succeed");

        let output = registry.export_prometheus();
        assert!(output.contains("# HELP http_requests_total Total HTTP requests"));
        assert!(output.contains("# TYPE http_requests_total counter"));
        assert!(output.contains("http_requests_total{method=\"GET\"} 42"));
    }

    #[test]
    fn test_prometheus_export_gauge() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "memory_usage_bytes".to_string(),
                description: "Memory usage in bytes".to_string(),
                metric_type: MetricType::Gauge,
                unit: Some("bytes".to_string()),
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .set_gauge("memory_usage_bytes", 1024.0, HashMap::new())
            .expect("should succeed");

        let output = registry.export_prometheus();
        assert!(output.contains("# TYPE memory_usage_bytes gauge"));
        assert!(output.contains("memory_usage_bytes 1024"));
    }

    #[test]
    fn test_invalid_metric_name_rejected() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        let metadata = MetricMetadata {
            name: "invalid-metric-name!".to_string(),
            description: "Invalid".to_string(),
            metric_type: MetricType::Counter,
            unit: None,
            tags: HashMap::new(),
        };

        assert!(registry.register_metric(metadata).is_err());
    }

    #[test]
    fn test_metric_type_value_mismatch_rejected() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "counter_metric".to_string(),
                description: "Counter".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        // Try to record a gauge value for a counter metric
        let result = registry.record("counter_metric", MetricValue::Gauge(42.0), HashMap::new());

        assert!(result.is_err());
    }

    #[test]
    fn test_data_point_count() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "test".to_string(),
                description: "Test".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        assert_eq!(registry.total_data_points(), 0);

        registry
            .increment_counter("test", 1.0, HashMap::new())
            .expect("should succeed");
        assert_eq!(registry.total_data_points(), 1);

        registry
            .increment_counter("test", 1.0, HashMap::new())
            .expect("should succeed");
        assert_eq!(registry.total_data_points(), 2);
    }

    #[test]
    fn test_get_metadata() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        let metadata = MetricMetadata {
            name: "test_metric".to_string(),
            description: "Test metric description".to_string(),
            metric_type: MetricType::Counter,
            unit: Some("items".to_string()),
            tags: HashMap::new(),
        };

        registry
            .register_metric(metadata.clone())
            .expect("should succeed");

        let retrieved = registry.get_metadata("test_metric");
        assert!(retrieved.is_some());
        assert_eq!(
            retrieved.expect("should succeed").description,
            "Test metric description"
        );
    }

    #[test]
    fn test_cleanup_retains_recent_data() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(1));

        registry
            .register_metric(MetricMetadata {
                name: "test".to_string(),
                description: "Test".to_string(),
                metric_type: MetricType::Counter,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .increment_counter("test", 1.0, HashMap::new())
            .expect("should succeed");

        registry.cleanup_old_data();

        assert_eq!(registry.total_data_points(), 1);
    }

    #[test]
    fn test_percentile_calculation() {
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];

        let p50 = CustomMetricsRegistry::percentile(&values, 50.0);
        assert_eq!(p50, Some(5.0));

        let p100 = CustomMetricsRegistry::percentile(&values, 100.0);
        assert_eq!(p100, Some(10.0));

        let p0 = CustomMetricsRegistry::percentile(&values, 0.0);
        assert_eq!(p0, Some(1.0));
    }

    #[test]
    fn test_min_max_aggregation() {
        let registry = CustomMetricsRegistry::new(Duration::from_secs(3600));

        registry
            .register_metric(MetricMetadata {
                name: "values".to_string(),
                description: "Values".to_string(),
                metric_type: MetricType::Gauge,
                unit: None,
                tags: HashMap::new(),
            })
            .expect("should succeed");

        registry
            .set_gauge("values", 10.0, HashMap::new())
            .expect("should succeed");
        registry
            .set_gauge("values", 50.0, HashMap::new())
            .expect("should succeed");
        registry
            .set_gauge("values", 30.0, HashMap::new())
            .expect("should succeed");

        let min = registry.compute_aggregation("values", AggregationStrategy::Min, None);
        assert_eq!(min, Some(10.0));

        let max = registry.compute_aggregation("values", AggregationStrategy::Max, None);
        assert_eq!(max, Some(50.0));
    }
}