liveplot 1.0.0

Realtime interactive plotting library using egui/eframe, with optional gRPC and Parquet export support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Data ingestion and trace bookkeeping for the multi-trace oscilloscope.
//!
//! This module hosts the non-UI pieces of `LivePlotApp`:
//! - receiving samples and maintaining per-trace buffers
//! - creating traces on first sighting and assigning distinct colors
//! - pruning by time window to keep memory bounded
//! - publishing snapshots to external controllers
//! - convenience setters for fixed datasets

use egui::Color32;
use std::collections::VecDeque;
use std::time::Duration;

use crate::controllers::{TraceInfo, TracesInfo};
use crate::sink::PlotCommand;

use super::trace_look::TraceLook;
use super::types::TraceState;
use super::LivePlotApp;

impl LivePlotApp {
    /// Directly set/replace the sample buffer for a named trace.
    ///
    /// The provided points are absolute `[t_seconds, value]` pairs. The trace is created if
    /// absent, a snapshot is taken, the app is paused, and auto-fit for both axes is requested.
    pub fn set_trace_data<S: Into<String>>(&mut self, name: S, points: Vec<[f64; 2]>) {
        let name = name.into();
        // Create trace if missing
        let is_new = !self.traces.contains_key(&name);
        let idx = self.trace_order.len();
        let entry = self.traces.entry(name.clone()).or_insert_with(|| {
            self.trace_order.push(name.clone());
            let mut look = TraceLook::default();
            look.color = Self::alloc_color(idx);
            TraceState {
                name: name.clone(),
                look,
                offset: 0.0,
                live: VecDeque::new(),
                snap: None,
                last_fft: None,
                is_math: false,
                info: String::new(),
            }
        });
        // Replace buffers
        entry.live = points.iter().copied().collect();
        entry.snap = Some(entry.live.clone());
        // Select first trace if none selected yet
        if is_new && self.selection_trace.is_none() {
            self.selection_trace = Some(name);
        }
        // Show fixed data without scrolling/pruning
        self.paused = true;
        // Request auto-fit to the provided data
        self.pending_auto_x = true;
        self.pending_auto_y = true;
    }

    /// Convenience: set/replace multiple traces at once.
    pub fn set_traces_data(&mut self, data: Vec<(String, Vec<[f64; 2]>)>) {
        for (name, pts) in data {
            self.set_trace_data(name, pts);
        }
        self.paused = true;
        self.pending_auto_x = true;
        self.pending_auto_y = true;
    }

    /// Drain incoming commands and append to per-trace buffers. Create traces on registration.
    pub(super) fn drain_rx_and_update_traces(&mut self) {
        while let Ok(cmd) = self.rx.try_recv() {
            match cmd {
                PlotCommand::RegisterTrace { id, name, info } => {
                    // Map ID to name for future lookups
                    self.id_to_name.insert(id, name.clone());
                    // Create trace state if missing
                    let is_new = !self.traces.contains_key(&name);
                    let entry = self.traces.entry(name.clone()).or_insert_with(|| {
                        let idx = self.trace_order.len();
                        self.trace_order.push(name.clone());
                        let mut look = TraceLook::default();
                        look.color = Self::alloc_color(idx);
                        TraceState {
                            name: name.clone(),
                            look,
                            offset: 0.0,
                            live: VecDeque::new(),
                            snap: None,
                            last_fft: None,
                            is_math: false,
                            info: String::new(),
                        }
                    });
                    if let Some(inf) = info.as_ref() {
                        entry.info = inf.clone();
                    }
                    if is_new && self.selection_trace.is_none() {
                        self.selection_trace = Some(name);
                    }
                }
                PlotCommand::Point { trace_id, point } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        let entry = self.traces.entry(name.clone()).or_insert_with(|| {
                            let idx = self.trace_order.len();
                            self.trace_order.push(name.clone());
                            let mut look = TraceLook::default();
                            look.color = Self::alloc_color(idx);
                            TraceState {
                                name: name.clone(),
                                look,
                                offset: 0.0,
                                live: VecDeque::new(),
                                snap: None,
                                last_fft: None,
                                is_math: false,
                                info: String::new(),
                            }
                        });
                        entry.live.push_back([point.x, point.y]);
                        if entry.live.len() > self.max_points {
                            entry.live.pop_front();
                        }
                    } else {
                        // If not registered, synthesize a name and register implicitly
                        let name = format!("trace-{}", trace_id);
                        self.id_to_name.insert(trace_id, name.clone());
                        let idx = self.trace_order.len();
                        self.trace_order.push(name.clone());
                        let mut look = TraceLook::default();
                        look.color = Self::alloc_color(idx);
                        let mut state = TraceState {
                            name: name.clone(),
                            look,
                            offset: 0.0,
                            live: VecDeque::new(),
                            snap: None,
                            last_fft: None,
                            is_math: false,
                            info: String::new(),
                        };
                        state.live.push_back([point.x, point.y]);
                        self.traces.insert(name.clone(), state);
                        if self.selection_trace.is_none() {
                            self.selection_trace = Some(name);
                        }
                    }
                }
                PlotCommand::Points { trace_id, points } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        let entry = self.traces.entry(name.clone()).or_insert_with(|| {
                            let idx = self.trace_order.len();
                            self.trace_order.push(name.clone());
                            let mut look = TraceLook::default();
                            look.color = Self::alloc_color(idx);
                            TraceState {
                                name: name.clone(),
                                look,
                                offset: 0.0,
                                live: VecDeque::new(),
                                snap: None,
                                last_fft: None,
                                is_math: false,
                                info: String::new(),
                            }
                        });
                        for p in points {
                            entry.live.push_back([p.x, p.y]);
                            if entry.live.len() > self.max_points {
                                entry.live.pop_front();
                            }
                        }
                    } else {
                        // Implicit registration path
                        let name = format!("trace-{}", trace_id);
                        self.id_to_name.insert(trace_id, name.clone());
                        let idx = self.trace_order.len();
                        self.trace_order.push(name.clone());
                        let mut look = TraceLook::default();
                        look.color = Self::alloc_color(idx);
                        let mut state = TraceState {
                            name: name.clone(),
                            look,
                            offset: 0.0,
                            live: VecDeque::new(),
                            snap: None,
                            last_fft: None,
                            is_math: false,
                            info: String::new(),
                        };
                        for p in points {
                            state.live.push_back([p.x, p.y]);
                            if state.live.len() > self.max_points {
                                state.live.pop_front();
                            }
                        }
                        self.traces.insert(name.clone(), state);
                        if self.selection_trace.is_none() {
                            self.selection_trace = Some(name);
                        }
                    }
                }
                PlotCommand::SetPointsY {
                    trace_id,
                    mut xs,
                    y,
                } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        if let Some(entry) = self.traces.get_mut(&name) {
                            // Sort X list for binary search membership checks
                            xs.sort_by(|a, b| {
                                a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
                            });
                            for p in entry.live.iter_mut() {
                                if xs
                                    .binary_search_by(|probe| probe.partial_cmp(&p[0]).unwrap())
                                    .is_ok()
                                {
                                    p[1] = y;
                                }
                            }
                        }
                    }
                }
                PlotCommand::DeletePointsX { trace_id, mut xs } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        if let Some(entry) = self.traces.get_mut(&name) {
                            xs.sort_by(|a, b| {
                                a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
                            });
                            entry.live.retain(|p| {
                                xs.binary_search_by(|probe| probe.partial_cmp(&p[0]).unwrap())
                                    .is_err()
                            });
                        }
                    }
                }
                PlotCommand::DeleteXRange {
                    trace_id,
                    x_min,
                    x_max,
                } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        if let Some(entry) = self.traces.get_mut(&name) {
                            // If either bound is NaN, interpret as start/end of the data vector.
                            if entry.live.is_empty() {
                                continue;
                            }
                            let min_x = entry.live.front().map(|p| p[0]).unwrap();
                            let max_x = entry.live.back().map(|p| p[0]).unwrap();
                            let lo_in = if x_min.is_nan() { min_x } else { x_min };
                            let hi_in = if x_max.is_nan() { max_x } else { x_max };
                            let (lo, hi) = if lo_in <= hi_in {
                                (lo_in, hi_in)
                            } else {
                                (hi_in, lo_in)
                            };
                            entry.live.retain(|p| p[0] < lo || p[0] > hi);
                        }
                    }
                }
                PlotCommand::ApplyYFnAtX {
                    trace_id,
                    mut xs,
                    f,
                } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        if let Some(entry) = self.traces.get_mut(&name) {
                            xs.sort_by(|a, b| {
                                a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
                            });
                            for p in entry.live.iter_mut() {
                                if xs
                                    .binary_search_by(|probe| probe.partial_cmp(&p[0]).unwrap())
                                    .is_ok()
                                {
                                    p[1] = f(p[1]);
                                }
                            }
                        }
                    }
                }
                PlotCommand::ApplyYFnInXRange {
                    trace_id,
                    x_min,
                    x_max,
                    f,
                } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        if let Some(entry) = self.traces.get_mut(&name) {
                            if entry.live.is_empty() {
                                continue;
                            }
                            let min_x = entry.live.front().map(|p| p[0]).unwrap();
                            let max_x = entry.live.back().map(|p| p[0]).unwrap();
                            let lo_in = if x_min.is_nan() { min_x } else { x_min };
                            let hi_in = if x_max.is_nan() { max_x } else { x_max };
                            let (lo, hi) = if lo_in <= hi_in {
                                (lo_in, hi_in)
                            } else {
                                (hi_in, lo_in)
                            };
                            for p in entry.live.iter_mut() {
                                if p[0] >= lo && p[0] <= hi {
                                    p[1] = f(p[1]);
                                }
                            }
                        }
                    }
                }
                PlotCommand::ClearData { trace_id } => {
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        if let Some(entry) = self.traces.get_mut(&name) {
                            entry.live.clear();
                            if let Some(s) = &mut entry.snap {
                                s.clear();
                            }
                        }
                    }
                }
                PlotCommand::SetData { trace_id, points } => {
                    // Replace the entire buffer for this trace with the provided points.
                    if let Some(name) = self.id_to_name.get(&trace_id).cloned() {
                        // Ensure the trace exists
                        let idx = self.trace_order.len();
                        let entry = self.traces.entry(name.clone()).or_insert_with(|| {
                            self.trace_order.push(name.clone());
                            let mut look = TraceLook::default();
                            look.color = Self::alloc_color(idx);
                            TraceState {
                                name: name.clone(),
                                look,
                                offset: 0.0,
                                live: VecDeque::new(),
                                snap: None,
                                last_fft: None,
                                is_math: false,
                                info: String::new(),
                            }
                        });
                        entry.live.clear();
                        entry.live.extend(points.iter().map(|p| [p.x, p.y]));
                        entry.snap = Some(entry.live.clone());
                        // Show fixed data without scrolling/pruning
                        self.paused = true;
                        // Request auto-fit to the provided data
                        self.pending_auto_x = true;
                        self.pending_auto_y = true;
                        if self.selection_trace.is_none() {
                            self.selection_trace = Some(name);
                        }
                    } else {
                        // Implicit registration path: synthesize a name and register the id
                        let name = format!("trace-{}", trace_id);
                        self.id_to_name.insert(trace_id, name.clone());
                        let idx = self.trace_order.len();
                        self.trace_order.push(name.clone());
                        let mut look = TraceLook::default();
                        look.color = Self::alloc_color(idx);
                        let mut state = TraceState {
                            name: name.clone(),
                            look,
                            offset: 0.0,
                            live: VecDeque::new(),
                            snap: None,
                            last_fft: None,
                            is_math: false,
                            info: String::new(),
                        };
                        state.live.extend(points.iter().map(|p| [p.x, p.y]));
                        state.snap = Some(state.live.clone());
                        self.traces.insert(name.clone(), state);
                        if self.selection_trace.is_none() {
                            self.selection_trace = Some(name);
                        }
                        self.paused = true;
                        self.pending_auto_x = true;
                        self.pending_auto_y = true;
                    }
                }
            }
        }
    }

    /// Prune each live buffer by a margin beyond the visible window to cap memory.
    pub(super) fn prune_by_time_window(&mut self) {
        if self.last_prune.elapsed() > Duration::from_millis(200) {
            for (_k, tr) in self.traces.iter_mut() {
                if let Some((&[t_latest, _], _)) = tr.live.back().map(|x| (x, ())) {
                    let cutoff = t_latest - self.time_window * 1.15;
                    while let Some(&[t, _]) = tr.live.front() {
                        if t < cutoff {
                            tr.live.pop_front();
                        } else {
                            break;
                        }
                    }
                }
            }
            self.last_prune = std::time::Instant::now();
        }
    }

    /// Compute latest overall time across traces respecting paused state.
    pub(super) fn latest_time_overall(&self) -> Option<f64> {
        let mut t_latest_overall = f64::NEG_INFINITY;
        for name in self.trace_order.iter() {
            if let Some(tr) = self.traces.get(name) {
                let last_t = if self.paused {
                    tr.snap.as_ref().and_then(|s| s.back()).map(|p| p[0])
                } else {
                    tr.live.back().map(|p| p[0])
                };
                if let Some(t) = last_t {
                    if t > t_latest_overall {
                        t_latest_overall = t;
                    }
                }
            }
        }
        if t_latest_overall.is_finite() {
            Some(t_latest_overall)
        } else {
            None
        }
    }

    /// Apply trace controller requests and publish snapshot to listeners.
    pub(super) fn apply_traces_controller_requests_and_publish(&mut self) {
        if let Some(ctrl) = &self.traces_controller {
            // Apply incoming requests first
            {
                let mut inner = ctrl.inner.lock().unwrap();
                for (name, rgb) in inner.color_requests.drain(..) {
                    if let Some(tr) = self.traces.get_mut(&name) {
                        tr.look.color = Color32::from_rgb(rgb[0], rgb[1], rgb[2]);
                    }
                }
                for (name, vis) in inner.visible_requests.drain(..) {
                    if let Some(tr) = self.traces.get_mut(&name) {
                        tr.look.visible = vis;
                    }
                }
                for (name, off) in inner.offset_requests.drain(..) {
                    if let Some(tr) = self.traces.get_mut(&name) {
                        tr.offset = off;
                    }
                }
                if let Some(sel) = inner.selection_request.take() {
                    self.selection_trace = sel;
                }
                if let Some(unit_opt) = inner.y_unit_request.take() {
                    self.y_unit = unit_opt;
                }
                if let Some(ylog) = inner.y_log_request.take() {
                    self.y_log = ylog;
                }
            }
            // Publish snapshot
            let traces: Vec<TraceInfo> = self
                .trace_order
                .iter()
                .filter_map(|n| {
                    self.traces.get(n).map(|tr| TraceInfo {
                        name: tr.name.clone(),
                        color_rgb: [tr.look.color.r(), tr.look.color.g(), tr.look.color.b()],
                        visible: tr.look.visible,
                        is_math: tr.is_math,
                        offset: tr.offset,
                    })
                })
                .collect();
            let info = TracesInfo {
                traces,
                marker_selection: self.selection_trace.clone(),
                y_unit: self.y_unit.clone(),
                y_log: self.y_log,
            };
            let mut inner = ctrl.inner.lock().unwrap();
            inner.listeners.retain(|s| s.send(info.clone()).is_ok());
        }
    }

    /// Allocate a visually distinct color for a given trace index.
    pub(super) fn alloc_color(index: usize) -> Color32 {
        // Simple distinct color palette
        const PALETTE: [Color32; 10] = [
            Color32::LIGHT_BLUE,
            Color32::LIGHT_RED,
            Color32::LIGHT_GREEN,
            Color32::GOLD,
            Color32::from_rgb(0xAA, 0x55, 0xFF), // purple
            Color32::from_rgb(0xFF, 0xAA, 0x00), // orange
            Color32::from_rgb(0x00, 0xDD, 0xDD), // cyan
            Color32::from_rgb(0xDD, 0x00, 0xDD), // magenta
            Color32::from_rgb(0x66, 0xCC, 0x66), // green2
            Color32::from_rgb(0xCC, 0x66, 0x66), // red2
        ];
        PALETTE[index % PALETTE.len()]
    }
}