Fix division by zero when memory data is not available (#85)

The total memory values may be zero when bottom is run on an unsupported
(or not-fully-supported) platform.

The previous behavior resulted in a NaN value for the memory datapoints,
which was passed through to tui-rs which ultimately panicked when
attempting to graph the memory widget.
This commit is contained in:
Mahmoud Al-Qudsi 2020-03-19 20:03:52 -05:00 committed by GitHub
parent cd6187ec5f
commit 2b418fb506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -144,9 +144,10 @@ impl DataCollection {
&mut self, harvested_data: &Data, harvested_time: Instant, new_entry: &mut TimedData,
) {
// Memory
let mem_percent = harvested_data.memory.mem_used_in_mb as f64
/ harvested_data.memory.mem_total_in_mb as f64
* 100.0;
let mem_percent = match harvested_data.memory.mem_total_in_mb {
0 => 0f64,
total => (harvested_data.memory.mem_used_in_mb as f64) / (total as f64) * 100.0,
};
let mem_joining_pts = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.mem_data.0, harvested_time, mem_percent)
} else {
@ -157,9 +158,10 @@ impl DataCollection {
// Swap
if harvested_data.swap.mem_total_in_mb > 0 {
let swap_percent = harvested_data.swap.mem_used_in_mb as f64
/ harvested_data.swap.mem_total_in_mb as f64
* 100.0;
let swap_percent = match harvested_data.swap.mem_total_in_mb {
0 => 0f64,
total => (harvested_data.swap.mem_used_in_mb as f64) / (total as f64) * 100.0,
};
let swap_joining_pt = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.swap_data.0, harvested_time, swap_percent)
} else {