Skip to main content

linkrs_metrics/manager/
errors.rs

1//! Error delegation to the dedicated error statistics manager.
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5use crate::error_stats::{ErrorInfo, ErrorType, QueryPhase};
6use crate::profile::QueryProfile;
7
8use super::core::StatsManager;
9
10/// Serializable error snapshot for handlers.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ErrorSnapshot {
13    pub total_errors: u64,
14    pub errors_by_type: HashMap<String, u64>,
15    pub errors_by_phase: HashMap<String, u64>,
16}
17
18/// Serializable recent error entry for handlers.
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct RecentErrorView {
21    pub timestamp_secs: u64,
22    pub error_type: String,
23    pub error_phase: String,
24    pub message: String,
25    pub query_text: Option<String>,
26}
27
28impl StatsManager {
29    pub fn record_error(&self, error_type: ErrorType, phase: QueryPhase) {
30        self.error_stats.record_error(error_type, phase);
31    }
32
33    pub fn get_error_count(&self, error_type: ErrorType) -> u64 {
34        self.error_stats.get_error_count(error_type)
35    }
36
37    pub fn get_error_count_by_phase(&self, phase: QueryPhase) -> u64 {
38        self.error_stats.get_error_count_by_phase(phase)
39    }
40
41    pub fn get_all_error_counts(&self) -> HashMap<ErrorType, u64> {
42        self.error_stats.get_all_error_counts()
43    }
44
45    pub fn get_all_error_counts_by_phase(&self) -> HashMap<QueryPhase, u64> {
46        self.error_stats.get_all_error_counts_by_phase()
47    }
48
49    pub fn reset_error_counts(&self) {
50        self.error_stats.reset_error_counts();
51    }
52
53    pub fn record_failed_query(&self, mut profile: QueryProfile, error_info: ErrorInfo) {
54        profile.mark_failed_with_info(error_info.clone());
55        self.error_stats
56            .record_error_with_context(&error_info, Some(profile.query_text.clone()));
57        self.record_query_profile(profile);
58    }
59
60    pub fn get_error_summary(&self) -> crate::error_stats::ErrorSummary {
61        self.error_stats.get_error_summary()
62    }
63
64    pub fn get_recent_errors(&self, limit: usize) -> Vec<crate::error_stats::RecentError> {
65        self.error_stats.get_recent_errors(limit)
66    }
67
68    pub fn error_snapshot(&self) -> ErrorSnapshot {
69        let summary = self.error_stats.get_error_summary();
70        ErrorSnapshot {
71            total_errors: summary.total_errors,
72            errors_by_type: summary
73                .errors_by_type
74                .into_iter()
75                .map(|(k, v)| (k.to_string(), v))
76                .collect(),
77            errors_by_phase: summary
78                .errors_by_phase
79                .into_iter()
80                .map(|(k, v)| (k.to_string(), v))
81                .collect(),
82        }
83    }
84
85    pub fn recent_errors_snapshot(&self, limit: usize) -> Vec<RecentErrorView> {
86        self.error_stats
87            .get_recent_errors(limit)
88            .into_iter()
89            .map(|e| RecentErrorView {
90                timestamp_secs: e.timestamp,
91                error_type: e.error_type.to_string(),
92                error_phase: e.error_phase.to_string(),
93                message: e.message,
94                query_text: e.query_text,
95            })
96            .collect()
97    }
98}