diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index fc468f7e..b40380c9 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -1,6 +1,6 @@ //! This is the main file to house data collection functions. -use crate::{constants, utils::error::Result}; +use crate::utils::error::Result; use std::{collections::HashMap, time::Instant}; use sysinfo::{System, SystemExt}; @@ -66,13 +66,11 @@ impl Data { pub struct DataState { pub data: Data, sys: System, - stale_max_seconds: u64, prev_pid_stats: HashMap, prev_idle: f64, prev_non_idle: f64, mem_total_kb: u64, temperature_type: temperature::TemperatureType, - last_clean: Instant, // Last time stale data was cleared use_current_cpu_total: bool, } @@ -81,13 +79,11 @@ impl Default for DataState { DataState { data: Data::default(), sys: System::new(), - stale_max_seconds: constants::STALE_MAX_MILLISECONDS / 1000, prev_pid_stats: HashMap::new(), prev_idle: 0_f64, prev_non_idle: 0_f64, mem_total_kb: 0, temperature_type: temperature::TemperatureType::Celsius, - last_clean: Instant::now(), use_current_cpu_total: false, } } @@ -173,23 +169,5 @@ impl DataState { // Update time self.data.last_collection_time = current_instant; - - // Filter out stale timed entries - let clean_instant = Instant::now(); - if clean_instant.duration_since(self.last_clean).as_secs() > self.stale_max_seconds { - let stale_list: Vec<_> = self - .prev_pid_stats - .iter() - .filter(|&(_, &v)| { - clean_instant.duration_since(v.1).as_secs() > self.stale_max_seconds - }) - .map(|(k, _)| k.clone()) - .collect(); - for stale in stale_list { - self.prev_pid_stats.remove(&stale); - } - - self.last_clean = clean_instant; - } } } diff --git a/src/constants.rs b/src/constants.rs index 1b93ea1b..a0811da7 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,5 +1,5 @@ // TODO: Store like three minutes of data, then change how much is shown based on scaling! -pub const STALE_MAX_MILLISECONDS: u64 = 180 * 1000; // We wish to store at most 60 seconds worth of data. This may change in the future, or be configurable. +pub const STALE_MAX_MILLISECONDS: u128 = 180 * 1000; // We wish to store at most 180 seconds worth of data. This may change in the future, or be configurable. pub const TIME_STARTS_FROM: u64 = 60 * 1000; pub const TICK_RATE_IN_MILLISECONDS: u64 = 200; // How fast the screen refreshes pub const DEFAULT_REFRESH_RATE_IN_MILLISECONDS: u128 = 1000; diff --git a/src/main.rs b/src/main.rs index 07a4d0a9..76f4563f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,7 @@ enum Event { KeyInput(I), MouseInput(J), Update(Box), + Clean, } enum ResetEvent { @@ -175,6 +176,16 @@ fn main() -> error::Result<()> { }); } + // Cleaning loop + { + let tx = tx.clone(); + thread::spawn(move || loop { + thread::sleep(Duration::from_millis( + constants::STALE_MAX_MILLISECONDS as u64, + )); + tx.send(Event::Clean).unwrap(); + }); + } // Event loop let (rtx, rrx) = mpsc::channel(); { @@ -301,6 +312,10 @@ fn main() -> error::Result<()> { handle_process_sorting(&mut app); } } + Event::Clean => { + app.data_collection + .clean_data(constants::STALE_MAX_MILLISECONDS); + } } }