ic_query/ic/model/requests/
metrics.rs1use super::IcDashboardRequest;
8
9use serde::Serialize;
10use std::{fmt, str::FromStr};
11
12#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize)]
19#[serde(rename_all = "kebab-case")]
20pub enum IcMetricKind {
21 InstructionRate,
23 MessageExecutionRate,
25 CycleBurnRate,
27 BlockRate,
29 IcNodeCount,
31 IcSubnetTotal,
33 RegisteredCanistersCount,
35 TotalIcEnergyConsumptionRateKwh,
37 BoundaryNodesCount,
39}
40
41impl IcMetricKind {
42 #[must_use]
44 pub const fn all() -> [Self; 9] {
45 [
46 Self::InstructionRate,
47 Self::MessageExecutionRate,
48 Self::CycleBurnRate,
49 Self::BlockRate,
50 Self::IcNodeCount,
51 Self::IcSubnetTotal,
52 Self::RegisteredCanistersCount,
53 Self::TotalIcEnergyConsumptionRateKwh,
54 Self::BoundaryNodesCount,
55 ]
56 }
57
58 #[must_use]
60 pub const fn as_str(self) -> &'static str {
61 match self {
62 Self::InstructionRate => "instruction-rate",
63 Self::MessageExecutionRate => "message-execution-rate",
64 Self::CycleBurnRate => "cycle-burn-rate",
65 Self::BlockRate => "block-rate",
66 Self::IcNodeCount => "ic-node-count",
67 Self::IcSubnetTotal => "ic-subnet-total",
68 Self::RegisteredCanistersCount => "registered-canisters-count",
69 Self::TotalIcEnergyConsumptionRateKwh => "total-ic-energy-consumption-rate-kwh",
70 Self::BoundaryNodesCount => "boundary-nodes-count",
71 }
72 }
73
74 #[cfg(feature = "dashboard-host")]
75 pub(crate) const fn series_names(self) -> &'static [&'static str] {
76 match self {
77 Self::InstructionRate => &["instruction_rate"],
78 Self::MessageExecutionRate => &["message_execution_rate"],
79 Self::CycleBurnRate => &["cycle_burn_rate"],
80 Self::BlockRate => &["block_rate"],
81 Self::IcNodeCount => &["total_nodes", "up_nodes"],
82 Self::IcSubnetTotal => &["ic_subnet_total"],
83 Self::RegisteredCanistersCount => &["running_canisters", "stopped_canisters"],
84 Self::TotalIcEnergyConsumptionRateKwh => &["energy_consumption_rate"],
85 Self::BoundaryNodesCount => &["boundary_nodes_count"],
86 }
87 }
88}
89
90impl fmt::Display for IcMetricKind {
91 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
92 formatter.write_str(self.as_str())
93 }
94}
95
96impl FromStr for IcMetricKind {
97 type Err = String;
98
99 fn from_str(value: &str) -> Result<Self, Self::Err> {
100 Self::all()
101 .into_iter()
102 .find(|metric| metric.as_str() == value)
103 .ok_or_else(|| format!("unsupported IC Dashboard metric {value:?}"))
104 }
105}
106
107#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
114pub struct IcMetricQuery {
115 pub metric: IcMetricKind,
117 pub start_unix_secs: u64,
119 pub end_unix_secs: u64,
121 pub step_secs: u32,
123}
124
125impl IcMetricQuery {
126 #[must_use]
128 pub const fn new(
129 metric: IcMetricKind,
130 start_unix_secs: u64,
131 end_unix_secs: u64,
132 step_secs: u32,
133 ) -> Self {
134 Self {
135 metric,
136 start_unix_secs,
137 end_unix_secs,
138 step_secs,
139 }
140 }
141}
142
143#[derive(Clone, Debug, Eq, PartialEq)]
150pub struct IcMetricRequest {
151 pub dashboard: IcDashboardRequest,
153 pub query: IcMetricQuery,
155}
156
157impl IcMetricRequest {
158 #[must_use]
160 pub const fn new(dashboard: IcDashboardRequest, query: IcMetricQuery) -> Self {
161 Self { dashboard, query }
162 }
163}