Added cleaning event loop

This commit is contained in:
ClementTsang 2020-01-28 22:11:00 -05:00
parent 3d4de7867c
commit 63299afaf0
3 changed files with 17 additions and 24 deletions

View File

@ -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<String, (f64, Instant)>,
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;
}
}
}

View File

@ -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;

View File

@ -46,6 +46,7 @@ enum Event<I, J> {
KeyInput(I),
MouseInput(J),
Update(Box<data_harvester::Data>),
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);
}
}
}