canic_core/workflow/metrics/
query.rs

1use crate::{
2    dto::{
3        metrics::{
4            AccessMetricEntry, EndpointHealthView, HttpMetricEntry, IccMetricEntry,
5            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
18pub struct MetricsQuery;
19
20impl MetricsQuery {
21    #[must_use]
22    pub fn system_snapshot() -> Vec<SystemMetricEntry> {
23        let snapshot = MetricsOps::system_snapshot();
24        let mut entries = MetricsMapper::system_metrics_to_view(snapshot.entries);
25
26        entries.sort_by(|a, b| a.kind.cmp(&b.kind));
27
28        entries
29    }
30
31    #[must_use]
32    pub fn icc_page(page: PageRequest) -> Page<IccMetricEntry> {
33        let snapshot = MetricsOps::icc_snapshot();
34        let mut entries = MetricsMapper::icc_metrics_to_view(snapshot.entries);
35
36        entries.sort_by(|a, b| {
37            a.target
38                .as_slice()
39                .cmp(b.target.as_slice())
40                .then_with(|| a.method.cmp(&b.method))
41        });
42
43        paginate_vec(entries, page)
44    }
45
46    #[must_use]
47    pub fn http_page(page: PageRequest) -> Page<HttpMetricEntry> {
48        let snapshot = MetricsOps::http_snapshot();
49        let mut entries = MetricsMapper::http_metrics_to_view(snapshot.entries);
50
51        entries.sort_by(|a, b| a.method.cmp(&b.method).then_with(|| a.label.cmp(&b.label)));
52
53        paginate_vec(entries, page)
54    }
55
56    #[must_use]
57    pub fn timer_page(page: PageRequest) -> Page<TimerMetricEntry> {
58        let snapshot = MetricsOps::timer_snapshot();
59        let mut entries = MetricsMapper::timer_metrics_to_view(snapshot.entries);
60
61        entries.sort_by(|a, b| {
62            a.mode
63                .cmp(&b.mode)
64                .then_with(|| a.delay_ms.cmp(&b.delay_ms))
65                .then_with(|| a.label.cmp(&b.label))
66        });
67
68        paginate_vec(entries, page)
69    }
70
71    #[must_use]
72    pub fn access_page(page: PageRequest) -> Page<AccessMetricEntry> {
73        let snapshot = MetricsOps::access_snapshot();
74        let mut entries = MetricsMapper::access_metrics_to_view(snapshot.entries);
75
76        entries.sort_by(|a, b| {
77            a.endpoint
78                .cmp(&b.endpoint)
79                .then_with(|| a.kind.cmp(&b.kind))
80        });
81
82        paginate_vec(entries, page)
83    }
84
85    #[must_use]
86    pub fn perf_page(page: PageRequest) -> Page<PerfEntry> {
87        let snapshot = PerfOps::snapshot();
88        paginate_vec(snapshot.entries, page)
89    }
90
91    #[must_use]
92    pub fn endpoint_health_page(
93        page: PageRequest,
94        exclude_endpoint: Option<&str>,
95    ) -> Page<EndpointHealthView> {
96        let snapshot = MetricsOps::endpoint_health_snapshot();
97        let mut entries = MetricsMapper::endpoint_health_to_view(
98            snapshot.attempts,
99            snapshot.results,
100            snapshot.access,
101            exclude_endpoint,
102        );
103
104        entries.sort_by(|a, b| a.endpoint.cmp(&b.endpoint));
105
106        paginate_vec(entries, page)
107    }
108}