diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/mod.rs b/src/rust/long_term_stats/lts_node/src/web/wss/mod.rs index 8d2ebc74..e6558c26 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/mod.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/mod.rs @@ -1,5 +1,4 @@ use std::sync::Arc; - use crate::web::wss::{ nodes::node_status, queries::{ @@ -15,7 +14,6 @@ use crate::web::wss::{ send_throughput_for_all_nodes_by_circuit, send_throughput_for_all_nodes_by_site, send_throughput_for_node, site_heat_map, site_tree::send_site_tree, - time_period::InfluxTimePeriod, }, }; use axum::{ @@ -29,7 +27,7 @@ use pgdb::sqlx::{Pool, Postgres}; use tokio::sync::{mpsc::Sender, Mutex}; use tracing::instrument; use wasm_pipe_types::{WasmRequest, WasmResponse}; -mod influx_query_builder; +use self::queries::InfluxTimePeriod; mod login; mod nodes; mod queries; diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/ext_device.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/ext_device.rs index 9a30ec76..632d7a3a 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/ext_device.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/ext_device.rs @@ -5,7 +5,7 @@ use influxdb2::{FromDataPoint, models::Query, Client}; use pgdb::{sqlx::{Pool, Postgres}, organization_cache::get_org_details}; use tokio::sync::mpsc::Sender; use wasm_pipe_types::WasmResponse; -use super::time_period::InfluxTimePeriod; +use super::influx::InfluxTimePeriod; #[tracing::instrument(skip(cnn, tx, key, circuit_id))] pub async fn send_extended_device_info( diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/influx_query_builder.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/influx_query_builder.rs similarity index 99% rename from src/rust/long_term_stats/lts_node/src/web/wss/influx_query_builder.rs rename to src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/influx_query_builder.rs index c3f1a4e7..c0c7f5a5 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/influx_query_builder.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/influx_query_builder.rs @@ -4,7 +4,7 @@ use influxdb2_structmap::FromMap; use pgdb::{sqlx::{Pool, Postgres}, organization_cache::get_org_details, OrganizationDetails}; use anyhow::{Result, Error}; use tracing::instrument; -use super::queries::time_period::InfluxTimePeriod; +use super::InfluxTimePeriod; #[derive(Debug)] pub struct InfluxQueryBuilder { diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/mod.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/mod.rs new file mode 100644 index 00000000..7b8a3719 --- /dev/null +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/mod.rs @@ -0,0 +1,4 @@ +mod influx_query_builder; +pub use influx_query_builder::*; +mod time_period; +pub use time_period::*; diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/time_period.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/time_period.rs similarity index 55% rename from src/rust/long_term_stats/lts_node/src/web/wss/queries/time_period.rs rename to src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/time_period.rs index 9533cd16..ee55f37b 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/time_period.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/influx/time_period.rs @@ -24,29 +24,44 @@ const fn aggregate_window(seconds: i32) -> i32 { seconds / SAMPLES_PER_GRAPH } +fn period_string_to_seconds(period: &str) -> i32 { + let last_char = period.chars().last().unwrap(); + let number_part = &period[..period.len() - 1]; + let number = number_part.parse::().unwrap_or(5); + let start_seconds = match last_char { + 's' => number, + 'm' => minutes_to_seconds(number), + 'h' => hours_to_seconds(number), + 'd' => days_to_seconds(number), + _ => { + tracing::warn!("Unknown time unit: {last_char}"); + minutes_to_seconds(5) + } + }; + start_seconds +} + impl InfluxTimePeriod { pub fn new(period: &str) -> Self { - let start_seconds = match period { - "5m" => minutes_to_seconds(5), - "15m" => minutes_to_seconds(15), - "1h" => hours_to_seconds(1), - "6h" => hours_to_seconds(6), - "12h" => hours_to_seconds(12), - "24h" => hours_to_seconds(24), - "7d" => days_to_seconds(7), - "28d" => days_to_seconds(28), + let last_char = period.chars().last().unwrap(); + let number_part = &period[..period.len() - 1]; + let number = number_part.parse::().unwrap_or(5); + let start_seconds = match last_char { + 's' => number, + 'm' => minutes_to_seconds(number), + 'h' => hours_to_seconds(number), + 'd' => days_to_seconds(number), _ => { - tracing::warn!("Unknown period: {}", period); - minutes_to_seconds(5) + tracing::warn!("Unknown time unit: {last_char}"); + minutes_to_seconds(5) } }; + let start = format!("-{}s", start_seconds); let aggregate_seconds = aggregate_window(start_seconds); let aggregate = format!("{}s", aggregate_seconds); let sample = start_seconds / 100; - println!("Period: {period}, Seconds: {start_seconds}, AggSec: {aggregate_seconds}, Samples: {sample}"); - Self { start: start.to_string(), aggregate: aggregate.to_string(), @@ -88,4 +103,20 @@ impl From<&String> for InfluxTimePeriod { fn from(period: &String) -> Self { Self::new(period) } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_period_to_seconds() { + assert_eq!(period_string_to_seconds("5s"), 5); + assert_eq!(period_string_to_seconds("5m"), 300); + assert_eq!(period_string_to_seconds("5h"), 18000); + assert_eq!(period_string_to_seconds("5d"), 432000); + + // Test that an unknown returns the default + assert_eq!(period_string_to_seconds("5x"), 300); + } } \ No newline at end of file diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/mod.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/mod.rs index d4e7b08e..b25f8735 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/mod.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/mod.rs @@ -1,6 +1,8 @@ //! Provides pre-packaged queries for obtaining data, that will //! then be used by the web server to respond to requests. +mod influx; +pub(crate) use influx::*; mod circuit_info; pub mod ext_device; mod node_perf; @@ -12,7 +14,6 @@ mod site_info; mod site_parents; pub mod site_tree; mod throughput; -pub mod time_period; pub use circuit_info::send_circuit_info; pub use node_perf::send_perf_for_node; pub use packet_counts::{send_packets_for_all_nodes, send_packets_for_node}; diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/node_perf.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/node_perf.rs index b7036dd3..e567899d 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/node_perf.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/node_perf.rs @@ -3,7 +3,8 @@ use influxdb2::{Client, FromDataPoint, models::Query}; use pgdb::{sqlx::{Pool, Postgres}, organization_cache::get_org_details}; use tokio::sync::mpsc::Sender; use wasm_pipe_types::{PerfHost, Perf, WasmResponse}; -use super::time_period::InfluxTimePeriod; + +use super::influx::InfluxTimePeriod; #[derive(Debug, FromDataPoint)] pub struct PerfRow { diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/packet_counts/mod.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/packet_counts/mod.rs index b04ee83d..9ea4fb80 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/packet_counts/mod.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/packet_counts/mod.rs @@ -1,12 +1,11 @@ //! Packet-per-second data queries mod packet_row; use self::packet_row::PacketRow; -use super::time_period::InfluxTimePeriod; -use crate::web::wss::influx_query_builder::InfluxQueryBuilder; use pgdb::sqlx::{Pool, Postgres}; use tokio::sync::mpsc::Sender; use tracing::instrument; use wasm_pipe_types::{PacketHost, Packets, WasmResponse}; +use super::influx::{InfluxTimePeriod, InfluxQueryBuilder}; fn add_by_direction(direction: &str, down: &mut Vec, up: &mut Vec, row: &PacketRow) { match direction { diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/mod.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/mod.rs index 6ef19914..717c2ca5 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/mod.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/mod.rs @@ -11,7 +11,7 @@ use tracing::instrument; use wasm_pipe_types::{RttHost, Rtt, WasmResponse}; use crate::web::wss::queries::rtt::rtt_row::RttCircuitRow; use self::rtt_row::RttRow; -use super::time_period::InfluxTimePeriod; +use super::influx::InfluxTimePeriod; mod rtt_row; #[instrument(skip(cnn, tx, key, site_id, period))] diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_node.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_node.rs index 0089adcb..6106679b 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_node.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_node.rs @@ -1,4 +1,3 @@ -use crate::web::wss::{queries::time_period::InfluxTimePeriod, influx_query_builder::InfluxQueryBuilder}; use pgdb::{ sqlx::{Pool, Postgres}, NodeStatus @@ -6,7 +5,7 @@ use pgdb::{ use tokio::sync::mpsc::Sender; use tracing::instrument; use wasm_pipe_types::{Rtt, RttHost, WasmResponse}; - +use crate::web::wss::queries::influx::{InfluxTimePeriod, InfluxQueryBuilder}; use super::rtt_row::{RttRow, RttHistoRow}; #[instrument(skip(cnn, tx, key, period))] diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_site.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_site.rs index 0a62e826..608a412a 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_site.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/rtt/per_site.rs @@ -1,4 +1,3 @@ -use crate::web::wss::{queries::time_period::InfluxTimePeriod, influx_query_builder::InfluxQueryBuilder}; use pgdb::{ sqlx::{Pool, Postgres}, NodeStatus @@ -6,6 +5,7 @@ use pgdb::{ use tokio::sync::mpsc::Sender; use tracing::instrument; use wasm_pipe_types::{Rtt, RttHost, WasmResponse}; +use crate::web::wss::queries::influx::{InfluxTimePeriod, InfluxQueryBuilder}; use super::rtt_row::RttSiteRow; #[instrument(skip(cnn, tx, key, period))] diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/site_heat_map.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/site_heat_map.rs index 52c8e657..208057b0 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/site_heat_map.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/site_heat_map.rs @@ -1,5 +1,3 @@ -use super::time_period::InfluxTimePeriod; -use crate::web::wss::influx_query_builder::InfluxQueryBuilder; use chrono::{DateTime, FixedOffset, Utc}; use influxdb2::FromDataPoint; use pgdb::sqlx::{Pool, Postgres}; @@ -10,6 +8,8 @@ use std::collections::HashMap; use wasm_pipe_types::WasmResponse; use itertools::Itertools; +use super::influx::{InfluxTimePeriod, InfluxQueryBuilder}; + fn headings_sorter(rows: Vec) -> HashMap, f64)>> { let mut headings = rows.iter().map(|r| r.time()).collect::>(); headings.sort(); diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/mod.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/mod.rs index 4dd9005e..7593acd8 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/mod.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/mod.rs @@ -6,11 +6,10 @@ use pgdb::{sqlx::{Pool, Postgres}, organization_cache::get_org_details}; use tokio::sync::mpsc::Sender; use tracing::instrument; use wasm_pipe_types::{ThroughputHost, Throughput, WasmResponse}; -use crate::web::wss::influx_query_builder::InfluxQueryBuilder; use self::throughput_row::{ThroughputRow, ThroughputRowBySite, ThroughputRowByCircuit}; -use super::time_period::InfluxTimePeriod; mod throughput_row; pub use site_stack::send_site_stack_map; +use super::influx::{InfluxQueryBuilder, InfluxTimePeriod}; fn add_by_direction(direction: &str, down: &mut Vec, up: &mut Vec, row: &ThroughputRow) { match direction { diff --git a/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/site_stack.rs b/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/site_stack.rs index f8bd0578..e75df06e 100644 --- a/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/site_stack.rs +++ b/src/rust/long_term_stats/lts_node/src/web/wss/queries/throughput/site_stack.rs @@ -1,4 +1,3 @@ -use crate::web::wss::queries::time_period::InfluxTimePeriod; use pgdb::{ organization_cache::get_org_details, sqlx::{Pool, Postgres}, @@ -7,6 +6,7 @@ use pgdb::{ use tokio::sync::mpsc::Sender; use tracing::{error, instrument}; use wasm_pipe_types::{SiteStackHost, WasmResponse}; +use crate::web::wss::queries::influx::InfluxTimePeriod; #[derive(Debug, influxdb2::FromDataPoint)] pub struct SiteStackRow {