From 549d61b836f738c5cafeb3f5d25338c85b882bb9 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Wed, 13 May 2020 00:14:54 -0400 Subject: [PATCH] bug: add extra check for process cpu value to be >= 0 --- src/app/data_harvester/processes.rs | 7 +- src/canvas/widgets/cpu_graph.rs | 2 +- src/canvas/widgets/network_graph.rs | 108 ++++++++++++++-------------- 3 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/app/data_harvester/processes.rs b/src/app/data_harvester/processes.rs index 2f825a02..912a3d94 100644 --- a/src/app/data_harvester/processes.rs +++ b/src/app/data_harvester/processes.rs @@ -288,8 +288,7 @@ pub fn linux_get_processes_list( .output()?; let ps_stdout = String::from_utf8_lossy(&ps_result.stdout); let split_string = ps_stdout.split('\n'); - let cpu_calc = cpu_usage_calculation(prev_idle, prev_non_idle); - if let Ok((cpu_usage, cpu_fraction)) = cpu_calc { + if let Ok((cpu_usage, cpu_fraction)) = cpu_usage_calculation(prev_idle, prev_non_idle) { let process_list = split_string.collect::>(); let mut new_pid_stats = HashMap::new(); @@ -362,7 +361,9 @@ pub fn windows_macos_get_processes_list( } else { process_val.cpu_usage() as f64 / num_cpus }; - let process_cpu_usage = if use_current_cpu_total { + let process_cpu_usage = if pcu < 0.0 { + 0.0 + } else if use_current_cpu_total { pcu / cpu_usage } else { pcu diff --git a/src/canvas/widgets/cpu_graph.rs b/src/canvas/widgets/cpu_graph.rs index 232e8a72..043f7025 100644 --- a/src/canvas/widgets/cpu_graph.rs +++ b/src/canvas/widgets/cpu_graph.rs @@ -148,7 +148,7 @@ impl CpuGraphWidget for Painter { .enumerate() .rev() .filter_map(|(itx, (cpu, cpu_show_vec))| { - if *cpu_show_vec { + if *cpu_show_vec { Some( Dataset::default() .marker(if use_dot { diff --git a/src/canvas/widgets/network_graph.rs b/src/canvas/widgets/network_graph.rs index 316bd8e6..07146eaa 100644 --- a/src/canvas/widgets/network_graph.rs +++ b/src/canvas/widgets/network_graph.rs @@ -132,37 +132,39 @@ impl NetworkGraphWidget for Painter { let dataset = if app_state.app_config_fields.use_old_network_legend && !hide_legend { let mut ret_val = vec![]; - ret_val.push( - Dataset::default() - .name(format!("RX: {:7}", app_state.canvas_data.rx_display)) - .marker(if app_state.app_config_fields.use_dot { - Marker::Dot - } else { - Marker::Braille - }) - .style(self.colours.rx_style) - .data(&network_data_rx).graph_type(tui::widgets::GraphType::Line), - ); + ret_val.push( + Dataset::default() + .name(format!("RX: {:7}", app_state.canvas_data.rx_display)) + .marker(if app_state.app_config_fields.use_dot { + Marker::Dot + } else { + Marker::Braille + }) + .style(self.colours.rx_style) + .data(&network_data_rx) + .graph_type(tui::widgets::GraphType::Line), + ); - ret_val.push( - Dataset::default() - .name(format!("TX: {:7}", app_state.canvas_data.tx_display)) - .marker(if app_state.app_config_fields.use_dot { - Marker::Dot - } else { - Marker::Braille - }) - .style(self.colours.tx_style) - .data(&network_data_tx).graph_type(tui::widgets::GraphType::Line), - ); - ret_val.push( - Dataset::default() - .name(format!( - "Total RX: {:7}", - app_state.canvas_data.total_rx_display - )) - .style(self.colours.total_rx_style), - ); + ret_val.push( + Dataset::default() + .name(format!("TX: {:7}", app_state.canvas_data.tx_display)) + .marker(if app_state.app_config_fields.use_dot { + Marker::Dot + } else { + Marker::Braille + }) + .style(self.colours.tx_style) + .data(&network_data_tx) + .graph_type(tui::widgets::GraphType::Line), + ); + ret_val.push( + Dataset::default() + .name(format!( + "Total RX: {:7}", + app_state.canvas_data.total_rx_display + )) + .style(self.colours.total_rx_style), + ); ret_val.push( Dataset::default() @@ -177,29 +179,31 @@ impl NetworkGraphWidget for Painter { } else { let mut ret_val = vec![]; - ret_val.push( - Dataset::default() - .name(&app_state.canvas_data.rx_display) - .marker(if app_state.app_config_fields.use_dot { - Marker::Dot - } else { - Marker::Braille - }) - .style(self.colours.rx_style) - .data(&network_data_rx).graph_type(tui::widgets::GraphType::Line), - ); + ret_val.push( + Dataset::default() + .name(&app_state.canvas_data.rx_display) + .marker(if app_state.app_config_fields.use_dot { + Marker::Dot + } else { + Marker::Braille + }) + .style(self.colours.rx_style) + .data(&network_data_rx) + .graph_type(tui::widgets::GraphType::Line), + ); - ret_val.push( - Dataset::default() - .name(&app_state.canvas_data.tx_display) - .marker(if app_state.app_config_fields.use_dot { - Marker::Dot - } else { - Marker::Braille - }) - .style(self.colours.tx_style) - .data(&network_data_tx).graph_type(tui::widgets::GraphType::Line), - ); + ret_val.push( + Dataset::default() + .name(&app_state.canvas_data.tx_display) + .marker(if app_state.app_config_fields.use_dot { + Marker::Dot + } else { + Marker::Braille + }) + .style(self.colours.tx_style) + .data(&network_data_tx) + .graph_type(tui::widgets::GraphType::Line), + ); ret_val };