Implement localhost prioritization and status display in dashboard

- Always select localhost as default host at startup
- Order hosts with localhost first, then predefined sequence
- Display hostname status colors in title bar based on metric aggregation
- Add gethostname dependency for localhost detection
This commit is contained in:
Christoffer Martinsson 2025-10-19 10:56:42 +02:00
parent 0141a6e111
commit 07633e4e0e
3 changed files with 224 additions and 103 deletions

1
Cargo.lock generated
View File

@ -255,6 +255,7 @@ dependencies = [
"clap", "clap",
"cm-dashboard-shared", "cm-dashboard-shared",
"crossterm", "crossterm",
"gethostname",
"ratatui", "ratatui",
"serde", "serde",
"serde_json", "serde_json",

View File

@ -17,4 +17,5 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true } tracing-subscriber = { workspace = true }
ratatui = { workspace = true } ratatui = { workspace = true }
crossterm = { workspace = true } crossterm = { workspace = true }
toml = { workspace = true } toml = { workspace = true }
gethostname = { workspace = true }

View File

@ -10,13 +10,13 @@ use std::collections::HashMap;
use std::time::Instant; use std::time::Instant;
use tracing::info; use tracing::info;
pub mod widgets;
pub mod theme; pub mod theme;
pub mod widgets;
use widgets::{CpuWidget, MemoryWidget, ServicesWidget, BackupWidget, Widget};
use crate::metrics::MetricStore; use crate::metrics::MetricStore;
use cm_dashboard_shared::{Metric, Status}; use cm_dashboard_shared::{Metric, Status};
use theme::{Theme, Layout as ThemeLayout, Typography, Components, StatusIcons}; use theme::{Components, Layout as ThemeLayout, StatusIcons, Theme, Typography};
use widgets::{BackupWidget, CpuWidget, MemoryWidget, ServicesWidget, Widget};
/// Widget states for a specific host /// Widget states for a specific host
#[derive(Clone)] #[derive(Clone)]
@ -69,12 +69,14 @@ impl TuiApp {
should_quit: false, should_quit: false,
} }
} }
/// Get or create host widgets for the given hostname /// Get or create host widgets for the given hostname
fn get_or_create_host_widgets(&mut self, hostname: &str) -> &mut HostWidgets { fn get_or_create_host_widgets(&mut self, hostname: &str) -> &mut HostWidgets {
self.host_widgets.entry(hostname.to_string()).or_insert_with(HostWidgets::new) self.host_widgets
.entry(hostname.to_string())
.or_insert_with(HostWidgets::new)
} }
/// Update widgets with metrics from store (only for current host) /// Update widgets with metrics from store (only for current host)
pub fn update_metrics(&mut self, metric_store: &MetricStore) { pub fn update_metrics(&mut self, metric_store: &MetricStore) {
if let Some(hostname) = self.current_host.clone() { if let Some(hostname) = self.current_host.clone() {
@ -82,47 +84,98 @@ impl TuiApp {
let all_metrics = metric_store.get_metrics_for_host(&hostname); let all_metrics = metric_store.get_metrics_for_host(&hostname);
if !all_metrics.is_empty() { if !all_metrics.is_empty() {
// Get metrics first while hostname is borrowed // Get metrics first while hostname is borrowed
let cpu_metrics: Vec<&Metric> = all_metrics.iter() let cpu_metrics: Vec<&Metric> = all_metrics
.filter(|m| m.name.starts_with("cpu_") || m.name.contains("c_state_") || m.name.starts_with("process_top_")) .iter()
.filter(|m| {
m.name.starts_with("cpu_")
|| m.name.contains("c_state_")
|| m.name.starts_with("process_top_")
})
.copied() .copied()
.collect(); .collect();
let memory_metrics: Vec<&Metric> = all_metrics.iter() let memory_metrics: Vec<&Metric> = all_metrics
.iter()
.filter(|m| m.name.starts_with("memory_") || m.name.starts_with("disk_tmp_")) .filter(|m| m.name.starts_with("memory_") || m.name.starts_with("disk_tmp_"))
.copied() .copied()
.collect(); .collect();
let service_metrics: Vec<&Metric> = all_metrics.iter() let service_metrics: Vec<&Metric> = all_metrics
.iter()
.filter(|m| m.name.starts_with("service_")) .filter(|m| m.name.starts_with("service_"))
.copied() .copied()
.collect(); .collect();
let all_backup_metrics: Vec<&Metric> = all_metrics.iter() let all_backup_metrics: Vec<&Metric> = all_metrics
.iter()
.filter(|m| m.name.starts_with("backup_")) .filter(|m| m.name.starts_with("backup_"))
.copied() .copied()
.collect(); .collect();
// Now get host widgets and update them // Now get host widgets and update them
let host_widgets = self.get_or_create_host_widgets(&hostname); let host_widgets = self.get_or_create_host_widgets(&hostname);
host_widgets.cpu_widget.update_from_metrics(&cpu_metrics); host_widgets.cpu_widget.update_from_metrics(&cpu_metrics);
host_widgets.memory_widget.update_from_metrics(&memory_metrics); host_widgets
host_widgets.services_widget.update_from_metrics(&service_metrics); .memory_widget
host_widgets.backup_widget.update_from_metrics(&all_backup_metrics); .update_from_metrics(&memory_metrics);
host_widgets
.services_widget
.update_from_metrics(&service_metrics);
host_widgets
.backup_widget
.update_from_metrics(&all_backup_metrics);
host_widgets.last_update = Some(Instant::now()); host_widgets.last_update = Some(Instant::now());
} }
} }
} }
/// Update available hosts /// Update available hosts with localhost prioritization
pub fn update_hosts(&mut self, hosts: Vec<String>) { pub fn update_hosts(&mut self, hosts: Vec<String>) {
self.available_hosts = hosts; // Get the current hostname (localhost)
let localhost = gethostname::gethostname().to_string_lossy().to_string();
// Set current host if none selected
// Sort hosts with localhost first, then others in predefined order
let predefined_order = vec!["cmbox", "labbox", "simonbox", "steambox", "srv01", "srv02"];
let mut sorted_hosts = Vec::new();
// Add localhost first if it's available
if hosts.contains(&localhost) {
sorted_hosts.push(localhost.clone());
}
// Add remaining hosts in predefined order
for predefined_host in &predefined_order {
if hosts.contains(&predefined_host.to_string()) && *predefined_host != localhost {
sorted_hosts.push(predefined_host.to_string());
}
}
// Add any remaining hosts not in predefined order
for host in &hosts {
if !sorted_hosts.contains(host) {
sorted_hosts.push(host.clone());
}
}
self.available_hosts = sorted_hosts;
// Always select localhost first if available, otherwise use first available host
if self.current_host.is_none() && !self.available_hosts.is_empty() { if self.current_host.is_none() && !self.available_hosts.is_empty() {
self.current_host = Some(self.available_hosts[0].clone()); self.current_host = Some(self.available_hosts[0].clone());
self.host_index = 0; self.host_index = 0;
} }
// If current host is not in the new list, reset to first available
if let Some(ref current) = self.current_host {
if !self.available_hosts.contains(current) && !self.available_hosts.is_empty() {
self.current_host = Some(self.available_hosts[0].clone());
self.host_index = 0;
} else if let Some(index) = self.available_hosts.iter().position(|h| h == current) {
self.host_index = index;
}
}
} }
/// Handle keyboard input /// Handle keyboard input
pub fn handle_input(&mut self, event: Event) -> Result<()> { pub fn handle_input(&mut self, event: Event) -> Result<()> {
if let Event::Key(key) = event { if let Event::Key(key) = event {
@ -148,119 +201,163 @@ impl TuiApp {
} }
Ok(()) Ok(())
} }
/// Navigate between hosts /// Navigate between hosts
fn navigate_host(&mut self, direction: i32) { fn navigate_host(&mut self, direction: i32) {
if self.available_hosts.is_empty() { if self.available_hosts.is_empty() {
return; return;
} }
let len = self.available_hosts.len(); let len = self.available_hosts.len();
if direction > 0 { if direction > 0 {
self.host_index = (self.host_index + 1) % len; self.host_index = (self.host_index + 1) % len;
} else { } else {
self.host_index = if self.host_index == 0 { len - 1 } else { self.host_index - 1 }; self.host_index = if self.host_index == 0 {
len - 1
} else {
self.host_index - 1
};
} }
self.current_host = Some(self.available_hosts[self.host_index].clone()); self.current_host = Some(self.available_hosts[self.host_index].clone());
info!("Switched to host: {}", self.current_host.as_ref().unwrap()); info!("Switched to host: {}", self.current_host.as_ref().unwrap());
} }
/// Render the dashboard (real btop-style multi-panel layout) /// Render the dashboard (real btop-style multi-panel layout)
pub fn render(&mut self, frame: &mut Frame, metric_store: &MetricStore) { pub fn render(&mut self, frame: &mut Frame, metric_store: &MetricStore) {
let size = frame.size(); let size = frame.size();
// Clear background to true black like btop // Clear background to true black like btop
frame.render_widget( frame.render_widget(
Block::default().style(Style::default().bg(Theme::background())), Block::default().style(Style::default().bg(Theme::background())),
size size,
); );
// Create real btop-style layout: multi-panel with borders // Create real btop-style layout: multi-panel with borders
// Top section: title bar // Top section: title bar
// Bottom section: split into left (mem + disks) and right (CPU + processes) // Bottom section: split into left (mem + disks) and right (CPU + processes)
let main_chunks = Layout::default() let main_chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([ .constraints([
Constraint::Length(1), // Title bar Constraint::Length(1), // Title bar
Constraint::Min(0), // Main content area Constraint::Min(0), // Main content area
]) ])
.split(size); .split(size);
// New layout: left panels | right services (100% height) // New layout: left panels | right services (100% height)
let content_chunks = ratatui::layout::Layout::default() let content_chunks = ratatui::layout::Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([ .constraints([
Constraint::Percentage(ThemeLayout::LEFT_PANEL_WIDTH), // Left side: system, backup Constraint::Percentage(ThemeLayout::LEFT_PANEL_WIDTH), // Left side: system, backup
Constraint::Percentage(ThemeLayout::RIGHT_PANEL_WIDTH), // Right side: services (100% height) Constraint::Percentage(ThemeLayout::RIGHT_PANEL_WIDTH), // Right side: services (100% height)
]) ])
.split(main_chunks[1]); .split(main_chunks[1]);
// Left side: system on top, backup on bottom (equal height) // Left side: system on top, backup on bottom (equal height)
let left_chunks = ratatui::layout::Layout::default() let left_chunks = ratatui::layout::Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([ .constraints([
Constraint::Percentage(ThemeLayout::SYSTEM_PANEL_HEIGHT), // System section Constraint::Percentage(ThemeLayout::SYSTEM_PANEL_HEIGHT), // System section
Constraint::Percentage(ThemeLayout::BACKUP_PANEL_HEIGHT), // Backup section Constraint::Percentage(ThemeLayout::BACKUP_PANEL_HEIGHT), // Backup section
]) ])
.split(content_chunks[0]); .split(content_chunks[0]);
// Render title bar // Render title bar
self.render_btop_title(frame, main_chunks[0]); self.render_btop_title(frame, main_chunks[0], metric_store);
// Render new panel layout // Render new panel layout
self.render_system_panel(frame, left_chunks[0], metric_store); self.render_system_panel(frame, left_chunks[0], metric_store);
self.render_backup_panel(frame, left_chunks[1]); self.render_backup_panel(frame, left_chunks[1]);
// Render services widget for current host // Render services widget for current host
if let Some(hostname) = self.current_host.clone() { if let Some(hostname) = self.current_host.clone() {
let host_widgets = self.get_or_create_host_widgets(&hostname); let host_widgets = self.get_or_create_host_widgets(&hostname);
host_widgets.services_widget.render(frame, content_chunks[1]); // Services takes full right side host_widgets
.services_widget
.render(frame, content_chunks[1]); // Services takes full right side
} }
} }
/// Render btop-style minimal title /// Render btop-style minimal title with host status colors
fn render_btop_title(&self, frame: &mut Frame, area: Rect) { fn render_btop_title(&self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) {
use ratatui::text::{Line, Span};
use ratatui::style::Modifier; use ratatui::style::Modifier;
use ratatui::text::{Line, Span};
use theme::StatusIcons;
if self.available_hosts.is_empty() { if self.available_hosts.is_empty() {
let title_text = "cm-dashboard • no hosts discovered"; let title_text = "cm-dashboard • no hosts discovered";
let title = Paragraph::new(title_text) let title = Paragraph::new(title_text).style(Typography::title());
.style(Typography::title());
frame.render_widget(title, area); frame.render_widget(title, area);
return; return;
} }
// Create spans for each host // Create spans for each host with status indicators
let mut spans = vec![Span::styled("cm-dashboard • ", Typography::title())]; let mut spans = vec![Span::styled("cm-dashboard • ", Typography::title())];
for (i, host) in self.available_hosts.iter().enumerate() { for (i, host) in self.available_hosts.iter().enumerate() {
if i > 0 { if i > 0 {
spans.push(Span::styled(" ", Typography::title())); spans.push(Span::styled(" ", Typography::title()));
} }
// Calculate overall host status from metrics
let host_status = self.calculate_host_status(host, metric_store);
let status_icon = StatusIcons::get_icon(host_status);
let status_color = Theme::status_color(host_status);
// Add status icon
spans.push(Span::styled(
format!("{} ", status_icon),
Style::default().fg(status_color),
));
if Some(host) == self.current_host.as_ref() { if Some(host) == self.current_host.as_ref() {
// Selected host in bold bright white // Selected host in bold bright white
spans.push(Span::styled( spans.push(Span::styled(
host.clone(), host.clone(),
Typography::title().add_modifier(Modifier::BOLD) Typography::title().add_modifier(Modifier::BOLD),
)); ));
} else { } else {
// Other hosts in normal style // Other hosts in normal style with status color
spans.push(Span::styled(host.clone(), Typography::title())); spans.push(Span::styled(
host.clone(),
Style::default().fg(status_color),
));
} }
} }
let title_line = Line::from(spans); let title_line = Line::from(spans);
let title = Paragraph::new(vec![title_line]); let title = Paragraph::new(vec![title_line]);
frame.render_widget(title, area); frame.render_widget(title, area);
} }
/// Calculate overall status for a host based on its metrics
fn calculate_host_status(&self, hostname: &str, metric_store: &MetricStore) -> Status {
let metrics = metric_store.get_metrics_for_host(hostname);
if metrics.is_empty() {
return Status::Unknown;
}
// Check if any metric is critical
if metrics.iter().any(|m| m.status == Status::Critical) {
return Status::Critical;
}
// Check if any metric is warning
if metrics.iter().any(|m| m.status == Status::Warning) {
return Status::Warning;
}
// Check if all metrics are ok
if metrics.iter().all(|m| m.status == Status::Ok) {
return Status::Ok;
}
// Default to unknown if mixed statuses
Status::Unknown
}
fn render_system_panel(&mut self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) { fn render_system_panel(&mut self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) {
let system_block = Components::widget_block("system"); let system_block = Components::widget_block("system");
let inner_area = system_block.inner(area); let inner_area = system_block.inner(area);
@ -268,12 +365,12 @@ impl TuiApp {
let content_chunks = ratatui::layout::Layout::default() let content_chunks = ratatui::layout::Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([ .constraints([
Constraint::Length(ThemeLayout::CPU_SECTION_HEIGHT), // CPU section (title, load) Constraint::Length(ThemeLayout::CPU_SECTION_HEIGHT), // CPU section (title, load)
Constraint::Length(ThemeLayout::MEMORY_SECTION_HEIGHT), // Memory section (title, used, /tmp) Constraint::Length(ThemeLayout::MEMORY_SECTION_HEIGHT), // Memory section (title, used, /tmp)
Constraint::Min(0) // Storage section Constraint::Min(0), // Storage section
]) ])
.split(inner_area); .split(inner_area);
// Get current host widgets, create if none exist // Get current host widgets, create if none exist
if let Some(hostname) = self.current_host.clone() { if let Some(hostname) = self.current_host.clone() {
let host_widgets = self.get_or_create_host_widgets(&hostname); let host_widgets = self.get_or_create_host_widgets(&hostname);
@ -282,20 +379,19 @@ impl TuiApp {
} }
self.render_storage_section(frame, content_chunks[2], metric_store); self.render_storage_section(frame, content_chunks[2], metric_store);
} }
fn render_backup_panel(&mut self, frame: &mut Frame, area: Rect) { fn render_backup_panel(&mut self, frame: &mut Frame, area: Rect) {
let backup_block = Components::widget_block("backup"); let backup_block = Components::widget_block("backup");
let inner_area = backup_block.inner(area); let inner_area = backup_block.inner(area);
frame.render_widget(backup_block, area); frame.render_widget(backup_block, area);
// Get current host widgets for backup widget // Get current host widgets for backup widget
if let Some(hostname) = self.current_host.clone() { if let Some(hostname) = self.current_host.clone() {
let host_widgets = self.get_or_create_host_widgets(&hostname); let host_widgets = self.get_or_create_host_widgets(&hostname);
host_widgets.backup_widget.render(frame, inner_area); host_widgets.backup_widget.render(frame, inner_area);
} }
} }
fn render_storage_section(&self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) { fn render_storage_section(&self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) {
if area.height < 2 { if area.height < 2 {
return; return;
@ -303,11 +399,12 @@ impl TuiApp {
if let Some(ref hostname) = self.current_host { if let Some(ref hostname) = self.current_host {
// Get disk count to determine how many disks to display // Get disk count to determine how many disks to display
let disk_count = if let Some(count_metric) = metric_store.get_metric(hostname, "disk_count") { let disk_count =
count_metric.value.as_i64().unwrap_or(0) as usize if let Some(count_metric) = metric_store.get_metric(hostname, "disk_count") {
} else { count_metric.value.as_i64().unwrap_or(0) as usize
0 } else {
}; 0
};
if disk_count == 0 { if disk_count == 0 {
// No disks found - show error/waiting message // No disks found - show error/waiting message
@ -319,19 +416,26 @@ impl TuiApp {
let disk_title = Paragraph::new("Storage:").style(Typography::widget_title()); let disk_title = Paragraph::new("Storage:").style(Typography::widget_title());
frame.render_widget(disk_title, content_chunks[0]); frame.render_widget(disk_title, content_chunks[0]);
let no_disks_spans = StatusIcons::create_status_spans(Status::Unknown, "No mounted disks detected"); let no_disks_spans =
StatusIcons::create_status_spans(Status::Unknown, "No mounted disks detected");
let no_disks_para = Paragraph::new(ratatui::text::Line::from(no_disks_spans)); let no_disks_para = Paragraph::new(ratatui::text::Line::from(no_disks_spans));
frame.render_widget(no_disks_para, content_chunks[1]); frame.render_widget(no_disks_para, content_chunks[1]);
return; return;
} }
// Group disks by physical device // Group disks by physical device
let mut physical_devices: std::collections::HashMap<String, Vec<usize>> = std::collections::HashMap::new(); let mut physical_devices: std::collections::HashMap<String, Vec<usize>> =
std::collections::HashMap::new();
for disk_index in 0..disk_count { for disk_index in 0..disk_count {
if let Some(physical_device_metric) = metric_store.get_metric(hostname, &format!("disk_{}_physical_device", disk_index)) { if let Some(physical_device_metric) = metric_store
.get_metric(hostname, &format!("disk_{}_physical_device", disk_index))
{
let physical_device = physical_device_metric.value.as_string(); let physical_device = physical_device_metric.value.as_string();
physical_devices.entry(physical_device).or_insert_with(Vec::new).push(disk_index); physical_devices
.entry(physical_device)
.or_insert_with(Vec::new)
.push(disk_index);
} }
} }
@ -342,7 +446,7 @@ impl TuiApp {
} }
let available_lines = area.height as usize; let available_lines = area.height as usize;
// Create constraints dynamically based on physical devices // Create constraints dynamically based on physical devices
let mut constraints = Vec::new(); let mut constraints = Vec::new();
let mut devices_to_show = Vec::new(); let mut devices_to_show = Vec::new();
@ -352,14 +456,14 @@ impl TuiApp {
let lines_for_this_device = 2 + partitions.len(); let lines_for_this_device = 2 + partitions.len();
if current_line + lines_for_this_device <= available_lines { if current_line + lines_for_this_device <= available_lines {
devices_to_show.push((physical_device.clone(), partitions.clone())); devices_to_show.push((physical_device.clone(), partitions.clone()));
// Add constraints for this device // Add constraints for this device
constraints.push(Constraint::Length(1)); // Device title constraints.push(Constraint::Length(1)); // Device title
constraints.push(Constraint::Length(1)); // Health line constraints.push(Constraint::Length(1)); // Health line
for _ in 0..partitions.len() { for _ in 0..partitions.len() {
constraints.push(Constraint::Length(1)); // Usage line per partition constraints.push(Constraint::Length(1)); // Usage line per partition
} }
current_line += lines_for_this_device; current_line += lines_for_this_device;
} else { } else {
break; // Can't fit more devices break; // Can't fit more devices
@ -382,16 +486,22 @@ impl TuiApp {
for (physical_device, partitions) in &devices_to_show { for (physical_device, partitions) in &devices_to_show {
// Device title // Device title
let disk_title_text = format!("Disk {}:", physical_device); let disk_title_text = format!("Disk {}:", physical_device);
let disk_title_para = Paragraph::new(disk_title_text).style(Typography::widget_title()); let disk_title_para =
Paragraph::new(disk_title_text).style(Typography::widget_title());
frame.render_widget(disk_title_para, content_chunks[chunk_index]); frame.render_widget(disk_title_para, content_chunks[chunk_index]);
chunk_index += 1; chunk_index += 1;
// Health status (one per physical device) // Health status (one per physical device)
let smart_health = metric_store.get_metric(hostname, &format!("disk_smart_{}_health", physical_device)) let smart_health = metric_store
.get_metric(hostname, &format!("disk_smart_{}_health", physical_device))
.map(|m| (m.value.as_string(), m.status)) .map(|m| (m.value.as_string(), m.status))
.unwrap_or_else(|| ("Unknown".to_string(), Status::Unknown)); .unwrap_or_else(|| ("Unknown".to_string(), Status::Unknown));
let smart_temp = metric_store.get_metric(hostname, &format!("disk_smart_{}_temperature", physical_device)) let smart_temp = metric_store
.get_metric(
hostname,
&format!("disk_smart_{}_temperature", physical_device),
)
.and_then(|m| m.value.as_f32()); .and_then(|m| m.value.as_f32());
let temp_text = if let Some(temp) = smart_temp { let temp_text = if let Some(temp) = smart_temp {
@ -402,7 +512,7 @@ impl TuiApp {
let health_spans = StatusIcons::create_status_spans( let health_spans = StatusIcons::create_status_spans(
smart_health.1, smart_health.1,
&format!("Health: {}{}", smart_health.0, temp_text) &format!("Health: {}{}", smart_health.0, temp_text),
); );
let health_para = Paragraph::new(ratatui::text::Line::from(health_spans)); let health_para = Paragraph::new(ratatui::text::Line::from(health_spans));
frame.render_widget(health_para, content_chunks[chunk_index]); frame.render_widget(health_para, content_chunks[chunk_index]);
@ -410,27 +520,32 @@ impl TuiApp {
// Usage lines (one per partition/mount point) // Usage lines (one per partition/mount point)
for &disk_index in partitions { for &disk_index in partitions {
let mount_point = metric_store.get_metric(hostname, &format!("disk_{}_mount_point", disk_index)) let mount_point = metric_store
.get_metric(hostname, &format!("disk_{}_mount_point", disk_index))
.map(|m| m.value.as_string()) .map(|m| m.value.as_string())
.unwrap_or("?".to_string()); .unwrap_or("?".to_string());
let usage_percent = metric_store.get_metric(hostname, &format!("disk_{}_usage_percent", disk_index)) let usage_percent = metric_store
.get_metric(hostname, &format!("disk_{}_usage_percent", disk_index))
.and_then(|m| m.value.as_f32()) .and_then(|m| m.value.as_f32())
.unwrap_or(0.0); .unwrap_or(0.0);
let used_gb = metric_store.get_metric(hostname, &format!("disk_{}_used_gb", disk_index)) let used_gb = metric_store
.get_metric(hostname, &format!("disk_{}_used_gb", disk_index))
.and_then(|m| m.value.as_f32()) .and_then(|m| m.value.as_f32())
.unwrap_or(0.0); .unwrap_or(0.0);
let total_gb = metric_store.get_metric(hostname, &format!("disk_{}_total_gb", disk_index)) let total_gb = metric_store
.get_metric(hostname, &format!("disk_{}_total_gb", disk_index))
.and_then(|m| m.value.as_f32()) .and_then(|m| m.value.as_f32())
.unwrap_or(0.0); .unwrap_or(0.0);
let usage_status = metric_store.get_metric(hostname, &format!("disk_{}_usage_percent", disk_index)) let usage_status = metric_store
.get_metric(hostname, &format!("disk_{}_usage_percent", disk_index))
.map(|m| m.status) .map(|m| m.status)
.unwrap_or(Status::Unknown); .unwrap_or(Status::Unknown);
// Format mount point for usage line // Format mount point for usage line
let mount_display = if mount_point == "/" { let mount_display = if mount_point == "/" {
"root".to_string() "root".to_string()
} else if mount_point == "/boot" { } else if mount_point == "/boot" {
@ -443,7 +558,10 @@ impl TuiApp {
let usage_spans = StatusIcons::create_status_spans( let usage_spans = StatusIcons::create_status_spans(
usage_status, usage_status,
&format!("Usage @{}: {:.1}% • {:.1}/{:.1} GB", mount_display, usage_percent, used_gb, total_gb) &format!(
"Usage @{}: {:.1}% • {:.1}/{:.1} GB",
mount_display, usage_percent, used_gb, total_gb
),
); );
let usage_para = Paragraph::new(ratatui::text::Line::from(usage_spans)); let usage_para = Paragraph::new(ratatui::text::Line::from(usage_spans));
frame.render_widget(usage_para, content_chunks[chunk_index]); frame.render_widget(usage_para, content_chunks[chunk_index]);
@ -455,9 +573,11 @@ impl TuiApp {
if devices_to_show.len() < physical_devices.len() { if devices_to_show.len() < physical_devices.len() {
if let Some(last_chunk) = content_chunks.last() { if let Some(last_chunk) = content_chunks.last() {
let truncated_count = physical_devices.len() - devices_to_show.len(); let truncated_count = physical_devices.len() - devices_to_show.len();
let truncated_text = format!("... and {} more disk{}", let truncated_text = format!(
"... and {} more disk{}",
truncated_count, truncated_count,
if truncated_count == 1 { "" } else { "s" }); if truncated_count == 1 { "" } else { "s" }
);
let truncated_para = Paragraph::new(truncated_text).style(Typography::muted()); let truncated_para = Paragraph::new(truncated_text).style(Typography::muted());
frame.render_widget(truncated_para, *last_chunk); frame.render_widget(truncated_para, *last_chunk);
} }
@ -472,11 +592,10 @@ impl TuiApp {
let disk_title = Paragraph::new("Storage:").style(Typography::widget_title()); let disk_title = Paragraph::new("Storage:").style(Typography::widget_title());
frame.render_widget(disk_title, content_chunks[0]); frame.render_widget(disk_title, content_chunks[0]);
let no_host_spans = StatusIcons::create_status_spans(Status::Unknown, "No host connected"); let no_host_spans =
StatusIcons::create_status_spans(Status::Unknown, "No host connected");
let no_host_para = Paragraph::new(ratatui::text::Line::from(no_host_spans)); let no_host_para = Paragraph::new(ratatui::text::Line::from(no_host_spans));
frame.render_widget(no_host_para, content_chunks[1]); frame.render_widget(no_host_para, content_chunks[1]);
} }
} }
} }