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        // packets that were received from a globally known network monitor agent
47        pub test_packets_received: usize,
48
49        // (forward) packets that had invalid, i.e. too large, delays
50        pub excessive_delay_packets: usize,
51
52        // forward hop packets (i.e. to mixnode)
53        pub forward_hop_packets_dropped: usize,
54
55        // final hop packets (i.e. to gateway)
56        pub final_hop_packets_dropped: usize,
57    }
58
59    #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
60    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
61    pub struct EgressMixingStats {
62        pub forward_hop_packets_sent: usize,
63
64        pub forward_hop_packets_dropped: usize,
65
66        pub ack_packets_sent: usize,
67    }
68}
69
70pub mod mixing {
71    use serde::{Deserialize, Serialize};
72    use time::OffsetDateTime;
73
74    #[derive(Serialize, Deserialize, Debug, Clone)]
75    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
76    pub struct LegacyMixingStats {
77        #[serde(with = "time::serde::rfc3339")]
78        pub update_time: OffsetDateTime,
79
80        #[serde(with = "time::serde::rfc3339")]
81        pub previous_update_time: OffsetDateTime,
82
83        pub received_since_startup: u64,
84
85        // note: sent does not imply forwarded. We don't know if it was delivered successfully
86        pub sent_since_startup: u64,
87
88        // we know for sure we dropped those packets
89        pub dropped_since_startup: u64,
90
91        pub received_since_last_update: u64,
92
93        // note: sent does not imply forwarded. We don't know if it was delivered successfully
94        pub sent_since_last_update: u64,
95
96        // we know for sure we dropped those packets
97        pub dropped_since_last_update: u64,
98    }
99}
100
101pub mod verloc {
102    use nym_crypto::asymmetric::ed25519::{self, serde_helpers::bs58_ed25519_pubkey};
103    use serde::{Deserialize, Serialize};
104    use std::time::Duration;
105    use time::OffsetDateTime;
106    #[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq)]
107    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
108    pub struct VerlocNodeResult {
109        #[serde(with = "bs58_ed25519_pubkey")]
110        #[cfg_attr(feature = "openapi", schema(value_type = String))]
111        pub node_identity: ed25519::PublicKey,
112
113        pub latest_measurement: Option<VerlocMeasurement>,
114    }
115
116    #[derive(Serialize, Deserialize, Default, Debug, Clone)]
117    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
118    pub struct VerlocStats {
119        pub previous: VerlocResult,
120        pub current: VerlocResult,
121    }
122
123    #[derive(Serialize, Deserialize, Default, Debug, Clone)]
124    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
125    #[serde(rename_all = "camelCase")]
126    pub enum VerlocResult {
127        Data(VerlocResultData),
128        MeasurementInProgress,
129        #[default]
130        Unavailable,
131    }
132
133    #[derive(Serialize, Deserialize, Debug, Clone)]
134    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
135    pub struct VerlocResultData {
136        pub nodes_tested: usize,
137
138        #[serde(with = "time::serde::rfc3339")]
139        pub run_started: OffsetDateTime,
140
141        #[serde(with = "time::serde::rfc3339::option")]
142        pub run_finished: Option<OffsetDateTime>,
143
144        pub results: Vec<VerlocNodeResult>,
145    }
146
147    #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
148    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
149    pub struct VerlocMeasurement {
150        /// Minimum RTT duration it took to receive an echo packet.
151        #[serde(serialize_with = "humantime_serde::serialize")]
152        pub minimum: Duration,
153
154        /// Average RTT duration it took to receive the echo packets.
155        #[serde(serialize_with = "humantime_serde::serialize")]
156        pub mean: Duration,
157
158        /// Maximum RTT duration it took to receive an echo packet.
159        #[serde(serialize_with = "humantime_serde::serialize")]
160        pub maximum: Duration,
161
162        /// The standard deviation of the RTT duration it took to receive the echo packets.
163        #[serde(serialize_with = "humantime_serde::serialize")]
164        pub standard_deviation: Duration,
165    }
166}
167
168pub mod session {
169    use serde::{Deserialize, Serialize};
170    use time::OffsetDateTime;
171
172    #[derive(Serialize, Deserialize, Debug, Clone)]
173    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
174    pub struct Session {
175        pub duration_ms: u64,
176        pub typ: String,
177    }
178
179    #[derive(Serialize, Deserialize, Debug, Clone)]
180    #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
181    pub struct SessionStats {
182        #[serde(with = "time::serde::rfc3339")]
183        #[cfg_attr(feature = "openapi", schema(value_type = String))]
184        pub update_time: OffsetDateTime,
185
186        pub unique_active_users: u32,
187
188        #[serde(default = "Vec::new")] // field was added later
189        pub unique_active_users_hashes: Vec<String>,
190
191        pub sessions: Vec<Session>,
192
193        pub sessions_started: u32,
194
195        pub sessions_finished: u32,
196    }
197}