From a0e80fcda73be12772251d23bf6062819b481f3f Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 4 Jan 2022 14:06:35 +0200 Subject: [PATCH 1/5] add deps for network and stats --- Cargo.lock | 2 ++ peach-web/Cargo.toml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 62614be..d2f34f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2548,6 +2548,8 @@ dependencies = [ "nest", "openssl", "peach-lib", + "peach-network", + "peach-stats", "percent-encoding 2.1.0", "regex", "rocket", diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index 648de74..821b278 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -41,6 +41,8 @@ log = "0.4" nest = "1.0.0" openssl = { version = "0.10", features = ["vendored"] } peach-lib = { path = "../peach-lib" } +peach-network = { path = "../peach-network", features = ["serde_support"] } +peach-stats = { path = "../peach-stats", features = ["serde_support"] } percent-encoding = "2.1.0" regex = "1" rocket = { version = "0.5.0-rc.1", features = ["json", "secrets"] } From 3ab3e65eb71993e0139982b6ef3cce218b415360 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 4 Jan 2022 14:06:57 +0200 Subject: [PATCH 2/5] replace stats rpc client calls with direct calls to peach_stats --- peach-web/src/routes/status/device.rs | 41 ++++++++++----------- peach-web/templates/status/device.html.tera | 6 +-- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/peach-web/src/routes/status/device.rs b/peach-web/src/routes/status/device.rs index 49f71cb..26992ac 100644 --- a/peach-web/src/routes/status/device.rs +++ b/peach-web/src/routes/status/device.rs @@ -13,8 +13,11 @@ use std::{ }; use peach_lib::config_manager::load_peach_config; -use peach_lib::stats_client::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat}; -use peach_lib::{dyndns_client, network_client, oled_client, sbot_client, stats_client}; +use peach_lib::{dyndns_client, network_client, oled_client, sbot_client}; +use peach_stats::{ + stats, + stats::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat}, +}; use crate::routes::authentication::Authenticated; use crate::utils::build_json_response; @@ -34,7 +37,6 @@ pub struct StatusContext { pub mem_stats: Option, pub network_ping: String, pub oled_ping: String, - pub stats_ping: String, pub dyndns_enabled: bool, pub dyndns_is_online: bool, pub config_is_valid: bool, @@ -46,9 +48,9 @@ pub struct StatusContext { impl StatusContext { pub fn build() -> StatusContext { // convert result to Option, discard any error - let cpu_stat_percent = stats_client::cpu_stats_percent().ok(); - let load_average = stats_client::load_average().ok(); - let mem_stats = stats_client::mem_stats().ok(); + let cpu_stat_percent = stats::cpu_stats_percent().ok(); + let load_average = stats::load_average().ok(); + let mem_stats = stats::mem_stats().ok(); let network_ping = match network_client::ping() { Ok(_) => "ONLINE".to_string(), Err(_) => "OFFLINE".to_string(), @@ -57,22 +59,21 @@ impl StatusContext { Ok(_) => "ONLINE".to_string(), Err(_) => "OFFLINE".to_string(), }; - let stats_ping = match stats_client::ping() { - Ok(_) => "ONLINE".to_string(), - Err(_) => "OFFLINE".to_string(), - }; - let uptime = match stats_client::uptime() { - Ok(mins) => mins, + + let uptime = match stats::uptime() { + Ok(secs) => { + let uptime_mins = secs / 60; + uptime_mins.to_string() + } Err(_) => "Unavailable".to_string(), }; + // parse the uptime string to a signed integer (for math) + let uptime_parsed = uptime.parse::().ok(); + // serialize disk usage data into Vec - let disk_usage_stats = match stats_client::disk_usage() { - Ok(disks) => { - let partitions: Vec = serde_json::from_str(disks.as_str()) - .expect("Failed to deserialize disk_usage response"); - partitions - } + let disk_usage_stats = match stats::disk_usage() { + Ok(disks) => disks, Err(_) => Vec::new(), }; @@ -84,9 +85,6 @@ impl StatusContext { } } - // parse the uptime string to a signed integer (for math) - let uptime_parsed = uptime.parse::().ok(); - // dyndns_is_online & config_is_valid let dyndns_enabled: bool; let dyndns_is_online: bool; @@ -139,7 +137,6 @@ impl StatusContext { mem_stats, network_ping, oled_ping, - stats_ping, dyndns_enabled, dyndns_is_online, config_is_valid, diff --git a/peach-web/templates/status/device.html.tera b/peach-web/templates/status/device.html.tera index 42b225c..f48bb58 100644 --- a/peach-web/templates/status/device.html.tera +++ b/peach-web/templates/status/device.html.tera @@ -42,11 +42,11 @@ -
- Stats +
+ Stats
- +
{# Display status for dynsdns, config & sbot #} From 567b0bbc2a12bcda0e382c0c8a560de59f5405a9 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 4 Jan 2022 14:55:17 +0200 Subject: [PATCH 3/5] replace network rpc client calls with direct calls to peach_network --- peach-web/src/routes/status/network.rs | 178 ++++++++++++------------- 1 file changed, 88 insertions(+), 90 deletions(-) diff --git a/peach-web/src/routes/status/network.rs b/peach-web/src/routes/status/network.rs index ff6c6af..a5c8606 100644 --- a/peach-web/src/routes/status/network.rs +++ b/peach-web/src/routes/status/network.rs @@ -2,25 +2,77 @@ use rocket::{get, request::FlashMessage}; use rocket_dyn_templates::Template; use serde::Serialize; -use peach_lib::network_client; -use peach_lib::stats_client::Traffic; +use peach_network::{ + network, + network::{Status, Traffic}, +}; use crate::routes::authentication::Authenticated; // HELPERS AND ROUTES FOR /status/network +#[derive(Debug, Serialize)] +pub struct IfaceTraffic { + pub rx: u64, + pub rx_unit: Option, + pub tx: u64, + pub tx_unit: Option, +} + +impl IfaceTraffic { + fn default() -> Self { + IfaceTraffic { + rx: 0, + rx_unit: None, + tx: 0, + tx_unit: None, + } + } +} + +fn convert_traffic(traffic: Traffic) -> Option { + let mut t = IfaceTraffic::default(); + // modify traffic values & assign measurement unit + // based on received and transmitted values + // if received > 999 MB, convert it to GB + if traffic.received > 1_047_527_424 { + t.rx = traffic.received / 1_073_741_824; + t.rx_unit = Some("GB".to_string()); + } else if traffic.received > 0 { + // otherwise, convert it to MB + t.rx = (traffic.received / 1024) / 1024; + t.rx_unit = Some("MB".to_string()); + } else { + t.rx = 0; + t.rx_unit = Some("MB".to_string()); + } + + if traffic.transmitted > 1_047_527_424 { + t.tx = traffic.transmitted / 1_073_741_824; + t.tx_unit = Some("GB".to_string()); + } else if traffic.transmitted > 0 { + t.tx = (traffic.transmitted / 1024) / 1024; + t.tx_unit = Some("MB".to_string()); + } else { + t.tx = 0; + t.tx_unit = Some("MB".to_string()); + } + + Some(t) +} + #[derive(Debug, Serialize)] pub struct NetworkContext { pub ap_ip: String, pub ap_ssid: String, pub ap_state: String, - pub ap_traffic: Option, + pub ap_traffic: Option, pub wlan_ip: String, pub wlan_rssi: Option, pub wlan_ssid: String, pub wlan_state: String, - pub wlan_status: String, - pub wlan_traffic: Option, + pub wlan_status: Option, + pub wlan_traffic: Option, pub flash_name: Option, pub flash_msg: Option, // page title for header in navbar @@ -31,101 +83,47 @@ pub struct NetworkContext { impl NetworkContext { pub fn build() -> NetworkContext { - let ap_ip = match network_client::ip("ap0") { - Ok(ip) => ip, - Err(_) => "x.x.x.x".to_string(), + let ap_ip = match network::ip("ap0") { + Ok(Some(ip)) => ip, + _ => "x.x.x.x".to_string(), }; - let ap_ssid = match network_client::ssid("ap0") { - Ok(ssid) => ssid, - Err(_) => "Not currently activated".to_string(), + let ap_ssid = match network::ssid("ap0") { + Ok(Some(ssid)) => ssid, + _ => "Not currently activated".to_string(), }; - let ap_state = match network_client::state("ap0") { - Ok(state) => state, - Err(_) => "Interface unavailable".to_string(), + let ap_state = match network::state("ap0") { + Ok(Some(state)) => state, + _ => "Interface unavailable".to_string(), }; - let ap_traffic = match network_client::traffic("ap0") { - Ok(traffic) => { - let mut t = traffic; - // modify traffic values & assign measurement unit - // based on received and transmitted values - // if received > 999 MB, convert it to GB - if t.received > 1_047_527_424 { - t.received /= 1_073_741_824; - t.rx_unit = Some("GB".to_string()); - } else if t.received > 0 { - // otherwise, convert it to MB - t.received = (t.received / 1024) / 1024; - t.rx_unit = Some("MB".to_string()); - } else { - t.received = 0; - t.rx_unit = Some("MB".to_string()); - } - - if t.transmitted > 1_047_527_424 { - t.transmitted /= 1_073_741_824; - t.tx_unit = Some("GB".to_string()); - } else if t.transmitted > 0 { - t.transmitted = (t.transmitted / 1024) / 1024; - t.tx_unit = Some("MB".to_string()); - } else { - t.transmitted = 0; - t.tx_unit = Some("MB".to_string()); - } - Some(t) - } - Err(_) => None, + let ap_traffic = match network::traffic("ap0") { + // convert bytes to mb or gb and add appropriate units + Ok(Some(traffic)) => convert_traffic(traffic), + _ => None, }; - let wlan_ip = match network_client::ip("wlan0") { - Ok(ip) => ip, - Err(_) => "x.x.x.x".to_string(), + let wlan_ip = match network::ip("wlan0") { + Ok(Some(ip)) => ip, + _ => "x.x.x.x".to_string(), }; - let wlan_rssi = match network_client::rssi_percent("wlan0") { - Ok(rssi) => Some(rssi), - Err(_) => None, + let wlan_rssi = match network::rssi_percent("wlan0") { + Ok(rssi) => rssi, + _ => None, }; - let wlan_ssid = match network_client::ssid("wlan0") { - Ok(ssid) => ssid, - Err(_) => "Not connected".to_string(), + let wlan_ssid = match network::ssid("wlan0") { + Ok(Some(ssid)) => ssid, + _ => "Not connected".to_string(), }; - let wlan_state = match network_client::state("wlan0") { - Ok(state) => state, - Err(_) => "Interface unavailable".to_string(), + let wlan_state = match network::state("wlan0") { + Ok(Some(state)) => state, + _ => "Interface unavailable".to_string(), }; - let wlan_status = match network_client::status("wlan0") { + let wlan_status = match network::status("wlan0") { Ok(status) => status, - Err(_) => "Interface unavailable".to_string(), + _ => None, }; - let wlan_traffic = match network_client::traffic("wlan0") { - Ok(traffic) => { - let mut t = traffic; - // modify traffic values & assign measurement unit - // based on received and transmitted values - // if received > 999 MB, convert it to GB - if t.received > 1_047_527_424 { - t.received /= 1_073_741_824; - t.rx_unit = Some("GB".to_string()); - } else if t.received > 0 { - // otherwise, convert it to MB - t.received = (t.received / 1024) / 1024; - t.rx_unit = Some("MB".to_string()); - } else { - t.received = 0; - t.rx_unit = Some("MB".to_string()); - } - - if t.transmitted > 1_047_527_424 { - t.transmitted /= 1_073_741_824; - t.tx_unit = Some("GB".to_string()); - } else if t.transmitted > 0 { - t.transmitted = (t.transmitted / 1024) / 1024; - t.tx_unit = Some("MB".to_string()); - } else { - t.transmitted = 0; - t.tx_unit = Some("MB".to_string()); - } - Some(t) - } - Err(_) => None, + let wlan_traffic = match network::traffic("wlan0") { + // convert bytes to mb or gb and add appropriate units + Ok(Some(traffic)) => convert_traffic(traffic), + _ => None, }; NetworkContext { From 49ad74595c4782fa9a59bc9f81145a6f1849677b Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 6 Jan 2022 11:56:23 +0200 Subject: [PATCH 4/5] cleanup use paths and leave network_ping note --- peach-web/src/routes/status/device.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/peach-web/src/routes/status/device.rs b/peach-web/src/routes/status/device.rs index 26992ac..35870cb 100644 --- a/peach-web/src/routes/status/device.rs +++ b/peach-web/src/routes/status/device.rs @@ -3,8 +3,8 @@ use rocket::{ get, post, request::FlashMessage, response::{Flash, Redirect}, + serde::json::Value, }; - use rocket_dyn_templates::Template; use serde::Serialize; use std::{ @@ -12,8 +12,9 @@ use std::{ process::{Command, Output}, }; -use peach_lib::config_manager::load_peach_config; -use peach_lib::{dyndns_client, network_client, oled_client, sbot_client}; +use peach_lib::{ + config_manager::load_peach_config, dyndns_client, network_client, oled_client, sbot_client, +}; use peach_stats::{ stats, stats::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat}, @@ -21,7 +22,6 @@ use peach_stats::{ use crate::routes::authentication::Authenticated; use crate::utils::build_json_response; -use rocket::serde::json::Value; // HELPERS AND ROUTES FOR /status @@ -51,6 +51,9 @@ impl StatusContext { let cpu_stat_percent = stats::cpu_stats_percent().ok(); let load_average = stats::load_average().ok(); let mem_stats = stats::mem_stats().ok(); + // TODO: add `wpa_supplicant_status` to peach_network to replace this ping call + // instead of: "is the network json-rpc server running?", we want to ask: + // "is the wpa_supplicant systemd service functioning correctly?" let network_ping = match network_client::ping() { Ok(_) => "ONLINE".to_string(), Err(_) => "OFFLINE".to_string(), From 57ed0ab66a6a053e000f5ebfb38ac4b9b223dc5e Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 6 Jan 2022 11:56:45 +0200 Subject: [PATCH 5/5] add fullstop to docs sentence --- peach-web/src/routes/status/network.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/peach-web/src/routes/status/network.rs b/peach-web/src/routes/status/network.rs index a5c8606..aefa766 100644 --- a/peach-web/src/routes/status/network.rs +++ b/peach-web/src/routes/status/network.rs @@ -32,8 +32,8 @@ impl IfaceTraffic { fn convert_traffic(traffic: Traffic) -> Option { let mut t = IfaceTraffic::default(); - // modify traffic values & assign measurement unit - // based on received and transmitted values + // modify traffic values & assign measurement units + // based on received and transmitted values. // if received > 999 MB, convert it to GB if traffic.received > 1_047_527_424 { t.rx = traffic.received / 1_073_741_824;