From 8f882903748f187565a2894106c33cdbf0862998 Mon Sep 17 00:00:00 2001 From: gennyble Date: Sun, 16 Feb 2025 16:45:30 -0600 Subject: statistics work better :) --- src/db.rs | 6 +++--- src/gatherer.rs | 37 +++++++++++++++++++++++++++---------- src/griph/mod.rs | 31 ++++++++++++++++++++++++++++--- src/main.rs | 10 +++++++++- 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/src/db.rs b/src/db.rs index 6edeba1..d0a2c7d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -59,7 +59,7 @@ impl Database { pub fn get_last_n_host_meminfo(&self, count: usize) -> Vec { let conn = self.conn.lock().unwrap(); let mut stmt = conn - .prepare("SELECT * FROM stats_hostmem ORDER BY stamp ASC LIMIT ?1") + .prepare("SELECT * FROM stats_hostmem ORDER BY stamp DESC LIMIT ?1") .unwrap(); stmt.query_map(params![count], |row| { @@ -82,7 +82,7 @@ impl Database { ) -> Vec { let conn = self.conn.lock().unwrap(); let mut stmt = conn - .prepare("SELECT * FROM stats_hostmem WHERE stamp > ?1 AND stamp < ?2 ORDER BY stamp ASC LIMIT ?3") + .prepare("SELECT * FROM stats_hostmem WHERE stamp > ?1 AND stamp < ?2 ORDER BY stamp DESC LIMIT ?3") .unwrap(); stmt.query_map(params![since, until, count], |row| { @@ -110,7 +110,7 @@ impl Database { pub fn get_last_n_hostnet(&self, count: usize) -> Vec { let conn = self.conn.lock().unwrap(); let mut stmt = conn - .prepare("SELECT * FROM stats_hostnet ORDER BY stamp ASC LIMIT ?1") + .prepare("SELECT * FROM stats_hostnet ORDER BY stamp DESC LIMIT ?1") .unwrap(); stmt.query_map(params![count], |row| { diff --git a/src/gatherer.rs b/src/gatherer.rs index 0cceea6..b1fa416 100644 --- a/src/gatherer.rs +++ b/src/gatherer.rs @@ -1,7 +1,7 @@ use std::{ fs::File, io::{BufRead, BufReader}, - sync::mpsc::Sender, + sync::{atomic::Ordering, mpsc::Sender}, thread::JoinHandle, time::Duration, }; @@ -78,7 +78,11 @@ pub fn make_mem_graph(state: &AwakeState) { let infos = state.database.get_last_n_host_meminfo(256); let max = infos[0].total_kb; - let usages: Vec = infos.into_iter().map(|mi| mi.usage()).collect(); + let mut usages: Vec = infos.into_iter().map(|mi| mi.usage()).collect(); + + // Reversing here because we want latest valeus on on the + // right side, so last in the array + usages.reverse(); let gif = griph::make_1line(0, max, &usages); @@ -90,20 +94,33 @@ pub fn make_net_graph(state: &AwakeState) { tracing::debug!("generating netinfo graph"); let infos = state.database.get_last_n_hostnet(256); - let rx_deltas: Vec = infos + // 125 is (1000 / 8) so it converst Bytes to kiloBITS + let mut rx_deltas: Vec = infos .iter() - .map(|ni| ni.rx_bytes_per_sec() as usize / 1000) + .map(|ni| ni.rx_bytes_per_sec() as usize / 124) .collect(); - let tx_deltas: Vec = infos + let mut tx_deltas: Vec = infos .iter() - .map(|ni| ni.tx_bytes_per_sec() as usize / 1000) + .map(|ni| ni.tx_bytes_per_sec() as usize / 125) .collect(); - for ahh in &tx_deltas { - tracing::trace!("ahh: {ahh} kbytes"); - } + // Reversing to put latest values on the right side + rx_deltas.reverse(); + tx_deltas.reverse(); + + // Mixing the TX/RX delta so we can pick a range. + let mut mixed = vec![0; 512]; + mixed[..256].copy_from_slice(&rx_deltas); + mixed[256..].copy_from_slice(&tx_deltas); + + mixed.sort(); + let kinda_highest = mixed[511 - 32]; + let high_bound = (kinda_highest as f32 / 256.0).ceil().min(1.0) as usize * 256; + state + .netinfo_upper_bound + .store(high_bound, Ordering::Release); - let gif = griph::make_2line(0, 1000, &rx_deltas, &tx_deltas); + let gif = griph::make_2line(0, high_bound, &tx_deltas, &rx_deltas); let path = state.cache_path.join("current_hostnetinfo.gif"); gif.save(path).unwrap(); diff --git a/src/griph/mod.rs b/src/griph/mod.rs index b034207..6b84233 100644 --- a/src/griph/mod.rs +++ b/src/griph/mod.rs @@ -6,8 +6,10 @@ pub const DARK_PALETTE: &[u8] = &[ 192, 192, 192, // Graphline - Mostly White 64, 64, 64, // Gridlines - Dark gray 32, 32, 32, // Minor Gridlines - Darker gray - 48, 48, 192, // Primary 2 Colour - Blue + 144, 144, 255, // Primary 2 Colour - Blue 48, 192, 48, // Secondary 2 Colour - Green + 96, 96, 224, // Primary Underfill - Light Blue + 48, 128, 48, // Secondary Underfill - Lesser Green ]; const BACKGROUND: u8 = 0; @@ -16,6 +18,8 @@ const GRIDLINE: u8 = 2; const MINOR_GRIDLINE: u8 = 3; const LINE1: u8 = 4; const LINE2: u8 = 5; +const LINE1_FILL: u8 = 6; +const LINE2_FILL: u8 = 7; const WIDTH: usize = 256; const HEIGHT: usize = 160; @@ -48,8 +52,8 @@ pub fn make_2line(min: usize, max: usize, values1: &[usize], values2: &[usize]) let mut raster = vec![0; SIZE]; draw_grid(&mut raster); - draw_line(&mut raster, values1, vpp, LINE1); - draw_line(&mut raster, values2, vpp, LINE2); + draw_line_underfill(&mut raster, values1, vpp, LINE1, LINE1_FILL); + draw_line_underfill(&mut raster, values2, vpp, LINE2, LINE2_FILL); let mut standard = Gif::new(WIDTH as u16, HEIGHT as u16); standard.set_palette(Some(DARK_PALETTE.try_into().unwrap())); @@ -96,3 +100,24 @@ fn draw_line(raster: &mut [u8], values: &[usize], vpp: usize, colour: u8) { raster[y_val * WIDTH + x] = colour; } } + +fn draw_line_underfill( + raster: &mut [u8], + values: &[usize], + vpp: usize, + colour: u8, + colour_fill: u8, +) { + for (x, value) in values.iter().enumerate() { + let value_height = value / vpp; + if value_height > (HEIGHT - 1) { + continue; + } + let y_val = (HEIGHT - 1) - value_height; + + for y in y_val + 1..HEIGHT { + raster[y * WIDTH + x] = colour_fill; + } + raster[y_val * WIDTH + x] = colour; + } +} diff --git a/src/main.rs b/src/main.rs index cddd2f1..cf5cda7 100755 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,10 @@ use std::{ io::{BufRead, BufReader, Write}, os::unix::fs::MetadataExt, str::FromStr, - sync::Arc, + sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, + }, time::Duration, }; @@ -51,6 +54,7 @@ use crate::{ pub struct AwakeState { pub database: Arc, pub cache_path: Utf8PathBuf, + pub netinfo_upper_bound: Arc, } #[tokio::main] @@ -86,6 +90,7 @@ async fn main() { let state = AwakeState { database: Arc::new(database), cache_path: cache.into(), + netinfo_upper_bound: Arc::new(AtomicUsize::new(256)), }; match std::env::args().nth(1).as_deref() { @@ -413,6 +418,9 @@ fn template_content(state: AwakeState, frontmatter: &Frontmatter, marked: String doc.set("stats.mem.total", mem.total_kb / 1000); doc.set("stats.mem.usage", mem.usage() / 1000); + + let netinfo_upper = state.netinfo_upper_bound.load(Ordering::Relaxed); + doc.set("stats.net.max_bound", netinfo_upper); } doc.compile() -- cgit 1.4.1-3-g733a5