about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2025-10-16 10:29:39 -0500
committergennyble <gen@nyble.dev>2025-10-16 10:29:39 -0500
commit19b1df3315fd7d291d8188899274c6e51f3c337f (patch)
tree411f8ba850f31e137533b876543e26deac5b85a1
parent03e5e364279222b6a983a66f344ac6ddc8c3fcf9 (diff)
downloadawake-19b1df3315fd7d291d8188899274c6e51f3c337f.tar.gz
awake-19b1df3315fd7d291d8188899274c6e51f3c337f.zip
change cpu graph to use continuous line
-rwxr-xr-xdev.conf4
-rw-r--r--src/gatherer.rs22
-rw-r--r--src/griph/mod.rs59
3 files changed, 70 insertions, 15 deletions
diff --git a/dev.conf b/dev.conf
index 90fd95c..460cf26 100755
--- a/dev.conf
+++ b/dev.conf
@@ -2,6 +2,6 @@ Webroot /Users/gen/src/inf/served
 Templates ../templates
 Hostname dreamy.place
 
-Statistics false
+Statistics true
 Database database.sqlite
-Cache cache
+Cache target
diff --git a/src/gatherer.rs b/src/gatherer.rs
index accd50d..329bc1e 100644
--- a/src/gatherer.rs
+++ b/src/gatherer.rs
@@ -117,7 +117,7 @@ pub fn make_mem_graph(state: &AwakeState) {
 	let infos = state.database.get_last_n_host_meminfo(256);
 	let max = infos[0].total_kb;
 
-	let now = OffsetDateTime::now_utc();
+	let now = get_clean_now();
 	let cleaned = clean_series(&infos, |mem| mem.stamp, now);
 	let mut usages: Vec<Option<usize>> = cleaned
 		.into_iter()
@@ -128,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, Style::Line);
+	let gif = griph::make_1line(0, max, &usages, Style::Discontinuous);
 
 	let path = state.cache_path.join("current_hostmeminfo.gif");
 	gif.save(path).unwrap();
@@ -139,7 +139,7 @@ const NET_INCREMENTS: usize = 256;
 pub fn make_net_graph(state: &AwakeState) {
 	tracing::debug!("generating netinfo graph");
 
-	let now = OffsetDateTime::now_utc();
+	let now = get_clean_now();
 	let infos = state.database.get_last_n_hostnet(256);
 	let cleaned = clean_series(&infos, |net| net.stamp, now);
 
@@ -188,6 +188,14 @@ pub fn make_net_graph(state: &AwakeState) {
 
 	tracing::debug!("tx_sum = {tx_sum} // rx_sum = {rx_sum} // order = {order:?}");
 
+	/*for (idx, (tx, rx)) in tx_deltas.iter().zip(rx_deltas).enumerate() {
+		println!(
+			"[{idx:<03}] tx = {} / rx = {}",
+			tx.unwrap_or_default(),
+			rx.unwrap_or_default()
+		);
+	}*/
+
 	let gif = griph::make_2line(0, high_bound, &tx_deltas, &rx_deltas, order);
 
 	let path = state.cache_path.join("current_hostnetinfo.gif");
@@ -199,7 +207,7 @@ pub fn make_cpu_graph(state: &AwakeState) {
 
 	let infos = state.database.get_last_n_hostcpu(256);
 
-	let now = OffsetDateTime::now_utc();
+	let now = get_clean_now();
 	let cleaned = clean_series(&infos, |cpu| cpu.stamp, now);
 
 	// Usages are of the unit of hundreths of a second of CPU usage averaged over a minute.
@@ -233,12 +241,16 @@ pub fn make_cpu_graph(state: &AwakeState) {
 	// right side, so last in the array
 	scaled.reverse();
 
-	let gif = griph::make_1line(0, 1000, &scaled, Style::UnderfilledLine);
+	let gif = griph::make_1line(0, 1000, &scaled, Style::Line);
 
 	let path = state.cache_path.join("current_hostcpuinfo.gif");
 	gif.save(path).unwrap();
 }
 
+fn get_clean_now() -> OffsetDateTime {
+	OffsetDateTime::now_utc().replace_second(0).unwrap()
+}
+
 fn clean_series<T, F>(series: &[T], time_extractor: F, end_time: OffsetDateTime) -> [Option<T>; 256]
 where
 	F: Fn(&T) -> OffsetDateTime,
diff --git a/src/griph/mod.rs b/src/griph/mod.rs
index 09879f3..bf496fd 100644
--- a/src/griph/mod.rs
+++ b/src/griph/mod.rs
@@ -10,7 +10,7 @@ pub const DARK_PALETTE: &[u8] = &[
 	 48, 192,  48, // Secondary 2 Colour - Green
 	 96,  96, 224, // Primary Underfill - Light Blue
 	 48, 128,  48, // Secondary Underfill - Lesser Green
-	144, 144, 144  // Graphline Underfill - 16 * 9 gray
+	255, 255, 255  // Graphline Underfill - 16 * 9 gray
 ];
 
 const BACKGROUND: u8 = 0;
@@ -28,6 +28,7 @@ const HEIGHT: usize = 160;
 const SIZE: usize = WIDTH * HEIGHT;
 
 pub enum Style {
+	Discontinuous,
 	Line,
 	UnderfilledLine,
 }
@@ -44,10 +45,15 @@ pub fn make_1line(min: usize, max: usize, values: &[Option<usize>], style: Style
 	draw_line(&mut raster, values, vpp, LINE);
 
 	let plt = match style {
-		Style::Line => {
+		Style::Discontinuous => {
 			draw_line(&mut raster, values, vpp, LINE);
 			&DARK_PALETTE[0..12]
 		}
+		Style::Line => {
+			draw_line_continuous(&mut raster, values, vpp, LINE);
+			//&DARK_PALETTE[0..12]
+			DARK_PALETTE
+		}
 		Style::UnderfilledLine => {
 			draw_line_underfill(&mut raster, values, vpp, LINE_FILL, LINE);
 			&DARK_PALETTE
@@ -143,6 +149,7 @@ fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: f32, colour: u8)
 		if let Some(value) = maybe {
 			let value_height = (*value as f32 / vpp) as usize;
 			if value_height > (HEIGHT - 1) {
+				//TODO: this causes too-high values not to draw at all; gaps
 				continue;
 			}
 			let y_val = (HEIGHT - 1) - value_height;
@@ -152,6 +159,41 @@ fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: f32, colour: u8)
 	}
 }
 
+fn draw_line_continuous(raster: &mut [u8], values: &[Option<usize>], vpp: f32, colour: u8) {
+	let mut last_value = None;
+
+	for (x, maybe) in values.iter().enumerate() {
+		if let Some(value) = maybe {
+			let value_height = (*value as f32 / vpp) as usize;
+			let overflow = value_height > HEIGHT - 1;
+			let y_val = (HEIGHT - 1) - value_height.min(HEIGHT - 1);
+
+			match last_value {
+				Some(last) if last > y_val => {
+					for y_offset in y_val..last {
+						raster[y_offset * WIDTH + x] = colour;
+					}
+					raster[y_val * WIDTH + x] = colour;
+				}
+				Some(last) if last < y_val => {
+					for y_offset in last..y_val {
+						raster[y_offset * WIDTH + x - 1] = colour;
+					}
+					raster[y_val * WIDTH + x] = colour;
+				}
+				_ => {
+					// This is the None case and the equal value case
+					raster[y_val * WIDTH + x] = colour;
+				}
+			}
+
+			last_value = Some(y_val);
+		} else {
+			last_value = None;
+		}
+	}
+}
+
 fn draw_line_underfill(
 	raster: &mut [u8],
 	values: &[Option<usize>],
@@ -162,15 +204,16 @@ fn draw_line_underfill(
 	for (x, maybe) in values.iter().enumerate() {
 		if let Some(value) = maybe {
 			let value_height = (*value as f32 / vpp) as usize;
-			if value_height > (HEIGHT - 1) {
-				continue;
-			}
-			let y_val = (HEIGHT - 1) - value_height;
+			let overflow = value_height > HEIGHT - 1;
+			let y_val = (HEIGHT - 1) - value_height.min(HEIGHT - 1);
 
-			for y in y_val + 1..HEIGHT {
+			for y in y_val..HEIGHT {
 				raster[y * WIDTH + x] = colour_fill;
 			}
-			raster[y_val * WIDTH + x] = colour;
+
+			if !overflow {
+				raster[y_val * WIDTH + x] = colour;
+			}
 		}
 	}
 }