X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=f11f7f4fd753b481f0f93d79a1dc6b8533458768;hb=d16870d03f1e374c4e65bb9e1570ba092df21c21;hp=414cee12486c2b46bd8cdfb55e85275032d35501;hpb=525a070cc4fb56394a81eeeb3683260b941a2d51;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index 414cee1..f11f7f4 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,4 +1,10 @@ use sysinfo::{ ProcessExt, SystemExt }; + +use std::{ fs, time::SystemTime }; + +use chrono::{ DateTime, offset::Local }; + +#[cfg(target_os = "linux")] use systemd::journal; #[derive(Clone, Debug)] @@ -8,6 +14,7 @@ pub struct ValheimExe { uptime: u64, // [s]. world_size: u64, // [B]. nb_of_players: u32, + last_backup: Option, } impl ValheimExe { @@ -23,7 +30,7 @@ impl ValheimExe { 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 { @@ -33,6 +40,16 @@ impl ValheimExe { pub fn get_nb_of_player(&self) -> u32 { self.nb_of_players } + + 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"]; @@ -55,6 +72,7 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const VALHEIM_PROCESS_NAME: &str = "valheim_server"; +#[cfg(target_os = "linux")] fn get_number_of_players() -> u32 { let mut journal = journal::OpenOptions::default().current_user(true).open().unwrap(); @@ -82,7 +100,27 @@ fn get_number_of_players() -> u32 { } } -pub fn get_valheim_executable_information(world_path : &str) -> Option { +#[cfg(target_os = "windows")] +fn get_number_of_players() -> u32 { + 0 +} + +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(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); @@ -98,7 +136,8 @@ pub fn get_valheim_executable_information(world_path : &str) -> Option