| 1 | use std::time::Instant; |
| 2 | |
| 3 | use ratatui::{ |
| 4 | Frame, |
| 5 | layout::{Constraint, Rect}, |
| 6 | style::Style, |
| 7 | }; |
| 8 | |
| 9 | use crate::{ |
| 10 | app::{App, data::Values}, |
| 11 | canvas::{ |
| 12 | Painter, |
| 13 | components::time_series::{GraphData, LegendConstraints}, |
| 14 | drawing_utils::should_hide_x_label, |
| 15 | }, |
| 16 | collection::memory::MemData, |
| 17 | components::time_series::GraphDrawCtx, |
| 18 | get_binary_unit_and_denominator, |
| 19 | }; |
| 20 | |
| 21 | /// Convert memory info into a combined memory label. |
| 22 | #[inline] |
| 23 | fn memory_legend_label(name: &str, data: Option<&MemData>) -> String { |
| 24 | if let Some(data) = data { |
| 25 | let total_bytes = data.total_bytes.get(); |
| 26 | let percentage = data.used_bytes as f64 / total_bytes as f64 * 100.0; |
| 27 | let (unit, denominator) = get_binary_unit_and_denominator(total_bytes); |
| 28 | let used = data.used_bytes as f64 / denominator; |
| 29 | let total = total_bytes as f64 / denominator; |
| 30 | |
| 31 | format!("{name}:{percentage:3.0}% {used:.1}{unit}/{total:.1}{unit}") |
| 32 | } else { |
| 33 | format!("{name}: 0% 0.0B/0.0B") |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// Get graph data. |
| 38 | #[inline] |
| 39 | fn graph_data<'a>( |
| 40 | out: &mut Vec<GraphData<'a>>, name: &str, last_harvest: Option<&'a MemData>, |
| 41 | time: &'a [Instant], values: &'a Values, style: Style, |
| 42 | ) { |
| 43 | if !values.no_elements() { |
| 44 | let label = memory_legend_label(name, last_harvest).into(); |
| 45 | |
| 46 | out.push( |
| 47 | GraphData::default() |
| 48 | .name(label) |
| 49 | .time(time) |
| 50 | .values(values) |
| 51 | .style(style), |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl Painter { |
| 57 | pub fn draw_memory_graph( |
| 58 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 59 | ) { |
| 60 | if let Some(mem_state) = app_state.states.mem_state.widget_states.get_mut(&widget_id) { |
| 61 | let hide_x_labels = should_hide_x_label( |
| 62 | app_state.app_config_fields.hide_time, |
| 63 | app_state.app_config_fields.autohide_time, |
| 64 | mem_state.graph.state_mut().autohide_timer_mut(), |
| 65 | draw_loc, |
| 66 | ); |
| 67 | let graph_data = { |
| 68 | let mut size = 1; |
| 69 | let data = app_state.data_store.get_data(); |
| 70 | |
| 71 | // TODO: is this optimization really needed...? This just pre-allocates a vec, |
| 72 | // but it'll probably never be that big... |
| 73 | |
| 74 | if data.swap_harvest.is_some() { |
| 75 | size += 1; // add capacity for SWAP |
| 76 | } |
| 77 | #[cfg(feature = "zfs")] |
| 78 | { |
| 79 | if data.arc_harvest.is_some() { |
| 80 | size += 1; // add capacity for ARC |
| 81 | } |
| 82 | } |
| 83 | #[cfg(feature = "gpu")] |
| 84 | { |
| 85 | size += data.gpu_harvest.len(); // add row(s) for gpu |
| 86 | } |
| 87 | |
| 88 | let mut points = Vec::with_capacity(size); |
| 89 | let time_series = &data.time_series_data; |
| 90 | let time = &time_series.time; |
| 91 | |
| 92 | // TODO: Add a "no data" option here/to time graph if there is no entries |
| 93 | graph_data( |
| 94 | &mut points, |
| 95 | "RAM", |
| 96 | data.ram_harvest.as_ref(), |
| 97 | time, |
| 98 | &time_series.ram, |
| 99 | self.styles.ram_style, |
| 100 | ); |
| 101 | |
| 102 | graph_data( |
| 103 | &mut points, |
| 104 | "SWP", |
| 105 | data.swap_harvest.as_ref(), |
| 106 | time, |
| 107 | &time_series.swap, |
| 108 | self.styles.swap_style, |
| 109 | ); |
| 110 | |
| 111 | #[cfg(not(target_os = "windows"))] |
| 112 | { |
| 113 | graph_data( |
| 114 | &mut points, |
| 115 | "CACHE", // TODO: Figure out how to line this up better |
| 116 | data.cache_harvest.as_ref(), |
| 117 | time, |
| 118 | &time_series.cache_mem, |
| 119 | self.styles.cache_style, |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | #[cfg(feature = "zfs")] |
| 124 | { |
| 125 | graph_data( |
| 126 | &mut points, |
| 127 | "ARC", |
| 128 | data.arc_harvest.as_ref(), |
| 129 | time, |
| 130 | &time_series.arc_mem, |
| 131 | self.styles.arc_style, |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | #[cfg(feature = "gpu")] |
| 136 | { |
| 137 | let mut colour_index = 0; |
| 138 | let gpu_styles = &self.styles.gpu_colours; |
| 139 | let short_gpu_names = app_state.app_config_fields.short_gpu_names; |
| 140 | let gpu_count = data.gpu_harvest.len(); |
| 141 | |
| 142 | for (gpu_index, (name, harvest)) in data.gpu_harvest.iter().enumerate() { |
| 143 | if let Some(gpu_data) = data.time_series_data.gpu_mem.get(name) { |
| 144 | let style = { |
| 145 | if gpu_styles.is_empty() { |
| 146 | Style::default() |
| 147 | } else { |
| 148 | let colour = gpu_styles[colour_index % gpu_styles.len()]; |
| 149 | colour_index += 1; |
| 150 | |
| 151 | colour |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | let display_name = if short_gpu_names { |
| 156 | if gpu_count == 1 { |
| 157 | "GPU".to_string() |
| 158 | } else { |
| 159 | format!("GPU{gpu_index}") |
| 160 | } |
| 161 | } else { |
| 162 | name.clone() |
| 163 | }; |
| 164 | |
| 165 | graph_data( |
| 166 | &mut points, |
| 167 | &display_name, |
| 168 | Some(harvest), |
| 169 | time, |
| 170 | gpu_data, |
| 171 | style, |
| 172 | ); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | points |
| 178 | }; |
| 179 | |
| 180 | let border_style = self.get_border_style(widget_id, app_state.current_widget.widget_id); |
| 181 | let marker = self.get_marker(app_state.app_config_fields.use_dot); |
| 182 | |
| 183 | mem_state.graph.draw( |
| 184 | f, |
| 185 | draw_loc, |
| 186 | GraphDrawCtx { |
| 187 | title: " Memory ".into(), |
| 188 | border_style, |
| 189 | title_style: self.styles.widget_title_style, |
| 190 | graph_style: self.styles.graph_style, |
| 191 | general_widget_style: self.styles.general_widget_style, |
| 192 | border_type: self.styles.border_type, |
| 193 | marker, |
| 194 | hide_x_labels, |
| 195 | is_selected: app_state.current_widget.widget_id == widget_id, |
| 196 | is_expanded: app_state.is_expanded, |
| 197 | legend_position: app_state.app_config_fields.memory_legend_position, |
| 198 | legend_constraints: Some(LegendConstraints { |
| 199 | width: Constraint::Ratio(3, 4), |
| 200 | height: Constraint::Ratio(3, 4), |
| 201 | }), |
| 202 | }, |
| 203 | graph_data, |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | if app_state.should_get_widget_bounds() { |
| 208 | // Update draw loc in widget map |
| 209 | if let Some(widget) = app_state.widget_map.get_mut(&widget_id) { |
| 210 | widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); |
| 211 | widget.bottom_right_corner = |
| 212 | Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |