Skip to main content

taba_cli/tui/screens/topics/
preview.rs

1//! The strip of charts under the list of topics: the three charts of the selected queue side by side, fed by the
2//! same reading as the charts dialog (`GET /stats/topics/{name}`), once per sampling interval, only while the list
3//! is what is shown. What was sampled of the topics selected lately is kept, so coming back to one does not start
4//! its charts over.
5
6use crate::tui::model::{Model, TopicMonitor, TopicTab};
7
8use super::selected;
9
10/// How many topics keep their samples.
11pub const MAX: usize = 8;
12
13/// The strip of the selected topic; none for a distributor, which has no queue, or when nothing is selected.
14pub fn current(m: &Model) -> Option<&TopicMonitor> {
15    let topic = selected(m)?;
16    if topic.is_distributor() {
17        return None;
18    }
19    m.topics.previews.iter().find(|p| p.name == topic.name)
20}
21
22/// Makes sure the selected topic has its strip, and that it is the most recent one.
23pub fn prepare(m: &mut Model) {
24    let Some(topic) = selected(m) else { return };
25    if topic.is_distributor() {
26        return;
27    }
28    let previews = &mut m.topics.previews;
29    match previews.iter().position(|p| p.name == topic.name) {
30        Some(at) => {
31            let kept = previews.remove(at);
32            previews.push(kept);
33        }
34        None => previews.push(TopicMonitor::new(
35            topic.name,
36            TopicTab::Charts,
37            false,
38            topic.dead_letter,
39        )),
40    }
41    if previews.len() > MAX {
42        previews.remove(0);
43    }
44}