bug: disk displays nothing on if IO fails to match

Obvious on macOS.
This commit is contained in:
Clement Tsang 2020-05-11 22:34:35 -04:00 committed by GitHub
parent 7b5685bf44
commit 137e3cea5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,4 @@
use lazy_static::lazy_static;
/// In charge of cleaning, processing, and managing data. I couldn't think of /// In charge of cleaning, processing, and managing data. I couldn't think of
/// a better name for the file. Since I called data collection "harvesting", /// a better name for the file. Since I called data collection "harvesting",
/// then this is the farmer I guess. /// then this is the farmer I guess.
@ -12,12 +13,12 @@
/// call the purging function. Failure to do so *will* result in a growing /// call the purging function. Failure to do so *will* result in a growing
/// memory usage and higher CPU usage - you will be trying to process more and /// memory usage and higher CPU usage - you will be trying to process more and
/// more points as this is used! /// more points as this is used!
use std::time::Instant; use std::{time::Instant, vec::Vec};
use std::vec::Vec;
use crate::data_harvester::{ use crate::data_harvester::{
battery_harvester, cpu, disks, mem, network, processes, temperature, Data, battery_harvester, cpu, disks, mem, network, processes, temperature, Data,
}; };
use regex::Regex;
pub type TimeOffset = f64; pub type TimeOffset = f64;
pub type Value = f64; pub type Value = f64;
@ -230,22 +231,38 @@ impl DataCollection {
for (itx, device) in disks.iter().enumerate() { for (itx, device) in disks.iter().enumerate() {
if let Some(trim) = device.name.split('/').last() { if let Some(trim) = device.name.split('/').last() {
let io_device = io.get(trim); let io_device = if cfg!(target_os = "macos") {
if let Some(io) = io_device { // Must trim one level further!
let io_r_pt = io.read_bytes;
let io_w_pt = io.write_bytes;
if self.io_labels_and_prev.len() <= itx { lazy_static! {
self.io_labels_and_prev.push(((0, 0), (io_r_pt, io_w_pt))); static ref DISK_REGEX: Regex = Regex::new(r"disk\d+").unwrap();
} else if let Some((io_curr, io_prev)) = self.io_labels_and_prev.get_mut(itx) {
let r_rate =
((io_r_pt - io_prev.0) as f64 / time_since_last_harvest).round() as u64;
let w_rate =
((io_w_pt - io_prev.1) as f64 / time_since_last_harvest).round() as u64;
*io_curr = (r_rate, w_rate);
*io_prev = (io_r_pt, io_w_pt);
} }
if let Some(disk_trim) = DISK_REGEX.find(trim) {
io.get(disk_trim.as_str())
} else {
None
}
} else {
io.get(trim)
};
let (io_r_pt, io_w_pt) = if let Some(io) = io_device {
(io.read_bytes, io.write_bytes)
} else {
(0, 0)
};
if self.io_labels_and_prev.len() <= itx {
self.io_labels_and_prev.push(((0, 0), (io_r_pt, io_w_pt)));
} else if let Some((io_curr, io_prev)) = self.io_labels_and_prev.get_mut(itx) {
let r_rate = ((io_r_pt.saturating_sub(io_prev.0)) as f64
/ time_since_last_harvest)
.round() as u64;
let w_rate = ((io_w_pt.saturating_sub(io_prev.1)) as f64
/ time_since_last_harvest)
.round() as u64;
*io_curr = (r_rate, w_rate);
*io_prev = (io_r_pt, io_w_pt);
} }
} }
} }