hopr_api/network/
types.rs

1/// Network health represented with colors, where green is the best and red
2/// is the worst possible observed network quality.
3#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, strum::Display, strum::EnumString)]
4pub enum Health {
5    /// Unknown health, on application startup
6    Unknown = 0,
7    /// No connection, default
8    Red = 1,
9    /// Low-quality connection to at least 1 public relay
10    Orange = 2,
11    /// High-quality connection to at least 1 public relay
12    Yellow = 3,
13    /// High-quality connection to at least 1 public relay and 1 NAT node
14    Green = 4,
15}
16
17pub enum Measurement {
18    Probe(std::result::Result<std::time::Duration, ()>),
19}
20
21pub trait Observable {
22    /// Record a new result of the probe towards the measured peer.
23    fn record_probe(&mut self, latency: std::result::Result<std::time::Duration, ()>);
24
25    /// The timestamp of the last update.
26    fn last_update(&self) -> std::time::Duration;
27
28    /// Return average latency observed for the measured peer.
29    fn average_latency(&self) -> Option<std::time::Duration>;
30
31    /// A value representing the average success rate of probes.
32    ///
33    /// It is from the range [0.0, 1.0]. The higher the value, the better the score.
34    fn average_probe_rate(&self) -> f64;
35
36    /// A value scoring the observed peer.
37    ///
38    /// It is from the range [0.0, 1.0]. The higher the value, the better the score.
39    fn score(&self) -> f64;
40}
41
42#[cfg(test)]
43mod tests {
44    use super::Health;
45
46    #[test]
47    fn network_health_should_be_ordered_numerically_for_hopr_metrics_output() {
48        assert_eq!(Health::Unknown as i32, 0);
49        assert_eq!(Health::Red as i32, 1);
50        assert_eq!(Health::Orange as i32, 2);
51        assert_eq!(Health::Yellow as i32, 3);
52        assert_eq!(Health::Green as i32, 4);
53    }
54
55    #[test]
56    fn network_health_should_serialize_to_a_proper_string() {
57        assert_eq!(format!("{}", Health::Orange), "Orange".to_owned())
58    }
59
60    #[test]
61    fn network_health_should_deserialize_from_proper_string() -> anyhow::Result<()> {
62        let parsed: Health = "Orange".parse()?;
63        assert_eq!(parsed, Health::Orange);
64
65        Ok(())
66    }
67}