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