From 85961af1a9b48ed55e642f808690e3039474ecb3 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 27 Jun 2024 11:16:03 -0500 Subject: [PATCH] Refactor the ticker engine to be less error-prone while adding new tickers. --- .../js_build/src/dashlets/rtt_histo_dash.js | 4 +- .../src/dashlets/shaped_unshaped_dash.js | 4 +- .../src/dashlets/throughput_bps_dash.js | 4 +- .../src/dashlets/throughput_pps_dash.js | 4 +- .../src/dashlets/throughput_ring_dash.js | 4 +- .../src/dashlets/top10_downloaders.js | 4 +- .../js_build/src/dashlets/top10flows_bytes.js | 4 +- .../js_build/src/dashlets/top10flows_rate.js | 4 +- .../src/dashlets/tracked_flow_count_dash.js | 4 +- .../src/dashlets/worst10_downloaders.js | 4 +- src/rust/lqosd/src/node_manager/ws.rs | 1 + .../ws/publish_subscribe/publisher_channel.rs | 2 +- .../src/node_manager/ws/published_channels.rs | 37 ++----------------- .../src/node_manager/ws/ticker/cadence.rs | 2 +- .../node_manager/ws/ticker/flow_counter.rs | 2 +- .../node_manager/ws/ticker/rtt_histogram.rs | 2 +- .../src/node_manager/ws/ticker/throughput.rs | 2 +- .../src/node_manager/ws/ticker/top_10.rs | 12 +++--- .../src/node_manager/ws/ticker/top_flows.rs | 8 ++-- 19 files changed, 40 insertions(+), 68 deletions(-) diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/rtt_histo_dash.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/rtt_histo_dash.js index eeefc286..a493408b 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/rtt_histo_dash.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/rtt_histo_dash.js @@ -11,7 +11,7 @@ export class RttHistoDash extends BaseDashlet{ } subscribeTo() { - return [ "rttHistogram" ]; + return [ "RttHistogram" ]; } buildContainer() { @@ -26,7 +26,7 @@ export class RttHistoDash extends BaseDashlet{ } onMessage(msg) { - if (msg.event === "rttHistogram") { + if (msg.event === "RttHistogram") { this.graph.update(msg.data); } } diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/shaped_unshaped_dash.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/shaped_unshaped_dash.js index 2ba44b05..e37d5ca9 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/shaped_unshaped_dash.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/shaped_unshaped_dash.js @@ -7,7 +7,7 @@ export class ShapedUnshapedDash extends BaseDashlet{ } subscribeTo() { - return [ "throughput" ]; + return [ "Throughput" ]; } buildContainer() { @@ -22,7 +22,7 @@ export class ShapedUnshapedDash extends BaseDashlet{ } onMessage(msg) { - if (msg.event === "throughput") { + if (msg.event === "Throughput") { let shaped = msg.data.shaped_bps[0] + msg.data.shaped_bps[1]; let unshaped = msg.data.bps[0] + msg.data.bps[1]; this.graph.update(shaped, unshaped); diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_bps_dash.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_bps_dash.js index 1979658d..c53085b6 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_bps_dash.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_bps_dash.js @@ -7,7 +7,7 @@ export class ThroughputBpsDash extends BaseDashlet{ } subscribeTo() { - return [ "throughput" ]; + return [ "Throughput" ]; } buildContainer() { @@ -22,7 +22,7 @@ export class ThroughputBpsDash extends BaseDashlet{ } onMessage(msg) { - if (msg.event === "throughput") { + if (msg.event === "Throughput") { this.graph.update(msg.data.bps[0], msg.data.bps[1], msg.data.max[0], msg.data.max[1]); } } diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_pps_dash.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_pps_dash.js index 7b4a3709..0123b89b 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_pps_dash.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_pps_dash.js @@ -7,7 +7,7 @@ export class ThroughputPpsDash extends BaseDashlet{ } subscribeTo() { - return [ "throughput" ]; + return [ "Throughput" ]; } buildContainer() { @@ -22,7 +22,7 @@ export class ThroughputPpsDash extends BaseDashlet{ } onMessage(msg) { - if (msg.event === "throughput") { + if (msg.event === "Throughput") { this.graph.update(msg.data.pps[0], msg.data.pps[1]); } } diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_ring_dash.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_ring_dash.js index 67ef158f..cad45780 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_ring_dash.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/throughput_ring_dash.js @@ -11,7 +11,7 @@ export class ThroughputRingDash extends BaseDashlet{ } subscribeTo() { - return [ "throughput" ]; + return [ "Throughput" ]; } buildContainer() { @@ -26,7 +26,7 @@ export class ThroughputRingDash extends BaseDashlet{ } onMessage(msg) { - if (msg.event === "throughput") { + if (msg.event === "Throughput") { let shaped = msg.data.shaped_bps[0] + msg.data.shaped_bps[1]; let unshaped = msg.data.bps[0] + msg.data.bps[1]; this.graph.update(msg.data.shaped_bps, msg.data.bps); diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_downloaders.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_downloaders.js index 2ecf1fed..a5a9cd7c 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_downloaders.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_downloaders.js @@ -13,7 +13,7 @@ export class Top10Downloaders extends BaseDashlet { } subscribeTo() { - return [ "top10downloaders" ]; + return [ "TopDownloads" ]; } buildContainer() { @@ -28,7 +28,7 @@ export class Top10Downloaders extends BaseDashlet { } onMessage(msg) { - if (msg.event === "top10downloaders") { + if (msg.event === "TopDownloads") { let target = document.getElementById(this.id); let t = document.createElement("table"); diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_bytes.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_bytes.js index a290590d..14572e18 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_bytes.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_bytes.js @@ -12,7 +12,7 @@ export class Top10FlowsBytes extends BaseDashlet { } subscribeTo() { - return [ "topFlowsBytes" ]; + return [ "TopFlowsBytes" ]; } buildContainer() { @@ -27,7 +27,7 @@ export class Top10FlowsBytes extends BaseDashlet { } onMessage(msg) { - if (msg.event === "topFlowsBytes") { + if (msg.event === "TopFlowsBytes") { let target = document.getElementById(this.id); let t = document.createElement("table"); diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_rate.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_rate.js index 91c8999e..c12165a2 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_rate.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10flows_rate.js @@ -12,7 +12,7 @@ export class Top10FlowsRate extends BaseDashlet { } subscribeTo() { - return [ "topFlowsRate" ]; + return [ "TopFlowsRate" ]; } buildContainer() { @@ -27,7 +27,7 @@ export class Top10FlowsRate extends BaseDashlet { } onMessage(msg) { - if (msg.event === "topFlowsRate") { + if (msg.event === "TopFlowsRate") { let target = document.getElementById(this.id); let t = document.createElement("table"); diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/tracked_flow_count_dash.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/tracked_flow_count_dash.js index 72ab3acb..18fa22a2 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/tracked_flow_count_dash.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/tracked_flow_count_dash.js @@ -7,7 +7,7 @@ export class TrackedFlowsCount extends BaseDashlet{ } subscribeTo() { - return [ "flowCount" ]; + return [ "FlowCount" ]; } buildContainer() { @@ -22,7 +22,7 @@ export class TrackedFlowsCount extends BaseDashlet{ } onMessage(msg) { - if (msg.event === "flowCount") { + if (msg.event === "FlowCount") { this.graph.update(msg.data); } } diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/worst10_downloaders.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/worst10_downloaders.js index 08775943..47e42527 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/worst10_downloaders.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/worst10_downloaders.js @@ -13,7 +13,7 @@ export class Worst10Downloaders extends BaseDashlet { } subscribeTo() { - return [ "worst10downloaders" ]; + return [ "WorstRTT" ]; } buildContainer() { @@ -28,7 +28,7 @@ export class Worst10Downloaders extends BaseDashlet { } onMessage(msg) { - if (msg.event === "worst10downloaders") { + if (msg.event === "WorstRTT") { let target = document.getElementById(this.id); let t = document.createElement("table"); diff --git a/src/rust/lqosd/src/node_manager/ws.rs b/src/rust/lqosd/src/node_manager/ws.rs index 128abb6a..ff5c6ef2 100644 --- a/src/rust/lqosd/src/node_manager/ws.rs +++ b/src/rust/lqosd/src/node_manager/ws.rs @@ -2,6 +2,7 @@ mod publish_subscribe; mod published_channels; mod ticker; +use std::str::FromStr; use std::sync::Arc; use axum::{extract::{ws::{Message, WebSocket}, WebSocketUpgrade}, response::IntoResponse, routing::get, Extension, Router}; use serde::Deserialize; diff --git a/src/rust/lqosd/src/node_manager/ws/publish_subscribe/publisher_channel.rs b/src/rust/lqosd/src/node_manager/ws/publish_subscribe/publisher_channel.rs index 52f27c28..22a24710 100644 --- a/src/rust/lqosd/src/node_manager/ws/publish_subscribe/publisher_channel.rs +++ b/src/rust/lqosd/src/node_manager/ws/publish_subscribe/publisher_channel.rs @@ -28,7 +28,7 @@ impl PublisherChannel { let welcome = json!( { "event" : "join", - "channel" : self.channel_type.as_str(), + "channel" : self.channel_type.to_string(), } ).to_string(); let _ = sender.send(welcome).await; diff --git a/src/rust/lqosd/src/node_manager/ws/published_channels.rs b/src/rust/lqosd/src/node_manager/ws/published_channels.rs index d91aadad..c225e0c8 100644 --- a/src/rust/lqosd/src/node_manager/ws/published_channels.rs +++ b/src/rust/lqosd/src/node_manager/ws/published_channels.rs @@ -1,43 +1,14 @@ -use strum::EnumIter; +use strum::{Display, EnumIter, EnumString}; -#[derive(PartialEq, Clone, Copy, Debug, EnumIter)] +#[derive(PartialEq, Clone, Copy, Debug, EnumIter, Display, EnumString)] pub enum PublishedChannels { /// Provides a 1-second tick notification to the client Cadence, Throughput, RttHistogram, FlowCount, - Top10Downloaders, - Worst10Downloaders, + TopDownloads, + WorstRTT, TopFlowsBytes, TopFlowsRate, } - -impl PublishedChannels { - pub(super) fn as_str(&self) -> &'static str { - match self { - Self::Throughput => "throughput", - Self::RttHistogram => "rttHistogram", - Self::FlowCount => "flowCount", - Self::Cadence => "cadence", - Self::Top10Downloaders => "top10downloaders", - Self::Worst10Downloaders => "worst10downloaders", - Self::TopFlowsBytes => "topFlowsBytes", - Self::TopFlowsRate => "topFlowsRate", - } - } - - pub(super) fn from_str(s: &str) -> Option { - match s { - "throughput" => Some(Self::Throughput), - "rttHistogram" => Some(Self::RttHistogram), - "flowCount" => Some(Self::FlowCount), - "cadence" => Some(Self::Cadence), - "top10downloaders" => Some(Self::Top10Downloaders), - "worst10downloaders" => Some(Self::Worst10Downloaders), - "topFlowsBytes" => Some(Self::TopFlowsBytes), - "topFlowsRate" => Some(Self::TopFlowsRate), - _ => None, - } - } -} \ No newline at end of file diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/cadence.rs b/src/rust/lqosd/src/node_manager/ws/ticker/cadence.rs index 6c463ec3..aee464aa 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker/cadence.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker/cadence.rs @@ -10,7 +10,7 @@ pub async fn cadence(channels: Arc) { let message = json!( { - "event": "tick" + "event": PublishedChannels::Cadence.to_string(), } ).to_string(); channels.send(PublishedChannels::Cadence, message).await; diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/flow_counter.rs b/src/rust/lqosd/src/node_manager/ws/ticker/flow_counter.rs index 55322d6d..489a87b3 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker/flow_counter.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker/flow_counter.rs @@ -15,7 +15,7 @@ pub async fn flow_count(channels: Arc) { }; let active_flows = json!( { - "event": "flowCount", + "event": PublishedChannels::FlowCount.to_string(), "data": active_flows, } ).to_string(); diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/rtt_histogram.rs b/src/rust/lqosd/src/node_manager/ws/ticker/rtt_histogram.rs index 9834ef43..8de67a15 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker/rtt_histogram.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker/rtt_histogram.rs @@ -14,7 +14,7 @@ pub async fn rtt_histo(channels: Arc) { if let BusResponse::RttHistogram(data) = &histo { let rtt_histo = json!( { - "event": "rttHistogram", + "event": PublishedChannels::RttHistogram.to_string(), "data": data, } ).to_string(); diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/throughput.rs b/src/rust/lqosd/src/node_manager/ws/ticker/throughput.rs index 027042cd..14eff9ca 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker/throughput.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker/throughput.rs @@ -27,7 +27,7 @@ pub async fn throughput(channels: Arc) { }; let bps = json!( { - "event" : "throughput", + "event" : PublishedChannels::Throughput.to_string(), "data": { "bps": bits_per_second, "pps": packets_per_second, diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs b/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs index 6a09d36d..b73f347d 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs @@ -7,7 +7,7 @@ use crate::node_manager::ws::ticker::ipstats_conversion::IpStatsWithPlan; use crate::throughput_tracker::{top_n, worst_n}; pub async fn top_10_downloaders(channels: Arc) { - if !channels.is_channel_alive(PublishedChannels::Top10Downloaders).await { + if !channels.is_channel_alive(PublishedChannels::TopDownloads).await { return; } @@ -19,16 +19,16 @@ pub async fn top_10_downloaders(channels: Arc) { let message = json!( { - "event": "top10downloaders", + "event": PublishedChannels::TopDownloads.to_string(), "data": result } ).to_string(); - channels.send(PublishedChannels::Top10Downloaders, message).await; + channels.send(PublishedChannels::TopDownloads, message).await; } } pub async fn worst_10_downloaders(channels: Arc) { - if !channels.is_channel_alive(PublishedChannels::Worst10Downloaders).await { + if !channels.is_channel_alive(PublishedChannels::WorstRTT).await { return; } @@ -40,10 +40,10 @@ pub async fn worst_10_downloaders(channels: Arc) { let message = json!( { - "event": "worst10downloaders", + "event": PublishedChannels::WorstRTT.to_string(), "data": result } ).to_string(); - channels.send(PublishedChannels::Worst10Downloaders, message).await; + channels.send(PublishedChannels::WorstRTT, message).await; } } \ No newline at end of file diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/top_flows.rs b/src/rust/lqosd/src/node_manager/ws/ticker/top_flows.rs index dc2e29da..8ccea486 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker/top_flows.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker/top_flows.rs @@ -16,7 +16,7 @@ pub async fn top_flows_bytes(channels: Arc) { if let BusResponse::TopFlows(flows) = throughput_tracker::top_flows(10, TopFlowType::Bytes) { let message = json!( { - "event": "topFlowsBytes", + "event": PublishedChannels::TopFlowsBytes.to_string(), "data": flows, } ).to_string(); @@ -25,17 +25,17 @@ pub async fn top_flows_bytes(channels: Arc) { } pub async fn top_flows_rate(channels: Arc) { - if !channels.is_channel_alive(PublishedChannels::TopFlowsBytes).await { + if !channels.is_channel_alive(PublishedChannels::TopFlowsRate).await { return; } if let BusResponse::TopFlows(flows) = throughput_tracker::top_flows(10, TopFlowType::RateEstimate) { let message = json!( { - "event": "topFlowsRate", + "event": PublishedChannels::TopFlowsRate.to_string(), "data": flows, } ).to_string(); - channels.send(PublishedChannels::TopFlowsBytes, message).await; + channels.send(PublishedChannels::TopFlowsRate, message).await; } } \ No newline at end of file