From: Greg Burri Date: Thu, 11 Mar 2021 10:07:03 +0000 (+0100) Subject: Add world size X-Git-Url: http://git.euphorik.ch/index.cgi?p=valheim_web.git;a=commitdiff_plain;h=ae59fd67818b8db8782222bd06c76a43213f0513 Add world size --- diff --git a/backend/src/main.rs b/backend/src/main.rs index ce9ae9f..f14592e 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -22,7 +22,8 @@ struct MainTemplate { text_status: String, memory: String, load_average: String, - uptime: String + uptime: String, + world_size: String } #[derive(Deserialize)] @@ -42,11 +43,12 @@ async fn main_page(/*key_shared: web::Data>,*/ query: Query { let value_unknown = String::from(VALUE_UNKNOWN); - MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), load_average: value_unknown.clone(), uptime: value_unknown.clone() } + MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), load_average: value_unknown.clone(), uptime: value_unknown.clone(), world_size: value_unknown.clone() } } } diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index f36307c..efad791 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,10 +1,11 @@ -use sysinfo::{ ProcessExt, SystemExt }; +use sysinfo::{ComponentExt, ProcessExt, SystemExt}; #[derive(Debug)] pub struct ValheimExe { memory: u64, // [kB]. load_average_5min: f64, // [%]. uptime: u64, // [s]. + world_size: u64, // [B]. } impl ValheimExe { @@ -20,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"]; @@ -43,20 +47,24 @@ 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(), 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() + uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(), + world_size } ) } else { diff --git a/backend/templates/main.html b/backend/templates/main.html index b096875..7f6dcb1 100644 --- a/backend/templates/main.html +++ b/backend/templates/main.html @@ -10,6 +10,7 @@

{{ text_status }}

+

World size: {{ world_size }}

Memory used: {{ memory }}

Load average (5 min): {{ load_average }}

Uptime: {{ uptime }}