X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=efad791d19252f6b4e1ff3be59a4e4f4133d7eac;hb=ae59fd67818b8db8782222bd06c76a43213f0513;hp=cf18e08c3afec715b3de660e47959137cd29a860;hpb=c06453cb2e95d2b4b7e81c3b80b45877d2010f75;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index cf18e08..efad791 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,20 +1,19 @@ -use std::cmp::min; - -use sysinfo::{ ProcessExt, SystemExt }; +use sysinfo::{ComponentExt, ProcessExt, SystemExt}; #[derive(Debug)] pub struct ValheimExe { memory: u64, // [kB]. - cpu_usage: f32, // [%]. + load_average_5min: f64, // [%]. uptime: u64, // [s]. + world_size: u64, // [B]. } impl ValheimExe { pub fn format_memory(&self) -> String { format_byte_size(self.memory * 1024, 2) } - pub fn format_cpu_usage(&self) -> String { - format!("{:.2} %", self.cpu_usage) + pub fn format_load_average(&self) -> String { + format!("{:.2} %", self.load_average_5min) } pub fn format_uptime(&self) -> String { let mins = self.uptime / 60; @@ -22,6 +21,9 @@ impl ValheimExe { let days = hours / 24; format!("{}d{}h{}min", days, hours - 24 * days, mins - 60 * hours) } + pub fn format_world_size(&self) -> String { + format_byte_size(self.world_size, 2) + } } const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]; @@ -45,23 +47,27 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { } const VALHEIM_PROCESS_NAME: &str = "valheim_server"; +const VALHEIM_WORLD_PATH: &str = "/home/greg/ValheimWorld/pouet.db"; // TODO: Put in conf. pub fn get_valheim_executable_information() -> Option { let mut system = sysinfo::System::new_all(); - system.refresh_all(); + system.refresh_system(); let processes = system.get_process_by_name(VALHEIM_PROCESS_NAME); if processes.len() >= 1 { let process = processes.first().unwrap(); + let world_size = match std::fs::metadata(VALHEIM_WORLD_PATH) { Ok(f) => f.len(), Err(_) => 0u64 }; + Some( ValheimExe { memory: process.memory(), - cpu_usage: process.cpu_usage(), - uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time() + load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100., + uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(), + world_size } ) } else { None } -} \ No newline at end of file +}