Skip to content

Commit

Permalink
src: webpage: src: Add Stats Frequency configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Nov 4, 2024
1 parent ac5765e commit 8ac5e20
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/webpage/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand All @@ -26,6 +27,7 @@ pub struct App {
mavlink_receiver: WsReceiver,
mavlink_sender: WsSender,
hub_messages_stats_receiver: WsReceiver,
stats_frequency: Arc<Mutex<f32>>,
hub_messages_stats_sender: WsSender,
hub_stats_receiver: WsReceiver,
hub_stats_sender: WsSender,
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions src/webpage/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
73 changes: 73 additions & 0 deletions src/webpage/src/stats/stats_frequency.rs
Original file line number Diff line number Diff line change
@@ -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<Mutex<f32>>) {
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<ehttp::Response>| 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<Mutex<f32>>, 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<ehttp::Response>| 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:?}"),
},
);
}

0 comments on commit 8ac5e20

Please sign in to comment.