channels-console 0.3.3

Real-time monitoring, metrics and logs for Rust channels.
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
use channels_console::{ChannelLogs, LogEntry, SerializableChannelStats};
use clap::Parser;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
use eyre::Result;
use ratatui::{
    layout::{Constraint, Layout},
    widgets::TableState,
    DefaultTerminal, Frame,
};
use std::time::{Duration, Instant};
use std::{collections::HashMap, io};

use super::http::{fetch_logs, fetch_metrics};
use super::views::bottom_bar::render_bottom_bar;
use super::views::main_view::render_main_view;
use super::views::top_bar::render_top_bar;

/// Represents which UI component has focus
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Focus {
    Channels,
    Logs,
    Inspect,
}

/// Cached logs with a lookup map for received entries
pub(crate) struct CachedLogs {
    pub(crate) logs: ChannelLogs,
    pub(crate) received_map: HashMap<u64, LogEntry>,
}

#[derive(Debug, Parser)]
pub struct ConsoleArgs {
    /// Port for the metrics server
    #[arg(long, default_value = "6770")]
    pub metrics_port: u16,
}

pub(crate) struct App {
    stats: Vec<SerializableChannelStats>,
    error: Option<String>,
    exit: bool,
    last_refresh: Instant,
    last_successful_fetch: Option<Instant>,
    metrics_port: u16,
    last_render_duration: Duration,
    table_state: TableState,
    logs_table_state: TableState,
    focus: Focus,
    show_logs: bool,
    logs: Option<CachedLogs>,
    paused: bool,
    inspected_log: Option<LogEntry>,
    agent: ureq::Agent,
    current_elapsed_ns: u64,
}

impl ConsoleArgs {
    pub fn run(&self) -> Result<()> {
        let config = ureq::Agent::config_builder()
            .timeout_connect(Some(Duration::from_millis(2000)))
            .timeout_recv_body(Some(Duration::from_millis(1500)))
            .build();

        let agent: ureq::Agent = config.into();

        let mut app = App {
            stats: Vec::new(),
            error: None,
            exit: false,
            last_refresh: Instant::now(),
            last_successful_fetch: None,
            metrics_port: self.metrics_port,
            last_render_duration: Duration::from_millis(0),
            table_state: TableState::default().with_selected(0),
            logs_table_state: TableState::default(),
            focus: Focus::Channels,
            show_logs: false,
            logs: None,
            paused: false,
            inspected_log: None,
            agent,
            current_elapsed_ns: 0,
        };

        let mut terminal = ratatui::init();
        let app_result = app.run(&mut terminal);
        ratatui::restore();
        app_result.map_err(|e| eyre::eyre!("TUI error: {}", e))
    }
}

impl App {
    pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
        let refresh_interval = std::env::var("CHANNELS_CONSOLE_TUI_REFRESH_MS")
            .ok()
            .and_then(|s| s.parse::<u64>().ok())
            .map(Duration::from_millis)
            .unwrap_or(Duration::from_millis(200));

        self.refresh_data();

        while !self.exit {
            if !self.paused && self.last_refresh.elapsed() >= refresh_interval {
                self.refresh_data();
            }

            let render_start = Instant::now();
            terminal.draw(|frame| self.draw(frame))?;
            self.last_render_duration = render_start.elapsed();

            self.handle_events()?;
        }
        Ok(())
    }

    fn refresh_data(&mut self) {
        let selected_channel_id = self
            .table_state
            .selected()
            .and_then(|idx| self.stats.get(idx))
            .map(|stat| stat.id);

        match fetch_metrics(&self.agent, self.metrics_port) {
            Ok(metrics) => {
                self.current_elapsed_ns = metrics.current_elapsed_ns;
                self.stats = metrics.stats;
                self.error = None;
                self.last_successful_fetch = Some(Instant::now());

                // Try to restore selection to the same channel ID
                if let Some(channel_id) = selected_channel_id {
                    // Find the new index of the previously selected channel
                    if let Some(new_idx) = self.stats.iter().position(|stat| stat.id == channel_id)
                    {
                        self.table_state.select(Some(new_idx));
                    } else {
                        // Channel no longer exists, select the last one if available
                        if !self.stats.is_empty() {
                            self.table_state.select(Some(self.stats.len() - 1));
                        }
                    }
                } else if let Some(selected) = self.table_state.selected() {
                    if selected >= self.stats.len() && !self.stats.is_empty() {
                        self.table_state.select(Some(self.stats.len() - 1));
                    }
                }

                if self.show_logs {
                    self.refresh_logs();
                }
            }
            Err(e) => {
                self.error = Some(format!("Failed to fetch metrics: {}", e));
            }
        }
        self.last_refresh = Instant::now();
    }

    fn draw(&mut self, frame: &mut Frame) {
        self.render_ui(frame);
    }

    fn handle_events(&mut self) -> io::Result<()> {
        if event::poll(Duration::from_millis(100))? {
            if let Event::Key(key_event) = event::read()? {
                if key_event.kind == KeyEventKind::Press {
                    self.handle_key_event(key_event);
                }
            }
        }
        Ok(())
    }

    fn handle_key_event(&mut self, key_event: KeyEvent) {
        match key_event.code {
            KeyCode::Char('q') | KeyCode::Char('Q') => self.exit(),
            KeyCode::Char('o') | KeyCode::Char('O') => match self.focus {
                Focus::Inspect => self.close_inspect_and_refocus_channels(),
                Focus::Logs => self.hide_logs(),
                Focus::Channels => self.toggle_logs(),
            },
            KeyCode::Char('p') | KeyCode::Char('P') => self.toggle_pause(),
            KeyCode::Left | KeyCode::Char('h') | KeyCode::Char('H') => {
                if self.focus == Focus::Inspect {
                    self.close_inspect_only();
                } else {
                    self.focus_channels();
                }
            }
            KeyCode::Right | KeyCode::Char('l') => self.focus_logs(),
            KeyCode::Char('i') | KeyCode::Char('I') => self.toggle_inspect(),
            KeyCode::Up | KeyCode::Char('k') => match self.focus {
                Focus::Channels => self.select_previous_channel(),
                Focus::Logs | Focus::Inspect => self.select_previous_log(),
            },
            KeyCode::Down | KeyCode::Char('j') => match self.focus {
                Focus::Channels => self.select_next_channel(),
                Focus::Logs | Focus::Inspect => self.select_next_log(),
            },
            _ => {}
        }
    }

    fn select_previous_channel(&mut self) {
        if !self.stats.is_empty() {
            let i = match self.table_state.selected() {
                Some(i) => i.saturating_sub(1),
                None => 0,
            };
            self.table_state.select(Some(i));

            if self.paused && self.show_logs {
                self.logs = None;
            } else if self.show_logs {
                self.refresh_logs();
            }
        }
    }

    fn select_next_channel(&mut self) {
        if !self.stats.is_empty() {
            let i = match self.table_state.selected() {
                Some(i) => (i + 1).min(self.stats.len() - 1),
                None => 0,
            };
            self.table_state.select(Some(i));

            if self.paused && self.show_logs {
                self.logs = None;
            } else if self.show_logs {
                self.refresh_logs();
            }
        }
    }

    fn toggle_logs(&mut self) {
        let has_valid_selection = self
            .table_state
            .selected()
            .map(|i| i < self.stats.len())
            .unwrap_or(false);

        if !self.stats.is_empty() && has_valid_selection {
            if self.show_logs {
                self.hide_logs();
            } else {
                self.show_logs = true;
                if self.paused {
                    self.logs = None;
                } else {
                    self.refresh_logs();
                }
            }
        }
    }

    fn hide_logs(&mut self) {
        self.show_logs = false;
        self.logs = None;
        self.logs_table_state.select(None);
        self.focus = Focus::Channels;
    }

    fn refresh_logs(&mut self) {
        if self.paused {
            return;
        }

        self.logs = None;

        if let Some(selected) = self.table_state.selected() {
            if !self.stats.is_empty() && selected < self.stats.len() {
                let channel_id = self.stats[selected].id;
                if let Ok(logs) = fetch_logs(&self.agent, self.metrics_port, channel_id) {
                    let received_map: std::collections::HashMap<u64, LogEntry> = logs
                        .received_logs
                        .iter()
                        .map(|entry| (entry.index, entry.clone()))
                        .collect();

                    self.logs = Some(CachedLogs { logs, received_map });

                    // Ensure logs table selection is valid
                    if let Some(ref cached_logs) = self.logs {
                        let log_count = cached_logs.logs.sent_logs.len();
                        if let Some(selected) = self.logs_table_state.selected() {
                            if selected >= log_count && log_count > 0 {
                                self.logs_table_state.select(Some(log_count - 1));
                            }
                        }
                    }
                }
            }
        }
    }

    fn toggle_pause(&mut self) {
        self.paused = !self.paused;
    }

    fn focus_channels(&mut self) {
        self.focus = Focus::Channels;
        // Clear logs table selection when not focused
        self.logs_table_state.select(None);
    }

    fn focus_logs(&mut self) {
        if !self.show_logs {
            self.toggle_logs();
        } else if !self.stats.is_empty() {
            if let Some(ref cached_logs) = self.logs {
                if !cached_logs.logs.sent_logs.is_empty() {
                    self.focus = Focus::Logs;
                    if self.logs_table_state.selected().is_none() {
                        self.logs_table_state.select(Some(0));
                    }
                }
            }
        }
    }

    fn select_previous_log(&mut self) {
        if let Some(ref cached_logs) = self.logs {
            let log_count = cached_logs.logs.sent_logs.len();
            if log_count > 0 {
                let i = match self.logs_table_state.selected() {
                    Some(i) => i.saturating_sub(1),
                    None => 0,
                };
                self.logs_table_state.select(Some(i));

                // Update inspected log if inspect popup is open
                if self.focus == Focus::Inspect {
                    if let Some(entry) = cached_logs.logs.sent_logs.get(i) {
                        self.inspected_log = Some(entry.clone());
                    }
                }
            }
        }
    }

    fn select_next_log(&mut self) {
        if let Some(ref cached_logs) = self.logs {
            let log_count = cached_logs.logs.sent_logs.len();
            if log_count > 0 {
                let i = match self.logs_table_state.selected() {
                    Some(i) => (i + 1).min(log_count - 1),
                    None => 0,
                };
                self.logs_table_state.select(Some(i));

                // Update inspected log if inspect popup is open
                if self.focus == Focus::Inspect {
                    if let Some(entry) = cached_logs.logs.sent_logs.get(i) {
                        self.inspected_log = Some(entry.clone());
                    }
                }
            }
        }
    }

    fn toggle_inspect(&mut self) {
        if self.focus == Focus::Inspect {
            // Closing inspect popup
            self.focus = Focus::Logs;
            self.inspected_log = None;
        } else if self.focus == Focus::Logs && self.logs_table_state.selected().is_some() {
            // Opening inspect popup - capture the current log entry
            if let Some(selected) = self.logs_table_state.selected() {
                if let Some(ref cached_logs) = self.logs {
                    if let Some(entry) = cached_logs.logs.sent_logs.get(selected) {
                        self.inspected_log = Some(entry.clone());
                        self.focus = Focus::Inspect;
                    }
                }
            }
        }
    }

    fn close_inspect_and_refocus_channels(&mut self) {
        self.inspected_log = None;
        self.hide_logs();
    }

    fn close_inspect_only(&mut self) {
        self.inspected_log = None;
        self.focus = Focus::Channels;
        self.logs_table_state.select(None);
    }

    fn exit(&mut self) {
        self.exit = true;
    }
}

impl App {
    fn render_ui(&mut self, frame: &mut Frame) {
        let area = frame.area();

        // Create 3-row vertical layout: top bar, main view, bottom bar
        let chunks = Layout::default()
            .direction(ratatui::layout::Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Top bar
                Constraint::Min(0),    // Main view (fills remaining space)
                Constraint::Length(3), // Bottom bar
            ])
            .split(area);

        // Render top status bar
        render_top_bar(
            frame,
            chunks[0],
            self.paused,
            self.last_successful_fetch,
            self.error.is_some(),
            !self.stats.is_empty(),
        );

        // Render main content area
        render_main_view(
            frame,
            chunks[1],
            &self.stats,
            &self.error,
            self.metrics_port,
            &mut self.table_state,
            &mut self.logs_table_state,
            self.focus,
            self.show_logs,
            &self.logs,
            self.paused,
            &self.inspected_log,
            self.current_elapsed_ns,
        );

        render_bottom_bar(frame, chunks[2], self.focus, self.last_render_duration);
    }
}