mirror of
				https://github.com/csehviktor/status-monitor.git
				synced 2025-08-08 18:06:14 +02:00 
			
		
		
		
	rename + correct data
This commit is contained in:
		| @@ -1,13 +1,13 @@ | ||||
| use common::metrics::{CPU, Disk, Memory, Metrics, Network, SystemInfo}; | ||||
| use sysinfo::{Disks, Networks, System}; | ||||
|  | ||||
| use crate::cpu::CPUStatReader; | ||||
| use crate::cpu::CpuStatReader; | ||||
|  | ||||
| pub struct Collector { | ||||
|     sys: System, | ||||
|     disks: Disks, | ||||
|     networks: Networks, | ||||
|     cpu_reader: CPUStatReader, | ||||
|     cpu_reader: CpuStatReader, | ||||
| } | ||||
|  | ||||
| impl Collector { | ||||
| @@ -16,7 +16,7 @@ impl Collector { | ||||
|             sys: System::new_all(), | ||||
|             disks: Disks::new_with_refreshed_list(), | ||||
|             networks: Networks::new_with_refreshed_list(), | ||||
|             cpu_reader: CPUStatReader::new(), | ||||
|             cpu_reader: CpuStatReader::new(), | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -1,9 +1,9 @@ | ||||
| use anyhow::anyhow; | ||||
| use common::metrics::CPUBreakdown; | ||||
| use common::metrics::CpuBreakdown; | ||||
| use std::collections::HashMap; | ||||
|  | ||||
| #[derive(Default, Debug, Clone, Copy)] | ||||
| struct CPUValues { | ||||
| struct CpuValues { | ||||
|     pub user: u64, | ||||
|     pub nice: u64, | ||||
|     pub system: u64, | ||||
| @@ -16,7 +16,7 @@ struct CPUValues { | ||||
|     pub guest_nice: u64, | ||||
| } | ||||
|  | ||||
| impl CPUValues { | ||||
| impl CpuValues { | ||||
|     pub fn total(&self) -> u64 { | ||||
|         self.user | ||||
|             + self.nice | ||||
| @@ -31,18 +31,18 @@ impl CPUValues { | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub struct CPUStatReader { | ||||
|     previous_stats: HashMap<String, CPUValues>, | ||||
| pub struct CpuStatReader { | ||||
|     previous_stats: HashMap<String, CpuValues>, | ||||
| } | ||||
|  | ||||
| impl CPUStatReader { | ||||
| impl CpuStatReader { | ||||
|     pub fn new() -> Self { | ||||
|         Self { | ||||
|             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 cpu_line = content | ||||
| @@ -53,7 +53,7 @@ impl CPUStatReader { | ||||
|         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 cpu_name = parts | ||||
| @@ -61,7 +61,7 @@ impl CPUStatReader { | ||||
|             .ok_or_else(|| anyhow!("missing cpu name"))? | ||||
|             .to_string(); | ||||
|  | ||||
|         let mut values = CPUValues::default(); | ||||
|         let mut values = CpuValues::default(); | ||||
|         let mut fields = [ | ||||
|             &mut values.user, | ||||
|             &mut values.nice, | ||||
| @@ -93,22 +93,25 @@ impl CPUStatReader { | ||||
|  | ||||
|     fn calculate_percentages( | ||||
|         &self, | ||||
|         current: &CPUValues, | ||||
|         previous: Option<CPUValues>, | ||||
|     ) -> CPUBreakdown { | ||||
|         current: &CpuValues, | ||||
|         previous: Option<CpuValues>, | ||||
|     ) -> CpuBreakdown { | ||||
|         let Some(prev) = previous else { | ||||
|             return CPUBreakdown::default(); | ||||
|             return CpuBreakdown::default(); | ||||
|         }; | ||||
|  | ||||
|         let total_delta = current.total().saturating_sub(prev.total()) as f32; | ||||
|         if total_delta <= 0.0 { | ||||
|             return CPUBreakdown::default(); | ||||
|             return CpuBreakdown::default(); | ||||
|         } | ||||
|  | ||||
|         let calculate_pct = | ||||
|             |current: u64, prev: u64| (current.saturating_sub(prev) as f32 / total_delta) * 100.0; | ||||
|         let idle = current.idle.saturating_sub(prev.idle) as f32 / total_delta; | ||||
|         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), | ||||
|             user: calculate_pct(current.user, prev.user), | ||||
|             idle: calculate_pct(current.idle, prev.idle), | ||||
|   | ||||
| @@ -22,11 +22,11 @@ pub struct SystemInfo { | ||||
| pub struct CPU { | ||||
|     pub usage: f32, | ||||
|     pub threads: usize, | ||||
|     pub breakdown: CPUBreakdown, | ||||
|     pub breakdown: CpuBreakdown, | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||||
| pub struct CPUBreakdown { | ||||
| pub struct CpuBreakdown { | ||||
|     pub system: f32, | ||||
|     pub user: f32, | ||||
|     pub idle: f32, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user