| 1 | mod auto_y; |
| 2 | mod percent; |
| 3 | |
| 4 | use std::{ |
| 5 | borrow::Cow, |
| 6 | cmp::{max, min}, |
| 7 | time::{Duration, Instant}, |
| 8 | }; |
| 9 | |
| 10 | pub use auto_y::*; |
| 11 | pub use percent::*; |
| 12 | use ratatui::{style::Style, symbols::Marker, widgets::BorderType}; |
| 13 | |
| 14 | use crate::canvas::components::time_series::{ |
| 15 | AxisBound, ChartScaling, GraphData, LegendConstraints, LegendPosition, TimeGraph, |
| 16 | }; |
| 17 | |
| 18 | const STALE_MIN_MILLISECONDS: u64 = Duration::from_secs(30).as_millis() as u64; |
| 19 | |
| 20 | /// Configuration values for a [`TimeseriesState`], sourced from [`crate::app::AppConfigFields`]. |
| 21 | #[derive(Copy, Clone, Debug)] |
| 22 | pub struct TimeseriesConfig { |
| 23 | pub time_interval: u64, |
| 24 | pub retention_ms: u64, |
| 25 | pub autohide_time: bool, |
| 26 | pub default_time_value: u64, |
| 27 | } |
| 28 | |
| 29 | /// A time_series graph widget displays data over a period of time. |
| 30 | pub struct TimeseriesState { |
| 31 | config: TimeseriesConfig, |
| 32 | current_display_time: u64, |
| 33 | autohide_timer: Option<Instant>, |
| 34 | } |
| 35 | |
| 36 | impl TimeseriesState { |
| 37 | /// Create a new [`TimeseriesState`] using the given config and `autohide_timer` setting. |
| 38 | pub fn new(config: TimeseriesConfig, autohide_timer: Option<Instant>) -> Self { |
| 39 | Self { |
| 40 | current_display_time: config.default_time_value, |
| 41 | config, |
| 42 | autohide_timer, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Get a mutable reference to the autohide timer. |
| 47 | pub fn autohide_timer_mut(&mut self) -> &mut Option<Instant> { |
| 48 | &mut self.autohide_timer |
| 49 | } |
| 50 | |
| 51 | /// Get the current display time. |
| 52 | pub(crate) fn current_display_time(&self) -> u64 { |
| 53 | self.current_display_time |
| 54 | } |
| 55 | |
| 56 | /// Zoom in on the x-axis (reducing the time range shown). |
| 57 | pub fn zoom_in(&mut self) { |
| 58 | let new_time = self |
| 59 | .current_display_time |
| 60 | .saturating_sub(self.config.time_interval); |
| 61 | self.current_display_time = max(new_time, STALE_MIN_MILLISECONDS); |
| 62 | self.maybe_start_autohide(); |
| 63 | } |
| 64 | |
| 65 | /// Zoom out on the x-axis (increasing the time range shown). |
| 66 | pub fn zoom_out(&mut self) { |
| 67 | let new_time = self |
| 68 | .current_display_time |
| 69 | .saturating_add(self.config.time_interval); |
| 70 | self.current_display_time = min(new_time, self.config.retention_ms); |
| 71 | self.maybe_start_autohide(); |
| 72 | } |
| 73 | |
| 74 | /// Reset the zoom level to the default. |
| 75 | pub fn reset_zoom(&mut self) { |
| 76 | self.current_display_time = self.config.default_time_value; |
| 77 | self.maybe_start_autohide(); |
| 78 | } |
| 79 | |
| 80 | fn maybe_start_autohide(&mut self) { |
| 81 | if self.config.autohide_time { |
| 82 | self.autohide_timer = Some(Instant::now()); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Per-render context passed to component draw methods. Carries everything that |
| 88 | /// varies each frame but is not persistent state. |
| 89 | pub struct GraphDrawCtx<'a> { |
| 90 | pub title: Cow<'a, str>, |
| 91 | pub border_style: Style, |
| 92 | pub title_style: Style, |
| 93 | pub graph_style: Style, |
| 94 | pub general_widget_style: Style, |
| 95 | pub border_type: BorderType, |
| 96 | pub marker: Marker, |
| 97 | pub hide_x_labels: bool, |
| 98 | pub is_selected: bool, |
| 99 | pub is_expanded: bool, |
| 100 | pub legend_position: Option<LegendPosition>, |
| 101 | pub legend_constraints: Option<LegendConstraints>, |
| 102 | } |
| 103 | |
| 104 | #[cfg(test)] |
| 105 | mod time_series_tests { |
| 106 | use super::*; |
| 107 | |
| 108 | const TEST_CONFIG: TimeseriesConfig = TimeseriesConfig { |
| 109 | time_interval: 15_000, |
| 110 | retention_ms: 300_000, |
| 111 | autohide_time: false, |
| 112 | default_time_value: 60_000, |
| 113 | }; |
| 114 | |
| 115 | fn state_at(display_time: u64, cfg: TimeseriesConfig) -> TimeseriesState { |
| 116 | TimeseriesState { |
| 117 | config: cfg, |
| 118 | current_display_time: display_time, |
| 119 | autohide_timer: None, |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn zoom_in_decreases_display_time() { |
| 125 | let mut state = state_at(60_000, TEST_CONFIG); |
| 126 | state.zoom_in(); |
| 127 | assert_eq!(state.current_display_time, 45_000); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn zoom_in_clamps_at_minimum() { |
| 132 | let mut state = state_at(35_000, TEST_CONFIG); |
| 133 | state.zoom_in(); |
| 134 | assert_eq!(state.current_display_time, STALE_MIN_MILLISECONDS); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn zoom_out_increases_display_time() { |
| 139 | let mut state = state_at(60_000, TEST_CONFIG); |
| 140 | state.zoom_out(); |
| 141 | assert_eq!(state.current_display_time, 75_000); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn zoom_out_clamps_at_retention() { |
| 146 | let mut state = state_at(290_000, TEST_CONFIG); |
| 147 | state.zoom_out(); |
| 148 | assert_eq!(state.current_display_time, 300_000); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn reset_zoom_restores_default() { |
| 153 | let mut state = state_at(120_000, TEST_CONFIG); |
| 154 | state.reset_zoom(); |
| 155 | assert_eq!(state.current_display_time, 60_000); |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn autohide_armed_on_change() { |
| 160 | let mut state = state_at( |
| 161 | 60_000, |
| 162 | TimeseriesConfig { |
| 163 | autohide_time: true, |
| 164 | ..TEST_CONFIG |
| 165 | }, |
| 166 | ); |
| 167 | state.zoom_in(); |
| 168 | assert!(state.autohide_timer.is_some()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #[cfg(test)] |
| 173 | mod graph_height_tests { |
| 174 | use timeless::data::ChunkedData; |
| 175 | |
| 176 | use super::*; |
| 177 | |
| 178 | fn build(times: &[Instant], values: &[f64]) -> ChunkedData<f64> { |
| 179 | assert_eq!(times.len(), values.len()); |
| 180 | let mut data = ChunkedData::default(); |
| 181 | for &v in values { |
| 182 | data.push(v); |
| 183 | } |
| 184 | data |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn picks_max_across_sources() { |
| 189 | let mut cache = GraphHeightCache::default(); |
| 190 | let now = Instant::now(); |
| 191 | let times = vec![ |
| 192 | now - Duration::from_millis(300), |
| 193 | now - Duration::from_millis(200), |
| 194 | now - Duration::from_millis(100), |
| 195 | now, |
| 196 | ]; |
| 197 | let a = build(×, &[1.0, 2.0, 3.0, 4.0]); |
| 198 | let b = build(×, &[10.0, 5.0, 0.5, 0.25]); |
| 199 | |
| 200 | let result = cache.get_or_update(&now, 1_000, [&a, &b].into_iter(), ×); |
| 201 | assert_eq!(result, 10.0); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn cache_hit_skips_older_points() { |
| 206 | // On a cache hit, only points after `right_edge` are rescanned. So if |
| 207 | // we pass a fresh source whose only large values are *older* than the |
| 208 | // previous `last_time`, the cached max should still win. |
| 209 | let mut cache = GraphHeightCache::default(); |
| 210 | let now = Instant::now(); |
| 211 | let times = vec![ |
| 212 | now - Duration::from_millis(200), |
| 213 | now - Duration::from_millis(100), |
| 214 | now, |
| 215 | ]; |
| 216 | let first = build(×, &[3.0, 5.0, 7.0]); |
| 217 | |
| 218 | let first_result = cache.get_or_update(&now, 10_000, [&first].into_iter(), ×); |
| 219 | assert_eq!(first_result, 7.0); |
| 220 | |
| 221 | // Older points carry huge values; newest is small. A full rescan would |
| 222 | // return 1000.0 — a true cache hit returns the cached 7.0. |
| 223 | let second = build(×, &[1000.0, 999.0, 4.0]); |
| 224 | let second_result = cache.get_or_update(&now, 10_000, [&second].into_iter(), ×); |
| 225 | assert_eq!(second_result, 7.0); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn cache_invalidates_on_period_change() { |
| 230 | // First call uses a small window that excludes the older high value. |
| 231 | // Second call uses a larger window that should include it; a stale |
| 232 | // cache would miss it and return the previous max. |
| 233 | let mut cache = GraphHeightCache::default(); |
| 234 | let now = Instant::now(); |
| 235 | let times = vec![ |
| 236 | now - Duration::from_millis(500), |
| 237 | now - Duration::from_millis(50), |
| 238 | now, |
| 239 | ]; |
| 240 | let data = build(×, &[100.0, 5.0, 7.0]); |
| 241 | |
| 242 | let first = cache.get_or_update(&now, 100, [&data].into_iter(), ×); |
| 243 | assert_eq!(first, 7.0); |
| 244 | |
| 245 | let second = cache.get_or_update(&now, 1_000, [&data].into_iter(), ×); |
| 246 | assert_eq!(second, 100.0); |
| 247 | } |
| 248 | } |