Verified Commit 52fcfdc8 authored by Mattia Righetti's avatar Mattia Righetti
Browse files

refactor: moved mapping logic into proper From trait

parent 79443beb
Loading
Loading
Loading
Loading
+2 −42
Original line number Diff line number Diff line
@@ -28,27 +28,7 @@ pub async fn get_bandwidth(
                .await
                .map_err(ErrorInternalServerError)?
                .into_iter()
                .map(|x| {
                    let write_history = VictoriaMetricsRefBuilder::new()
                        .with_metric(MetricsLabel::WriteBandwidthHistory)
                        .with_fingerprint(x.fingerprint.clone())
                        .build()
                        .unwrap();

                    let read_history = VictoriaMetricsRefBuilder::new()
                        .with_metric(MetricsLabel::ReadBandwidthHistory)
                        .with_fingerprint(x.fingerprint.clone())
                        .build()
                        .unwrap();

                    RelayBandwidth::new(
                        x.fingerprint,
                        Some(write_history),
                        Some(read_history),
                        x.overload_ratelimits_ratelimit,
                        x.overload_fd_exhausted_timestamp.map(|x| x.to_string()),
                    )
                })
                .map(|x| RelayBandwidth::from(x))
                .collect();

            response.relays(relays);
@@ -60,27 +40,7 @@ pub async fn get_bandwidth(
                .await
                .map_err(ErrorInternalServerError)?
                .into_iter()
                .map(|x| {
                    let write_history = VictoriaMetricsRefBuilder::new()
                        .with_metric(MetricsLabel::WriteBandwidthHistory)
                        .with_fingerprint(x.fingerprint.clone())
                        .build()
                        .unwrap();

                    let read_history = VictoriaMetricsRefBuilder::new()
                        .with_metric(MetricsLabel::ReadBandwidthHistory)
                        .with_fingerprint(x.fingerprint.clone())
                        .build()
                        .unwrap();

                    BridgeBandwidth::new(
                        x.fingerprint,
                        Some(write_history),
                        Some(read_history),
                        x.overload_ratelimits_ratelimit,
                        x.overload_fd_exhausted_timestamp.map(|x| x.to_string()),
                    )
                })
                .map(|x| BridgeBandwidth::from(x))
                .collect();

            response.bridges(bridges);
+52 −1
Original line number Diff line number Diff line
@@ -2,7 +2,10 @@ use actix_web::Responder;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::victoriametrics::VictoriaMetricsRef;
use crate::{
    metrics::{BridgeBandwidthRow, RelayBandwidthRow},
    victoriametrics::{labels::MetricsLabel, VictoriaMetricsRef, VictoriaMetricsRefBuilder},
};

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -63,6 +66,30 @@ impl RelayBandwidth {
    }
}

impl From<RelayBandwidthRow> for RelayBandwidth {
    fn from(rbr: RelayBandwidthRow) -> Self {
        let write_history = VictoriaMetricsRefBuilder::new()
            .with_metric(MetricsLabel::WriteBandwidthHistory)
            .with_fingerprint(rbr.fingerprint.clone())
            .build()
            .unwrap();

        let read_history = VictoriaMetricsRefBuilder::new()
            .with_metric(MetricsLabel::ReadBandwidthHistory)
            .with_fingerprint(rbr.fingerprint.clone())
            .build()
            .unwrap();

        RelayBandwidth::new(
            rbr.fingerprint,
            Some(write_history),
            Some(read_history),
            rbr.overload_ratelimits_ratelimit,
            rbr.overload_fd_exhausted_timestamp.map(|x| x.to_string()),
        )
    }
}

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BridgeBandwidth {
@@ -115,3 +142,27 @@ impl BridgeBandwidth {
        }
    }
}

impl From<BridgeBandwidthRow> for BridgeBandwidth {
    fn from(bbr: BridgeBandwidthRow) -> Self {
        let write_history = VictoriaMetricsRefBuilder::new()
            .with_metric(MetricsLabel::WriteBandwidthHistory)
            .with_fingerprint(bbr.fingerprint.clone())
            .build()
            .unwrap();

        let read_history = VictoriaMetricsRefBuilder::new()
            .with_metric(MetricsLabel::ReadBandwidthHistory)
            .with_fingerprint(bbr.fingerprint.clone())
            .build()
            .unwrap();

        BridgeBandwidth::new(
            bbr.fingerprint,
            Some(write_history),
            Some(read_history),
            bbr.overload_ratelimits_ratelimit,
            bbr.overload_fd_exhausted_timestamp.map(|x| x.to_string()),
        )
    }
}