From 2d20ec7f6f8fd4917444a40dcf2c015e6ef2a322 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Wed, 9 Oct 2019 19:19:39 -0400 Subject: [PATCH] Fix for cleaning times, as well as made it not run every loop. --- TODO.md | 2 + src/app/data_collection.rs | 111 ++++++++++++++++++------------------- src/canvas.rs | 7 ++- src/constants.rs | 1 + src/main.rs | 2 +- 5 files changed, 62 insertions(+), 61 deletions(-) diff --git a/TODO.md b/TODO.md index 6bc722bc..d2091f40 100644 --- a/TODO.md +++ b/TODO.md @@ -22,6 +22,8 @@ Note this will probably migrate to GitHub's native Issues; this was mostly for p ## Less important +- Ellipsis + - Rebalance cpu usage in process by using current value (it's currently just summing to 100%) - Tests diff --git a/src/app/data_collection.rs b/src/app/data_collection.rs index 05e86586..b46e39bc 100644 --- a/src/app/data_collection.rs +++ b/src/app/data_collection.rs @@ -44,6 +44,7 @@ pub struct DataState { prev_idle : f64, prev_non_idle : f64, temperature_type : temperature::TemperatureType, + last_clean : Instant, // Last time stale data was cleared } impl Default for DataState { @@ -57,6 +58,7 @@ impl Default for DataState { prev_idle : 0_f64, prev_non_idle : 0_f64, temperature_type : temperature::TemperatureType::Celsius, + last_clean : Instant::now(), } } } @@ -113,65 +115,60 @@ impl DataState { // Filter out stale timed entries let current_instant = std::time::Instant::now(); - let stale_list : Vec<_> = self - .prev_pid_stats - .iter() - .filter(|&(_, &v)| current_instant.duration_since(v.1).as_secs() > self.stale_max_seconds) - .map(|(k, _)| k.clone()) - .collect(); - for stale in stale_list { - debug!("Removing: {:?}", self.prev_pid_stats[&stale]); - self.prev_pid_stats.remove(&stale); + if current_instant.duration_since(self.last_clean).as_secs() > self.stale_max_seconds { + let stale_list : Vec<_> = self + .prev_pid_stats + .iter() + .filter(|&(_, &v)| current_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.data.list_of_cpu_packages = self + .data + .list_of_cpu_packages + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.stale_max_seconds) + .collect::>(); + + self.data.memory = self + .data + .memory + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.stale_max_seconds) + .collect::>(); + + self.data.swap = self + .data + .swap + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.stale_max_seconds) + .collect::>(); + + self.data.network = self + .data + .network + .iter() + .cloned() + .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.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() <= self.stale_max_seconds) + .collect::>(); + + self.last_clean = current_instant; } - self.data.list_of_cpu_packages = self - .data - .list_of_cpu_packages - .iter() - .cloned() - .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.stale_max_seconds) - .collect::>(); - - self.data.memory = self - .data - .memory - .iter() - .cloned() - .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.stale_max_seconds) - .collect::>(); - - self.data.swap = self - .data - .swap - .iter() - .cloned() - .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.stale_max_seconds) - .collect::>(); - - self.data.network = self - .data - .network - .iter() - .cloned() - .filter(|entry| current_instant.duration_since(entry.instant).as_secs() <= self.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() <= self.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() <= self.stale_max_seconds) - // .collect::>(); - debug!("End updating..."); } } diff --git a/src/canvas.rs b/src/canvas.rs index f1e29463..2612dda5 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -40,6 +40,7 @@ pub fn draw_data(terminal : &mut Terminal, app_state : let border_style : Style = Style::default().fg(BORDER_STYLE_COLOUR); let highlighted_border_style : Style = Style::default().fg(HIGHLIGHTED_BORDER_STYLE_COLOUR); + terminal.autoresize()?; terminal.draw(|mut f| { //debug!("Drawing!"); let vertical_chunks = Layout::default() @@ -129,8 +130,8 @@ pub fn draw_data(terminal : &mut Terminal, app_state : let x_axis : Axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]); let y_axis = Axis::default() .style(Style::default().fg(GRAPH_COLOUR)) - .bounds([-0.5, 100.5]) - .labels(&["0%", "100%"]); // Offset as the zero value isn't drawn otherwise... + .bounds([-0.5, 100.5]) // Offset as the zero value isn't drawn otherwise... + .labels(&["0%", "100%"]); let mem_name = "RAM:".to_string() + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)) @@ -139,7 +140,7 @@ pub fn draw_data(terminal : &mut Terminal, app_state : canvas_data.mem_values[0].0 as f64 / 1024.0, canvas_data.mem_values[0].1 as f64 / 1024.0 ); - let swap_name; + let swap_name : String; let mut mem_canvas_vec : Vec = vec![Dataset::default() .name(&mem_name) diff --git a/src/constants.rs b/src/constants.rs index af794b47..94ef2acd 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,4 +1,5 @@ // TODO: Store like three minutes of data, then change how much is shown based on scaling! pub const STALE_MAX_MILLISECONDS : u64 = 60 * 1000; // We wish to store at most 60 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; // We use this as it's a good value to work with. pub const DEFAULT_REFRESH_RATE_IN_MILLISECONDS : u128 = 1000; diff --git a/src/main.rs b/src/main.rs index c948005f..8d238e71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,7 +154,7 @@ fn main() -> error::Result<()> { // Event loop let mut data_state = data_collection::DataState::default(); data_state.init(); - data_state.set_stale_max_seconds(STALE_MAX_MILLISECONDS); + data_state.set_stale_max_seconds(STALE_MAX_MILLISECONDS / 1000); data_state.set_temperature_type(app.temperature_type.clone()); { let tx = tx.clone();