omnyssh 1.0.0

TUI SSH dashboard & server manager
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Server card rendering primitives for the dashboard grid.
//!
//! A card displays a single host's name, connection status, and live
//! metrics (CPU / RAM / Disk) inside a bordered ratatui [`Block`].

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

use crate::event::{Alert, DetectedService, Metrics, ServiceKind};
use crate::ssh::client::ConnectionStatus;
use crate::ssh::metrics::threshold_color;
use crate::ui::theme::Theme;

// ---------------------------------------------------------------------------
// Card dimensions (kept in sync with dashboard.rs column calculation)
// ---------------------------------------------------------------------------

/// Minimum card width (characters), including borders.
/// Increased to 34 to properly display enriched format (A.4.1).
pub const CARD_MIN_WIDTH: u16 = 34;
/// Fixed card height (lines), including borders (increased for services display).
pub const CARD_HEIGHT: u16 = 10;

// ---------------------------------------------------------------------------
// Status indicators
// ---------------------------------------------------------------------------

fn status_dot(status: Option<&ConnectionStatus>) -> (&'static str, Color) {
    match status {
        Some(ConnectionStatus::Connected) => ("", Color::Green),
        Some(ConnectionStatus::Connecting) => ("", Color::Yellow),
        Some(ConnectionStatus::Failed(_)) => ("", Color::Red),
        Some(ConnectionStatus::Unknown) | None => ("?", Color::DarkGray),
    }
}

// ---------------------------------------------------------------------------
// Public render function
// ---------------------------------------------------------------------------

/// Host display data passed to [`render_card`].
pub struct CardData<'a> {
    pub host_name: &'a str,
    pub hostname: &'a str,
    pub user: &'a str,
    pub port: u16,
    pub tags: &'a [String],
    pub metrics: Option<&'a Metrics>,
    pub status: Option<&'a ConnectionStatus>,
    /// Detected services.
    pub services: Option<&'a [DetectedService]>,
    /// Active alerts.
    pub alerts: Option<&'a [Alert]>,
}

/// Render a single server card into `rect`.
///
/// `is_selected` highlights the card border using the active theme accent
/// colour and uses thick double borders.
pub fn render_card(
    frame: &mut Frame,
    rect: Rect,
    data: &CardData<'_>,
    is_selected: bool,
    theme: &Theme,
) {
    let host_name = data.host_name;
    let hostname = data.hostname;
    let user = data.user;
    let port = data.port;
    let tags = data.tags;
    let metrics = data.metrics;
    let status = data.status;
    // ---- Border ----
    let (dot, dot_color) = status_dot(status);
    let title = format!(
        " {} ",
        truncate(host_name, rect.width.saturating_sub(6) as usize)
    );
    let title_right = format!(" {} ", dot);

    let border_color = if is_selected {
        theme.accent
    } else {
        theme.border
    };
    let title_color = if is_selected {
        theme.accent
    } else {
        theme.title
    };
    let border_type = if is_selected {
        BorderType::Double
    } else {
        BorderType::Rounded
    };

    let block = Block::default()
        .title(title)
        .title_alignment(Alignment::Left)
        .title_style(
            Style::default()
                .fg(title_color)
                .add_modifier(Modifier::BOLD),
        )
        .title_top(
            Line::from(Span::styled(title_right, Style::default().fg(dot_color)))
                .alignment(Alignment::Right),
        )
        .borders(Borders::ALL)
        .border_type(border_type)
        .border_style(Style::default().fg(border_color));

    let inner = block.inner(rect);
    frame.render_widget(block, rect);

    // ---- Inner layout: 8 rows (Enriched format per A.4.1) ----
    // Row 0: hostname + user:port
    // Row 1: CPU + RAM (combined)
    // Row 2: DSK + Uptime (combined)
    // Row 3: horizontal separator
    // Row 4: services
    // Row 5: alerts
    // Row 6: alerts continued
    // Row 7: tags
    if inner.height == 0 || inner.width == 0 {
        return;
    }

    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // hostname
            Constraint::Length(1), // cpu + ram
            Constraint::Length(1), // disk + uptime
            Constraint::Length(1), // separator
            Constraint::Length(1), // services
            Constraint::Length(1), // alerts line 1
            Constraint::Length(1), // alerts line 2
            Constraint::Length(1), // tags
            Constraint::Min(0),    // remainder (safety)
        ])
        .split(inner);

    // Row 0: hostname + user:port
    let user_port = format!("{}:{}", user, port);
    let hostname_trunc = truncate(
        hostname,
        inner.width.saturating_sub(user_port.len() as u16 + 1) as usize,
    );
    let addr_line = Line::from(vec![
        Span::styled(hostname_trunc, Style::default().fg(Color::Cyan)),
        Span::raw(" "),
        Span::styled(user_port, Style::default().fg(Color::DarkGray)),
    ]);
    frame.render_widget(Paragraph::new(addr_line), rows[0]);

    // Check if offline
    let is_offline = matches!(
        status,
        Some(ConnectionStatus::Failed(_)) | Some(ConnectionStatus::Unknown) | None
    ) && metrics.is_none();

    if is_offline {
        // Rows 1-2: offline message
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled(
                "─── offline ───",
                Style::default().fg(Color::Red),
            ))),
            rows[1],
        );
    } else {
        // Row 1: CPU + RAM combined (A.4.1 spec: "CPU: ████░░ 73%  RAM: 2.1/4GB")
        let cpu = metrics.and_then(|m| m.cpu_percent);
        let ram = metrics.and_then(|m| m.ram_percent);
        frame.render_widget(
            Paragraph::new(render_cpu_ram_line(cpu, ram, inner.width)),
            rows[1],
        );

        // Row 2: DSK + Uptime combined (A.4.1 spec: "DSK: ████░░ 61%  Up: 43 days")
        let disk = metrics.and_then(|m| m.disk_percent);
        let uptime_str = metrics.and_then(|m| m.uptime.as_deref()).unwrap_or("");
        frame.render_widget(
            Paragraph::new(render_disk_uptime_line(disk, uptime_str, inner.width)),
            rows[2],
        );
    }

    // Row 3: horizontal separator (A.4.1 spec: "│───────────────────────────────│")
    let separator = "".repeat(inner.width as usize);
    frame.render_widget(
        Paragraph::new(Line::from(Span::styled(
            separator,
            Style::default().fg(Color::DarkGray),
        ))),
        rows[3],
    );

    // Rows 4-5: services
    // Use TWO lines for services to avoid "+N" overflow
    let mut service_row_offset = 0;
    if let Some(services) = data.services {
        if !services.is_empty() {
            let service_lines = render_services_lines(services, inner.width, 2);
            for (i, line) in service_lines.iter().enumerate() {
                if i < 2 {
                    frame.render_widget(Paragraph::new(line.clone()), rows[4 + i]);
                    service_row_offset = i + 1;
                }
            }
        }
    }

    // Rows 6-7 (or 5-6 if no services): alerts
    // Show up to 2 alert lines for important issues, but shift down if services used both rows
    if let Some(alerts) = data.alerts {
        if !alerts.is_empty() {
            let alert_start_row = 4 + service_row_offset;
            let alert_lines = render_alert_lines(alerts, inner.width, 2);
            for (i, line) in alert_lines.iter().enumerate() {
                let row_idx = alert_start_row + i;
                if row_idx < 7 {
                    // Don't overlap with tags row
                    frame.render_widget(Paragraph::new(line.clone()), rows[row_idx]);
                }
            }
        }
    }

    // Row 7: tags
    if !tags.is_empty() {
        let tag_spans: Vec<Span> = tags
            .iter()
            .flat_map(|t| {
                [
                    Span::styled("[", Style::default().fg(Color::DarkGray)),
                    Span::styled(t.as_str(), Style::default().fg(Color::Gray)),
                    Span::styled("] ", Style::default().fg(Color::DarkGray)),
                ]
            })
            .collect();
        frame.render_widget(Paragraph::new(Line::from(tag_spans)), rows[7]);
    }
}

// ---------------------------------------------------------------------------
// Combined metric line rendering (A.4.1 Enriched format)
// ---------------------------------------------------------------------------

/// Render CPU + RAM on one line: "CPU: ████░░ 73%  RAM: 2.1/4GB"
fn render_cpu_ram_line(cpu: Option<f64>, ram: Option<f64>, _width: u16) -> Line<'static> {
    let mut spans = Vec::new();

    // CPU part with compact bar
    spans.push(Span::styled("CPU: ", Style::default().fg(Color::Gray)));
    if let Some(pct) = cpu {
        let bar_width = 6; // Fixed short bar
        let filled = ((pct / 100.0) * bar_width as f64).round() as usize;
        let filled = filled.min(bar_width);
        let mut bar = String::with_capacity(bar_width * 3);
        for _ in 0..filled {
            bar.push('');
        }
        for _ in filled..bar_width {
            bar.push('');
        }
        let color = threshold_color(pct);
        spans.push(Span::styled(bar, Style::default().fg(color)));
        spans.push(Span::styled(
            format!(" {:>3.0}%", pct),
            Style::default().fg(color).add_modifier(Modifier::BOLD),
        ));
    } else {
        spans.push(Span::styled(
            "░░░░░░ --",
            Style::default().fg(Color::DarkGray),
        ));
    }

    spans.push(Span::raw("  "));

    // RAM part with compact bar
    spans.push(Span::styled("RAM: ", Style::default().fg(Color::Gray)));
    if let Some(pct) = ram {
        let bar_width = 6;
        let filled = ((pct / 100.0) * bar_width as f64).round() as usize;
        let filled = filled.min(bar_width);
        let mut bar = String::with_capacity(bar_width * 3);
        for _ in 0..filled {
            bar.push('');
        }
        for _ in filled..bar_width {
            bar.push('');
        }
        let color = threshold_color(pct);
        spans.push(Span::styled(bar, Style::default().fg(color)));
        spans.push(Span::styled(
            format!(" {:>3.0}%", pct),
            Style::default().fg(color).add_modifier(Modifier::BOLD),
        ));
    } else {
        spans.push(Span::styled(
            "░░░░░░ --",
            Style::default().fg(Color::DarkGray),
        ));
    }

    Line::from(spans)
}

/// Render Disk + Uptime on one line: "DSK: ████░░ 61%  Up: 43 days"
fn render_disk_uptime_line(disk: Option<f64>, uptime: &str, _width: u16) -> Line<'static> {
    let mut spans = Vec::new();

    // Disk part with compact bar
    spans.push(Span::styled("DSK: ", Style::default().fg(Color::Gray)));
    if let Some(pct) = disk {
        let bar_width = 6;
        let filled = ((pct / 100.0) * bar_width as f64).round() as usize;
        let filled = filled.min(bar_width);
        let mut bar = String::with_capacity(bar_width * 3);
        for _ in 0..filled {
            bar.push('');
        }
        for _ in filled..bar_width {
            bar.push('');
        }
        let color = threshold_color(pct);
        spans.push(Span::styled(bar, Style::default().fg(color)));
        spans.push(Span::styled(
            format!(" {:>3.0}%", pct),
            Style::default().fg(color).add_modifier(Modifier::BOLD),
        ));
    } else {
        spans.push(Span::styled(
            "░░░░░░ --",
            Style::default().fg(Color::DarkGray),
        ));
    }

    spans.push(Span::raw("  "));

    // Uptime part - no truncation, always show full uptime
    if !uptime.is_empty() {
        let uptime_display = format!("Up: {}", uptime);
        spans.push(Span::styled(
            uptime_display,
            Style::default().fg(Color::DarkGray),
        ));
    }

    Line::from(spans)
}

// ---------------------------------------------------------------------------
// Service and Alert rendering
// ---------------------------------------------------------------------------

/// Render services, ONE service per line (user requirement).
/// Returns up to `max_lines` lines with services displayed.
/// Example line: "🐳 Docker: 4 running"
fn render_services_lines(
    services: &[DetectedService],
    width: u16,
    max_lines: usize,
) -> Vec<Line<'static>> {
    use crate::event::ServiceStatus;

    let mut lines = Vec::new();

    for service in services.iter().take(max_lines) {
        let (icon, base_color) = service_icon(&service.kind);

        // Color by status: red for critical, yellow for degraded, green for healthy
        let color = match &service.status {
            ServiceStatus::Critical(_) => Color::Red,
            ServiceStatus::Degraded(_) => Color::Yellow,
            ServiceStatus::Healthy => base_color,
            ServiceStatus::Unknown => Color::DarkGray,
        };

        // Format: "🐳 Docker: 4 running"
        let service_name = service_name_short(&service.kind);
        let info = service_info(service);

        // Build line with icon, name, and info (truncate if too long)
        let text = if !info.is_empty() {
            format!("{} {}: {}", icon, service_name, info)
        } else {
            // If no info (e.g., systemd with no metrics), skip this service
            continue;
        };

        // Truncate to fit width
        let truncated = truncate(&text, (width as usize).saturating_sub(1));

        lines.push(Line::from(Span::styled(
            truncated,
            Style::default().fg(color),
        )));
    }

    lines
}

/// Render alert lines (A.4.1 format).
/// Example: "⚠ nginx-proxy restarting (x5)"
fn render_alert_lines(alerts: &[Alert], width: u16, max_lines: usize) -> Vec<Line<'static>> {
    use crate::event::AlertSeverity;

    let mut lines = Vec::new();

    // Prioritize: critical first, then warnings, then info
    let mut sorted_alerts = alerts.to_vec();
    sorted_alerts.sort_by(|a, b| b.severity.cmp(&a.severity));

    for alert in sorted_alerts.iter().take(max_lines) {
        let (icon, color) = match alert.severity {
            AlertSeverity::Critical => ("", Color::Red),
            AlertSeverity::Warning => ("", Color::Yellow),
            AlertSeverity::Info => ("", Color::Cyan),
        };

        let msg = truncate(&alert.message, width.saturating_sub(3) as usize);
        lines.push(Line::from(vec![
            Span::styled(icon, Style::default().fg(color)),
            Span::raw(" "),
            Span::styled(msg, Style::default().fg(color)),
        ]));
    }

    lines
}

/// Get icon and color for a service kind.
fn service_icon(kind: &ServiceKind) -> (&'static str, Color) {
    match kind {
        ServiceKind::Docker => ("🐳", Color::Cyan),
        ServiceKind::Nginx => ("🌐", Color::Green),
        ServiceKind::PostgreSQL => ("🐘", Color::Blue),
        ServiceKind::Redis => ("📦", Color::Red),
        ServiceKind::NodeJS => ("🟢", Color::Green),
    }
}

/// Get short service name for display (A.4.1 format).
fn service_name_short(kind: &ServiceKind) -> &str {
    match kind {
        ServiceKind::Docker => "Docker",
        ServiceKind::Nginx => "Nginx",
        ServiceKind::PostgreSQL => "PG",
        ServiceKind::Redis => "Redis",
        ServiceKind::NodeJS => "Node",
    }
}

/// Extract detailed info for a service per A.4.1 spec.
/// Examples: "8 containers", "repl lag 2.3s", "0 errors/5min"
fn service_info(service: &DetectedService) -> String {
    use crate::event::MetricValue;

    match service.kind {
        ServiceKind::Docker => {
            // Show detailed container breakdown: "4 running, 2 stopped, 1 restarting"
            if service.metrics.is_empty() {
                return String::new(); // Deep Probe hasn't run yet
            }

            let mut running = 0i64;
            let mut stopped = 0i64;
            let mut restarting = 0i64;

            for metric in &service.metrics {
                match metric.name.as_str() {
                    "containers_running" => {
                        if let MetricValue::Integer(n) = metric.value {
                            running = n;
                        }
                    }
                    "containers_stopped" => {
                        if let MetricValue::Integer(n) = metric.value {
                            stopped = n;
                        }
                    }
                    "containers_restarting" => {
                        if let MetricValue::Integer(n) = metric.value {
                            restarting = n;
                        }
                    }
                    _ => {}
                }
            }

            let total = running + stopped + restarting;
            if total == 0 {
                return String::from("no containers");
            }

            // Build status string with only non-zero counts
            let mut parts = Vec::new();
            if running > 0 {
                parts.push(format!("{} running", running));
            }
            if stopped > 0 {
                parts.push(format!("{} stopped", stopped));
            }
            if restarting > 0 {
                parts.push(format!("{} restarting", restarting));
            }

            parts.join(", ")
        }
        ServiceKind::PostgreSQL => {
            // Show replication lag if present (A.4.1: "repl lag 2.3s")
            for metric in &service.metrics {
                if metric.name == "replication_lag_seconds" {
                    if let MetricValue::Integer(lag) = metric.value {
                        if lag > 0 {
                            return format!("repl lag {}s", lag);
                        }
                    } else if let MetricValue::Float(lag) = metric.value {
                        if lag > 0.0 {
                            return format!("repl lag {:.1}s", lag);
                        }
                    }
                }
            }
            String::from("ok")
        }
        ServiceKind::Nginx => {
            // Show error count (A.4.1 format)
            for metric in &service.metrics {
                if metric.name == "recent_502_504_errors" {
                    if let MetricValue::Integer(errors) = metric.value {
                        if errors > 0 {
                            return format!("{} errors/5min", errors);
                        }
                    }
                }
            }
            String::from("ok")
        }
        ServiceKind::Redis => {
            // Show memory usage
            let mut mem_used = 0i64;
            for metric in &service.metrics {
                if metric.name == "memory_used_mb" {
                    if let MetricValue::Integer(mb) = metric.value {
                        mem_used = mb;
                    }
                }
            }
            if mem_used > 0 {
                format!("{}MB used", mem_used)
            } else {
                String::from("ok")
            }
        }
        ServiceKind::NodeJS => {
            // Show node processes count
            let mut node_processes = 0i64;
            for metric in &service.metrics {
                if metric.name == "node_processes" {
                    if let MetricValue::Integer(count) = metric.value {
                        node_processes = count;
                    }
                }
            }
            if node_processes > 0 {
                format!("{} process(es)", node_processes)
            } else {
                String::from("no processes")
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Utility
// ---------------------------------------------------------------------------

fn truncate(s: &str, max_chars: usize) -> String {
    if max_chars == 0 {
        return String::new();
    }
    // Walk char boundaries without collecting into a Vec<char>.
    let mut iter = s.char_indices();
    match iter.nth(max_chars.saturating_sub(1)) {
        // Fewer than max_chars characters — return as-is.
        None => s.to_string(),
        Some((byte_pos, _)) => {
            if iter.next().is_none() {
                // Exactly max_chars characters — return as-is.
                s.to_string()
            } else {
                // More than max_chars characters — truncate and append ellipsis.
                format!("{}", &s[..byte_pos])
            }
        }
    }
}