Skip to main content

canic_core/workflow/metrics/
query.rs

1use crate::{
2    dto::{
3        metrics::{MetricEntry, MetricsKind, MetricsRequest, MetricsResponse},
4        page::{Page, PageRequest},
5    },
6    ops::runtime::metrics::MetricsOps,
7    workflow::view::paginate::paginate_vec,
8};
9
10///
11/// MetricsQuery
12///
13/// Read-only query façade over metric snapshots.
14/// Responsible for mapping, sorting, and pagination only.
15///
16
17pub struct MetricsQuery;
18
19impl MetricsQuery {
20    #[must_use]
21    pub fn dispatch(req: MetricsRequest) -> MetricsResponse {
22        let entries = Self::page(req.kind, req.page);
23
24        MetricsResponse { entries }
25    }
26
27    #[must_use]
28    pub fn page(kind: MetricsKind, page: PageRequest) -> Page<MetricEntry> {
29        let mut entries = MetricsOps::entries(kind);
30        entries.sort_by(|a, b| {
31            a.labels
32                .cmp(&b.labels)
33                .then_with(|| a.principal.cmp(&b.principal))
34                .then_with(|| a.count.cmp(&b.count))
35                .then_with(|| a.value_u64.cmp(&b.value_u64))
36                .then_with(|| a.value_u128.cmp(&b.value_u128))
37        });
38
39        paginate_vec(entries, page)
40    }
41}