formatter + implement status history

This commit is contained in:
csehviktor
2025-07-15 01:08:51 +02:00
parent 61fb1b1583
commit 6a311246f6
16 changed files with 245 additions and 110 deletions

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap;
use anyhow::anyhow;
use common::metrics::CPUBreakdown;
use std::collections::HashMap;
#[derive(Default, Debug, Clone, Copy)]
struct CPUValues {
@@ -18,8 +18,16 @@ struct CPUValues {
impl CPUValues {
pub fn total(&self) -> u64 {
self.user + self.nice + self.system + self.idle + self.iowait +
self.irq + self.softirq + self.steal + self.guest + self.guest_nice
self.user
+ self.nice
+ self.system
+ self.idle
+ self.iowait
+ self.irq
+ self.softirq
+ self.steal
+ self.guest
+ self.guest_nice
}
}
@@ -29,7 +37,9 @@ pub struct CPUStatReader {
impl CPUStatReader {
pub fn new() -> Self {
Self { previous_stats: HashMap::new() }
Self {
previous_stats: HashMap::new(),
}
}
pub fn read_global_cpu_stats(&mut self) -> anyhow::Result<CPUBreakdown> {
@@ -46,7 +56,8 @@ impl CPUStatReader {
fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CPUBreakdown> {
let mut parts = line.split_whitespace();
let cpu_name = parts.next()
let cpu_name = parts
.next()
.ok_or_else(|| anyhow!("missing cpu name"))?
.to_string();
@@ -80,7 +91,11 @@ impl CPUStatReader {
Ok(percentages)
}
fn calculate_percentages(&self, current: &CPUValues, previous: Option<CPUValues>) -> CPUBreakdown {
fn calculate_percentages(
&self,
current: &CPUValues,
previous: Option<CPUValues>,
) -> CPUBreakdown {
let Some(prev) = previous else {
return CPUBreakdown::default();
};
@@ -90,9 +105,8 @@ impl CPUStatReader {
return CPUBreakdown::default();
}
let calculate_pct = |current: u64, prev: u64| {
(current.saturating_sub(prev) as f32 / total_delta) * 100.0
};
let calculate_pct =
|current: u64, prev: u64| (current.saturating_sub(prev) as f32 / total_delta) * 100.0;
CPUBreakdown {
system: calculate_pct(current.system, prev.system),