X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=8c04e238f21e022a84810ee11640013ca724907c;hb=150411e0cffb5ad0127d822c39a38c2e92b2bd99;hp=efad791d19252f6b4e1ff3be59a4e4f4133d7eac;hpb=ae59fd67818b8db8782222bd06c76a43213f0513;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index efad791..8c04e23 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,29 +1,59 @@ -use sysinfo::{ComponentExt, ProcessExt, SystemExt}; +use sysinfo::{ ProcessExt, SystemExt }; -#[derive(Debug)] +use std::{ fs, time::SystemTime }; + +use chrono::{ DateTime, offset::Local }; + +#[cfg(target_os = "linux")] +use systemd::journal; + +#[derive(Clone, Debug)] pub struct ValheimExe { memory: u64, // [kB]. load_average_5min: f64, // [%]. uptime: u64, // [s]. world_size: u64, // [B]. + active_players: Vec, + last_backup: Option, } 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) + 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 format_active_players(&self) -> String { + if self.active_players.len() == 0 { + String::from("") + } else { + self.active_players.join(", ") + } + } + + pub fn format_last_backup(&self) -> String { + match self.last_backup { + Some(t) => { + let datetime: DateTime = t.into(); + datetime.format("%d/%m/%Y %T").to_string() + }, + None => String::from("?") + } + } } const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]; @@ -31,9 +61,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]); @@ -47,9 +75,79 @@ 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. +const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from"; +const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections"; + +#[cfg(target_os = "linux")] +fn get_active_players() -> Vec { + let mut journal = + journal::OpenOptions::default().current_user(true).open().unwrap(); + + journal.seek_tail().unwrap(); + + let mut number_of_connections = -1i32; + let mut players : Vec = Vec::new(); + + 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(STRING_BEFORE_CHARACTER_NAME) { + let character_str = mess.get(pos+STRING_BEFORE_CHARACTER_NAME.len()+1..).unwrap(); + if let Some(pos_end) = character_str.find(" : ") { + let player_name = String::from(character_str.get(0..pos_end).unwrap()); + if !players.contains(&player_name) { + players.push(player_name); + if players.len() as i32 == number_of_connections { + return players; + } + } + } + } + else if let Some(pos) = mess.find(STRING_BEFORE_NB_OF_CONNECTIONS) { + let nb_of_connections_str = mess.get(pos+STRING_BEFORE_NB_OF_CONNECTIONS.len()+1..).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() { + if number_of_connections == -1 { + number_of_connections = n as i32 + + if players.len() as i32 >= number_of_connections { + return players; + } + } + } + } + } + } + } + }, + _ => return players + } + } +} + +#[cfg(target_os = "windows")] +fn get_active_players() -> Vec { + Vec::new() +} + +fn get_last_backup_datetime(backup_path: &str) -> Option { + let mut times = + fs::read_dir(backup_path).ok()?.filter_map( + |e| { + let dir = e.ok()?; + if dir.path().is_file() { Some(dir.metadata().ok()?.modified().ok()?) } else { None } + } + ) + .collect::>(); + + times.sort(); + + Some(times.last()?.clone()) +} -pub fn get_valheim_executable_information() -> Option { +pub fn get_valheim_executable_information(world_path: &str, backup_path: &str) -> Option { let mut system = sysinfo::System::new_all(); system.refresh_system(); let processes = system.get_process_by_name(VALHEIM_PROCESS_NAME); @@ -57,14 +155,16 @@ pub fn get_valheim_executable_information() -> Option { 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 }; + 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(), - world_size + world_size, + active_players: get_active_players(), + last_backup: get_last_backup_datetime(backup_path) } ) } else {