diskwatch 0.1.2

Single-host, read-only disk diagnostics TUI. Eight tabs across devices, volumes, filesystems, IO, SMART, hot files, and insights. Sibling to netwatch and syswatch.
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Overview tab — port of `dwRenderOverview`.
//!
//! Compositional: reads everything App already collected (devices,
//! filesystems, IO history, insights) and presents it as 5 KPI tiles +
//! device summary + aggregate IO sparkline + insights strip + a
//! segmented capacity bar.

use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};
use ratatui::Frame;

use crate::app::App;
use crate::insights::Severity;
use crate::ui::format::{fmt_rate, fmt_size, pad_left, pad_right};
use crate::ui::palette as p;
use crate::ui::sparkline::BaselineSparkline;

pub fn draw(f: &mut Frame, area: Rect, app: &App) {
    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(5), // KPI tiles
            Constraint::Min(8),    // devices + IO chart
            Constraint::Length(8), // insights strip + hot files note
            Constraint::Length(4), // capacity bar
        ])
        .split(area);

    draw_tiles(f, rows[0], app);
    draw_middle(f, rows[1], app);
    draw_bottom_strip(f, rows[2], app);
    draw_capacity_bar(f, rows[3], app);
}

// ---------- KPI tiles ----------

fn draw_tiles(f: &mut Frame, area: Rect, app: &App) {
    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Ratio(1, 5),
            Constraint::Ratio(1, 5),
            Constraint::Ratio(1, 5),
            Constraint::Ratio(1, 5),
            Constraint::Ratio(1, 5),
        ])
        .split(area);
    draw_capacity_tile(f, cols[0], app);
    draw_io_tile(f, cols[1], app);
    draw_latency_tile(f, cols[2], app);
    draw_health_tile(f, cols[3], app);
    draw_insights_tile(f, cols[4], app);
}

fn tile_block(title: &'static str) -> Block<'static> {
    Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(p::FAINT).bg(p::BG))
        .title(Span::styled(
            format!(" {} ", title),
            Style::default().fg(p::DIM),
        ))
        .style(Style::default().bg(p::BG))
}

fn render_tile(
    f: &mut Frame,
    area: Rect,
    title: &'static str,
    dot_color: ratatui::style::Color,
    value: &str,
    sub: &str,
) {
    let block = tile_block(title);
    let inner = block.inner(area);
    f.render_widget(block, area);
    if inner.height < 3 {
        return;
    }
    let line1 = Line::from(vec![
        Span::styled(" \u{25cf}  ", Style::default().fg(dot_color)),
        Span::styled(
            value.to_string(),
            Style::default()
                .fg(p::BR_WHITE)
                .add_modifier(Modifier::BOLD),
        ),
    ]);
    let line2 = Line::from(Span::styled(
        format!("  {}", sub),
        Style::default().fg(p::DIM),
    ));
    f.render_widget(
        Paragraph::new(vec![line1, line2]).style(Style::default().bg(p::BG)),
        inner,
    );
}

fn draw_capacity_tile(f: &mut Frame, area: Rect, app: &App) {
    let total: u64 = app.devices.iter().map(|d| d.size_bytes).sum();
    let used: u64 = app.devices.iter().map(|d| d.used_bytes).sum();
    let pct = if total > 0 {
        (used as f64 / total as f64 * 100.0).round() as u32
    } else {
        0
    };
    let color = if pct >= 90 {
        p::RED
    } else if pct >= 80 {
        p::YELLOW
    } else {
        p::GREEN
    };
    render_tile(
        f,
        area,
        "CAPACITY",
        color,
        &format!("{}%", pct),
        &format!("used {} / {}", fmt_size(used), fmt_size(total)),
    );
}

fn draw_io_tile(f: &mut Frame, area: Rect, app: &App) {
    let (rate, _) = crate::collect::io::aggregate(&app.io.latest);
    let active = app.io.latest.iter().filter(|t| t.bps > 1_000.0).count();
    let color = if rate > 50_000_000.0 {
        p::YELLOW
    } else if rate > 1_000.0 {
        p::GREEN
    } else {
        p::DIM
    };
    render_tile(
        f,
        area,
        "IO",
        color,
        fmt_rate(rate).trim(),
        &format!("{} of {} active", active, app.io.latest.len()),
    );
}

fn draw_latency_tile(f: &mut Frame, area: Rect, app: &App) {
    match crate::collect::io::worst_p99_us(&app.io.latest) {
        Some(us) => {
            let (value, color) = if us >= 10_000.0 {
                (format!("{:.1}ms", us / 1_000.0), p::RED)
            } else if us >= 2_000.0 {
                (format!("{:.1}ms", us / 1_000.0), p::YELLOW)
            } else if us >= 1_000.0 {
                (format!("{:.1}ms", us / 1_000.0), p::GREEN)
            } else if us > 0.0 {
                (format!("{:.0}µs", us), p::GREEN)
            } else {
                ("".to_string(), p::DIM)
            };
            render_tile(
                f,
                area,
                "p99 LATENCY",
                color,
                &value,
                "max across devices  60s window",
            );
        }
        None => {
            render_tile(f, area, "p99 LATENCY", p::DIM, "", "no IO observed yet");
        }
    }
}

fn draw_health_tile(f: &mut Frame, area: Rect, app: &App) {
    let total = app.devices.len();
    let healthy = app
        .devices
        .iter()
        .filter(|d| matches!(d.smart_ok, Some(true)))
        .count();
    let failing = app
        .devices
        .iter()
        .filter(|d| matches!(d.smart_ok, Some(false)))
        .count();
    let unknown = total.saturating_sub(healthy).saturating_sub(failing);
    let color = if failing > 0 {
        p::RED
    } else if unknown > 0 {
        p::YELLOW
    } else {
        p::GREEN
    };
    render_tile(
        f,
        area,
        "HEALTH",
        color,
        &format!("{}/{}", healthy, total),
        &format!("{} failing  {} unknown", failing, unknown),
    );
}

fn draw_insights_tile(f: &mut Frame, area: Rect, app: &App) {
    let crit = app
        .insights
        .iter()
        .filter(|i| i.sev == Severity::Crit)
        .count();
    let warn = app
        .insights
        .iter()
        .filter(|i| i.sev == Severity::Warn)
        .count();
    let total = app.insights.len();
    let color = if crit > 0 {
        p::RED
    } else if warn > 0 {
        p::YELLOW
    } else {
        p::CYAN
    };
    render_tile(
        f,
        area,
        "INSIGHTS",
        color,
        &total.to_string(),
        &format!("{} crit  {} warn", crit, warn),
    );
}

// ---------- middle: devices + IO sparkline ----------

fn draw_middle(f: &mut Frame, area: Rect, app: &App) {
    let split = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(60), Constraint::Percentage(40)])
        .split(area);
    draw_devices_summary(f, split[0], app);
    draw_io_sparkline(f, split[1], app);
}

fn draw_devices_summary(f: &mut Frame, area: Rect, app: &App) {
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(p::FAINT).bg(p::BG))
        .title(Span::styled(
            format!(" DEVICES  {} attached ", app.devices.len()),
            Style::default().fg(p::CYAN).add_modifier(Modifier::BOLD),
        ))
        .style(Style::default().bg(p::BG));
    let inner = block.inner(area);
    f.render_widget(block, area);
    if inner.height < 2 {
        return;
    }
    let header = "   DEVICE     MODEL                         SIZE     USED   SMART";
    f.render_widget(
        Paragraph::new(Line::from(Span::styled(
            header.to_string(),
            Style::default().fg(p::DIM),
        )))
        .style(Style::default().bg(p::BG)),
        Rect {
            x: inner.x + 1,
            y: inner.y,
            width: inner.width.saturating_sub(1),
            height: 1,
        },
    );
    let visible = ((inner.height as usize).saturating_sub(1)).min(app.devices.len());
    for i in 0..visible {
        let d = &app.devices[i];
        let used_pct = if d.size_bytes > 0 {
            (d.used_bytes as f64 / d.size_bytes as f64 * 100.0).round() as u32
        } else {
            0
        };
        let used_col = if used_pct >= 90 {
            p::RED
        } else if used_pct >= 80 {
            p::YELLOW
        } else {
            p::FG
        };
        let (smart_text, smart_col) = match d.smart_ok {
            Some(true) => ("ok", p::GREEN),
            Some(false) => ("FAIL", p::RED),
            None => ("", p::DIM),
        };
        let dot_col = match d.smart_ok {
            Some(true) => p::GREEN,
            Some(false) => p::RED,
            None => p::DIM,
        };
        let line = Line::from(vec![
            Span::raw(" "),
            Span::styled("\u{25cf}", Style::default().fg(dot_col)),
            Span::raw(" "),
            Span::styled(pad_right(&d.name, 11), Style::default().fg(p::FG)),
            Span::styled(pad_right(&d.model, 30), Style::default().fg(p::FG)),
            Span::styled(
                pad_left(&fmt_size(d.size_bytes), 8),
                Style::default().fg(p::DIM),
            ),
            Span::raw("  "),
            Span::styled(
                pad_left(&format!("{}%", used_pct), 4),
                Style::default().fg(used_col),
            ),
            Span::raw("  "),
            Span::styled(smart_text.to_string(), Style::default().fg(smart_col)),
        ]);
        f.render_widget(
            Paragraph::new(line).style(Style::default().bg(p::BG)),
            Rect {
                x: inner.x + 1,
                y: inner.y + 1 + i as u16,
                width: inner.width.saturating_sub(2),
                height: 1,
            },
        );
    }
}

fn draw_io_sparkline(f: &mut Frame, area: Rect, app: &App) {
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(p::FAINT).bg(p::BG))
        .title(Span::styled(
            " AGG IO  60s ",
            Style::default().fg(p::CYAN).add_modifier(Modifier::BOLD),
        ))
        .style(Style::default().bg(p::BG));
    let inner = block.inner(area);
    f.render_widget(block, area);
    if inner.height < 3 {
        return;
    }
    let (agg, _) = crate::collect::io::aggregate(&app.io.latest);
    let summary = Line::from(vec![
        Span::raw(" "),
        Span::styled(
            fmt_rate(agg),
            Style::default().fg(p::CYAN).add_modifier(Modifier::BOLD),
        ),
        Span::raw("  "),
        Span::styled("all devices", Style::default().fg(p::DIM)),
    ]);
    f.render_widget(
        Paragraph::new(summary).style(Style::default().bg(p::BG)),
        Rect {
            x: inner.x + 1,
            y: inner.y,
            width: inner.width.saturating_sub(2),
            height: 1,
        },
    );

    // Aggregate sparkline across all devices. One cell per sample,
    // with a `▁` baseline filling any leading cells that don't yet
    // have data (rather than upsampling or padding zeros).
    let buckets = aggregate_history(app);
    f.render_widget(
        BaselineSparkline::new(&buckets).style(Style::default().fg(p::CYAN).bg(p::BG)),
        Rect {
            x: inner.x + 1,
            y: inner.y + 1,
            width: inner.width.saturating_sub(2),
            height: inner.height.saturating_sub(1),
        },
    );
}

fn aggregate_history(app: &App) -> Vec<f64> {
    // Sum every device's per-tick rates index-wise into a single
    // aggregate series. Length matches the underlying ring; the
    // baseline sparkline widget handles the case where the area is
    // wider than the data.
    let mut buckets: Vec<f64> = Vec::new();
    for h in app.io.history.values() {
        for (i, v) in h.combined.iter().enumerate() {
            if i >= buckets.len() {
                buckets.push(0.0);
            }
            buckets[i] += v;
        }
    }
    buckets
}

// ---------- bottom strip: insights + hot files note ----------

fn draw_bottom_strip(f: &mut Frame, area: Rect, app: &App) {
    let split = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
        .split(area);
    draw_insights_summary(f, split[0], app);
    draw_hot_files_note(f, split[1]);
}

fn draw_insights_summary(f: &mut Frame, area: Rect, app: &App) {
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(p::FAINT).bg(p::BG))
        .title(Span::styled(
            " INSIGHTS ",
            Style::default().fg(p::YELLOW).add_modifier(Modifier::BOLD),
        ))
        .style(Style::default().bg(p::BG));
    let inner = block.inner(area);
    f.render_widget(block, area);
    let visible = (inner.height as usize).min(app.insights.len()).min(3);
    for i in 0..visible {
        let ins = &app.insights[i];
        let (badge_fg, badge_bg) = match ins.sev {
            Severity::Crit => (p::RED, p::ERR_BG),
            Severity::Warn => (p::YELLOW, p::WARN_BG),
            Severity::Info => (p::CYAN, p::OK_BG),
        };
        let line = Line::from(vec![
            Span::raw(" "),
            Span::styled(
                format!(" {} ", ins.sev.label()),
                Style::default()
                    .fg(badge_fg)
                    .bg(badge_bg)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("  "),
            Span::styled(ins.title.clone(), Style::default().fg(p::FG)),
        ]);
        f.render_widget(
            Paragraph::new(line).style(Style::default().bg(p::BG)),
            Rect {
                x: inner.x,
                y: inner.y + i as u16,
                width: inner.width,
                height: 1,
            },
        );
    }
    if visible == 0 {
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                "  no insights yet",
                Style::default().fg(p::DIM),
            )))
            .style(Style::default().bg(p::BG)),
            inner,
        );
    }
}

fn draw_hot_files_note(f: &mut Frame, area: Rect) {
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(p::FAINT).bg(p::BG))
        .title(Span::styled(
            " HOT FILES ",
            Style::default().fg(p::DIM).add_modifier(Modifier::BOLD),
        ))
        .style(Style::default().bg(p::BG));
    let inner = block.inner(area);
    f.render_widget(block, area);
    f.render_widget(
        Paragraph::new(vec![
            Line::from(""),
            Line::from(Span::styled(
                "  per-process write rate deferred",
                Style::default().fg(p::DIM),
            )),
            Line::from(Span::styled(
                "  see [7] for what's needed",
                Style::default().fg(p::DIM),
            )),
        ])
        .style(Style::default().bg(p::BG)),
        inner,
    );
}

// ---------- bottom capacity bar ----------

fn draw_capacity_bar(f: &mut Frame, area: Rect, app: &App) {
    let total: u64 = app.devices.iter().map(|d| d.size_bytes).sum();
    let used_sum: u64 = app.devices.iter().map(|d| d.used_bytes).sum();
    let free = total.saturating_sub(used_sum);
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(p::FAINT).bg(p::BG))
        .title(Span::styled(
            format!(
                " CAPACITY  {} used / {} ",
                fmt_size(used_sum),
                fmt_size(total)
            ),
            Style::default().fg(p::CYAN).add_modifier(Modifier::BOLD),
        ))
        .style(Style::default().bg(p::BG));
    let inner = block.inner(area);
    f.render_widget(block, area);
    if inner.height < 2 || inner.width < 10 || total == 0 {
        return;
    }
    // Each device contributes a colored segment proportional to its
    // **used** bytes; the remaining slice is faint "free". This matches
    // the JSX design — the bar shows where capacity is being consumed,
    // not how big each disk is.
    let bar_w = inner.width as usize;
    let mut spans: Vec<Span> = Vec::with_capacity(app.devices.len() + 1);
    let mut consumed_cells = 0usize;
    for d in &app.devices {
        if d.used_bytes == 0 || consumed_cells >= bar_w {
            continue;
        }
        let seg_w = ((d.used_bytes as f64 / total as f64) * bar_w as f64).round() as usize;
        let seg_w = seg_w.max(1).min(bar_w - consumed_cells);
        let color = if d.is_removable {
            p::MAGENTA
        } else if matches!(d.kind, crate::collect::DeviceKind::Nvme) {
            p::CYAN
        } else {
            p::GREEN
        };
        let block: String = "\u{2588}".repeat(seg_w);
        spans.push(Span::styled(block, Style::default().fg(color).bg(p::BG)));
        consumed_cells += seg_w;
    }
    if consumed_cells < bar_w {
        let free_w = bar_w - consumed_cells;
        let block: String = "\u{2591}".repeat(free_w);
        spans.push(Span::styled(block, Style::default().fg(p::FAINT).bg(p::BG)));
    }
    f.render_widget(
        Paragraph::new(Line::from(spans)).style(Style::default().bg(p::BG)),
        Rect {
            x: inner.x,
            y: inner.y,
            width: inner.width,
            height: 1,
        },
    );

    // Legend: each device's used bytes + a "free" entry.
    let mut legend: Vec<Span> = Vec::new();
    for d in &app.devices {
        let color = if d.is_removable {
            p::MAGENTA
        } else if matches!(d.kind, crate::collect::DeviceKind::Nvme) {
            p::CYAN
        } else {
            p::GREEN
        };
        legend.push(Span::raw("  "));
        legend.push(Span::styled("\u{25fc} ", Style::default().fg(color)));
        legend.push(Span::styled(
            format!("{} used {}", d.name, fmt_size(d.used_bytes)),
            Style::default().fg(p::DIM),
        ));
    }
    legend.push(Span::raw("  "));
    legend.push(Span::styled("\u{25fc} ", Style::default().fg(p::FAINT)));
    legend.push(Span::styled(
        format!("free {}", fmt_size(free)),
        Style::default().fg(p::DIM),
    ));
    if inner.height >= 2 {
        f.render_widget(
            Paragraph::new(Line::from(legend)).style(Style::default().bg(p::BG)),
            Rect {
                x: inner.x,
                y: inner.y + 1,
                width: inner.width,
                height: 1,
            },
        );
    }
}