3c2bab6e35d6a34a08629e33fa49b067fa5957bb
[valheim_web.git] / backend / src / valheim_controller.rs
1 use sysinfo::{ ProcessExt, SystemExt };
2
3 #[cfg(target_os = "unix")]
4 use systemd::journal;
5
6 #[derive(Clone, Debug)]
7 pub struct ValheimExe {
8 memory: u64, // [kB].
9 load_average_5min: f64, // [%].
10 uptime: u64, // [s].
11 world_size: u64, // [B].
12 nb_of_players: u32,
13 }
14
15 impl ValheimExe {
16 pub fn format_memory(&self) -> String {
17 format_byte_size(self.memory * 1024, 2)
18 }
19
20 pub fn format_load_average(&self) -> String {
21 format!("{:.2} %", self.load_average_5min)
22 }
23
24 pub fn format_uptime(&self) -> String {
25 let mins = self.uptime / 60;
26 let hours = mins / 60;
27 let days = hours / 24;
28 format!("{}d{}h{}min", days, hours - 24 * days, mins - 60 * hours)
29 }
30
31 pub fn format_world_size(&self) -> String {
32 format_byte_size(self.world_size, 2)
33 }
34
35 pub fn get_nb_of_player(&self) -> u32 {
36 self.nb_of_players
37 }
38 }
39
40 const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
41
42 fn format_byte_size(bytes: u64, precision: usize) -> String {
43 for i in 0 .. 8 {
44 let mut size: u64 = 1;
45 size *= 1024u64.pow(i as u32);
46
47 if bytes < 1024 {
48 return format!("{} {}", std::cmp::max(0u64, bytes), BINARY_PREFIXES[i]);
49 }
50 else if bytes < 1024 * size {
51 return format!("{:.prec$} {}", bytes as f64 / size as f64, BINARY_PREFIXES[i], prec = precision);
52 }
53 }
54
55 String::from("")
56 }
57
58 const VALHEIM_PROCESS_NAME: &str = "valheim_server";
59
60 #[cfg(target_os = "unix")]
61 fn get_number_of_players() -> u32 {
62 let mut journal =
63 journal::OpenOptions::default().current_user(true).open().unwrap();
64
65 journal.seek_tail().unwrap();
66
67 loop {
68 match journal.previous_entry() {
69 Ok(Some(entry)) => {
70 if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) {
71 if unit == "valheim.service" {
72 if let Some(pos) = mess.find("Connections") {
73 let nb_of_connections_str = mess.get(pos+12..).unwrap();
74 if let Some(pos_end) = nb_of_connections_str.find(' ') {
75 if let Ok(n) = nb_of_connections_str.get(0..pos_end).unwrap().parse() {
76 return n;
77 }
78 }
79 }
80 }
81 }
82 },
83 _ => return 0
84 }
85 }
86 }
87
88 #[cfg(target_os = "windows")]
89 fn get_number_of_players() -> u32 {
90 0
91 }
92
93 pub fn get_valheim_executable_information(world_path : &str) -> Option<ValheimExe> {
94 let mut system = sysinfo::System::new_all();
95 system.refresh_system();
96 let processes = system.get_process_by_name(VALHEIM_PROCESS_NAME);
97
98 if processes.len() >= 1 {
99 let process = processes.first().unwrap();
100
101 let world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), Err(_) => 0u64 };
102
103 Some(
104 ValheimExe {
105 memory: process.memory(),
106 load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100.,
107 uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(),
108 world_size,
109 nb_of_players: get_number_of_players()
110 }
111 )
112 } else {
113 None
114 }
115 }