X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=414cee12486c2b46bd8cdfb55e85275032d35501;hb=525a070cc4fb56394a81eeeb3683260b941a2d51;hp=f36307cefa9e87094cb1973900ea144b9ace8740;hpb=184720b8ec54231f178ce76d83cd72d2a2b206b7;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index f36307c..414cee1 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,25 +1,38 @@ use sysinfo::{ ProcessExt, SystemExt }; +use systemd::journal; -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct ValheimExe { memory: u64, // [kB]. load_average_5min: f64, // [%]. uptime: u64, // [s]. + world_size: u64, // [B]. + nb_of_players: u32, } impl ValheimExe { pub fn format_memory(&self) -> String { format_byte_size(self.memory * 1024, 2) } + pub fn format_load_average(&self) -> String { format!("{:.2} %", self.load_average_5min) } + pub fn format_uptime(&self) -> String { let mins = self.uptime / 60; let hours = mins / 60; 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) + } + + pub fn get_nb_of_player(&self) -> u32 { + self.nb_of_players + } } const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]; @@ -27,9 +40,7 @@ const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB fn format_byte_size(bytes: u64, precision: usize) -> String { for i in 0 .. 8 { let mut size: u64 = 1; - for j in 0 .. i { - size *= 1024; - } + size *= 1024u64.pow(i as u32); if bytes < 1024 { return format!("{} {}", std::cmp::max(0u64, bytes), BINARY_PREFIXES[i]); @@ -44,19 +55,50 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const VALHEIM_PROCESS_NAME: &str = "valheim_server"; -pub fn get_valheim_executable_information() -> Option { +fn get_number_of_players() -> u32 { + let mut journal = + journal::OpenOptions::default().current_user(true).open().unwrap(); + + journal.seek_tail().unwrap(); + + loop { + match journal.previous_entry() { + Ok(Some(entry)) => { + if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) { + if unit == "valheim.service" { + if let Some(pos) = mess.find("Connections") { + let nb_of_connections_str = mess.get(pos+12..).unwrap(); + if let Some(pos_end) = nb_of_connections_str.find(' ') { + if let Ok(n) = nb_of_connections_str.get(0..pos_end).unwrap().parse() { + return n; + } + } + } + } + } + }, + _ => return 0 + } + } +} + +pub fn get_valheim_executable_information(world_path : &str) -> 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(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, + nb_of_players: get_number_of_players() } ) } else {