diff --git a/src/main.rs b/src/main.rs index a2a3e6bf..debf4afd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -238,27 +238,25 @@ fn draw_data(terminal : &mut Terminal, app_data : } fn init_logger() -> Result<(), fern::InitError> { - if cfg!(debug_assertions) { - fern::Dispatch::new() - .format(|out, message, record| { - out.finish(format_args!( - "{}[{}][{}] {}", - chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), - record.target(), - record.level(), - message - )) - }) - .level(log::LevelFilter::Debug) - .chain(fern::log_file("debug.log")?) - .apply()?; - } + fern::Dispatch::new() + .format(|out, message, record| { + out.finish(format_args!( + "{}[{}][{}] {}", + chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), + record.target(), + record.level(), + message + )) + }) + .level(if cfg!(debug_assertions) { log::LevelFilter::Debug } else { log::LevelFilter::Info }) + .chain(fern::log_file("debug.log")?) + .apply()?; Ok(()) } fn try_debug(result_log : &Result<(), fern::InitError>, message : &str) { - if cfg!(debug_assertions) && result_log.is_ok() { + if result_log.is_ok() { debug!("{}", message); } } diff --git a/src/widgets/cpu.rs b/src/widgets/cpu.rs index 73a313f9..f6c7f31d 100644 --- a/src/widgets/cpu.rs +++ b/src/widgets/cpu.rs @@ -5,10 +5,15 @@ use sysinfo::{ProcessorExt, System, SystemExt}; pub struct CPUData { pub cpu_name : Box, pub cpu_usage : u32, +} + +#[derive(Clone)] +pub struct CPUPackage { + pub cpu_vec : Vec, pub instant : Instant, } -pub fn get_cpu_data_list(sys : &System) -> Result, heim::Error> { +pub fn get_cpu_data_list(sys : &System) -> Result { let cpu_data = sys.get_processor_list(); let mut cpu_vec = Vec::new(); @@ -16,9 +21,8 @@ pub fn get_cpu_data_list(sys : &System) -> Result, heim::Error> { cpu_vec.push(CPUData { cpu_name : Box::from(cpu.get_name()), cpu_usage : (cpu.get_cpu_usage() * 100_f32).ceil() as u32, - instant : Instant::now(), }) } - Ok(cpu_vec) + Ok(CPUPackage { cpu_vec, instant : Instant::now() }) } diff --git a/src/widgets/disks.rs b/src/widgets/disks.rs index 4eb7133b..dcfe58fe 100644 --- a/src/widgets/disks.rs +++ b/src/widgets/disks.rs @@ -15,10 +15,15 @@ pub struct IOData { pub mount_point : Box, pub read_bytes : u64, pub write_bytes : u64, +} + +#[derive(Clone)] +pub struct IOPackage { + pub io_list : Vec, pub instant : Instant, } -pub async fn get_io_usage_list(get_physical : bool) -> Result, heim::Error> { +pub async fn get_io_usage_list(get_physical : bool) -> Result { let mut io_list : Vec = Vec::new(); if get_physical { let mut physical_counter_stream = heim::disk::io_counters_physical(); @@ -28,7 +33,6 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result, heim: mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")), read_bytes : io.read_bytes().get::(), write_bytes : io.write_bytes().get::(), - instant : Instant::now(), }) } } @@ -40,12 +44,11 @@ pub async fn get_io_usage_list(get_physical : bool) -> Result, heim: mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")), read_bytes : io.read_bytes().get::(), write_bytes : io.write_bytes().get::(), - instant : Instant::now(), }) } } - Ok(io_list) + Ok(IOPackage { io_list, instant : Instant::now() }) } pub async fn get_disk_usage_list() -> Result, heim::Error> { diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 9721788a..9f2cbd38 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -30,9 +30,9 @@ fn push_if_valid(result : &Result, vector #[derive(Default, Clone)] pub struct Data { - pub list_of_cpu_packages : Vec>, - pub list_of_io : Vec>, - pub list_of_physical_io : Vec>, + pub list_of_cpu_packages : Vec, + pub list_of_io : Vec, + pub list_of_physical_io : Vec, pub memory : Vec, pub swap : Vec, pub list_of_temperature : Vec, @@ -61,6 +61,8 @@ impl DataState { self.sys.refresh_system(); self.sys.refresh_network(); + const STALE_MAX_SECONDS : u64 = 60; + // What we want to do: For timed data, if there is an error, just do not add. For other data, just don't update! push_if_valid(&network::get_network_data(&self.sys), &mut self.data.network); push_if_valid(&cpu::get_cpu_data_list(&self.sys), &mut self.data.list_of_cpu_packages); @@ -74,6 +76,57 @@ impl DataState { push_if_valid(&disks::get_io_usage_list(false).await, &mut self.data.list_of_io); push_if_valid(&disks::get_io_usage_list(true).await, &mut self.data.list_of_physical_io); set_if_valid(&temperature::get_temperature_data().await, &mut self.data.list_of_temperature); + + // Filter out stale timed entries... + let current_instant = std::time::Instant::now(); + self.data.list_of_cpu_packages = self + .data + .list_of_cpu_packages + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS) + .collect::>(); + + self.data.memory = self + .data + .memory + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS) + .collect::>(); + + self.data.swap = self + .data + .swap + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS) + .collect::>(); + + self.data.network = self + .data + .network + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS) + .collect::>(); + + self.data.list_of_io = self + .data + .list_of_io + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS) + .collect::>(); + + self.data.list_of_physical_io = self + .data + .list_of_physical_io + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= STALE_MAX_SECONDS) + .collect::>(); + debug!("End updating..."); } } diff --git a/src/widgets/network.rs b/src/widgets/network.rs index 3094422b..24a89678 100644 --- a/src/widgets/network.rs +++ b/src/widgets/network.rs @@ -1,9 +1,11 @@ +use std::time::Instant; use sysinfo::{NetworkExt, System, SystemExt}; -#[derive(Clone, Default)] +#[derive(Clone)] pub struct NetworkData { pub rx : u64, pub tx : u64, + pub instant : Instant, } pub fn get_network_data(sys : &System) -> Result { @@ -11,5 +13,6 @@ pub fn get_network_data(sys : &System) -> Result { Ok(NetworkData { rx : network_data.get_income(), tx : network_data.get_outcome(), + instant : Instant::now(), }) }