Add the date and time of the latest backup.
[valheim_web.git] / backend / src / valheim_controller.rs
index 3d99033..04f75a4 100644 (file)
@@ -1,5 +1,8 @@
 use sysinfo::{ ProcessExt, SystemExt };\r
-use chrono::{ DateTime, Datelike, Timelike, Utc };\r
+\r
+use std::{ fs, time::SystemTime };\r
+\r
+use chrono::{ DateTime, offset::Local };\r
 \r
 #[cfg(target_os = "unix")]\r
 use systemd::journal;\r
@@ -11,7 +14,7 @@ pub struct ValheimExe {
     uptime: u64, // [s].\r
     world_size: u64, // [B].\r
     nb_of_players: u32,\r
-    last_backup: DateTime,\r
+    last_backup: Option<SystemTime>,\r
 }\r
 \r
 impl ValheimExe {\r
@@ -39,7 +42,13 @@ impl ValheimExe {
     }\r
 \r
     pub fn format_last_backup(&self) -> String {\r
-        string::from("")\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
@@ -96,8 +105,19 @@ fn get_number_of_players() -> u32 {
     0\r
 }\r
 \r
-fn get_last_backup_datetime(backup_path: &str) -> DateTime {\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