From e6d75c9cd0200037557aa73000f98a02e3db7162 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Tue, 2 Jul 2024 16:12:39 -0500 Subject: [PATCH] Include shaped device and unknown IP counts in UI2 template. --- .../src/node_manager/js_build/src/template.js | 11 ++++- src/rust/lqosd/src/node_manager/local_api.rs | 2 + .../node_manager/local_api/device_counts.rs | 42 +++++++++++++++++++ .../src/node_manager/static2/template.html | 4 +- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/rust/lqosd/src/node_manager/local_api/device_counts.rs diff --git a/src/rust/lqosd/src/node_manager/js_build/src/template.js b/src/rust/lqosd/src/node_manager/js_build/src/template.js index 432c72eb..5a1b1187 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/template.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/template.js @@ -37,4 +37,13 @@ function initDayNightMode() { }); } -initDayNightMode(); \ No newline at end of file +function getDeviceCounts() { + $.get("/local-api/deviceCount", (data) => { + console.log(data); + $("#shapedDeviceCount").text(data.shaped_devices); + $("#unknownIpCount").text(data.unknown_ips); + }) +} + +initDayNightMode(); +getDeviceCounts(); \ No newline at end of file diff --git a/src/rust/lqosd/src/node_manager/local_api.rs b/src/rust/lqosd/src/node_manager/local_api.rs index f3334b8f..0ff0d8b3 100644 --- a/src/rust/lqosd/src/node_manager/local_api.rs +++ b/src/rust/lqosd/src/node_manager/local_api.rs @@ -1,5 +1,6 @@ mod dashboard_themes; mod version_check; +mod device_counts; use axum::Router; use axum::routing::{get, post}; @@ -11,4 +12,5 @@ pub fn local_api() -> Router { .route("/dashletDelete", post(dashboard_themes::delete_theme)) .route("/dashletGet", post(dashboard_themes::get_theme)) .route("/versionCheck", get(version_check::version_check)) + .route("/deviceCount", get(device_counts::count_users)) } \ No newline at end of file diff --git a/src/rust/lqosd/src/node_manager/local_api/device_counts.rs b/src/rust/lqosd/src/node_manager/local_api/device_counts.rs new file mode 100644 index 00000000..9ffb8e6d --- /dev/null +++ b/src/rust/lqosd/src/node_manager/local_api/device_counts.rs @@ -0,0 +1,42 @@ +use std::net::IpAddr; +use axum::Json; +use serde::Serialize; +use lqos_bus::{bus_request, BusRequest, BusResponse, IpStats}; +use crate::shaped_devices_tracker::SHAPED_DEVICES; + +#[derive(Serialize)] +pub struct DeviceCount { + pub shaped_devices: usize, + pub unknown_ips: usize, +} + +fn unknown_device_count() -> usize { + if let BusResponse::AllUnknownIps(unknowns) = crate::throughput_tracker::all_unknown_ips() { + let cfg = SHAPED_DEVICES.read().unwrap(); + let really_unknown: Vec = unknowns + .iter() + .filter(|ip| { + if let Ok(ip) = ip.ip_address.parse::() { + let lookup = match ip { + IpAddr::V4(ip) => ip.to_ipv6_mapped(), + IpAddr::V6(ip) => ip, + }; + cfg.trie.longest_match(lookup).is_none() + } else { + false + } + }) + .cloned() + .collect(); + return really_unknown.len(); + } + + 0 +} + +pub async fn count_users() -> Json { + Json(DeviceCount{ + shaped_devices: SHAPED_DEVICES.read().unwrap().devices.len(), + unknown_ips: unknown_device_count(), + }) +} diff --git a/src/rust/lqosd/src/node_manager/static2/template.html b/src/rust/lqosd/src/node_manager/static2/template.html index 0beaaed8..8c3d8639 100644 --- a/src/rust/lqosd/src/node_manager/static2/template.html +++ b/src/rust/lqosd/src/node_manager/static2/template.html @@ -56,13 +56,13 @@