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
use crate::tui::model::investigations::{InvestigationStatus, InvestigationsModel, StepState};
use crate::tui::model::Model;
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Cell, Paragraph, Row, Table, Wrap},
    Frame,
};

/// Render the Investigations tab
pub fn render(f: &mut Frame, model: &mut Model, area: Rect) {
    // Check if we're collecting inputs
    if model.investigations.is_collecting_inputs() {
        render_input_collection(f, &model.investigations, area);
        return;
    }

    // Check if there's an active investigation
    if model.investigations.active.is_some() {
        render_active_investigation(f, &model.investigations, area);
        return;
    }

    // Normal view: pack list + details
    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(40), Constraint::Percentage(60)])
        .split(area);

    render_pack_list(f, &mut model.investigations, chunks[0]);
    render_pack_details(f, &model.investigations, chunks[1]);
}

/// Render the list of investigation packs
fn render_pack_list(f: &mut Frame, model: &mut InvestigationsModel, area: Rect) {
    // Show loading or error state
    if model.loading {
        let loading_paragraph = Paragraph::new("Loading investigation packs...")
            .block(Block::default().borders(Borders::ALL).title("Investigations"))
            .style(Style::default().fg(Color::Yellow));
        f.render_widget(loading_paragraph, area);
        return;
    }

    if let Some(error) = &model.error {
        let error_paragraph = Paragraph::new(format!("Error: {}", error))
            .block(Block::default().borders(Borders::ALL).title("Investigations"))
            .style(Style::default().fg(Color::Red));
        f.render_widget(error_paragraph, area);
        return;
    }

    // Show empty state
    if model.packs.is_empty() {
        let empty_lines = vec![
            Line::from(""),
            Line::from("No investigation packs found"),
            Line::from(""),
            Line::from(vec![
                Span::raw("Create packs in: "),
                Span::styled(
                    "~/.kql-panopticon/investigations/",
                    Style::default().fg(Color::Cyan),
                ),
            ]),
            Line::from(""),
            Line::from("Press 'r' to refresh"),
        ];

        let empty_paragraph = Paragraph::new(empty_lines)
            .block(Block::default().borders(Borders::ALL).title("Investigations"))
            .style(Style::default().fg(Color::Gray))
            .wrap(Wrap { trim: true });
        f.render_widget(empty_paragraph, area);
        return;
    }

    // Create header
    let header = Row::new(vec!["Investigation", "Steps", "Inputs"])
        .style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .bottom_margin(1);

    // Create rows
    let rows: Vec<Row> = model
        .packs
        .iter()
        .map(|entry| {
            let name = entry.get_display_name();
            let step_count = entry
                .get_step_count()
                .map(|c| c.to_string())
                .unwrap_or_else(|| "?".to_string());
            let input_count = entry
                .get_input_count()
                .map(|c| c.to_string())
                .unwrap_or_else(|| "?".to_string());

            // Show error indicator if pack has issues
            let name_with_indicator = if entry.has_error() {
                format!("! {}", name)
            } else {
                name
            };

            let style = if entry.has_error() {
                Style::default().fg(Color::Red)
            } else {
                Style::default()
            };

            Row::new(vec![
                Cell::from(name_with_indicator).style(style),
                Cell::from(step_count),
                Cell::from(input_count),
            ])
        })
        .collect();

    // Calculate column widths
    let widths = [
        Constraint::Percentage(60),
        Constraint::Percentage(20),
        Constraint::Percentage(20),
    ];

    let table = Table::new(rows, widths)
        .header(header)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(format!("Investigations ({})", model.pack_count())),
        )
        .highlight_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol(">> ");

    f.render_stateful_widget(table, area, &mut model.table_state);
}

/// Render details for the selected pack
fn render_pack_details(f: &mut Frame, model: &InvestigationsModel, area: Rect) {
    let selected_entry = model.get_selected_entry();

    if selected_entry.is_none() {
        let help_paragraph = Paragraph::new(vec![
            Line::from(""),
            Line::from("No investigation selected"),
            Line::from(""),
            Line::from("Use Up/Down to select an investigation"),
        ])
        .block(
            Block::default()
                .borders(Borders::TOP | Borders::RIGHT | Borders::BOTTOM)
                .title("Investigation Details"),
        )
        .style(Style::default().fg(Color::Gray));
        f.render_widget(help_paragraph, area);
        return;
    }

    let entry = selected_entry.unwrap();

    // Show error if pack has issues
    if let Some(error) = entry.get_error() {
        let error_paragraph = Paragraph::new(vec![
            Line::from(""),
            Line::from(Span::styled(
                "Failed to load investigation",
                Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
            )),
            Line::from(""),
            Line::from(error),
            Line::from(""),
            Line::from(Span::styled(
                format!("File: {}", entry.relative_path),
                Style::default().fg(Color::Gray),
            )),
        ])
        .block(
            Block::default()
                .borders(Borders::TOP | Borders::RIGHT | Borders::BOTTOM)
                .title("Investigation Details"),
        )
        .wrap(Wrap { trim: true });
        f.render_widget(error_paragraph, area);
        return;
    }

    // Show loading state if pack not loaded yet
    if entry.pack.is_none() {
        let loading_paragraph =
            Paragraph::new("Press Enter to load investigation details...")
                .block(
                    Block::default()
                        .borders(Borders::TOP | Borders::RIGHT | Borders::BOTTOM)
                        .title("Investigation Details"),
                )
                .style(Style::default().fg(Color::Yellow));
        f.render_widget(loading_paragraph, area);
        return;
    }

    // Render pack details
    let pack = entry.pack.as_ref().unwrap();
    let mut lines = vec![
        Line::from(vec![
            Span::styled("Name: ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw(&pack.name),
        ]),
        Line::from(""),
    ];

    // Add description if present
    if let Some(description) = &pack.description {
        lines.push(Line::from(vec![
            Span::styled(
                "Description: ",
                Style::default().add_modifier(Modifier::BOLD),
            ),
            Span::raw(description),
        ]));
        lines.push(Line::from(""));
    }

    // Add version if present
    if let Some(version) = &pack.version {
        lines.push(Line::from(vec![
            Span::styled("Version: ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw(version),
        ]));
        lines.push(Line::from(""));
    }

    // Add inputs section if present
    if !pack.inputs.is_empty() {
        lines.push(Line::from(Span::styled(
            "Inputs:",
            Style::default().add_modifier(Modifier::BOLD),
        )));

        for input in &pack.inputs {
            let required = if input.required { " *" } else { "" };
            let default_text = input
                .default
                .as_ref()
                .map(|d| format!(" [{}]", d))
                .unwrap_or_default();

            lines.push(Line::from(vec![
                Span::styled(format!("  {}", input.name), Style::default().fg(Color::Cyan)),
                Span::styled(required, Style::default().fg(Color::Red)),
                Span::styled(default_text, Style::default().fg(Color::Gray)),
            ]));

            if let Some(desc) = &input.description {
                lines.push(Line::from(vec![
                    Span::raw("    "),
                    Span::styled(desc, Style::default().fg(Color::Gray)),
                ]));
            }
        }
        lines.push(Line::from(""));
    }

    // Add steps section
    lines.push(Line::from(Span::styled(
        "Steps:",
        Style::default().add_modifier(Modifier::BOLD),
    )));

    for (i, step) in pack.steps.iter().enumerate() {
        let deps = if step.depends_on.is_empty() {
            String::new()
        } else {
            format!(" <- {}", step.depends_on.join(", "))
        };

        let extracts = if step.extract.is_empty() {
            String::new()
        } else {
            format!(
                " -> {}",
                step.extract.keys().cloned().collect::<Vec<_>>().join(", ")
            )
        };

        lines.push(Line::from(vec![
            Span::styled(format!("  {}. ", i + 1), Style::default().fg(Color::Yellow)),
            Span::raw(&step.name),
            Span::styled(deps, Style::default().fg(Color::Gray)),
            Span::styled(extracts, Style::default().fg(Color::Green)),
        ]));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(""));

    // Add controls
    lines.push(Line::from(Span::styled(
        "Controls:",
        Style::default().add_modifier(Modifier::BOLD),
    )));
    lines.push(Line::from("  Enter - View details / Load pack"));
    lines.push(Line::from("  e - Execute investigation"));
    lines.push(Line::from("  r - Refresh list"));

    let details_paragraph = Paragraph::new(lines)
        .block(
            Block::default()
                .borders(Borders::TOP | Borders::RIGHT | Borders::BOTTOM)
                .title("Investigation Details"),
        )
        .wrap(Wrap { trim: true });

    f.render_widget(details_paragraph, area);
}

/// Render input collection dialog
fn render_input_collection(f: &mut Frame, model: &InvestigationsModel, area: Rect) {
    let Some(state) = &model.input_collection else {
        return;
    };

    let entry = model.packs.iter().find(|e| e.path == state.pack_path);
    let pack = entry.and_then(|e| e.pack.as_ref());

    let mut lines = vec![
        Line::from(Span::styled(
            "Configure Investigation Inputs",
            Style::default()
                .add_modifier(Modifier::BOLD)
                .fg(Color::Yellow),
        )),
        Line::from(""),
    ];

    if let Some(pack) = pack {
        for (i, input) in pack.inputs.iter().enumerate() {
            let is_current = i == state.current_input;
            let value = if is_current {
                &state.current_value
            } else {
                state.inputs.get(&input.name).map(|s| s.as_str()).unwrap_or("")
            };

            let prefix = if is_current { "> " } else { "  " };
            let required = if input.required { " *" } else { "" };

            let name_style = if is_current {
                Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Cyan)
            };

            lines.push(Line::from(vec![
                Span::raw(prefix),
                Span::styled(&input.name, name_style),
                Span::styled(required, Style::default().fg(Color::Red)),
                Span::raw(": "),
                Span::styled(
                    if is_current {
                        format!("{}|", value)
                    } else {
                        value.to_string()
                    },
                    if is_current {
                        Style::default().fg(Color::White)
                    } else {
                        Style::default().fg(Color::Gray)
                    },
                ),
            ]));

            if let Some(desc) = &input.description {
                lines.push(Line::from(vec![
                    Span::raw("    "),
                    Span::styled(desc, Style::default().fg(Color::DarkGray)),
                ]));
            }
        }
    }

    lines.push(Line::from(""));
    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        "Controls:",
        Style::default().add_modifier(Modifier::BOLD),
    )));
    lines.push(Line::from("  Tab/Down - Next input"));
    lines.push(Line::from("  Shift+Tab/Up - Previous input"));
    lines.push(Line::from("  Enter - Execute investigation"));
    lines.push(Line::from("  Esc - Cancel"));

    let paragraph = Paragraph::new(lines)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Investigation Inputs"),
        )
        .wrap(Wrap { trim: true });

    f.render_widget(paragraph, area);
}

/// Render active investigation progress
fn render_active_investigation(f: &mut Frame, model: &InvestigationsModel, area: Rect) {
    let Some(active) = &model.active else {
        return;
    };

    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
        .split(area);

    // Left side: Steps progress
    let mut step_lines = vec![
        Line::from(Span::styled(
            &active.name,
            Style::default()
                .add_modifier(Modifier::BOLD)
                .fg(Color::Yellow),
        )),
        Line::from(""),
        Line::from(Span::styled(
            "Steps:",
            Style::default().add_modifier(Modifier::BOLD),
        )),
    ];

    for step in &active.steps {
        let (icon, style) = match &step.status {
            StepState::Pending => ("[ ]", Style::default().fg(Color::Gray)),
            StepState::Running => ("[~]", Style::default().fg(Color::Yellow)),
            StepState::Completed { rows } => {
                step_lines.push(Line::from(vec![
                    Span::styled("[+] ", Style::default().fg(Color::Green)),
                    Span::raw(&step.name),
                    Span::styled(format!(" ({} rows)", rows), Style::default().fg(Color::Gray)),
                ]));
                continue;
            }
            StepState::Failed { .. } => ("[X]", Style::default().fg(Color::Red)),
            StepState::Skipped => ("[-]", Style::default().fg(Color::DarkGray)),
        };

        step_lines.push(Line::from(vec![
            Span::styled(format!("{} ", icon), style),
            Span::styled(&step.name, style),
        ]));
    }

    let steps_paragraph = Paragraph::new(step_lines)
        .block(Block::default().borders(Borders::ALL).title("Progress"))
        .wrap(Wrap { trim: true });

    f.render_widget(steps_paragraph, chunks[0]);

    // Right side: Workspace progress
    let mut ws_lines = vec![
        Line::from(Span::styled(
            "Workspaces:",
            Style::default().add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
    ];

    for (name, progress) in &active.workspace_progress {
        let status_icon = match progress.status {
            InvestigationStatus::Pending => "[ ]",
            InvestigationStatus::Running => "[~]",
            InvestigationStatus::Completed => "[+]",
            InvestigationStatus::Failed => "[X]",
        };

        let status_style = match progress.status {
            InvestigationStatus::Pending => Style::default().fg(Color::Gray),
            InvestigationStatus::Running => Style::default().fg(Color::Yellow),
            InvestigationStatus::Completed => Style::default().fg(Color::Green),
            InvestigationStatus::Failed => Style::default().fg(Color::Red),
        };

        ws_lines.push(Line::from(vec![
            Span::styled(format!("{} ", status_icon), status_style),
            Span::raw(name),
            Span::styled(
                format!(" ({}/{})", progress.completed_steps, progress.total_steps),
                Style::default().fg(Color::Gray),
            ),
        ]));

        if let Some(current_step) = &progress.current_step {
            ws_lines.push(Line::from(vec![
                Span::raw("    "),
                Span::styled(current_step, Style::default().fg(Color::DarkGray)),
            ]));
        }
    }

    let ws_paragraph = Paragraph::new(ws_lines)
        .block(
            Block::default()
                .borders(Borders::TOP | Borders::RIGHT | Borders::BOTTOM)
                .title("Workspace Status"),
        )
        .wrap(Wrap { trim: true });

    f.render_widget(ws_paragraph, chunks[1]);
}