From 256c5aba685a9f15704a2d7fde3fb9806dd19e86 Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Fri, 10 Oct 2025 18:07:34 +1100 Subject: [PATCH] fix: sort disk I/O by original rates instead of readable strings (#1833) --- src/app/data/store.rs | 26 +++++++++++++------------- src/widgets/disk_table.rs | 33 ++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/app/data/store.rs b/src/app/data/store.rs index dd4210d9..4b98a3ce 100644 --- a/src/app/data/store.rs +++ b/src/app/data/store.rs @@ -9,7 +9,6 @@ use crate::collection::batteries; use crate::{ app::AppConfigFields, collection::{Data, cpu, disks, memory::MemData, network}, - dec_bytes_per_second_string, utils::data_units::DataUnit, widgets::{DiskWidgetData, TempWidgetData}, }; @@ -246,21 +245,22 @@ impl StoredData { } }; - let (mut io_read, mut io_write) = ("N/A".into(), "N/A".into()); + let (mut io_read_rate_bytes, mut io_write_rate_bytes) = (None, None); if let Some(Some(io_device)) = io_device { if let Some(prev_io) = self.prev_io.get_mut(itx) { - let r_rate = ((io_device.read_bytes.saturating_sub(prev_io.0)) as f64 - / time_since_last_harvest) - .round() as u64; + io_read_rate_bytes = Some( + ((io_device.read_bytes.saturating_sub(prev_io.0)) as f64 + / time_since_last_harvest) + .round() as u64, + ); - let w_rate = ((io_device.write_bytes.saturating_sub(prev_io.1)) as f64 - / time_since_last_harvest) - .round() as u64; + io_write_rate_bytes = Some( + ((io_device.write_bytes.saturating_sub(prev_io.1)) as f64 + / time_since_last_harvest) + .round() as u64, + ); *prev_io = (io_device.read_bytes, io_device.write_bytes); - - io_read = dec_bytes_per_second_string(r_rate).into(); - io_write = dec_bytes_per_second_string(w_rate).into(); } } @@ -276,8 +276,8 @@ impl StoredData { used_bytes: device.used_space, total_bytes: device.total_space, summed_total_bytes, - io_read, - io_write, + io_read_rate_bytes, + io_write_rate_bytes, }); } } diff --git a/src/widgets/disk_table.rs b/src/widgets/disk_table.rs index cd9b55e7..9e2e64f2 100644 --- a/src/widgets/disk_table.rs +++ b/src/widgets/disk_table.rs @@ -9,7 +9,10 @@ use crate::{ SortDataTable, SortDataTableProps, SortOrder, SortsRow, }, options::config::style::Styles, - utils::{data_units::get_decimal_bytes, general::sort_partial_fn}, + utils::{ + conversion::dec_bytes_per_second_string, data_units::get_decimal_bytes, + general::sort_partial_fn, + }, }; #[derive(Clone, Debug)] @@ -20,8 +23,8 @@ pub struct DiskWidgetData { pub used_bytes: Option, pub total_bytes: Option, pub summed_total_bytes: Option, - pub io_read: Cow<'static, str>, - pub io_write: Cow<'static, str>, + pub io_read_rate_bytes: Option, + pub io_write_rate_bytes: Option, } impl DiskWidgetData { @@ -79,6 +82,18 @@ impl DiskWidgetData { None } } + + fn io_read(&self) -> Cow<'static, str> { + self.io_read_rate_bytes.map_or("N/A".into(), |r_rate| { + dec_bytes_per_second_string(r_rate).into() + }) + } + + fn io_write(&self) -> Cow<'static, str> { + self.io_write_rate_bytes.map_or("N/A".into(), |w_rate| { + dec_bytes_per_second_string(w_rate).into() + }) + } } #[derive(Debug, Clone)] @@ -177,8 +192,8 @@ impl DataToCell for DiskWidgetData { DiskColumn::UsedPercent => percent_string(self.used_percent()), DiskColumn::FreePercent => percent_string(self.free_percent()), DiskColumn::Total => self.total_space(), - DiskColumn::IoRead => self.io_read.clone(), - DiskColumn::IoWrite => self.io_write.clone(), + DiskColumn::IoRead => self.io_read(), + DiskColumn::IoWrite => self.io_write(), }; Some(text) @@ -235,10 +250,14 @@ impl SortsRow for DiskColumn { data.sort_by(|a, b| sort_partial_fn(descending)(&a.total_bytes, &b.total_bytes)); } DiskColumn::IoRead => { - data.sort_by(|a, b| sort_partial_fn(descending)(&a.io_read, &b.io_read)); + data.sort_by(|a, b| { + sort_partial_fn(descending)(&a.io_read_rate_bytes, &b.io_read_rate_bytes) + }); } DiskColumn::IoWrite => { - data.sort_by(|a, b| sort_partial_fn(descending)(&a.io_write, &b.io_write)); + data.sort_by(|a, b| { + sort_partial_fn(descending)(&a.io_write_rate_bytes, &b.io_write_rate_bytes) + }); } } }