Skip to main content

nym_node_requests/api/v1/metrics/
models.rs

1// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4pub use mixing::*;
5pub use session::*;
6pub use verloc::*;
7pub use wireguard::*;
8
9pub mod wireguard {
10    use serde::{Deserialize, Serialize};
11
12    #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
13    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
14    pub struct WireguardStats {
15        pub bytes_tx: usize,
16
17        pub bytes_rx: usize,
18    }
19}
20
21pub mod packets {
22    use serde::{Deserialize, Serialize};
23
24    #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
25    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
26    pub struct PacketsStats {
27        pub ingress_mixing: IngressMixingStats,
28        pub egress_mixing: EgressMixingStats,
29    }
30
31    #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
32    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
33    pub struct IngressMixingStats {
34        // forward hop packets (i.e. to mixnode)
35        pub forward_hop_packets_received: usize,
36
37        // final hop packets (i.e. to gateway)
38        pub final_hop_packets_received: usize,
39
40        // packets that failed to get unwrapped
41        pub malformed_packets_received: usize,
42
43        // packets that were already received and processed before
44        pub replayed_packets_received: usize,
45
46        // (forward) packets that had invalid, i.e. too large, delays
47        pub excessive_delay_packets: usize,
48
49        // forward hop packets (i.e. to mixnode)
50        pub forward_hop_packets_dropped: usize,
51
52        // final hop packets (i.e. to gateway)
53        pub final_hop_packets_dropped: usize,
54    }
55
56    #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
57    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
58    pub struct EgressMixingStats {
59        pub forward_hop_packets_sent: usize,
60
61        pub forward_hop_packets_dropped: usize,
62
63        pub ack_packets_sent: usize,
64    }
65}
66
67pub mod mixing {
68    use serde::{Deserialize, Serialize};
69    use time::OffsetDateTime;
70
71    #[derive(Serialize, Deserialize, Debug, Clone)]
72    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
73    pub struct LegacyMixingStats {
74        #[serde(with = "time::serde::rfc3339")]
75        pub update_time: OffsetDateTime,
76
77        #[serde(with = "time::serde::rfc3339")]
78        pub previous_update_time: OffsetDateTime,
79
80        pub received_since_startup: u64,
81
82        // note: sent does not imply forwarded. We don't know if it was delivered successfully
83        pub sent_since_startup: u64,
84
85        // we know for sure we dropped those packets
86        pub dropped_since_startup: u64,
87
88        pub received_since_last_update: u64,
89
90        // note: sent does not imply forwarded. We don't know if it was delivered successfully
91        pub sent_since_last_update: u64,
92
93        // we know for sure we dropped those packets
94        pub dropped_since_last_update: u64,
95    }
96}
97
98pub mod verloc {
99    use nym_crypto::asymmetric::ed25519::{self, serde_helpers::bs58_ed25519_pubkey};
100    use serde::{Deserialize, Serialize};
101    use std::time::Duration;
102    use time::OffsetDateTime;
103    #[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq)]
104    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
105    pub struct VerlocNodeResult {
106        #[serde(with = "bs58_ed25519_pubkey")]
107        #[cfg_attr(feature = "openapi", schema(value_type = String))]
108        pub node_identity: ed25519::PublicKey,
109
110        pub latest_measurement: Option<VerlocMeasurement>,
111    }
112
113    #[derive(Serialize, Deserialize, Default, Debug, Clone)]
114    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
115    pub struct VerlocStats {
116        pub previous: VerlocResult,
117        pub current: VerlocResult,
118    }
119
120    #[derive(Serialize, Deserialize, Default, Debug, Clone)]
121    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
122    #[serde(rename_all = "camelCase")]
123    pub enum VerlocResult {
124        Data(VerlocResultData),
125        MeasurementInProgress,
126        #[default]
127        Unavailable,
128    }
129
130    #[derive(Serialize, Deserialize, Debug, Clone)]
131    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
132    pub struct VerlocResultData {
133        pub nodes_tested: usize,
134
135        #[serde(with = "time::serde::rfc3339")]
136        pub run_started: OffsetDateTime,
137
138        #[serde(with = "time::serde::rfc3339::option")]
139        pub run_finished: Option<OffsetDateTime>,
140
141        pub results: Vec<VerlocNodeResult>,
142    }
143
144    #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
145    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
146    pub struct VerlocMeasurement {
147        /// Minimum RTT duration it took to receive an echo packet.
148        #[serde(serialize_with = "humantime_serde::serialize")]
149        pub minimum: Duration,
150
151        /// Average RTT duration it took to receive the echo packets.
152        #[serde(serialize_with = "humantime_serde::serialize")]
153        pub mean: Duration,
154
155        /// Maximum RTT duration it took to receive an echo packet.
156        #[serde(serialize_with = "humantime_serde::serialize")]
157        pub maximum: Duration,
158
159        /// The standard deviation of the RTT duration it took to receive the echo packets.
160        #[serde(serialize_with = "humantime_serde::serialize")]
161        pub standard_deviation: Duration,
162    }
163}
164
165pub mod session {
166    use serde::{Deserialize, Serialize};
167    use time::OffsetDateTime;
168
169    #[derive(Serialize, Deserialize, Debug, Clone)]
170    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
171    pub struct Session {
172        pub duration_ms: u64,
173        pub typ: String,
174    }
175
176    #[derive(Serialize, Deserialize, Debug, Clone)]
177    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
178    pub struct SessionStats {
179        #[serde(with = "time::serde::rfc3339")]
180        #[cfg_attr(feature = "openapi", schema(value_type = String))]
181        pub update_time: OffsetDateTime,
182
183        pub unique_active_users: u32,
184
185        #[serde(default = "Vec::new")] // field was added later
186        pub unique_active_users_hashes: Vec<String>,
187
188        pub sessions: Vec<Session>,
189
190        pub sessions_started: u32,
191
192        pub sessions_finished: u32,
193    }
194}