kql-panopticon 0.3.0

KQL tooling for Azure Log Analytics - concurrent multi-workspace queries, chained investigations, HTTP enrichment, and automated reports
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
pub mod message;
pub mod model;
pub mod update;
pub mod view;

use crate::client::Client;
use crate::error::Result;
use message::{Message, Tab};
use model::{query::EditorMode, Model};
use ratatui::crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use std::time::Duration;

/// Main TUI entry point
pub async fn run_tui(client: Client) -> Result<()> {
    // Setup terminal
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // Check minimum terminal size
    let size = terminal.size()?;
    if size.width < 80 || size.height < 24 {
        disable_raw_mode()?;
        execute!(
            terminal.backend_mut(),
            LeaveAlternateScreen,
            DisableMouseCapture
        )?;
        return Err(crate::error::KqlPanopticonError::Other(format!(
            "Terminal too small. Minimum size: 80x24, current: {}x{}",
            size.width, size.height
        )));
    }

    // Create model
    let mut model = Model::new(client.clone());

    // Create a channel for initialization messages
    let (init_tx, mut init_rx) = tokio::sync::mpsc::unbounded_channel::<message::Message>();

    // Start async initialization in background
    let init_client = client;
    let tx = init_tx.clone();
    tokio::spawn(async move {
        // Load sessions from disk (no async needed)
        let _ = tx.send(message::Message::SessionsRefresh);

        // Load query packs from disk (no async needed)
        let _ = tx.send(message::Message::PacksRefresh);

        // Load investigation packs from disk (no async needed)
        let _ = tx.send(message::Message::InvestigationsRefresh);

        // Authenticate and load workspaces
        match init_client.force_validate_auth().await {
            Ok(_) => {
                let _ = tx.send(message::Message::AuthCompleted);

                // Now load workspaces
                match init_client.list_workspaces().await {
                    Ok(workspaces) => {
                        let _ = tx.send(message::Message::WorkspacesLoaded(workspaces));
                        let _ = tx.send(message::Message::InitCompleted);
                    }
                    Err(e) => {
                        let _ = tx.send(message::Message::ShowError(format!(
                            "Failed to load workspaces: {}",
                            e
                        )));
                        let _ = tx.send(message::Message::InitCompleted);
                    }
                }
            }
            Err(e) => {
                let _ = tx.send(message::Message::AuthFailed(e.to_string()));
            }
        }
    });

    // Run the application loop with init channel
    let result = run_app(&mut terminal, &mut model, &mut init_rx).await;

    // Restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    result
}

/// Main application loop
async fn run_app(
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
    model: &mut Model,
    init_rx: &mut tokio::sync::mpsc::UnboundedReceiver<Message>,
) -> Result<()> {
    loop {
        // Process any pending job updates
        model.process_job_updates();

        // Process any init messages
        while let Ok(msg) = init_rx.try_recv() {
            // Handle SessionsRefresh specially (like in main loop)
            if matches!(msg, Message::SessionsRefresh) {
                match crate::session::Session::list_all() {
                    Ok(sessions) => {
                        model.sessions.refresh_from_disk(sessions);
                    }
                    Err(e) => {
                        log::error!("Failed to refresh sessions during init: {}", e);
                    }
                }
                continue;
            }

            let new_messages = update::update(model, msg);
            for new_msg in new_messages {
                let _ = update::update(model, new_msg);
            }
        }

        // Increment spinner frame for loading animation
        if model.init_state == model::InitState::Initializing {
            model.spinner_frame = model.spinner_frame.wrapping_add(1);
        }

        terminal.draw(|f| view::ui(f, model))?;

        // Handle events with timeout (50ms for smooth spinner animation)
        if event::poll(Duration::from_millis(50))? {
            match event::read()? {
                Event::Key(key) => {
                    let message = handle_key_event(key.code, key.modifiers, model);

                    // Process the message and any subsequent messages
                    let mut messages_to_process = vec![message];
                    while let Some(msg) = messages_to_process.pop() {
                        // Check for quit
                        if matches!(msg, Message::Quit) {
                            return Ok(());
                        }

                        // Handle workspace refresh (async operation)
                        if matches!(msg, Message::WorkspacesRefresh) {
                            match model.client.list_workspaces().await {
                                Ok(workspaces) => {
                                    messages_to_process.push(Message::WorkspacesLoaded(workspaces));
                                }
                                Err(e) => {
                                    messages_to_process.push(Message::ShowError(format!(
                                        "Failed to refresh workspaces: {}",
                                        e
                                    )));
                                }
                            }
                            continue;
                        }

                        // Handle sessions refresh (load from disk)
                        if matches!(msg, Message::SessionsRefresh) {
                            match crate::session::Session::list_all() {
                                Ok(sessions) => {
                                    model.sessions.refresh_from_disk(sessions);
                                }
                                Err(e) => {
                                    messages_to_process.push(Message::ShowError(format!(
                                        "Failed to refresh sessions: {}",
                                        e
                                    )));
                                }
                            }
                            continue;
                        }

                        // Update model and collect new messages
                        let new_messages = update::update(model, msg);
                        messages_to_process.extend(new_messages);
                    }
                }
                Event::Resize(_width, _height) => {
                    // Terminal was resized, force a redraw on next iteration
                    // The terminal.draw() call will automatically adapt to new size
                }
                _ => {
                    // Ignore other events (mouse, etc.)
                }
            }
        }
    }
}

/// Convert key events into messages
fn handle_key_event(key: KeyCode, modifiers: KeyModifiers, model: &Model) -> Message {
    // Handle popup interactions first
    if let Some(popup) = &model.popup {
        return handle_popup_key(key, popup, model);
    }

    // Check if we're in query edit mode (blocks most global keys)
    let in_query_edit_mode = model.current_tab == Tab::Query
        && (model.query.mode == EditorMode::Insert || model.query.mode == EditorMode::Visual);

    // Handle global keys (only work outside query edit mode and input collection)
    let in_investigation_input = model.investigations.is_collecting_inputs();
    if !in_query_edit_mode && !in_investigation_input {
        match key {
            KeyCode::Char('q') => return Message::Quit,
            KeyCode::Char('r') => {
                if model.current_tab == Tab::Workspaces {
                    return Message::WorkspacesRefresh;
                } else if model.current_tab == Tab::Sessions {
                    return Message::SessionsRefresh;
                } else if model.current_tab == Tab::Investigations {
                    return Message::InvestigationsRefresh;
                }
            }
            KeyCode::Char('1') => return Message::SwitchTab(Tab::Query),
            KeyCode::Char('2') => return Message::SwitchTab(Tab::Packs),
            KeyCode::Char('3') => return Message::SwitchTab(Tab::Investigations),
            KeyCode::Char('4') => return Message::SwitchTab(Tab::Workspaces),
            KeyCode::Char('5') => return Message::SwitchTab(Tab::Settings),
            KeyCode::Char('6') => return Message::SwitchTab(Tab::Jobs),
            KeyCode::Char('7') => return Message::SwitchTab(Tab::Sessions),
            _ => {}
        }
    }

    // Tab key always works
    if key == KeyCode::Tab {
        return Message::SwitchTab(model.current_tab.next());
    }

    // BackTab (Shift+Tab) goes to previous tab
    if key == KeyCode::BackTab {
        return Message::SwitchTab(model.current_tab.previous());
    }

    // Ctrl+J for query execution (works in any mode)
    if modifiers.contains(KeyModifiers::CONTROL)
        && key == KeyCode::Char('j')
        && model.current_tab == Tab::Query
    {
        return Message::QueryStartExecution;
    }

    // Handle tab-specific keys
    match model.current_tab {
        Tab::Settings => handle_settings_key(key),
        Tab::Workspaces => handle_workspaces_key(key),
        Tab::Query => handle_query_key(key, modifiers, model),
        Tab::Jobs => handle_jobs_key(key),
        Tab::Sessions => handle_sessions_key(key, modifiers),
        Tab::Packs => handle_packs_key(key),
        Tab::Investigations => handle_investigations_key(key, modifiers, model),
    }
}

/// Handle key events when a popup is open
fn handle_popup_key(key: KeyCode, popup: &model::Popup, model: &Model) -> Message {
    match popup {
        model::Popup::Error(_) | model::Popup::Success(_) => {
            if matches!(key, KeyCode::Esc | KeyCode::Enter) {
                Message::ClosePopup
            } else {
                Message::NoOp
            }
        }
        model::Popup::SettingsEdit => match key {
            KeyCode::Esc => Message::SettingsCancel,
            KeyCode::Enter => Message::SettingsSave,
            KeyCode::Backspace => Message::SettingsInputBackspace,
            KeyCode::Char(c) => Message::SettingsInputChar(c),
            _ => Message::NoOp,
        },
        model::Popup::JobNameInput => match key {
            KeyCode::Esc => Message::ClosePopup,
            KeyCode::Enter => {
                if let Some(ref job_name) = model.query.job_name_input {
                    if !job_name.trim().is_empty() {
                        return Message::ExecuteQuery(job_name.clone());
                    }
                }
                Message::ClosePopup
            }
            KeyCode::Backspace => Message::JobNameInputBackspace,
            KeyCode::Char(c) => Message::JobNameInputChar(c),
            _ => Message::NoOp,
        },
        model::Popup::SessionNameInput => match key {
            KeyCode::Esc => Message::ClosePopup,
            KeyCode::Enter => {
                if let Some(ref name) = model.sessions.name_input {
                    if !name.trim().is_empty() {
                        return Message::SessionsSave(None);
                    }
                }
                Message::ClosePopup
            }
            KeyCode::Backspace => Message::SessionNameInputBackspace,
            KeyCode::Char(c) => Message::SessionNameInputChar(c),
            _ => Message::NoOp,
        },
        model::Popup::JobDetails(job_idx) => {
            match key {
                KeyCode::Esc | KeyCode::Enter => Message::ClosePopup,
                KeyCode::Char('r') => {
                    // Validate that the job can and should be retried
                    if let Some(job) = model.jobs.jobs.get(*job_idx) {
                        use crate::tui::model::jobs::JobStatus;

                        // Check basic retry eligibility
                        let can_retry =
                            matches!(job.status, JobStatus::Failed | JobStatus::Completed)
                                && job.retry_context.is_some();

                        if !can_retry {
                            return Message::ShowError(
                                "Job cannot be retried (missing context)".to_string(),
                            );
                        }

                        // Check if error type is retryable
                        if let Some(error) = &job.error {
                            if !error.is_retryable() {
                                return Message::ShowError(
                                    "Cannot retry: query syntax error - fix query first"
                                        .to_string(),
                                );
                            }
                        }

                        // Error is retryable - close popup and trigger retry
                        // Note: We can't return Vec<Message> from this function,
                        // so we'll just trigger retry and let the update handler close the popup
                        return Message::JobsRetry;
                    }
                    Message::NoOp
                }
                _ => Message::NoOp,
            }
        }
    }
}

/// Handle key events for the Settings tab
fn handle_settings_key(key: KeyCode) -> Message {
    match key {
        KeyCode::Up => Message::SettingsPrevious,
        KeyCode::Down => Message::SettingsNext,
        KeyCode::Enter | KeyCode::Char(' ') => Message::SettingsStartEdit,
        _ => Message::NoOp,
    }
}

/// Handle key events for the Workspaces tab
fn handle_workspaces_key(key: KeyCode) -> Message {
    match key {
        KeyCode::Up => Message::WorkspacesPrevious,
        KeyCode::Down => Message::WorkspacesNext,
        KeyCode::Char(' ') => Message::WorkspacesToggle,
        KeyCode::Char('a') => Message::WorkspacesSelectAll,
        KeyCode::Char('n') => Message::WorkspacesSelectNone,
        _ => Message::NoOp,
    }
}

/// Handle key events for the Query tab
fn handle_query_key(key: KeyCode, modifiers: KeyModifiers, model: &Model) -> Message {
    // If load panel is open, handle panel-specific keys
    if model.query.load_panel.is_some() {
        match key {
            KeyCode::Esc => return Message::QueryLoadPanelCancel,
            KeyCode::Enter => return Message::QueryLoadPanelConfirm,
            KeyCode::Up => return Message::QueryLoadPanelNavigate(-1),
            KeyCode::Down => return Message::QueryLoadPanelNavigate(1),
            KeyCode::Tab => return Message::QueryLoadPanelCycleSort,
            KeyCode::Char('i') => return Message::QueryLoadPanelInvertSort,
            _ => return Message::NoOp,
        }
    }

    match model.query.mode {
        EditorMode::Normal => {
            // Normal mode - vim-style navigation and commands
            match key {
                KeyCode::Char('i') => Message::QueryEnterInsertMode,
                KeyCode::Char('v') => Message::QueryEnterVisualMode, // Enter visual mode
                KeyCode::Char('a') => Message::QueryAppend,          // Insert after cursor
                KeyCode::Char('A') => Message::QueryAppendEnd,       // Insert at end of line
                KeyCode::Char('o') => Message::QueryOpenBelow,       // Open new line below
                KeyCode::Char('O') => Message::QueryOpenAbove,       // Open new line above
                KeyCode::Char('x') => Message::QueryDeleteChar, // Delete character under cursor
                KeyCode::Char('d') if modifiers.contains(KeyModifiers::CONTROL) => {
                    Message::QueryDeleteLine
                } // Delete line
                KeyCode::Char('u') if modifiers.contains(KeyModifiers::CONTROL) => {
                    Message::QueryUndo
                }
                KeyCode::Char('r') if modifiers.contains(KeyModifiers::CONTROL) => {
                    Message::QueryRedo
                }
                KeyCode::Char('c') => Message::QueryClear, // Clear all text
                KeyCode::Char('l') => Message::QueryOpenLoadPanel, // Load query from job
                KeyCode::Char('[') => Message::QueryPrevPackQuery, // Previous query in pack
                KeyCode::Char(']') => Message::QueryNextPackQuery, // Next query in pack
                // Navigation in normal mode
                KeyCode::Char('h') | KeyCode::Left => Message::QueryMoveCursor(KeyCode::Left),
                KeyCode::Char('j') | KeyCode::Down => Message::QueryMoveCursor(KeyCode::Down),
                KeyCode::Char('k') | KeyCode::Up => Message::QueryMoveCursor(KeyCode::Up),
                KeyCode::Right => Message::QueryMoveCursor(KeyCode::Right),
                KeyCode::Char('0') => Message::QueryMoveCursor(KeyCode::Home),
                KeyCode::Char('$') => Message::QueryMoveCursor(KeyCode::End),
                KeyCode::Char('g') => Message::QueryMoveTop,
                KeyCode::Char('G') => Message::QueryMoveBottom,
                _ => Message::NoOp,
            }
        }
        EditorMode::Insert => {
            // Insert mode - pass most keys to tui-textarea
            match key {
                KeyCode::Esc => Message::QueryExitInsertMode,
                _ => Message::QueryInput(ratatui::crossterm::event::KeyEvent::new(key, modifiers)),
            }
        }
        EditorMode::Visual => {
            // Visual mode - text selection
            match key {
                KeyCode::Esc => Message::QueryExitVisualMode,
                KeyCode::Char('y') => Message::QueryYank, // Copy selected text
                KeyCode::Char('d') | KeyCode::Char('x') => Message::QueryDeleteSelection, // Delete selection
                // Navigation extends selection
                KeyCode::Char('h') | KeyCode::Left => Message::QueryMoveCursor(KeyCode::Left),
                KeyCode::Char('j') | KeyCode::Down => Message::QueryMoveCursor(KeyCode::Down),
                KeyCode::Char('k') | KeyCode::Up => Message::QueryMoveCursor(KeyCode::Up),
                KeyCode::Char('l') | KeyCode::Right => Message::QueryMoveCursor(KeyCode::Right),
                KeyCode::Char('0') => Message::QueryMoveCursor(KeyCode::Home),
                KeyCode::Char('$') => Message::QueryMoveCursor(KeyCode::End),
                KeyCode::Char('g') => Message::QueryMoveTop,
                KeyCode::Char('G') => Message::QueryMoveBottom,
                _ => Message::NoOp,
            }
        }
    }
}

/// Handle key events for the Jobs tab
fn handle_jobs_key(key: KeyCode) -> Message {
    match key {
        KeyCode::Up => Message::JobsPrevious,
        KeyCode::Down => Message::JobsNext,
        KeyCode::Enter => Message::JobsViewDetails,
        KeyCode::Char('c') => Message::JobsClearCompleted,
        KeyCode::Char('r') => Message::JobsRetry,
        _ => Message::NoOp,
    }
}

/// Handle key events for the Sessions tab
fn handle_sessions_key(key: KeyCode, modifiers: KeyModifiers) -> Message {
    match key {
        KeyCode::Up => Message::SessionsPrevious,
        KeyCode::Down => Message::SessionsNext,
        KeyCode::Char('n') => Message::SessionsStartNew,
        KeyCode::Char('s') => {
            // 's' = save current session
            // 'S' (shift+s) = save as new name
            if modifiers.contains(KeyModifiers::SHIFT) {
                Message::SessionsStartNew
            } else {
                Message::SessionsSave(None)
            }
        }
        KeyCode::Char('l') => Message::SessionsLoad,
        KeyCode::Char('d') => Message::SessionsDelete,
        KeyCode::Char('p') => Message::SessionExportAsPack,
        _ => Message::NoOp,
    }
}

/// Handle key events for the Packs tab
fn handle_packs_key(key: KeyCode) -> Message {
    match key {
        KeyCode::Up => Message::PacksPrevious,
        KeyCode::Down => Message::PacksNext,
        KeyCode::Char('r') => Message::PacksRefresh,
        KeyCode::Enter => Message::PacksLoadQuery,
        KeyCode::Char('e') => Message::PacksExecute,
        KeyCode::Char('s') => Message::PacksSave,
        _ => Message::NoOp,
    }
}

/// Handle key events for the Investigations tab
fn handle_investigations_key(key: KeyCode, _modifiers: KeyModifiers, model: &Model) -> Message {
    // If collecting inputs, handle input-specific keys
    if model.investigations.is_collecting_inputs() {
        match key {
            KeyCode::Esc => Message::InvestigationsInputCancel,
            KeyCode::Enter => Message::InvestigationsInputConfirm,
            KeyCode::Tab | KeyCode::Down => Message::InvestigationsInputNext,
            KeyCode::BackTab | KeyCode::Up => Message::InvestigationsInputPrev,
            KeyCode::Backspace => Message::InvestigationsInputBackspace,
            KeyCode::Char(c) => Message::InvestigationsInputChar(c),
            _ => Message::NoOp,
        }
    } else {
        // Normal investigation browsing
        match key {
            KeyCode::Up => Message::InvestigationsPrevious,
            KeyCode::Down => Message::InvestigationsNext,
            KeyCode::Char('r') => Message::InvestigationsRefresh,
            KeyCode::Enter => Message::InvestigationsLoadDetails,
            KeyCode::Char('e') => Message::InvestigationsStartExecution,
            _ => Message::NoOp,
        }
    }
}