mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
formatter + implement status history
This commit is contained in:
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user