Skip to main content

linkrs_metrics/manager/
aggregated.rs

1//! Aggregated pattern delegation to the dedicated pattern manager.
2use serde::{Deserialize, Serialize};
3
4use crate::profile::QueryProfile;
5
6use super::core::StatsManager;
7
8/// Serializable pattern entry for handlers.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct QueryPatternSnapshot {
11    pub normalized_query: String,
12    pub query_type: String,
13    pub labels: Vec<String>,
14    pub execution_count: u64,
15    pub avg_duration_ms: f64,
16    pub p95_duration_ms: f64,
17    pub p99_duration_ms: f64,
18    pub error_rate: f64,
19    pub error_count: u64,
20}
21
22impl StatsManager {
23    /// Record aggregated query statistics
24    pub fn record_aggregated_query(&self, profile: &QueryProfile, is_slow: bool) {
25        self.aggregated_stats.record_query(profile, is_slow);
26    }
27
28    /// Get top N slow query patterns by average duration
29    pub fn get_top_n_slow_query_patterns(
30        &self,
31        limit: usize,
32    ) -> Vec<crate::aggregated_stats::AggregatedQueryStats> {
33        self.aggregated_stats.get_top_n_slow_queries(limit)
34    }
35
36    pub fn pattern_snapshot(&self, limit: usize) -> Vec<QueryPatternSnapshot> {
37        self.aggregated_stats
38            .get_top_n_slow_queries(limit)
39            .into_iter()
40            .map(|s| {
41                let avg_duration_ms = s.avg_duration_ms();
42                let p95_duration_ms = s.p95_duration_ms();
43                let p99_duration_ms = s.p99_duration_ms();
44                let error_rate = s.error_rate();
45                let execution_count = s.execution_count;
46                let error_count = s.error_count;
47                let pattern = s.pattern;
48                QueryPatternSnapshot {
49                    normalized_query: pattern.normalized_query,
50                    query_type: pattern.query_type,
51                    labels: pattern.labels,
52                    execution_count,
53                    avg_duration_ms,
54                    p95_duration_ms,
55                    p99_duration_ms,
56                    error_rate,
57                    error_count,
58                }
59            })
60            .collect()
61    }
62
63    /// Get top N slow query patterns by total duration
64    pub fn get_top_n_patterns_by_total_duration(
65        &self,
66        limit: usize,
67    ) -> Vec<crate::aggregated_stats::AggregatedQueryStats> {
68        self.aggregated_stats.get_top_n_by_total_duration(limit)
69    }
70
71    /// Get top N slow query patterns by execution count
72    pub fn get_top_n_patterns_by_execution_count(
73        &self,
74        limit: usize,
75    ) -> Vec<crate::aggregated_stats::AggregatedQueryStats> {
76        self.aggregated_stats.get_top_n_by_execution_count(limit)
77    }
78
79    /// Get statistics for a specific query pattern
80    pub fn get_pattern_stats(
81        &self,
82        normalized_query: &str,
83    ) -> Option<crate::aggregated_stats::AggregatedQueryStats> {
84        self.aggregated_stats.get_pattern_stats(normalized_query)
85    }
86
87    /// Get all aggregated statistics
88    pub fn get_all_aggregated_stats(&self) -> Vec<crate::aggregated_stats::AggregatedQueryStats> {
89        self.aggregated_stats.get_all_stats()
90    }
91
92    /// Get total number of queries processed
93    pub fn get_total_aggregated_queries(&self) -> u64 {
94        self.aggregated_stats.get_total_queries()
95    }
96
97    /// Get total number of slow queries
98    pub fn get_total_aggregated_slow_queries(&self) -> u64 {
99        self.aggregated_stats.get_total_slow_queries()
100    }
101
102    /// Get current queries per second
103    pub fn get_current_qps(&self) -> u64 {
104        self.aggregated_stats.get_current_qps()
105    }
106
107    /// Get number of query patterns
108    pub fn get_pattern_count(&self) -> usize {
109        self.aggregated_stats.get_pattern_count()
110    }
111
112    /// Clear all aggregated statistics
113    pub fn clear_aggregated_stats(&self) {
114        self.aggregated_stats.clear();
115    }
116
117    /// Cleanup expired aggregated statistics
118    pub fn cleanup_aggregated_stats(&self) {
119        self.aggregated_stats.cleanup();
120    }
121}