From 7f7de8055b360669da5284a11e70b2a8d1eb9ac3 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Wed, 22 Oct 2025 23:24:15 -0400 Subject: [PATCH] fix potential logic bug with generate points logic, but this isn't the actual issue --- CHANGELOG.md | 3 +- src/app.rs | 2 +- src/app/data/store.rs | 10 +++--- src/app/data/time_series.rs | 2 ++ src/canvas.rs | 2 +- src/canvas/widgets/cpu_basic.rs | 2 +- src/canvas/widgets/cpu_graph.rs | 55 ++++++++++++++++----------------- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 482bf3fb..ef06aa6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ That said, these are more guidelines rather than hardset rules, though the proje --- -## [0.11.3]/[0.12.0] - Unreleased +## [0.11.3] - Unreleased ### Features @@ -30,6 +30,7 @@ That said, these are more guidelines rather than hardset rules, though the proje - [#1833](https://github.com/ClementTsang/bottom/pull/1833): Sort disk I/O using actual value rather than string representation. - [#1812](https://github.com/ClementTsang/bottom/pull/1812): Fix ARC collection on FreeBSD. +- [](): Fix displayed average CPU value being wrong in graphs. ### Other diff --git a/src/app.rs b/src/app.rs index 2e3188a2..66e63514 100644 --- a/src/app.rs +++ b/src/app.rs @@ -148,7 +148,7 @@ impl App { for cpu in self.states.cpu_state.widget_states.values_mut() { if cpu.force_update_data { - cpu.set_legend_data(&data_source.cpu_harvest); + cpu.set_legend_data(&data_source.cpu_data); } } diff --git a/src/app/data/store.rs b/src/app/data/store.rs index 4b98a3ce..9c70af41 100644 --- a/src/app/data/store.rs +++ b/src/app/data/store.rs @@ -29,7 +29,7 @@ pub struct StoredData { pub arc_harvest: Option, #[cfg(feature = "gpu")] pub gpu_harvest: Vec<(String, MemData)>, - pub cpu_harvest: Vec, + pub cpu_data: Vec, pub load_avg_harvest: cpu::LoadAvgHarvest, pub process_data: ProcessData, /// TODO: (points_rework_v1) Might be a better way to do this without having to store here? @@ -50,7 +50,7 @@ impl Default for StoredData { #[cfg(not(target_os = "windows"))] cache_harvest: None, swap_harvest: None, - cpu_harvest: Default::default(), + cpu_data: Default::default(), load_avg_harvest: cpu::LoadAvgHarvest::default(), process_data: Default::default(), prev_io: Vec::default(), @@ -113,16 +113,16 @@ impl StoredData { } if let Some(cpu) = data.cpu { - self.cpu_harvest.clear(); + self.cpu_data.clear(); if let Some(avg) = cpu.avg { - self.cpu_harvest.push(cpu::CpuData { + self.cpu_data.push(cpu::CpuData { data_type: cpu::CpuDataType::Avg, usage: avg, }); } - self.cpu_harvest + self.cpu_data .extend( cpu.cpus .into_iter() diff --git a/src/app/data/time_series.rs b/src/app/data/time_series.rs index fdbf2856..c62acb4d 100644 --- a/src/app/data/time_series.rs +++ b/src/app/data/time_series.rs @@ -103,6 +103,8 @@ impl TimeSeriesData { // If there isn't avg then we never had any to begin with. if let Some(avg) = cpu_harvest.avg { self.avg_cpu.push(avg.into()); + } else { + self.avg_cpu.insert_break(); } } else { for c in &mut self.cpu { diff --git a/src/canvas.rs b/src/canvas.rs index 463f37ec..67e4b844 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -244,7 +244,7 @@ impl Painter { } let data = app_state.data_store.get_data(); - let actual_cpu_data_len = data.cpu_harvest.len(); + let actual_cpu_data_len = data.cpu_data.len(); // This fixes #397, apparently if the height is 1, it can't render the CPU // bars... diff --git a/src/canvas/widgets/cpu_basic.rs b/src/canvas/widgets/cpu_basic.rs index cd4ecf02..fdc04439 100644 --- a/src/canvas/widgets/cpu_basic.rs +++ b/src/canvas/widgets/cpu_basic.rs @@ -21,7 +21,7 @@ impl Painter { pub fn draw_basic_cpu( &self, f: &mut Frame<'_>, app_state: &mut App, mut draw_loc: Rect, widget_id: u64, ) { - let cpu_data = &app_state.data_store.get_data().cpu_harvest; + let cpu_data = &app_state.data_store.get_data().cpu_data; // This is a bit complicated, but basically, we want to draw SOME number // of columns to draw all CPUs. Ideally, as well, we want to not have diff --git a/src/canvas/widgets/cpu_graph.rs b/src/canvas/widgets/cpu_graph.rs index 1140409f..8f4f18e6 100644 --- a/src/canvas/widgets/cpu_graph.rs +++ b/src/canvas/widgets/cpu_graph.rs @@ -120,7 +120,7 @@ impl Painter { &self, cpu_widget_state: &'a CpuWidgetState, data: &'a StoredData, show_avg_cpu: bool, ) -> Vec> { let current_scroll_position = cpu_widget_state.table.state.current_index; - let cpu_entries = &data.cpu_harvest; + let cpu_entries = &data.cpu_data; let cpu_points = &data.timeseries_data.cpu; let time = &data.timeseries_data.time; @@ -152,36 +152,33 @@ impl Painter { } points - } else if let Some(CpuData { .. }) = cpu_entries.get(current_scroll_position - 1) { - // We generally subtract one from current scroll position because of the all entry. + } else if show_avg_cpu && current_scroll_position == AVG_POSITION { + let style = self.styles.avg_cpu_colour; - let show_avg_offset = if show_avg_cpu { AVG_POSITION } else { 0 }; - let is_avg = show_avg_cpu && current_scroll_position == AVG_POSITION; - - let style = if is_avg { - self.styles.avg_cpu_colour - } else { - self.styles.cpu_colour_styles[(current_scroll_position - 1 - show_avg_offset) - % self.styles.cpu_colour_styles.len()] - }; - - if is_avg { - vec![ - GraphData::default() - .style(style) - .time(time) - .values(&data.timeseries_data.avg_cpu), - ] - } else { - vec![ - GraphData::default() - .style(style) - .time(time) - .values(&cpu_points[current_scroll_position - 1 - show_avg_offset]), - ] - } + vec![ + GraphData::default() + .style(style) + .time(time) + .values(&data.timeseries_data.avg_cpu), + ] } else { - vec![] + // Note, we generally subtract one from current scroll position because of the all entry. + let show_avg_offset = if show_avg_cpu { AVG_POSITION } else { 0 }; + let corrected_offset = current_scroll_position - 1 - show_avg_offset; + + if let Some(CpuData { .. }) = cpu_entries.get(corrected_offset) { + let style = self.styles.cpu_colour_styles + [(corrected_offset) % self.styles.cpu_colour_styles.len()]; + + vec![ + GraphData::default() + .style(style) + .time(time) + .values(&cpu_points[corrected_offset]), + ] + } else { + vec![] + } } }