From: Greg Burri Date: Wed, 10 Mar 2021 15:22:37 +0000 (+0100) Subject: Replace cpu usage by load average X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=17b32f18e23bf3cd60f6cbcb24df6cc729d935c0;p=valheim_web.git Replace cpu usage by load average --- diff --git a/backend/src/main.rs b/backend/src/main.rs index 88424da..ce9ae9f 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -21,7 +21,7 @@ mod valheim_controller; struct MainTemplate { text_status: String, memory: String, - cpu: String, + load_average: String, uptime: String } @@ -41,12 +41,12 @@ async fn main_page(/*key_shared: web::Data>,*/ query: Query { let value_unknown = String::from(VALUE_UNKNOWN); - MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), cpu: value_unknown.clone(), uptime: value_unknown.clone() } + MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), load_average: value_unknown.clone(), uptime: value_unknown.clone() } } } diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index cf18e08..fd6dcc4 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,11 +1,9 @@ -use std::cmp::min; - use sysinfo::{ ProcessExt, SystemExt }; #[derive(Debug)] pub struct ValheimExe { memory: u64, // [kB]. - cpu_usage: f32, // [%]. + load_average_5min: f64, // [%]. uptime: u64, // [s]. } @@ -13,8 +11,8 @@ impl ValheimExe { pub fn format_memory(&self) -> String { format_byte_size(self.memory * 1024, 2) } - pub fn format_cpu_usage(&self) -> String { - format!("{:.2} %", self.cpu_usage) + pub fn format_load_average(&self) -> String { + format!("{:.2} %", self.load_average_5min) } pub fn format_uptime(&self) -> String { let mins = self.uptime / 60; @@ -44,7 +42,7 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { String::from("") } -const VALHEIM_PROCESS_NAME: &str = "valheim_server"; +const VALHEIM_PROCESS_NAME: &str = "firefox"; //valheim_server"; pub fn get_valheim_executable_information() -> Option { let mut system = sysinfo::System::new_all(); @@ -57,11 +55,11 @@ pub fn get_valheim_executable_information() -> Option { Some( ValheimExe { memory: process.memory(), - cpu_usage: process.cpu_usage(), + load_average_5min: system.get_load_average().five, uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time() } ) } else { None } -} \ No newline at end of file +} diff --git a/backend/templates/main.html b/backend/templates/main.html index 2d7991e..b096875 100644 --- a/backend/templates/main.html +++ b/backend/templates/main.html @@ -10,8 +10,8 @@

{{ text_status }}

-

Memory: {{ memory }}

-

CPU: {{ cpu }}

+

Memory used: {{ memory }}

+

Load average (5 min): {{ load_average }}

Uptime: {{ uptime }}