rename + correct data

This commit is contained in:
csehviktor
2025-07-15 04:28:59 +02:00
parent 20f4c1f521
commit 86d807a19c
3 changed files with 25 additions and 22 deletions

View File

@@ -1,13 +1,13 @@
use common::metrics::{CPU, Disk, Memory, Metrics, Network, SystemInfo}; use common::metrics::{CPU, Disk, Memory, Metrics, Network, SystemInfo};
use sysinfo::{Disks, Networks, System}; use sysinfo::{Disks, Networks, System};
use crate::cpu::CPUStatReader; use crate::cpu::CpuStatReader;
pub struct Collector { pub struct Collector {
sys: System, sys: System,
disks: Disks, disks: Disks,
networks: Networks, networks: Networks,
cpu_reader: CPUStatReader, cpu_reader: CpuStatReader,
} }
impl Collector { impl Collector {
@@ -16,7 +16,7 @@ impl Collector {
sys: System::new_all(), sys: System::new_all(),
disks: Disks::new_with_refreshed_list(), disks: Disks::new_with_refreshed_list(),
networks: Networks::new_with_refreshed_list(), networks: Networks::new_with_refreshed_list(),
cpu_reader: CPUStatReader::new(), cpu_reader: CpuStatReader::new(),
} }
} }

View File

@@ -1,9 +1,9 @@
use anyhow::anyhow; use anyhow::anyhow;
use common::metrics::CPUBreakdown; use common::metrics::CpuBreakdown;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Default, Debug, Clone, Copy)] #[derive(Default, Debug, Clone, Copy)]
struct CPUValues { struct CpuValues {
pub user: u64, pub user: u64,
pub nice: u64, pub nice: u64,
pub system: u64, pub system: u64,
@@ -16,7 +16,7 @@ struct CPUValues {
pub guest_nice: u64, pub guest_nice: u64,
} }
impl CPUValues { impl CpuValues {
pub fn total(&self) -> u64 { pub fn total(&self) -> u64 {
self.user self.user
+ self.nice + self.nice
@@ -31,18 +31,18 @@ impl CPUValues {
} }
} }
pub struct CPUStatReader { pub struct CpuStatReader {
previous_stats: HashMap<String, CPUValues>, previous_stats: HashMap<String, CpuValues>,
} }
impl CPUStatReader { impl CpuStatReader {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
previous_stats: HashMap::new(), previous_stats: HashMap::new(),
} }
} }
pub fn read_global_cpu_stats(&mut self) -> anyhow::Result<CPUBreakdown> { pub fn read_global_cpu_stats(&mut self) -> anyhow::Result<CpuBreakdown> {
let content = std::fs::read_to_string("/proc/stat").unwrap(); let content = std::fs::read_to_string("/proc/stat").unwrap();
let cpu_line = content let cpu_line = content
@@ -53,7 +53,7 @@ impl CPUStatReader {
self.parse_cpu_line(cpu_line) self.parse_cpu_line(cpu_line)
} }
fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CPUBreakdown> { fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CpuBreakdown> {
let mut parts = line.split_whitespace(); let mut parts = line.split_whitespace();
let cpu_name = parts let cpu_name = parts
@@ -61,7 +61,7 @@ impl CPUStatReader {
.ok_or_else(|| anyhow!("missing cpu name"))? .ok_or_else(|| anyhow!("missing cpu name"))?
.to_string(); .to_string();
let mut values = CPUValues::default(); let mut values = CpuValues::default();
let mut fields = [ let mut fields = [
&mut values.user, &mut values.user,
&mut values.nice, &mut values.nice,
@@ -93,22 +93,25 @@ impl CPUStatReader {
fn calculate_percentages( fn calculate_percentages(
&self, &self,
current: &CPUValues, current: &CpuValues,
previous: Option<CPUValues>, previous: Option<CpuValues>,
) -> CPUBreakdown { ) -> CpuBreakdown {
let Some(prev) = previous else { let Some(prev) = previous else {
return CPUBreakdown::default(); return CpuBreakdown::default();
}; };
let total_delta = current.total().saturating_sub(prev.total()) as f32; let total_delta = current.total().saturating_sub(prev.total()) as f32;
if total_delta <= 0.0 { if total_delta <= 0.0 {
return CPUBreakdown::default(); return CpuBreakdown::default();
} }
let calculate_pct = let idle = current.idle.saturating_sub(prev.idle) as f32 / total_delta;
|current: u64, prev: u64| (current.saturating_sub(prev) as f32 / total_delta) * 100.0; let total = 1.0 - idle as f32;
CPUBreakdown { let calculate_pct =
|curr: u64, prev: u64| total * (curr.saturating_sub(prev) as f32 / total_delta) * 100.0;
CpuBreakdown {
system: calculate_pct(current.system, prev.system), system: calculate_pct(current.system, prev.system),
user: calculate_pct(current.user, prev.user), user: calculate_pct(current.user, prev.user),
idle: calculate_pct(current.idle, prev.idle), idle: calculate_pct(current.idle, prev.idle),

View File

@@ -22,11 +22,11 @@ pub struct SystemInfo {
pub struct CPU { pub struct CPU {
pub usage: f32, pub usage: f32,
pub threads: usize, pub threads: usize,
pub breakdown: CPUBreakdown, pub breakdown: CpuBreakdown,
} }
#[derive(Debug, Clone, Serialize, Deserialize, Default)] #[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CPUBreakdown { pub struct CpuBreakdown {
pub system: f32, pub system: f32,
pub user: f32, pub user: f32,
pub idle: f32, pub idle: f32,