diff --git a/src/webpage/src/app.rs b/src/webpage/src/app.rs index adcda30a..69792c3a 100644 --- a/src/webpage/src/app.rs +++ b/src/webpage/src/app.rs @@ -17,6 +17,7 @@ use crate::{ drivers_stats::{DriversStatsHistorical, DriversStatsSample}, hub_messages_stats::{HubMessagesStatsHistorical, HubMessagesStatsSample}, hub_stats::{HubStatsHistorical, HubStatsSample}, + stats_frequency::{get_stats_frequency, set_stats_frequency}, ByteStatsHistorical, DelayStatsHistorical, MessageStatsHistorical, StatsInner, }, }; @@ -26,6 +27,7 @@ pub struct App { mavlink_receiver: WsReceiver, mavlink_sender: WsSender, hub_messages_stats_receiver: WsReceiver, + stats_frequency: Arc>, hub_messages_stats_sender: WsSender, hub_stats_receiver: WsReceiver, hub_stats_sender: WsSender, @@ -63,6 +65,9 @@ impl Default for App { connect(url, ewebsock::Options::default()).expect("Can't connect") }; + let stats_frequency = Arc::new(Mutex::new(1.)); + get_stats_frequency(&stats_frequency); + let url = format!("{protocol}//{host}/stats/messages/ws"); let (hub_messages_stats_sender, hub_messages_stats_receiver) = { let url = Url::parse(&url).unwrap().to_string(); @@ -87,6 +92,7 @@ impl Default for App { mavlink_sender, hub_messages_stats_receiver, hub_messages_stats_sender, + stats_frequency, hub_stats_receiver, hub_stats_sender, drivers_stats_receiver, @@ -121,19 +127,21 @@ impl App { connect(url, ewebsock::Options::default()).expect("Can't connect") }; - let url = format!("{protocol}//{host}/stats/messages/ws?frequency=20"); + set_stats_frequency(&self.stats_frequency.clone(), *self.stats_frequency.lock()); + + let url = format!("{protocol}//{host}/stats/messages/ws"); let (hub_messages_stats_sender, hub_messages_stats_receiver) = { let url = Url::parse(&url).unwrap().to_string(); connect(url, ewebsock::Options::default()).expect("Can't connect") }; - let url = format!("{protocol}//{host}/stats/hub/ws?frequency=20"); + let url = format!("{protocol}//{host}/stats/hub/ws"); let (hub_stats_sender, hub_stats_receiver) = { let url = Url::parse(&url).unwrap().to_string(); connect(url, ewebsock::Options::default()).expect("Can't connect") }; - let url = format!("{protocol}//{host}/stats/drivers/ws?frequency=20"); + let url = format!("{protocol}//{host}/stats/drivers/ws"); let (drivers_stats_sender, drivers_stats_receiver) = { let url = Url::parse(&url).unwrap().to_string(); connect(url, ewebsock::Options::default()).expect("Can't connect") @@ -692,6 +700,26 @@ impl eframe::App for App { ui.checkbox(&mut self.show_messages_stats.lock(), "Messages Stats"); ui.checkbox(&mut self.showdrivers_stats.lock(), "Drivers Stats"); }); + + ui.separator(); + + ui.vertical(|ui| { + let mut stats_frequency = self.stats_frequency.lock().to_owned(); + ui.label("Stats Frequency"); + if ui + .add( + egui::Slider::new(&mut stats_frequency, 0.1..=10.) + .suffix("Hz") + .fixed_decimals(1) + .step_by(0.1) + .logarithmic(true) + .trailing_fill(true), + ) + .drag_stopped() + { + set_stats_frequency(&self.stats_frequency.clone(), stats_frequency); + } + }); }); egui::SidePanel::left("messages_inspector") diff --git a/src/webpage/src/stats/mod.rs b/src/webpage/src/stats/mod.rs index 039edd0e..ace609f5 100644 --- a/src/webpage/src/stats/mod.rs +++ b/src/webpage/src/stats/mod.rs @@ -7,6 +7,7 @@ use crate::messages::FieldInfo; pub mod drivers_stats; pub mod hub_messages_stats; pub mod hub_stats; +pub mod stats_frequency; pub type SystemId = u8; pub type ComponentId = u8; diff --git a/src/webpage/src/stats/stats_frequency.rs b/src/webpage/src/stats/stats_frequency.rs new file mode 100644 index 00000000..79f78680 --- /dev/null +++ b/src/webpage/src/stats/stats_frequency.rs @@ -0,0 +1,73 @@ +use std::sync::Arc; + +use egui::mutex::Mutex; +use serde::{Deserialize, Serialize}; +use url::Url; +use web_sys::window; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Frequency { + pub frequency: f32, +} + +pub fn get_stats_frequency(stats_frequency: &Arc>) { + let location = window().unwrap().location(); + let host = location.host().unwrap(); + let protocol = location.protocol().unwrap(); + let url = Url::parse(&format!("{protocol}//{host}/stats/frequency")) + .unwrap() + .to_string(); + + let mut request = ehttp::Request::get(url); + request.headers.insert("Content-Type", "application/json"); + + let stats_frequency = stats_frequency.clone(); + ehttp::fetch( + request, + move |result: ehttp::Result| match result { + Ok(response) => { + if let Some(json) = response.text() { + if let Ok(Frequency { frequency }) = serde_json::from_str(json) { + *stats_frequency.lock() = frequency; + } + } + } + Err(error) => log::error!("Status code: {error:?}"), + }, + ); +} + +pub fn set_stats_frequency(stats_frequency: &Arc>, new_stats_frequency: f32) { + *stats_frequency.lock() = new_stats_frequency; + + let location = window().unwrap().location(); + let host = location.host().unwrap(); + let protocol = location.protocol().unwrap(); + let url = Url::parse(&format!("{protocol}//{host}/stats/frequency")) + .unwrap() + .to_string(); + let body = serde_json::json!({ + "frequency": new_stats_frequency, + }) + .to_string() + .as_bytes() + .to_vec(); + + let mut request = ehttp::Request::post(url, body); + request.headers.insert("Content-Type", "application/json"); + + let stats_frequency = stats_frequency.clone(); + ehttp::fetch( + request, + move |result: ehttp::Result| match result { + Ok(response) => { + if let Some(json) = response.text() { + if let Ok(Frequency { frequency }) = serde_json::from_str(json) { + *stats_frequency.lock() = frequency; + } + } + } + Err(error) => log::error!("Status code: {error:?}"), + }, + ); +}