about summary refs log tree commit diff
path: root/src/gatherer.rs
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2025-02-25 19:21:59 -0600
committergennyble <gen@nyble.dev>2025-02-25 19:21:59 -0600
commit337b1faeeb414c5dad9a1b2cf80460a5e74eacbe (patch)
tree39c5d8e630672c4ffc51488556184c4fc3f189a4 /src/gatherer.rs
parentf9df72033b29ed08811ac87d1ef22002eac4b992 (diff)
downloadawake-337b1faeeb414c5dad9a1b2cf80460a5e74eacbe.tar.gz
awake-337b1faeeb414c5dad9a1b2cf80460a5e74eacbe.zip
Make CPU graph better
Diffstat (limited to 'src/gatherer.rs')
-rw-r--r--src/gatherer.rs43
1 files changed, 36 insertions, 7 deletions
diff --git a/src/gatherer.rs b/src/gatherer.rs
index 7289f60..53e46f1 100644
--- a/src/gatherer.rs
+++ b/src/gatherer.rs
@@ -10,7 +10,11 @@ use std::{
 use regex_lite::Regex;
 use time::OffsetDateTime;
 
-use crate::{db::DbMeminfo, griph, AwakeState};
+use crate::{
+	db::DbMeminfo,
+	griph::{self, Style},
+	AwakeState,
+};
 
 pub struct Gatherer {
 	state: AwakeState,
@@ -124,7 +128,7 @@ pub fn make_mem_graph(state: &AwakeState) {
 	// right side, so last in the array
 	usages.reverse();
 
-	let gif = griph::make_1line(0, max, &usages);
+	let gif = griph::make_1line(0, max, &usages, Style::Line);
 
 	let path = state.cache_path.join("current_hostmeminfo.gif");
 	gif.save(path).unwrap();
@@ -162,7 +166,7 @@ pub fn make_net_graph(state: &AwakeState) {
 
 	mixed.sort();
 	let kinda_highest = mixed[511 - 32];
-	let high_bound = (kinda_highest as f32 / 256.0).ceil().min(1.0) as usize * 256;
+	let high_bound = (kinda_highest as f32 / 256.0).ceil().max(1.0) as usize * 256;
 	state
 		.netinfo_upper_bound
 		.store(high_bound, Ordering::Release);
@@ -180,14 +184,39 @@ pub fn make_cpu_graph(state: &AwakeState) {
 
 	let now = OffsetDateTime::now_utc();
 	let cleaned = clean_series(&infos, |cpu| cpu.stamp, now);
-	// Scalling by 10 because the graph system does not like to have a max of only 100 due to some bad programming
-	let mut usages = extract(&cleaned, |cpu| (cpu.average_usage() * 10.0) as usize);
+
+	// Usages are of the unit of hundreths of a second of CPU usage averaged over a minute.
+	// A value of 1 means the cpu saw 1% CPU usage
+	let usages = extract(&cleaned, |cpu| cpu.average_usage());
+
+	let mut zeroed: Vec<f32> = usages.iter().map(|m| m.unwrap_or(0.0)).collect();
+	zeroed.sort_by(|a, b| a.partial_cmp(&b).unwrap());
+	let kinda_highest = zeroed[255 - 8];
+
+	tracing::debug!(
+		"kinda_highest = {kinda_highest} // highest = {}",
+		zeroed[255]
+	);
+
+	// high_vound unit: hundreths of a second (1% / 1)
+	let high_bound = kinda_highest.max(1.0);
+	state
+		.cpuinfo_upper_bound
+		.store(high_bound as usize, Ordering::Release);
+
+	// can't scale the range, so we have to scale the inputs.
+	// Finally, we multiply by ten to put it in a 0-1000 range
+	let scale_factor = 100.0 / high_bound;
+	let mut scaled: Vec<Option<usize>> = usages
+		.iter()
+		.map(|m| m.map(|v| (v * scale_factor) as usize * 10))
+		.collect();
 
 	// Reversing here because we want latest valeus on on the
 	// right side, so last in the array
-	usages.reverse();
+	scaled.reverse();
 
-	let gif = griph::make_1line(0, 1000, &usages);
+	let gif = griph::make_1line(0, 1000, &scaled, Style::UnderfilledLine);
 
 	let path = state.cache_path.join("current_hostcpuinfo.gif");
 	gif.save(path).unwrap();