Skip to content

Commit

Permalink
feat(api): disk history
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Sep 26, 2023
1 parent b0a606a commit e72e561
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
54 changes: 34 additions & 20 deletions backend/src/resource_watcher.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
}
Expand All @@ -47,8 +47,8 @@ pub struct ResourceWatcher {
pub mem_history: Arc<Mutex<Vec<Memory>>>,
pub swap_history: Arc<Mutex<Vec<Memory>>>,
pub cpu_history: Arc<Mutex<Vec<Cpu>>>,
pub disks: Arc<Mutex<Vec<Disk>>>,
pub network: Arc<Mutex<Vec<Network>>>,
pub disks: Arc<Mutex<HashMap<String, Vec<DiskUsage>>>>,
pub network: Arc<Mutex<HashMap<String, Vec<NetworkUsage>>>>,
pub mem_history_max: usize,
pub cpu_history_max: usize,
pub process_list: Arc<Mutex<Vec<Process>>>,
Expand All @@ -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())),
Expand All @@ -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();
Expand Down Expand Up @@ -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(),
});
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/QuickStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
16 changes: 12 additions & 4 deletions frontend/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e72e561

Please sign in to comment.