X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=79b92b3f984d86cf530d215a7bd363fd1a1a2041;hb=8688a849ec5e277bbbaf1860a621cae6d11f566b;hp=3d990331bbd0d5487cacff78e51137d32ebae39f;hpb=9deef63b608b62780a4dfed031410c25e10e477a;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index 3d99033..79b92b3 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,5 +1,8 @@ use sysinfo::{ ProcessExt, SystemExt }; -use chrono::{ DateTime, Datelike, Timelike, Utc }; + +use std::{ fs, time::SystemTime }; + +use chrono::{ DateTime, offset::Local }; #[cfg(target_os = "unix")] use systemd::journal; @@ -11,7 +14,7 @@ pub struct ValheimExe { uptime: u64, // [s]. world_size: u64, // [B]. nb_of_players: u32, - last_backup: DateTime, + last_backup: Option, } impl ValheimExe { @@ -39,7 +42,13 @@ impl ValheimExe { } pub fn format_last_backup(&self) -> String { - string::from("") + match self.last_backup { + Some(t) => { + let datetime: DateTime = t.into(); + datetime.format("%d/%m/%Y %T").to_string() + }, + None => String::from("?") + } } } @@ -63,7 +72,7 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const VALHEIM_PROCESS_NAME: &str = "valheim_server"; -#[cfg(target_os = "unix")] +#[cfg(target_os = "linux")] fn get_number_of_players() -> u32 { let mut journal = journal::OpenOptions::default().current_user(true).open().unwrap(); @@ -96,8 +105,19 @@ fn get_number_of_players() -> u32 { 0 } -fn get_last_backup_datetime(backup_path: &str) -> DateTime { +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()?.created().ok()?) } else { None } + } + ) + .collect::>(); + + times.sort(); + Some(times.last()?.clone()) } pub fn get_valheim_executable_information(world_path: &str, backup_path: &str) -> Option {