'unix' -> 'linux' (bis)
[valheim_web.git] / backend / src / valheim_controller.rs
index 3c2bab6..ea502b0 100644 (file)
@@ -1,6 +1,10 @@
 use sysinfo::{ ProcessExt, SystemExt };\r
 \r
-#[cfg(target_os = "unix")]\r
+use std::{ fs, time::SystemTime };\r
+\r
+use chrono::{ DateTime, offset::Local };\r
+\r
+#[cfg(target_os = "linux")]\r
 use systemd::journal;\r
 \r
 #[derive(Clone, Debug)]\r
@@ -10,6 +14,7 @@ pub struct ValheimExe {
     uptime: u64, // [s].\r
     world_size: u64, // [B].\r
     nb_of_players: u32,\r
+    last_backup: Option<SystemTime>,\r
 }\r
 \r
 impl ValheimExe {\r
@@ -35,6 +40,16 @@ impl ValheimExe {
     pub fn get_nb_of_player(&self) -> u32 {\r
         self.nb_of_players\r
     }\r
+\r
+    pub fn format_last_backup(&self) -> String {\r
+        match self.last_backup {\r
+            Some(t) => {\r
+                let datetime: DateTime<Local> = t.into();\r
+                datetime.format("%d/%m/%Y %T").to_string()\r
+            },\r
+            None => String::from("?")\r
+        }\r
+    }\r
 }\r
 \r
 const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];\r
@@ -57,7 +72,7 @@ fn format_byte_size(bytes: u64, precision: usize) -> String {
 \r
 const VALHEIM_PROCESS_NAME: &str = "valheim_server";\r
 \r
-#[cfg(target_os = "unix")]\r
+#[cfg(target_os = "linux")]\r
 fn get_number_of_players() -> u32 {\r
     let mut journal =\r
         journal::OpenOptions::default().current_user(true).open().unwrap();\r
@@ -90,7 +105,22 @@ fn get_number_of_players() -> u32 {
     0\r
 }\r
 \r
-pub fn get_valheim_executable_information(world_path : &str) -> Option<ValheimExe> {\r
+fn get_last_backup_datetime(backup_path: &str) -> Option<SystemTime> {\r
+    let mut times =\r
+        fs::read_dir(backup_path).ok()?.filter_map(\r
+            |e| {\r
+                let dir = e.ok()?;\r
+                if dir.path().is_file() { Some(dir.metadata().ok()?.created().ok()?) } else { None }\r
+            }\r
+        )\r
+        .collect::<Vec<SystemTime>>();\r
+\r
+    times.sort();\r
+\r
+    Some(times.last()?.clone())\r
+}\r
+\r
+pub fn get_valheim_executable_information(world_path: &str, backup_path: &str) -> Option<ValheimExe> {\r
     let mut system = sysinfo::System::new_all();\r
     system.refresh_system();\r
     let processes = system.get_process_by_name(VALHEIM_PROCESS_NAME);\r
@@ -106,7 +136,8 @@ pub fn get_valheim_executable_information(world_path : &str) -> Option<ValheimEx
                 load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100.,\r
                 uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(),\r
                 world_size,\r
-                nb_of_players: get_number_of_players()\r
+                nb_of_players: get_number_of_players(),\r
+                last_backup: get_last_backup_datetime(backup_path)\r
             }\r
         )\r
     } else {\r