diff --git a/backend/src/resource_watcher.rs b/backend/src/resource_watcher.rs index 0e9118c..16fb897 100644 --- a/backend/src/resource_watcher.rs +++ b/backend/src/resource_watcher.rs @@ -1,5 +1,5 @@ use serde::Serialize; -use std::sync::{Arc, Mutex}; +use std::{sync::{Arc, Mutex}, collections::HashMap}; use sysinfo::{CpuExt, DiskExt, NetworkExt, PidExt, ProcessExt, System, SystemExt}; use crate::api::ApiSettings; @@ -28,15 +28,15 @@ pub struct Process { } #[derive(Clone, Serialize)] -pub struct Disk { - name: String, +pub struct DiskUsage { + timestamp: u64, total: u64, used: u64, } #[derive(Clone, Serialize)] -pub struct Network { - name: String, +pub struct NetworkUsage { + timestamp: u64, recieve: u64, transmit: u64, } @@ -47,8 +47,8 @@ pub struct ResourceWatcher { pub mem_history: Arc>>, pub swap_history: Arc>>, pub cpu_history: Arc>>, - pub disks: Arc>>, - pub network: Arc>>, + pub disks: Arc>>>, + pub network: Arc>>>, pub mem_history_max: usize, pub cpu_history_max: usize, pub process_list: Arc>>, @@ -62,8 +62,8 @@ impl ResourceWatcher { mem_history: Arc::new(Mutex::new(Vec::new())), swap_history: Arc::new(Mutex::new(Vec::new())), cpu_history: Arc::new(Mutex::new(Vec::new())), - disks: Arc::new(Mutex::new(Vec::new())), - network: Arc::new(Mutex::new(Vec::new())), + disks: Arc::new(Mutex::new(HashMap::new())), + network: Arc::new(Mutex::new(HashMap::new())), mem_history_max: settings.mem_history_max as usize, cpu_history_max: settings.cpu_history_max as usize, process_list: Arc::new(Mutex::new(Vec::new())), @@ -87,7 +87,7 @@ impl ResourceWatcher { let mut swap_history = self.swap_history.as_ref().lock().unwrap(); let mut cpu_history = self.cpu_history.as_ref().lock().unwrap(); let mut disks = self.disks.as_ref().lock().unwrap(); - let mut network = self.network.as_ref().lock().unwrap(); + let mut networks = self.network.as_ref().lock().unwrap(); let mut process_list = self.process_list.as_ref().lock().unwrap(); system.refresh_cpu(); @@ -143,23 +143,37 @@ impl ResourceWatcher { cpu_history.remove(0); } - // Clear the old disk list - disks.clear(); - + // For each disk name, add it's current usage to it's list in the hashmap for disk in system.disks() { - disks.push(Disk { - name: format!("{:?}", disk.name()), + let name = format!("{:?}", disk.name()); + + if !disks.contains_key(&name) { + disks.insert(name.clone(), Vec::new()); + } + + let disk_vec = disks.get_mut(&name).unwrap(); + + disk_vec.push(DiskUsage { + timestamp, total: disk.total_space(), used: disk.total_space() - disk.available_space(), }); - } - // Clear the old network list - network.clear(); + if disk_vec.len() > self.mem_history_max { + disk_vec.remove(0); + } + } + // Same with network usage for (name, data) in system.networks() { - network.push(Network { - name: name.to_string(), + if !networks.contains_key(name) { + networks.insert(name.clone(), Vec::new()); + } + + let network_vec = networks.get_mut(name).unwrap(); + + network_vec.push(NetworkUsage { + timestamp, recieve: data.received(), transmit: data.transmitted(), }); diff --git a/frontend/src/components/QuickStats.tsx b/frontend/src/components/QuickStats.tsx index e3eaa37..109bf42 100644 --- a/frontend/src/components/QuickStats.tsx +++ b/frontend/src/components/QuickStats.tsx @@ -16,10 +16,10 @@ interface Props { export function QuickStats(props: Props) { const memoryUse = props.memoryData[props.memoryData.length - 1]?.used const swapUse = props.swapData[props.swapData.length - 1]?.used - const diskUse = props.diskData.map(d => d.used).reduce((a, b) => a + b) - const diskTotal = props.diskData.map(d => d.total).reduce((a, b) => a + b) - const netRecieve = props.networkData.map(n => n.recieve).reduce((a, b) => a + b) - const netTransmit = props.networkData.map(n => n.transmit).reduce((a, b) => a + b) + const diskUse = Object.keys(props.diskData).map(d => props.diskData[d][props.diskData[d].length - 1].used).reduce((a, b) => a + b) + const diskTotal = Object.keys(props.diskData).map(d => props.diskData[d][props.diskData[d].length - 1].total).reduce((a, b) => a + b) + const netRecieve = Object.keys(props.networkData).map(n => props.networkData[n][props.networkData[n].length - 1].recieve).reduce((a, b) => a + b) + const netTransmit = Object.keys(props.networkData).map(n => props.networkData[n][props.networkData[n].length - 1].transmit).reduce((a, b) => a + b) const cpuPct = props.cpuData[props.cpuData.length - 1]?.used return ( diff --git a/frontend/types.d.ts b/frontend/types.d.ts index 8e72917..1ca618e 100644 --- a/frontend/types.d.ts +++ b/frontend/types.d.ts @@ -10,18 +10,26 @@ interface CPU { used: number; // This is a percentage } -interface Disk { - name: string, +interface DiskUsage { + timestamp: number, total: number, used: number, } -interface Network { - name: string, +interface Disk { + [key:string]: DiskUsage +} + +interface NetworkUsage { + timestamp: number, recieve: number, transmit: number, } +interface Network { + [key:string]: NetworkUsage +} + interface Process { name: string; pid: number;