canic_core/workflow/metrics/
query.rs

1use crate::{
2    dto::{
3        metrics::{
4            AccessMetricEntry, DelegationMetricEntry, EndpointHealthView, HttpMetricEntry,
5            IccMetricEntry, SystemMetricEntry, TimerMetricEntry,
6        },
7        page::{Page, PageRequest},
8    },
9    ops::{perf::PerfOps, runtime::metrics::MetricsOps},
10    perf::PerfEntry,
11    workflow::{metrics::mapper::MetricsMapper, view::paginate::paginate_vec},
12};
13
14///
15/// MetricsQuery
16///
17/// Read-only query façade over metric snapshots.
18/// Responsible for mapping, sorting, and pagination only.
19///
20
21pub struct MetricsQuery;
22
23impl MetricsQuery {
24    #[must_use]
25    pub fn system_snapshot() -> Vec<SystemMetricEntry> {
26        let snapshot = MetricsOps::system_snapshot();
27        let mut entries = MetricsMapper::system_metrics_to_view(snapshot.entries);
28
29        entries.sort_by(|a, b| a.kind.cmp(&b.kind));
30
31        entries
32    }
33
34    #[must_use]
35    pub fn icc_page(page: PageRequest) -> Page<IccMetricEntry> {
36        let snapshot = MetricsOps::icc_snapshot();
37        let mut entries = MetricsMapper::icc_metrics_to_view(snapshot.entries);
38
39        entries.sort_by(|a, b| {
40            a.target
41                .as_slice()
42                .cmp(b.target.as_slice())
43                .then_with(|| a.method.cmp(&b.method))
44        });
45
46        paginate_vec(entries, page)
47    }
48
49    #[must_use]
50    pub fn http_page(page: PageRequest) -> Page<HttpMetricEntry> {
51        let snapshot = MetricsOps::http_snapshot();
52        let mut entries = MetricsMapper::http_metrics_to_view(snapshot.entries);
53
54        entries.sort_by(|a, b| a.method.cmp(&b.method).then_with(|| a.label.cmp(&b.label)));
55
56        paginate_vec(entries, page)
57    }
58
59    #[must_use]
60    pub fn timer_page(page: PageRequest) -> Page<TimerMetricEntry> {
61        let snapshot = MetricsOps::timer_snapshot();
62        let mut entries = MetricsMapper::timer_metrics_to_view(snapshot.entries);
63
64        entries.sort_by(|a, b| {
65            a.mode
66                .cmp(&b.mode)
67                .then_with(|| a.delay_ms.cmp(&b.delay_ms))
68                .then_with(|| a.label.cmp(&b.label))
69        });
70
71        paginate_vec(entries, page)
72    }
73
74    #[must_use]
75    pub fn access_page(page: PageRequest) -> Page<AccessMetricEntry> {
76        let snapshot = MetricsOps::access_snapshot();
77        let mut entries = MetricsMapper::access_metrics_to_view(snapshot.entries);
78
79        entries.sort_by(|a, b| {
80            a.endpoint
81                .cmp(&b.endpoint)
82                .then_with(|| a.kind.cmp(&b.kind))
83        });
84
85        paginate_vec(entries, page)
86    }
87
88    #[must_use]
89    pub fn delegation_page(page: PageRequest) -> Page<DelegationMetricEntry> {
90        let snapshot = MetricsOps::delegation_snapshot();
91        let mut entries = MetricsMapper::delegation_metrics_to_view(snapshot.entries);
92
93        entries.sort_by(|a, b| a.authority.as_slice().cmp(b.authority.as_slice()));
94
95        paginate_vec(entries, page)
96    }
97
98    #[must_use]
99    pub fn perf_page(page: PageRequest) -> Page<PerfEntry> {
100        let snapshot = PerfOps::snapshot();
101        paginate_vec(snapshot.entries, page)
102    }
103
104    #[must_use]
105    pub fn endpoint_health_page(
106        page: PageRequest,
107        exclude_endpoint: Option<&str>,
108    ) -> Page<EndpointHealthView> {
109        let snapshot = MetricsOps::endpoint_health_snapshot();
110        let mut entries = MetricsMapper::endpoint_health_to_view(
111            snapshot.attempts,
112            snapshot.results,
113            snapshot.access,
114            exclude_endpoint,
115        );
116
117        entries.sort_by(|a, b| a.endpoint.cmp(&b.endpoint));
118
119        paginate_vec(entries, page)
120    }
121}