mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-27 07:34:27 +02:00
refactor: add network drawing cache system
This commit is contained in:
parent
2bff04d8a4
commit
64d47d54d3
@ -158,13 +158,11 @@ impl Scrollable {
|
|||||||
let new_index = self.current_index + change_by;
|
let new_index = self.current_index + change_by;
|
||||||
if new_index >= self.num_items {
|
if new_index >= self.num_items {
|
||||||
EventResult::NoRedraw
|
EventResult::NoRedraw
|
||||||
|
} else if self.current_index == new_index {
|
||||||
|
EventResult::NoRedraw
|
||||||
} else {
|
} else {
|
||||||
if self.current_index == new_index {
|
self.update_index(new_index);
|
||||||
EventResult::NoRedraw
|
EventResult::Redraw
|
||||||
} else {
|
|
||||||
self.update_index(new_index);
|
|
||||||
EventResult::Redraw
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,8 +130,8 @@ impl SortableColumn {
|
|||||||
|
|
||||||
impl TableColumn for SortableColumn {
|
impl TableColumn for SortableColumn {
|
||||||
fn display_name(&self) -> Cow<'static, str> {
|
fn display_name(&self) -> Cow<'static, str> {
|
||||||
const UP_ARROW: &'static str = "▲";
|
const UP_ARROW: &str = "▲";
|
||||||
const DOWN_ARROW: &'static str = "▼";
|
const DOWN_ARROW: &str = "▼";
|
||||||
format!(
|
format!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
self.internal.display_name(),
|
self.internal.display_name(),
|
||||||
|
@ -220,6 +220,7 @@ impl TimeGraph {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the current display time boundary.
|
||||||
pub fn get_current_display_time(&self) -> u64 {
|
pub fn get_current_display_time(&self) -> u64 {
|
||||||
self.current_display_time
|
self.current_display_time
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::{collections::HashMap, time::Instant};
|
use std::{borrow::Cow, collections::HashMap, time::Instant};
|
||||||
|
|
||||||
use tui::{
|
use tui::{
|
||||||
backend::Backend,
|
backend::Backend,
|
||||||
@ -401,17 +401,14 @@ fn adjust_network_data_point(
|
|||||||
/// A struct containing useful cached information for a [`NetGraph`].
|
/// A struct containing useful cached information for a [`NetGraph`].
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct NetGraphCache {
|
pub struct NetGraphCache {
|
||||||
max_range: f64,
|
max_value: f64,
|
||||||
labels: Vec<String>,
|
cached_upper_bound: f64,
|
||||||
time_start: f64,
|
labels: Vec<Cow<'static, str>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum NetGraphCacheState {
|
enum NetGraphCacheState {
|
||||||
Uncached,
|
Uncached,
|
||||||
Cached {
|
Cached(NetGraphCache),
|
||||||
cached_area: Rect,
|
|
||||||
data: NetGraphCache,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A widget denoting network usage via a graph. This version is self-contained within a single [`TimeGraph`];
|
/// A widget denoting network usage via a graph. This version is self-contained within a single [`TimeGraph`];
|
||||||
@ -419,7 +416,7 @@ enum NetGraphCacheState {
|
|||||||
///
|
///
|
||||||
/// As of now, this is essentially just a wrapper around a [`TimeGraph`].
|
/// As of now, this is essentially just a wrapper around a [`TimeGraph`].
|
||||||
pub struct NetGraph {
|
pub struct NetGraph {
|
||||||
/// The graph itself. Just a [`TimeGraph`].
|
/// The graph itself. Just a [`TimeGraph`].
|
||||||
graph: TimeGraph,
|
graph: TimeGraph,
|
||||||
|
|
||||||
// Cached details for drawing purposes; probably want to move at some point...
|
// Cached details for drawing purposes; probably want to move at some point...
|
||||||
@ -466,54 +463,54 @@ impl NetGraph {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the associated cache on a [`NetGraph`].
|
/// Sets the draw cache for a [`NetGraph`].
|
||||||
pub fn set_cache(&mut self, area: Rect, max_range: f64, labels: Vec<String>, time_start: f64) {
|
pub fn set_draw_cache(&mut self) {
|
||||||
self.draw_cache = NetGraphCacheState::Cached {
|
let current_time = -(self.graph.get_current_display_time() as f64);
|
||||||
cached_area: area,
|
let (_current_max_time, current_max_value) = get_max_entry(
|
||||||
data: NetGraphCache {
|
&self.network_data_rx,
|
||||||
max_range,
|
&self.network_data_tx,
|
||||||
labels,
|
current_time,
|
||||||
time_start,
|
&self.scale_type,
|
||||||
},
|
self.use_binary_prefix,
|
||||||
}
|
);
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns whether the [`NetGraph`] contains a cache from drawing.
|
match &mut self.draw_cache {
|
||||||
pub fn is_cache_valid(&self, area: Rect) -> bool {
|
NetGraphCacheState::Uncached => {
|
||||||
match &self.draw_cache {
|
debug!("No cache!");
|
||||||
NetGraphCacheState::Uncached => false,
|
|
||||||
NetGraphCacheState::Cached {
|
|
||||||
cached_area,
|
|
||||||
data: _,
|
|
||||||
} => *cached_area == area,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a reference to the [`NetGraphCache`] tied to the [`NetGraph`].
|
let (cached_upper_bound, labels) = adjust_network_data_point(
|
||||||
pub fn get_cache(&self) -> Option<&NetGraphCache> {
|
current_max_value,
|
||||||
match &self.draw_cache {
|
&self.scale_type,
|
||||||
NetGraphCacheState::Uncached => None,
|
&self.unit_type,
|
||||||
NetGraphCacheState::Cached {
|
self.use_binary_prefix,
|
||||||
cached_area: _,
|
);
|
||||||
data,
|
|
||||||
} => Some(data),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`NetGraphCache`] tied to the [`NetGraph`].
|
let labels: Vec<Cow<'static, str>> = labels.into_iter().map(Into::into).collect();
|
||||||
pub fn get_cache_owned(&self) -> Option<NetGraphCache> {
|
|
||||||
match &self.draw_cache {
|
|
||||||
NetGraphCacheState::Uncached => None,
|
|
||||||
NetGraphCacheState::Cached {
|
|
||||||
cached_area: _,
|
|
||||||
data,
|
|
||||||
} => Some(data.clone()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A wrapper function around checking the cache validity and setting/getting the cache.
|
self.draw_cache = NetGraphCacheState::Cached(NetGraphCache {
|
||||||
pub fn check_get_cache(&mut self) -> NetGraphCache {
|
max_value: current_max_value,
|
||||||
todo!()
|
cached_upper_bound,
|
||||||
|
labels: labels.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
NetGraphCacheState::Cached(cache) => {
|
||||||
|
if current_max_value != cache.max_value {
|
||||||
|
// Invalidated.
|
||||||
|
let (upper_bound, labels) = adjust_network_data_point(
|
||||||
|
current_max_value,
|
||||||
|
&self.scale_type,
|
||||||
|
&self.unit_type,
|
||||||
|
self.use_binary_prefix,
|
||||||
|
);
|
||||||
|
|
||||||
|
*cache = NetGraphCache {
|
||||||
|
max_value: current_max_value,
|
||||||
|
cached_upper_bound: upper_bound,
|
||||||
|
labels: labels.into_iter().map(Into::into).collect(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -555,6 +552,8 @@ impl Widget for NetGraph {
|
|||||||
})
|
})
|
||||||
.borders(Borders::ALL);
|
.borders(Borders::ALL);
|
||||||
|
|
||||||
|
self.set_draw_cache();
|
||||||
|
|
||||||
let chart_data = vec![
|
let chart_data = vec![
|
||||||
TimeGraphData {
|
TimeGraphData {
|
||||||
data: &self.network_data_rx,
|
data: &self.network_data_rx,
|
||||||
@ -576,28 +575,16 @@ impl Widget for NetGraph {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let (_best_time, max_entry) = get_max_entry(
|
let (y_bounds, y_bound_labels) = match &self.draw_cache {
|
||||||
&self.network_data_rx,
|
NetGraphCacheState::Cached(cache) => ([0.0, cache.cached_upper_bound], &cache.labels),
|
||||||
&self.network_data_tx,
|
NetGraphCacheState::Uncached => unreachable!(),
|
||||||
-(self.graph.get_current_display_time() as f64),
|
};
|
||||||
&self.scale_type,
|
|
||||||
self.use_binary_prefix,
|
|
||||||
);
|
|
||||||
|
|
||||||
let (max_range, labels) = adjust_network_data_point(
|
|
||||||
max_entry,
|
|
||||||
&self.scale_type,
|
|
||||||
&self.unit_type,
|
|
||||||
self.use_binary_prefix,
|
|
||||||
);
|
|
||||||
let y_bounds = [0.0, max_range];
|
|
||||||
let y_bound_labels = labels.into_iter().map(Into::into).collect::<Vec<_>>();
|
|
||||||
|
|
||||||
self.graph.draw_tui_chart(
|
self.graph.draw_tui_chart(
|
||||||
painter,
|
painter,
|
||||||
f,
|
f,
|
||||||
&chart_data,
|
&chart_data,
|
||||||
&y_bound_labels,
|
y_bound_labels,
|
||||||
y_bounds,
|
y_bounds,
|
||||||
false,
|
false,
|
||||||
block,
|
block,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user