Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 516d159d2f | |||
| 1656f20e96 | |||
| dcd350ec2c | |||
| a34b095857 | |||
| 7362464b46 | |||
| c8b79576fa | |||
| f53df5440b | |||
| d1b0e2c431 | |||
| b1719a60fc | |||
| d922e8d6f3 | |||
| 407bc9dbc2 | |||
| 3c278351c9 | |||
| 8da4522d85 | |||
| 5b1e39cfca | |||
| ffecbc3166 |
6
Cargo.lock
generated
6
Cargo.lock
generated
@@ -279,7 +279,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.260"
|
version = "0.1.276"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
@@ -301,7 +301,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.260"
|
version = "0.1.275"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@@ -325,7 +325,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.260"
|
version = "0.1.275"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.261"
|
version = "0.1.275"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use cm_dashboard_shared::{AgentData, BackupData, BackupDiskData, Status};
|
use cm_dashboard_shared::{AgentData, BackupData, BackupRepositoryData, Status};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use tracing::{debug, warn};
|
use tracing::{debug, warn};
|
||||||
@@ -21,7 +21,7 @@ impl BackupCollector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scan directory for all backup status files
|
/// Scan directory for backup status file (nfs-backup.toml)
|
||||||
async fn scan_status_files(&self) -> Result<Vec<PathBuf>, CollectorError> {
|
async fn scan_status_files(&self) -> Result<Vec<PathBuf>, CollectorError> {
|
||||||
let status_path = Path::new(&self.status_dir);
|
let status_path = Path::new(&self.status_dir);
|
||||||
|
|
||||||
@@ -30,30 +30,15 @@ impl BackupCollector {
|
|||||||
return Ok(Vec::new());
|
return Ok(Vec::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut status_files = Vec::new();
|
// Look for nfs-backup.toml (new NFS-based backup)
|
||||||
|
let nfs_backup_file = status_path.join("nfs-backup.toml");
|
||||||
match fs::read_dir(status_path) {
|
if nfs_backup_file.exists() {
|
||||||
Ok(entries) => {
|
return Ok(vec![nfs_backup_file]);
|
||||||
for entry in entries {
|
|
||||||
if let Ok(entry) = entry {
|
|
||||||
let path = entry.path();
|
|
||||||
if path.is_file() {
|
|
||||||
if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
|
|
||||||
if filename.starts_with("backup-status-") && filename.ends_with(".toml") {
|
|
||||||
status_files.push(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Failed to read backup status directory: {}", e);
|
|
||||||
return Ok(Vec::new());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(status_files)
|
// No backup status file found
|
||||||
|
debug!("No nfs-backup.toml found in {}", self.status_dir);
|
||||||
|
Ok(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read a single backup status file
|
/// Read a single backup status file
|
||||||
@@ -76,24 +61,13 @@ impl BackupCollector {
|
|||||||
/// Calculate backup status from TOML status field
|
/// Calculate backup status from TOML status field
|
||||||
fn calculate_backup_status(status_str: &str) -> Status {
|
fn calculate_backup_status(status_str: &str) -> Status {
|
||||||
match status_str.to_lowercase().as_str() {
|
match status_str.to_lowercase().as_str() {
|
||||||
"success" => Status::Ok,
|
"success" | "completed" => Status::Ok,
|
||||||
"warning" => Status::Warning,
|
"warning" => Status::Warning,
|
||||||
"failed" | "error" => Status::Critical,
|
"failed" | "error" => Status::Critical,
|
||||||
_ => Status::Unknown,
|
_ => Status::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate usage status from disk usage percentage
|
|
||||||
fn calculate_usage_status(usage_percent: f32) -> Status {
|
|
||||||
if usage_percent < 80.0 {
|
|
||||||
Status::Ok
|
|
||||||
} else if usage_percent < 90.0 {
|
|
||||||
Status::Warning
|
|
||||||
} else {
|
|
||||||
Status::Critical
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convert BackupStatusToml to BackupData and populate AgentData
|
/// Convert BackupStatusToml to BackupData and populate AgentData
|
||||||
async fn populate_backup_data(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
async fn populate_backup_data(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||||
let status_files = self.scan_status_files().await?;
|
let status_files = self.scan_status_files().await?;
|
||||||
@@ -101,76 +75,47 @@ impl BackupCollector {
|
|||||||
if status_files.is_empty() {
|
if status_files.is_empty() {
|
||||||
debug!("No backup status files found");
|
debug!("No backup status files found");
|
||||||
agent_data.backup = BackupData {
|
agent_data.backup = BackupData {
|
||||||
|
last_backup_time: None,
|
||||||
|
backup_status: Status::Unknown,
|
||||||
repositories: Vec::new(),
|
repositories: Vec::new(),
|
||||||
repository_status: Status::Unknown,
|
|
||||||
disks: Vec::new(),
|
|
||||||
};
|
};
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut all_repositories = HashSet::new();
|
// Aggregate repository data across all backup status files
|
||||||
let mut disks = Vec::new();
|
let mut repo_map: HashMap<String, BackupRepositoryData> = HashMap::new();
|
||||||
let mut worst_status = Status::Ok;
|
let mut worst_status = Status::Ok;
|
||||||
|
let mut latest_backup_time: Option<String> = None;
|
||||||
|
|
||||||
for status_file in status_files {
|
for status_file in status_files {
|
||||||
match self.read_status_file(&status_file).await {
|
match self.read_status_file(&status_file).await {
|
||||||
Ok(backup_status) => {
|
Ok(backup_status) => {
|
||||||
// Collect all service names
|
|
||||||
for service_name in backup_status.services.keys() {
|
|
||||||
all_repositories.insert(service_name.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate backup status
|
// Calculate backup status
|
||||||
let backup_status_enum = Self::calculate_backup_status(&backup_status.status);
|
let backup_status_enum = Self::calculate_backup_status(&backup_status.status);
|
||||||
|
worst_status = worst_status.max(backup_status_enum);
|
||||||
|
|
||||||
// Calculate usage status from disk space
|
// Track latest backup time
|
||||||
let (usage_percent, used_gb, total_gb, usage_status) = if let Some(disk_space) = &backup_status.disk_space {
|
if latest_backup_time.is_none() || Some(&backup_status.start_time) > latest_backup_time.as_ref() {
|
||||||
let usage_pct = disk_space.usage_percent as f32;
|
latest_backup_time = Some(backup_status.start_time.clone());
|
||||||
(
|
}
|
||||||
usage_pct,
|
|
||||||
disk_space.used_gb as f32,
|
|
||||||
disk_space.total_gb as f32,
|
|
||||||
Self::calculate_usage_status(usage_pct),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(0.0, 0.0, 0.0, Status::Unknown)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Update worst status
|
// Process each service in this backup
|
||||||
worst_status = worst_status.max(backup_status_enum).max(usage_status);
|
for (service_name, service_status) in backup_status.services {
|
||||||
|
// Convert bytes to GB
|
||||||
|
let repo_size_gb = service_status.repo_size_bytes as f32 / 1_073_741_824.0;
|
||||||
|
|
||||||
// Build service list for this disk
|
// Calculate service status
|
||||||
let services: Vec<String> = backup_status.services.keys().cloned().collect();
|
let service_status_enum = Self::calculate_backup_status(&service_status.status);
|
||||||
|
worst_status = worst_status.max(service_status_enum);
|
||||||
|
|
||||||
// Get min and max archive counts to detect inconsistencies
|
// Update or insert repository data
|
||||||
let archives_min: i64 = backup_status.services.values()
|
repo_map.insert(service_name.clone(), BackupRepositoryData {
|
||||||
.map(|service| service.archive_count)
|
name: service_name,
|
||||||
.min()
|
archive_count: service_status.archive_count,
|
||||||
.unwrap_or(0);
|
repo_size_gb,
|
||||||
|
status: service_status_enum,
|
||||||
let archives_max: i64 = backup_status.services.values()
|
});
|
||||||
.map(|service| service.archive_count)
|
}
|
||||||
.max()
|
|
||||||
.unwrap_or(0);
|
|
||||||
|
|
||||||
// Create disk data
|
|
||||||
let disk_data = BackupDiskData {
|
|
||||||
serial: backup_status.disk_serial_number.unwrap_or_else(|| "Unknown".to_string()),
|
|
||||||
product_name: backup_status.disk_product_name,
|
|
||||||
wear_percent: backup_status.disk_wear_percent,
|
|
||||||
temperature_celsius: None, // Not available in current TOML
|
|
||||||
last_backup_time: Some(backup_status.start_time),
|
|
||||||
backup_status: backup_status_enum,
|
|
||||||
disk_usage_percent: usage_percent,
|
|
||||||
disk_used_gb: used_gb,
|
|
||||||
disk_total_gb: total_gb,
|
|
||||||
usage_status,
|
|
||||||
services,
|
|
||||||
archives_min,
|
|
||||||
archives_max,
|
|
||||||
};
|
|
||||||
|
|
||||||
disks.push(disk_data);
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Failed to read backup status file {:?}: {}", status_file, e);
|
warn!("Failed to read backup status file {:?}: {}", status_file, e);
|
||||||
@@ -178,12 +123,14 @@ impl BackupCollector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let repositories: Vec<String> = all_repositories.into_iter().collect();
|
// Convert HashMap to sorted Vec
|
||||||
|
let mut repositories: Vec<BackupRepositoryData> = repo_map.into_values().collect();
|
||||||
|
repositories.sort_by(|a, b| a.name.cmp(&b.name));
|
||||||
|
|
||||||
agent_data.backup = BackupData {
|
agent_data.backup = BackupData {
|
||||||
|
last_backup_time: latest_backup_time,
|
||||||
|
backup_status: worst_status,
|
||||||
repositories,
|
repositories,
|
||||||
repository_status: worst_status,
|
|
||||||
disks,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ impl DiskCollector {
|
|||||||
let mut cmd = TokioCommand::new("lsblk");
|
let mut cmd = TokioCommand::new("lsblk");
|
||||||
cmd.args(&["-rn", "-o", "NAME,MOUNTPOINT"]);
|
cmd.args(&["-rn", "-o", "NAME,MOUNTPOINT"]);
|
||||||
|
|
||||||
let output = run_command_with_timeout(cmd, 2).await
|
let output = run_command_with_timeout(cmd, 10).await
|
||||||
.map_err(|e| CollectorError::SystemRead {
|
.map_err(|e| CollectorError::SystemRead {
|
||||||
path: "block devices".to_string(),
|
path: "block devices".to_string(),
|
||||||
error: e.to_string(),
|
error: e.to_string(),
|
||||||
@@ -184,7 +184,7 @@ impl DiskCollector {
|
|||||||
/// Get filesystem info for a single mount point
|
/// Get filesystem info for a single mount point
|
||||||
fn get_filesystem_info(&self, mount_point: &str) -> Result<(u64, u64), CollectorError> {
|
fn get_filesystem_info(&self, mount_point: &str) -> Result<(u64, u64), CollectorError> {
|
||||||
let output = StdCommand::new("timeout")
|
let output = StdCommand::new("timeout")
|
||||||
.args(&["2", "df", "--block-size=1", mount_point])
|
.args(&["10", "df", "--block-size=1", mount_point])
|
||||||
.output()
|
.output()
|
||||||
.map_err(|e| CollectorError::SystemRead {
|
.map_err(|e| CollectorError::SystemRead {
|
||||||
path: format!("df {}", mount_point),
|
path: format!("df {}", mount_point),
|
||||||
@@ -433,7 +433,7 @@ impl DiskCollector {
|
|||||||
cmd.args(&["-a", &format!("/dev/{}", drive_name)]);
|
cmd.args(&["-a", &format!("/dev/{}", drive_name)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let output = run_command_with_timeout(cmd, 3).await
|
let output = run_command_with_timeout(cmd, 15).await
|
||||||
.map_err(|e| CollectorError::SystemRead {
|
.map_err(|e| CollectorError::SystemRead {
|
||||||
path: format!("SMART data for {}", drive_name),
|
path: format!("SMART data for {}", drive_name),
|
||||||
error: e.to_string(),
|
error: e.to_string(),
|
||||||
@@ -772,7 +772,7 @@ impl DiskCollector {
|
|||||||
fn get_drive_info_for_path(&self, path: &str) -> anyhow::Result<PoolDrive> {
|
fn get_drive_info_for_path(&self, path: &str) -> anyhow::Result<PoolDrive> {
|
||||||
// Use lsblk to find the backing device with timeout
|
// Use lsblk to find the backing device with timeout
|
||||||
let output = StdCommand::new("timeout")
|
let output = StdCommand::new("timeout")
|
||||||
.args(&["2", "lsblk", "-rn", "-o", "NAME,MOUNTPOINT"])
|
.args(&["10", "lsblk", "-rn", "-o", "NAME,MOUNTPOINT"])
|
||||||
.output()
|
.output()
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to run lsblk: {}", e))?;
|
.map_err(|e| anyhow::anyhow!("Failed to run lsblk: {}", e))?;
|
||||||
|
|
||||||
|
|||||||
@@ -217,14 +217,46 @@ impl SystemdCollector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if service_name == "tailscaled" && status_info.active_state == "active" {
|
if service_name == "tailscaled" && status_info.active_state == "active" {
|
||||||
// Add Tailscale connection method as sub-service
|
// Add Tailscale peers with their connection methods as sub-services
|
||||||
if let Some(conn_method) = self.get_tailscale_connection_method() {
|
let peers = self.get_tailscale_peers();
|
||||||
|
for (peer_name, conn_method) in peers {
|
||||||
let metrics = Vec::new();
|
let metrics = Vec::new();
|
||||||
sub_services.push(SubServiceData {
|
sub_services.push(SubServiceData {
|
||||||
name: format!("Connection: {}", conn_method),
|
name: format!("{}: {}", peer_name, conn_method),
|
||||||
service_status: Status::Info,
|
service_status: Status::Info,
|
||||||
metrics,
|
metrics,
|
||||||
service_type: "tailscale_connection".to_string(),
|
service_type: "tailscale_peer".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if service_name == "nfs-server" && status_info.active_state == "active" {
|
||||||
|
// Add NFS exports as sub-services
|
||||||
|
let exports = self.get_nfs_exports();
|
||||||
|
for (export_path, info) in exports {
|
||||||
|
let display = if !info.is_empty() {
|
||||||
|
format!("{} {}", export_path, info)
|
||||||
|
} else {
|
||||||
|
export_path
|
||||||
|
};
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: display,
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics: Vec::new(),
|
||||||
|
service_type: "nfs_export".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (service_name == "smbd" || service_name == "samba-smbd") && status_info.active_state == "active" {
|
||||||
|
// Add SMB shares as sub-services
|
||||||
|
let shares = self.get_smb_shares();
|
||||||
|
for (share_name, share_path, mode) in shares {
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("{}: {} {}", share_name, share_path, mode),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics: Vec::new(),
|
||||||
|
service_type: "smb_share".to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -936,50 +968,235 @@ impl SystemdCollector {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get Tailscale connection method (direct, relay, or proxy)
|
/// Get Tailscale connected peers with their connection methods
|
||||||
fn get_tailscale_connection_method(&self) -> Option<String> {
|
/// Returns a list of (device_name, connection_method) tuples
|
||||||
|
fn get_tailscale_peers(&self) -> Vec<(String, String)> {
|
||||||
match Command::new("timeout")
|
match Command::new("timeout")
|
||||||
.args(["2", "tailscale", "status", "--json"])
|
.args(["2", "tailscale", "status"])
|
||||||
.output()
|
.output()
|
||||||
{
|
{
|
||||||
Ok(output) if output.status.success() => {
|
Ok(output) if output.status.success() => {
|
||||||
let json_str = String::from_utf8_lossy(&output.stdout);
|
let status_output = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let mut peers = Vec::new();
|
||||||
|
|
||||||
if let Ok(json_data) = serde_json::from_str::<serde_json::Value>(&json_str) {
|
// Get current hostname to filter it out
|
||||||
// Look for the self peer (current node) in the peer list
|
let current_hostname = gethostname::gethostname()
|
||||||
if let Some(peers) = json_data["Peer"].as_object() {
|
.to_string_lossy()
|
||||||
// Find the first active peer connection to determine connection method
|
.to_string();
|
||||||
for (_peer_id, peer_data) in peers {
|
|
||||||
if peer_data["Active"].as_bool().unwrap_or(false) {
|
// Parse tailscale status output
|
||||||
// Check if using relay
|
// Format: IP hostname user os status
|
||||||
let relay_node = peer_data["Relay"].as_str().unwrap_or("");
|
// Example: 100.110.98.3 wslbox cm@ linux active; direct 192.168.30.227:53757
|
||||||
if !relay_node.is_empty() {
|
// Note: First line is always the current host, skip it
|
||||||
return Some("relay".to_string());
|
for (idx, line) in status_output.lines().enumerate() {
|
||||||
|
if idx == 0 {
|
||||||
|
continue; // Skip first line (current host)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if using direct connection
|
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||||
if let Some(endpoints) = peer_data["CurAddr"].as_str() {
|
if parts.len() < 5 {
|
||||||
if !endpoints.is_empty() {
|
continue; // Skip invalid lines
|
||||||
return Some("direct".to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parts[0] = IP
|
||||||
|
// parts[1] = hostname
|
||||||
|
// parts[2] = user
|
||||||
|
// parts[3] = OS
|
||||||
|
// parts[4+] = status (e.g., "active;", "direct", "192.168.30.227:53757" or "idle;" or "offline")
|
||||||
|
|
||||||
|
let hostname = parts[1];
|
||||||
|
|
||||||
|
// Skip if this is the current host (double-check in case format changes)
|
||||||
|
if hostname == current_hostname {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let status_parts = &parts[4..];
|
||||||
|
|
||||||
|
// Determine connection method from status
|
||||||
|
let connection_method = if status_parts.is_empty() {
|
||||||
|
continue; // Skip if no status
|
||||||
|
} else {
|
||||||
|
let status_str = status_parts.join(" ");
|
||||||
|
if status_str.contains("offline") {
|
||||||
|
continue; // Skip offline peers
|
||||||
|
} else if status_str.contains("direct") {
|
||||||
|
"direct"
|
||||||
|
} else if status_str.contains("relay") {
|
||||||
|
"relay"
|
||||||
|
} else if status_str.contains("idle") {
|
||||||
|
"idle"
|
||||||
|
} else if status_str.contains("active") {
|
||||||
|
"active"
|
||||||
|
} else {
|
||||||
|
continue; // Skip unknown status
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
peers.push((hostname.to_string(), connection_method.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
peers
|
||||||
|
}
|
||||||
|
_ => Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get NFS exports from exportfs
|
||||||
|
/// Returns a list of (export_path, info_string) tuples
|
||||||
|
fn get_nfs_exports(&self) -> Vec<(String, String)> {
|
||||||
|
let output = match Command::new("timeout")
|
||||||
|
.args(["2", "exportfs", "-v"])
|
||||||
|
.output()
|
||||||
|
{
|
||||||
|
Ok(output) if output.status.success() => output,
|
||||||
|
_ => return Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let exports_output = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let mut exports_map: std::collections::HashMap<String, Vec<(String, String)>> =
|
||||||
|
std::collections::HashMap::new();
|
||||||
|
let mut current_path: Option<String> = None;
|
||||||
|
|
||||||
|
for line in exports_output.lines() {
|
||||||
|
let trimmed = line.trim();
|
||||||
|
|
||||||
|
if trimmed.is_empty() || trimmed.starts_with('#') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if trimmed.starts_with('/') {
|
||||||
|
// Export path line - may have network on same line or continuation
|
||||||
|
let parts: Vec<&str> = trimmed.splitn(2, char::is_whitespace).collect();
|
||||||
|
let path = parts[0].to_string();
|
||||||
|
current_path = Some(path.clone());
|
||||||
|
|
||||||
|
// Check if network info is on the same line
|
||||||
|
if parts.len() > 1 {
|
||||||
|
let rest = parts[1].trim();
|
||||||
|
if let Some(paren_pos) = rest.find('(') {
|
||||||
|
let network = rest[..paren_pos].trim();
|
||||||
|
|
||||||
|
if let Some(end_paren) = rest.find(')') {
|
||||||
|
let options = &rest[paren_pos+1..end_paren];
|
||||||
|
let mode = if options.contains(",rw,") || options.ends_with(",rw") {
|
||||||
|
"rw"
|
||||||
|
} else {
|
||||||
|
"ro"
|
||||||
|
};
|
||||||
|
|
||||||
|
exports_map.entry(path)
|
||||||
|
.or_insert_with(Vec::new)
|
||||||
|
.push((network.to_string(), mode.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Some(ref path) = current_path {
|
||||||
|
// Continuation line with network and options
|
||||||
|
if let Some(paren_pos) = trimmed.find('(') {
|
||||||
|
let network = trimmed[..paren_pos].trim();
|
||||||
|
|
||||||
|
if let Some(end_paren) = trimmed.find(')') {
|
||||||
|
let options = &trimmed[paren_pos+1..end_paren];
|
||||||
|
let mode = if options.contains(",rw,") || options.ends_with(",rw") {
|
||||||
|
"rw"
|
||||||
|
} else {
|
||||||
|
"ro"
|
||||||
|
};
|
||||||
|
|
||||||
|
exports_map.entry(path.clone())
|
||||||
|
.or_insert_with(Vec::new)
|
||||||
|
.push((network.to_string(), mode.to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if using proxy from backend state
|
// Build display strings: "path: mode [networks]"
|
||||||
if let Some(backend_state) = json_data["BackendState"].as_str() {
|
let mut exports: Vec<(String, String)> = exports_map
|
||||||
if backend_state == "Running" {
|
.into_iter()
|
||||||
// If we're running but have no direct or relay, might be proxy
|
.map(|(path, mut entries)| {
|
||||||
// This is a fallback heuristic
|
if entries.is_empty() {
|
||||||
return Some("unknown".to_string());
|
return (path, String::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mode = entries[0].1.clone();
|
||||||
|
let networks: Vec<String> = entries.drain(..).map(|(n, _)| n).collect();
|
||||||
|
let info = format!("{} [{}]", mode, networks.join(", "));
|
||||||
|
(path, info)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
exports.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
|
exports
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get SMB shares from smb.conf
|
||||||
|
/// Returns a list of (share_name, share_path, mode) tuples
|
||||||
|
fn get_smb_shares(&self) -> Vec<(String, String, String)> {
|
||||||
|
match std::fs::read_to_string("/etc/samba/smb.conf") {
|
||||||
|
Ok(config) => {
|
||||||
|
let mut shares = Vec::new();
|
||||||
|
let mut current_share: Option<String> = None;
|
||||||
|
let mut current_path: Option<String> = None;
|
||||||
|
let mut current_mode: String = "ro".to_string(); // Default to read-only
|
||||||
|
|
||||||
|
for line in config.lines() {
|
||||||
|
let line = line.trim();
|
||||||
|
|
||||||
|
// Skip comments and empty lines
|
||||||
|
if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detect share section [sharename]
|
||||||
|
if line.starts_with('[') && line.ends_with(']') {
|
||||||
|
// Save previous share if we have both name and path
|
||||||
|
if let (Some(name), Some(path)) = (current_share.take(), current_path.take()) {
|
||||||
|
// Skip special sections
|
||||||
|
if name != "global" && name != "homes" && name != "printers" {
|
||||||
|
shares.push((name, path, current_mode.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start new share
|
||||||
|
let share_name = line[1..line.len()-1].trim().to_string();
|
||||||
|
current_share = Some(share_name);
|
||||||
|
current_path = None;
|
||||||
|
current_mode = "ro".to_string(); // Reset to default
|
||||||
|
}
|
||||||
|
// Look for path = /some/path
|
||||||
|
else if line.starts_with("path") && line.contains('=') {
|
||||||
|
if let Some(path_value) = line.split('=').nth(1) {
|
||||||
|
current_path = Some(path_value.trim().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Look for read only = yes/no
|
||||||
|
else if line.to_lowercase().starts_with("read only") && line.contains('=') {
|
||||||
|
if let Some(value) = line.split('=').nth(1) {
|
||||||
|
let val = value.trim().to_lowercase();
|
||||||
|
current_mode = if val == "no" || val == "false" { "rw" } else { "ro" }.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Look for writable = yes/no (opposite of read only)
|
||||||
|
else if line.to_lowercase().starts_with("writable") && line.contains('=') {
|
||||||
|
if let Some(value) = line.split('=').nth(1) {
|
||||||
|
let val = value.trim().to_lowercase();
|
||||||
|
current_mode = if val == "yes" || val == "true" { "rw" } else { "ro" }.to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
// Don't forget the last share
|
||||||
|
if let (Some(name), Some(path)) = (current_share, current_path) {
|
||||||
|
if name != "global" && name != "homes" && name != "printers" {
|
||||||
|
shares.push((name, path, current_mode));
|
||||||
}
|
}
|
||||||
_ => None,
|
}
|
||||||
|
|
||||||
|
shares
|
||||||
|
}
|
||||||
|
_ => Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.261"
|
version = "0.1.276"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ pub struct Dashboard {
|
|||||||
headless: bool,
|
headless: bool,
|
||||||
initial_commands_sent: std::collections::HashSet<String>,
|
initial_commands_sent: std::collections::HashSet<String>,
|
||||||
config: DashboardConfig,
|
config: DashboardConfig,
|
||||||
title_area: Rect, // Store title area for mouse event handling
|
|
||||||
system_area: Rect, // Store system area for mouse event handling
|
system_area: Rect, // Store system area for mouse event handling
|
||||||
services_area: Rect, // Store services area for mouse event handling
|
services_area: Rect, // Store services area for mouse event handling
|
||||||
}
|
}
|
||||||
@@ -124,7 +123,6 @@ impl Dashboard {
|
|||||||
headless,
|
headless,
|
||||||
initial_commands_sent: std::collections::HashSet::new(),
|
initial_commands_sent: std::collections::HashSet::new(),
|
||||||
config,
|
config,
|
||||||
title_area: Rect::default(),
|
|
||||||
system_area: Rect::default(),
|
system_area: Rect::default(),
|
||||||
services_area: Rect::default(),
|
services_area: Rect::default(),
|
||||||
})
|
})
|
||||||
@@ -138,11 +136,12 @@ impl Dashboard {
|
|||||||
let metrics_check_interval = Duration::from_millis(100); // Check for metrics every 100ms
|
let metrics_check_interval = Duration::from_millis(100); // Check for metrics every 100ms
|
||||||
let mut last_heartbeat_check = Instant::now();
|
let mut last_heartbeat_check = Instant::now();
|
||||||
let heartbeat_check_interval = Duration::from_secs(1); // Check for host connectivity every 1 second
|
let heartbeat_check_interval = Duration::from_secs(1); // Check for host connectivity every 1 second
|
||||||
|
let mut needs_render = true; // Track if we need to render
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Handle terminal events (keyboard and mouse input) only if not headless
|
// Handle terminal events (keyboard and mouse input) only if not headless
|
||||||
if !self.headless {
|
if !self.headless {
|
||||||
match event::poll(Duration::from_millis(50)) {
|
match event::poll(Duration::from_millis(200)) {
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
match event::read() {
|
match event::read() {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
@@ -152,6 +151,7 @@ impl Dashboard {
|
|||||||
// Handle keyboard input
|
// Handle keyboard input
|
||||||
match tui_app.handle_input(event) {
|
match tui_app.handle_input(event) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
needs_render = true;
|
||||||
// Check if we should quit
|
// Check if we should quit
|
||||||
if tui_app.should_quit() {
|
if tui_app.should_quit() {
|
||||||
info!("Quit requested, exiting dashboard");
|
info!("Quit requested, exiting dashboard");
|
||||||
@@ -168,10 +168,11 @@ impl Dashboard {
|
|||||||
if let Err(e) = self.handle_mouse_event(mouse_event) {
|
if let Err(e) = self.handle_mouse_event(mouse_event) {
|
||||||
error!("Error handling mouse event: {}", e);
|
error!("Error handling mouse event: {}", e);
|
||||||
}
|
}
|
||||||
|
needs_render = true;
|
||||||
}
|
}
|
||||||
Event::Resize(_width, _height) => {
|
Event::Resize(_width, _height) => {
|
||||||
// Terminal was resized - just continue and re-render
|
// Terminal was resized - mark for re-render
|
||||||
// The next render will automatically use the new size
|
needs_render = true;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -189,38 +190,6 @@ impl Dashboard {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render UI immediately after handling input for responsive feedback
|
|
||||||
if let Some(ref mut terminal) = self.terminal {
|
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
|
||||||
// Clear and autoresize terminal to handle any resize events
|
|
||||||
if let Err(e) = terminal.autoresize() {
|
|
||||||
warn!("Error autoresizing terminal: {}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check minimum terminal size to prevent panics
|
|
||||||
let size = terminal.size().unwrap_or_default();
|
|
||||||
if size.width < 90 || size.height < 15 {
|
|
||||||
// Terminal too small, show error message
|
|
||||||
let msg_text = format!("Terminal too small\n\nMinimum: 90x15\nCurrent: {}x{}", size.width, size.height);
|
|
||||||
let _ = terminal.draw(|frame| {
|
|
||||||
use ratatui::widgets::{Paragraph, Block, Borders};
|
|
||||||
use ratatui::layout::Alignment;
|
|
||||||
let msg = Paragraph::new(msg_text.clone())
|
|
||||||
.alignment(Alignment::Center)
|
|
||||||
.block(Block::default().borders(Borders::ALL));
|
|
||||||
frame.render_widget(msg, frame.size());
|
|
||||||
});
|
|
||||||
} else if let Err(e) = terminal.draw(|frame| {
|
|
||||||
let (title_area, system_area, services_area) = tui_app.render(frame, &self.metric_store);
|
|
||||||
self.title_area = title_area;
|
|
||||||
self.system_area = system_area;
|
|
||||||
self.services_area = services_area;
|
|
||||||
}) {
|
|
||||||
error!("Error rendering TUI after input: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for new metrics
|
// Check for new metrics
|
||||||
@@ -259,6 +228,8 @@ impl Dashboard {
|
|||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
tui_app.update_metrics(&mut self.metric_store);
|
tui_app.update_metrics(&mut self.metric_store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
needs_render = true; // New metrics received, need to render
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also check for command output messages
|
// Also check for command output messages
|
||||||
@@ -287,10 +258,11 @@ impl Dashboard {
|
|||||||
tui_app.update_hosts(connected_hosts);
|
tui_app.update_hosts(connected_hosts);
|
||||||
}
|
}
|
||||||
last_heartbeat_check = Instant::now();
|
last_heartbeat_check = Instant::now();
|
||||||
|
needs_render = true; // Heartbeat check happened, may have changed hosts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render TUI (only if not headless)
|
// Render TUI only when needed (not headless and something changed)
|
||||||
if !self.headless {
|
if !self.headless && needs_render {
|
||||||
if let Some(ref mut terminal) = self.terminal {
|
if let Some(ref mut terminal) = self.terminal {
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
// Clear and autoresize terminal to handle any resize events
|
// Clear and autoresize terminal to handle any resize events
|
||||||
@@ -298,22 +270,9 @@ impl Dashboard {
|
|||||||
warn!("Error autoresizing terminal: {}", e);
|
warn!("Error autoresizing terminal: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check minimum terminal size to prevent panics
|
// Render TUI regardless of terminal size
|
||||||
let size = terminal.size().unwrap_or_default();
|
if let Err(e) = terminal.draw(|frame| {
|
||||||
if size.width < 90 || size.height < 15 {
|
let (_title_area, system_area, services_area) = tui_app.render(frame, &self.metric_store);
|
||||||
// Terminal too small, show error message
|
|
||||||
let msg_text = format!("Terminal too small\n\nMinimum: 90x15\nCurrent: {}x{}", size.width, size.height);
|
|
||||||
let _ = terminal.draw(|frame| {
|
|
||||||
use ratatui::widgets::{Paragraph, Block, Borders};
|
|
||||||
use ratatui::layout::Alignment;
|
|
||||||
let msg = Paragraph::new(msg_text.clone())
|
|
||||||
.alignment(Alignment::Center)
|
|
||||||
.block(Block::default().borders(Borders::ALL));
|
|
||||||
frame.render_widget(msg, frame.size());
|
|
||||||
});
|
|
||||||
} else if let Err(e) = terminal.draw(|frame| {
|
|
||||||
let (title_area, system_area, services_area) = tui_app.render(frame, &self.metric_store);
|
|
||||||
self.title_area = title_area;
|
|
||||||
self.system_area = system_area;
|
self.system_area = system_area;
|
||||||
self.services_area = services_area;
|
self.services_area = services_area;
|
||||||
}) {
|
}) {
|
||||||
@@ -322,10 +281,8 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
needs_render = false; // Reset flag after rendering
|
||||||
}
|
}
|
||||||
|
|
||||||
// Small sleep to prevent excessive CPU usage
|
|
||||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Dashboard main loop ended");
|
info!("Dashboard main loop ended");
|
||||||
@@ -420,19 +377,13 @@ impl Dashboard {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for title bar clicks (host selection)
|
// Check for tab clicks in right panel (hosts | services)
|
||||||
if matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
|
if matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
|
||||||
if is_in_area(x, y, &self.title_area) {
|
let services_end = self.services_area.x.saturating_add(self.services_area.width);
|
||||||
// Click in title bar - check if it's on a hostname
|
if y == self.services_area.y && x >= self.services_area.x && x < services_end {
|
||||||
// The title bar has "cm-dashboard vX.X.X" on the left (22 chars)
|
// Click on top border of services area (where tabs are)
|
||||||
// Then hostnames start at position 22
|
|
||||||
if x >= 22 {
|
|
||||||
let hostname = self.find_hostname_at_position(x);
|
|
||||||
if let Some(host) = hostname {
|
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
tui_app.switch_to_host(&host);
|
tui_app.handle_tab_click(x, &self.services_area);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -496,11 +447,32 @@ impl Dashboard {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate which service was clicked
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
if tui_app.focus_hosts {
|
||||||
|
// Hosts tab is active - handle host click
|
||||||
|
// The services area includes a border and header, so account for that
|
||||||
|
let relative_y = y.saturating_sub(self.services_area.y + 2) as usize; // +2 for border and header
|
||||||
|
|
||||||
|
let total_hosts = tui_app.get_available_hosts().len();
|
||||||
|
let clicked_index = tui_app.hosts_widget.y_to_host_index(relative_y);
|
||||||
|
|
||||||
|
if clicked_index < total_hosts {
|
||||||
|
match button {
|
||||||
|
MouseButton::Left => {
|
||||||
|
// Left click: set selector and switch to host immediately
|
||||||
|
tui_app.hosts_widget.set_selected_index(clicked_index, total_hosts);
|
||||||
|
let selected_host = tui_app.get_available_hosts()[clicked_index].clone();
|
||||||
|
tui_app.switch_to_host(&selected_host);
|
||||||
|
debug!("Clicked host at index {}: {}", clicked_index, selected_host);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Services tab is active - handle service click
|
||||||
// The services area includes a border, so we need to account for that
|
// The services area includes a border, so we need to account for that
|
||||||
let relative_y = y.saturating_sub(self.services_area.y + 2) as usize; // +2 for border and header
|
let relative_y = y.saturating_sub(self.services_area.y + 2) as usize; // +2 for border and header
|
||||||
|
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
|
||||||
if let Some(hostname) = tui_app.current_host.clone() {
|
if let Some(hostname) = tui_app.current_host.clone() {
|
||||||
let host_widgets = tui_app.get_or_create_host_widgets(&hostname);
|
let host_widgets = tui_app.get_or_create_host_widgets(&hostname);
|
||||||
|
|
||||||
@@ -537,6 +509,7 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -628,76 +601,12 @@ impl Dashboard {
|
|||||||
.unwrap_or_else(|| hostname.to_string())
|
.unwrap_or_else(|| hostname.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find which hostname is at a given x position in the title bar
|
|
||||||
fn find_hostname_at_position(&self, x: u16) -> Option<String> {
|
|
||||||
if let Some(ref tui_app) = self.tui_app {
|
|
||||||
// The hosts are RIGHT-ALIGNED in chunks[1]!
|
|
||||||
// Need to calculate total width first, then right-align
|
|
||||||
|
|
||||||
// Get terminal width
|
|
||||||
let terminal_width = if let Some(ref terminal) = self.terminal {
|
|
||||||
terminal.size().unwrap_or_default().width
|
|
||||||
} else {
|
|
||||||
80
|
|
||||||
};
|
|
||||||
|
|
||||||
// Calculate total width of all host text
|
|
||||||
let mut total_width = 0_u16;
|
|
||||||
for (i, host) in tui_app.get_available_hosts().iter().enumerate() {
|
|
||||||
if i > 0 {
|
|
||||||
total_width += 1; // space between hosts
|
|
||||||
}
|
|
||||||
total_width += 2; // icon + space
|
|
||||||
let is_selected = Some(host) == tui_app.current_host.as_ref();
|
|
||||||
if is_selected {
|
|
||||||
total_width += 1 + host.len() as u16 + 1; // [hostname]
|
|
||||||
} else {
|
|
||||||
total_width += host.len() as u16;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
total_width += 1; // right padding
|
|
||||||
|
|
||||||
// chunks[1] starts at 22, has width of (terminal_width - 22)
|
|
||||||
let chunk_width = terminal_width - 22;
|
|
||||||
|
|
||||||
// Right-aligned position
|
|
||||||
let hosts_start_x = if total_width < chunk_width {
|
|
||||||
22 + (chunk_width - total_width)
|
|
||||||
} else {
|
|
||||||
22
|
|
||||||
};
|
|
||||||
|
|
||||||
// Now calculate positions starting from hosts_start_x
|
|
||||||
let mut pos = hosts_start_x;
|
|
||||||
|
|
||||||
for (i, host) in tui_app.get_available_hosts().iter().enumerate() {
|
|
||||||
if i > 0 {
|
|
||||||
pos += 1; // " "
|
|
||||||
}
|
|
||||||
|
|
||||||
let host_start = pos;
|
|
||||||
pos += 2; // "● "
|
|
||||||
|
|
||||||
let is_selected = Some(host) == tui_app.current_host.as_ref();
|
|
||||||
if is_selected {
|
|
||||||
pos += 1 + host.len() as u16 + 1; // [hostname]
|
|
||||||
} else {
|
|
||||||
pos += host.len() as u16;
|
|
||||||
}
|
|
||||||
|
|
||||||
if x >= host_start && x < pos {
|
|
||||||
return Some(host.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a point is within a rectangular area
|
/// Check if a point is within a rectangular area
|
||||||
fn is_in_area(x: u16, y: u16, area: &Rect) -> bool {
|
fn is_in_area(x: u16, y: u16, area: &Rect) -> bool {
|
||||||
x >= area.x && x < area.x + area.width
|
x >= area.x && x < area.x.saturating_add(area.width)
|
||||||
&& y >= area.y && y < area.y + area.height
|
&& y >= area.y && y < area.y.saturating_add(area.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Dashboard {
|
impl Drop for Dashboard {
|
||||||
|
|||||||
@@ -86,16 +86,6 @@ impl MetricStore {
|
|||||||
self.current_agent_data.get(hostname)
|
self.current_agent_data.get(hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get ZMQ communication statistics for a host
|
|
||||||
pub fn get_zmq_stats(&mut self, hostname: &str) -> Option<ZmqStats> {
|
|
||||||
let now = Instant::now();
|
|
||||||
self.zmq_stats.get_mut(hostname).map(|stats| {
|
|
||||||
// Update packet age
|
|
||||||
stats.last_packet_age_secs = now.duration_since(stats.last_packet_time).as_secs_f64();
|
|
||||||
stats.clone()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get connected hosts (hosts with recent heartbeats)
|
/// Get connected hosts (hosts with recent heartbeats)
|
||||||
pub fn get_connected_hosts(&self, timeout: Duration) -> Vec<String> {
|
pub fn get_connected_hosts(&self, timeout: Duration) -> Vec<String> {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ use crate::config::DashboardConfig;
|
|||||||
use crate::metrics::MetricStore;
|
use crate::metrics::MetricStore;
|
||||||
use cm_dashboard_shared::Status;
|
use cm_dashboard_shared::Status;
|
||||||
use theme::{Components, Layout as ThemeLayout, Theme};
|
use theme::{Components, Layout as ThemeLayout, Theme};
|
||||||
use widgets::{ServicesWidget, SystemWidget, Widget};
|
use widgets::{HostsWidget, ServicesWidget, SystemWidget, Widget};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -64,8 +64,6 @@ pub struct TuiApp {
|
|||||||
pub current_host: Option<String>,
|
pub current_host: Option<String>,
|
||||||
/// Available hosts
|
/// Available hosts
|
||||||
available_hosts: Vec<String>,
|
available_hosts: Vec<String>,
|
||||||
/// Host index for navigation
|
|
||||||
host_index: usize,
|
|
||||||
/// Should quit application
|
/// Should quit application
|
||||||
should_quit: bool,
|
should_quit: bool,
|
||||||
/// Track if user manually navigated away from localhost
|
/// Track if user manually navigated away from localhost
|
||||||
@@ -76,6 +74,10 @@ pub struct TuiApp {
|
|||||||
localhost: String,
|
localhost: String,
|
||||||
/// Active popup menu (if any)
|
/// Active popup menu (if any)
|
||||||
pub popup_menu: Option<PopupMenu>,
|
pub popup_menu: Option<PopupMenu>,
|
||||||
|
/// Focus on hosts tab (false = Services, true = Hosts)
|
||||||
|
pub focus_hosts: bool,
|
||||||
|
/// Hosts widget for navigation and rendering
|
||||||
|
pub hosts_widget: HostsWidget,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TuiApp {
|
impl TuiApp {
|
||||||
@@ -85,12 +87,13 @@ impl TuiApp {
|
|||||||
host_widgets: HashMap::new(),
|
host_widgets: HashMap::new(),
|
||||||
current_host: None,
|
current_host: None,
|
||||||
available_hosts: config.hosts.keys().cloned().collect(),
|
available_hosts: config.hosts.keys().cloned().collect(),
|
||||||
host_index: 0,
|
|
||||||
should_quit: false,
|
should_quit: false,
|
||||||
user_navigated_away: false,
|
user_navigated_away: false,
|
||||||
config,
|
config,
|
||||||
localhost,
|
localhost,
|
||||||
popup_menu: None,
|
popup_menu: None,
|
||||||
|
focus_hosts: true, // Start with Hosts tab focused by default
|
||||||
|
hosts_widget: HostsWidget::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sort predefined hosts
|
// Sort predefined hosts
|
||||||
@@ -143,26 +146,31 @@ impl TuiApp {
|
|||||||
all_hosts.sort();
|
all_hosts.sort();
|
||||||
self.available_hosts = all_hosts;
|
self.available_hosts = all_hosts;
|
||||||
|
|
||||||
|
// Track if we had a host before this update
|
||||||
|
let had_host = self.current_host.is_some();
|
||||||
|
|
||||||
// Get the current hostname (localhost) for auto-selection
|
// Get the current hostname (localhost) for auto-selection
|
||||||
if !self.available_hosts.is_empty() {
|
if !self.available_hosts.is_empty() {
|
||||||
if self.available_hosts.contains(&self.localhost) && !self.user_navigated_away {
|
if self.available_hosts.contains(&self.localhost) && !self.user_navigated_away {
|
||||||
// Localhost is available and user hasn't navigated away - switch to it
|
// Localhost is available and user hasn't navigated away - switch to it
|
||||||
self.current_host = Some(self.localhost.clone());
|
self.current_host = Some(self.localhost.clone());
|
||||||
// Find the actual index of localhost in the sorted list
|
// Initialize selector bar on first host selection
|
||||||
self.host_index = self.available_hosts.iter().position(|h| h == &self.localhost).unwrap_or(0);
|
if !had_host {
|
||||||
|
let index = self.available_hosts.iter().position(|h| h == &self.localhost).unwrap_or(0);
|
||||||
|
self.hosts_widget.set_selected_index(index, self.available_hosts.len());
|
||||||
|
}
|
||||||
} else if self.current_host.is_none() {
|
} else if self.current_host.is_none() {
|
||||||
// No current host - select first available (which is localhost if available)
|
// No current host - select first available (which is localhost if available)
|
||||||
self.current_host = Some(self.available_hosts[0].clone());
|
self.current_host = Some(self.available_hosts[0].clone());
|
||||||
self.host_index = 0;
|
// Initialize selector bar
|
||||||
|
self.hosts_widget.set_selected_index(0, self.available_hosts.len());
|
||||||
} else if let Some(ref current) = self.current_host {
|
} else if let Some(ref current) = self.current_host {
|
||||||
if !self.available_hosts.contains(current) {
|
if !self.available_hosts.contains(current) {
|
||||||
// Current host disconnected - select first available and reset navigation flag
|
// Current host disconnected - FORCE switch to first available
|
||||||
self.current_host = Some(self.available_hosts[0].clone());
|
self.current_host = Some(self.available_hosts[0].clone());
|
||||||
self.host_index = 0;
|
// Reset selector bar since we're forcing a host change
|
||||||
|
self.hosts_widget.set_selected_index(0, self.available_hosts.len());
|
||||||
self.user_navigated_away = false; // Reset since we're forced to switch
|
self.user_navigated_away = false; // Reset since we're forced to switch
|
||||||
} else if let Some(index) = self.available_hosts.iter().position(|h| h == current) {
|
|
||||||
// Update index for current host
|
|
||||||
self.host_index = index;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -183,12 +191,6 @@ impl TuiApp {
|
|||||||
KeyCode::Char('q') => {
|
KeyCode::Char('q') => {
|
||||||
self.should_quit = true;
|
self.should_quit = true;
|
||||||
}
|
}
|
||||||
KeyCode::Left => {
|
|
||||||
self.navigate_host(-1);
|
|
||||||
}
|
|
||||||
KeyCode::Right => {
|
|
||||||
self.navigate_host(1);
|
|
||||||
}
|
|
||||||
KeyCode::Char('r') => {
|
KeyCode::Char('r') => {
|
||||||
// System rebuild command - works on any panel for current host
|
// System rebuild command - works on any panel for current host
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
@@ -356,18 +358,28 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Tab => {
|
KeyCode::Tab => {
|
||||||
// Tab cycles to next host
|
// Tab toggles between Services and Hosts tabs
|
||||||
self.navigate_host(1);
|
self.focus_hosts = !self.focus_hosts;
|
||||||
}
|
}
|
||||||
KeyCode::Up | KeyCode::Char('k') => {
|
KeyCode::Up | KeyCode::Char('k') => {
|
||||||
// Move service selection up
|
if self.focus_hosts {
|
||||||
|
// Move blue selector bar up when in Hosts tab
|
||||||
|
self.hosts_widget.select_previous();
|
||||||
|
} else {
|
||||||
|
// Move service selection up when in Services tab
|
||||||
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.select_previous();
|
host_widgets.services_widget.select_previous();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
KeyCode::Down | KeyCode::Char('j') => {
|
KeyCode::Down | KeyCode::Char('j') => {
|
||||||
// Move service selection down
|
if self.focus_hosts {
|
||||||
|
// Move blue selector bar down when in Hosts tab
|
||||||
|
let total_hosts = self.available_hosts.len();
|
||||||
|
self.hosts_widget.select_next(total_hosts);
|
||||||
|
} else {
|
||||||
|
// Move service selection down when in Services tab
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
let total_services = {
|
let total_services = {
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
||||||
@@ -377,6 +389,17 @@ impl TuiApp {
|
|||||||
host_widgets.services_widget.select_next(total_services);
|
host_widgets.services_widget.select_next(total_services);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Enter => {
|
||||||
|
if self.focus_hosts {
|
||||||
|
// Enter key switches to the selected host
|
||||||
|
let selected_idx = self.hosts_widget.get_selected_index();
|
||||||
|
if selected_idx < self.available_hosts.len() {
|
||||||
|
let selected_host = self.available_hosts[selected_idx].clone();
|
||||||
|
self.switch_to_host(&selected_host);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -386,7 +409,8 @@ impl TuiApp {
|
|||||||
/// Switch to a specific host by name
|
/// Switch to a specific host by name
|
||||||
pub fn switch_to_host(&mut self, hostname: &str) {
|
pub fn switch_to_host(&mut self, hostname: &str) {
|
||||||
if let Some(index) = self.available_hosts.iter().position(|h| h == hostname) {
|
if let Some(index) = self.available_hosts.iter().position(|h| h == hostname) {
|
||||||
self.host_index = index;
|
// Update selector bar position
|
||||||
|
self.hosts_widget.set_selected_index(index, self.available_hosts.len());
|
||||||
self.current_host = Some(hostname.to_string());
|
self.current_host = Some(hostname.to_string());
|
||||||
|
|
||||||
// Check if user navigated away from localhost
|
// Check if user navigated away from localhost
|
||||||
@@ -400,36 +424,28 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Navigate between hosts
|
/// Handle mouse click on tab title area
|
||||||
fn navigate_host(&mut self, direction: i32) {
|
pub fn handle_tab_click(&mut self, x: u16, area: &Rect) {
|
||||||
if self.available_hosts.is_empty() {
|
// Tab title format: "hosts | services"
|
||||||
return;
|
// Calculate positions relative to area start
|
||||||
}
|
let title_start_x = area.x + 1; // +1 for left border
|
||||||
|
|
||||||
let len = self.available_hosts.len();
|
// "hosts | services"
|
||||||
if direction > 0 {
|
// 0123456789...
|
||||||
self.host_index = (self.host_index + 1) % len;
|
let hosts_start = title_start_x;
|
||||||
} else {
|
let hosts_end = hosts_start + 5; // "hosts" is 5 chars
|
||||||
self.host_index = if self.host_index == 0 {
|
let services_start = hosts_end + 3; // After " | "
|
||||||
len - 1
|
let services_end = services_start + 8; // "services" is 8 chars
|
||||||
} else {
|
|
||||||
self.host_index - 1
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
self.current_host = Some(self.available_hosts[self.host_index].clone());
|
if x >= hosts_start && x < hosts_end {
|
||||||
|
// Clicked on "hosts"
|
||||||
// Check if user navigated away from localhost
|
self.focus_hosts = true;
|
||||||
if let Some(ref current) = self.current_host {
|
} else if x >= services_start && x < services_end {
|
||||||
if current != &self.localhost {
|
// Clicked on "services"
|
||||||
self.user_navigated_away = true;
|
self.focus_hosts = false;
|
||||||
} else {
|
|
||||||
self.user_navigated_away = false; // User navigated back to localhost
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Switched to host: {}", self.current_host.as_ref().unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -519,15 +535,9 @@ impl TuiApp {
|
|||||||
let system_area = left_chunks[0];
|
let system_area = left_chunks[0];
|
||||||
self.render_system_panel(frame, system_area, metric_store);
|
self.render_system_panel(frame, system_area, metric_store);
|
||||||
|
|
||||||
// Render services widget for current host
|
// Render right panel with tabs (Services | Hosts)
|
||||||
let services_area = content_chunks[1];
|
let services_area = content_chunks[1];
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
self.render_right_panel_with_tabs(frame, services_area, metric_store);
|
||||||
let is_focused = true; // Always show service selection
|
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
|
||||||
host_widgets
|
|
||||||
.services_widget
|
|
||||||
.render(frame, services_area, is_focused); // Services takes full right side
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render statusbar at the bottom
|
// Render statusbar at the bottom
|
||||||
self.render_statusbar(frame, main_chunks[2], metric_store);
|
self.render_statusbar(frame, main_chunks[2], metric_store);
|
||||||
@@ -545,7 +555,6 @@ impl TuiApp {
|
|||||||
fn render_btop_title(&self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) {
|
fn render_btop_title(&self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) {
|
||||||
use ratatui::style::Modifier;
|
use ratatui::style::Modifier;
|
||||||
use ratatui::text::{Line, Span};
|
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";
|
||||||
@@ -568,86 +577,34 @@ impl TuiApp {
|
|||||||
// Use the worst status color as background
|
// Use the worst status color as background
|
||||||
let background_color = Theme::status_color(worst_status);
|
let background_color = Theme::status_color(worst_status);
|
||||||
|
|
||||||
// Split the title bar into left and right sections
|
// Single line title bar showing dashboard name (left) and dashboard IP (right)
|
||||||
let chunks = Layout::default()
|
let left_text = format!(" cm-dashboard v{}", env!("CARGO_PKG_VERSION"));
|
||||||
.direction(Direction::Horizontal)
|
|
||||||
.constraints([Constraint::Length(22), Constraint::Min(0)])
|
|
||||||
.split(area);
|
|
||||||
|
|
||||||
// Left side: "cm-dashboard" text with version
|
// Get dashboard local IP for right side
|
||||||
let title_text = format!(" cm-dashboard v{}", env!("CARGO_PKG_VERSION"));
|
let dashboard_ip = Self::get_local_ip();
|
||||||
let left_span = Span::styled(
|
let right_text = format!("{} ", dashboard_ip);
|
||||||
&title_text,
|
|
||||||
|
// Calculate spacing to push right text to the right
|
||||||
|
let total_text_len = left_text.len() + right_text.len();
|
||||||
|
let spacing = (area.width as usize).saturating_sub(total_text_len).max(1);
|
||||||
|
let spacing_str = " ".repeat(spacing);
|
||||||
|
|
||||||
|
let title = Paragraph::new(Line::from(vec![
|
||||||
|
Span::styled(
|
||||||
|
left_text,
|
||||||
Style::default().fg(Theme::background()).bg(background_color).add_modifier(Modifier::BOLD)
|
Style::default().fg(Theme::background()).bg(background_color).add_modifier(Modifier::BOLD)
|
||||||
);
|
),
|
||||||
let left_title = Paragraph::new(Line::from(vec![left_span]))
|
Span::styled(
|
||||||
|
spacing_str,
|
||||||
|
Style::default().bg(background_color)
|
||||||
|
),
|
||||||
|
Span::styled(
|
||||||
|
right_text,
|
||||||
|
Style::default().fg(Theme::background()).bg(background_color)
|
||||||
|
),
|
||||||
|
]))
|
||||||
.style(Style::default().bg(background_color));
|
.style(Style::default().bg(background_color));
|
||||||
frame.render_widget(left_title, chunks[0]);
|
frame.render_widget(title, area);
|
||||||
|
|
||||||
// Right side: hosts with status indicators
|
|
||||||
let mut host_spans = Vec::new();
|
|
||||||
|
|
||||||
for (i, host) in self.available_hosts.iter().enumerate() {
|
|
||||||
if i > 0 {
|
|
||||||
host_spans.push(Span::styled(
|
|
||||||
" ",
|
|
||||||
Style::default().fg(Theme::background()).bg(background_color)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always show normal status icon based on metrics (no command status at host level)
|
|
||||||
let host_status = self.calculate_host_status(host, metric_store);
|
|
||||||
let status_icon = StatusIcons::get_icon(host_status);
|
|
||||||
|
|
||||||
// Add status icon with background color as foreground against status background
|
|
||||||
host_spans.push(Span::styled(
|
|
||||||
format!("{} ", status_icon),
|
|
||||||
Style::default().fg(Theme::background()).bg(background_color),
|
|
||||||
));
|
|
||||||
|
|
||||||
if Some(host) == self.current_host.as_ref() {
|
|
||||||
// Selected host with brackets in bold background color against status background
|
|
||||||
host_spans.push(Span::styled(
|
|
||||||
"[",
|
|
||||||
Style::default()
|
|
||||||
.fg(Theme::background())
|
|
||||||
.bg(background_color)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
));
|
|
||||||
host_spans.push(Span::styled(
|
|
||||||
host.clone(),
|
|
||||||
Style::default()
|
|
||||||
.fg(Theme::background())
|
|
||||||
.bg(background_color)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
));
|
|
||||||
host_spans.push(Span::styled(
|
|
||||||
"]",
|
|
||||||
Style::default()
|
|
||||||
.fg(Theme::background())
|
|
||||||
.bg(background_color)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
// Other hosts in normal background color against status background
|
|
||||||
host_spans.push(Span::styled(
|
|
||||||
host.clone(),
|
|
||||||
Style::default().fg(Theme::background()).bg(background_color),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add right padding
|
|
||||||
host_spans.push(Span::styled(
|
|
||||||
" ",
|
|
||||||
Style::default().fg(Theme::background()).bg(background_color)
|
|
||||||
));
|
|
||||||
|
|
||||||
let host_line = Line::from(host_spans);
|
|
||||||
let host_title = Paragraph::new(vec![host_line])
|
|
||||||
.style(Style::default().bg(background_color))
|
|
||||||
.alignment(ratatui::layout::Alignment::Right);
|
|
||||||
frame.render_widget(host_title, chunks[1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate overall status for a host based on its structured data
|
/// Calculate overall status for a host based on its structured data
|
||||||
@@ -757,18 +714,15 @@ impl TuiApp {
|
|||||||
("None".to_string(), "N/A".to_string(), "N/A".to_string(), "N/A".to_string())
|
("None".to_string(), "N/A".to_string(), "N/A".to_string(), "N/A".to_string())
|
||||||
};
|
};
|
||||||
|
|
||||||
let left_text = format!("Host: {} | {} | Build:{} | Agent:{}", hostname_str, host_ip, build_version, agent_version);
|
let left_text = format!(" Host: {} | {}", hostname_str, host_ip);
|
||||||
|
let right_text = format!("Build:{} | Agent:{} ", build_version, agent_version);
|
||||||
|
|
||||||
// Get dashboard local IP
|
// Calculate spacing to push right text to the right
|
||||||
let dashboard_ip = Self::get_local_ip();
|
let total_text_len = left_text.len() + right_text.len();
|
||||||
let right_text = format!("Dashboard: {}", dashboard_ip);
|
let spacing = (area.width as usize).saturating_sub(total_text_len).max(1);
|
||||||
|
let spacing_str = " ".repeat(spacing);
|
||||||
// Calculate spacing to push right text to the right (accounting for 1 char left padding)
|
|
||||||
let spacing = area.width as usize - left_text.len() - right_text.len() - 2; // -2 for left padding
|
|
||||||
let spacing_str = " ".repeat(spacing.max(1));
|
|
||||||
|
|
||||||
let line = Line::from(vec![
|
let line = Line::from(vec![
|
||||||
Span::raw(" "), // 1 char left padding
|
|
||||||
Span::styled(left_text, Style::default().fg(Theme::border())),
|
Span::styled(left_text, Style::default().fg(Theme::border())),
|
||||||
Span::raw(spacing_str),
|
Span::raw(spacing_str),
|
||||||
Span::styled(right_text, Style::default().fg(Theme::border())),
|
Span::styled(right_text, Style::default().fg(Theme::border())),
|
||||||
@@ -808,6 +762,73 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Render right panel with tabs (hosts | services)
|
||||||
|
fn render_right_panel_with_tabs(&mut self, frame: &mut Frame, area: Rect, metric_store: &MetricStore) {
|
||||||
|
use ratatui::style::Modifier;
|
||||||
|
use ratatui::text::{Line, Span};
|
||||||
|
use ratatui::widgets::{Block, Borders};
|
||||||
|
|
||||||
|
// Build tab title with bold styling for active tab (like cm-player)
|
||||||
|
let hosts_style = if self.focus_hosts {
|
||||||
|
Style::default().fg(Theme::border_title()).add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(Theme::border_title())
|
||||||
|
};
|
||||||
|
|
||||||
|
let services_style = if !self.focus_hosts {
|
||||||
|
Style::default().fg(Theme::border_title()).add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(Theme::border_title())
|
||||||
|
};
|
||||||
|
|
||||||
|
let title = Line::from(vec![
|
||||||
|
Span::styled("hosts", hosts_style),
|
||||||
|
Span::raw(" | "),
|
||||||
|
Span::styled("services", services_style),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Create ONE block with tab title (like cm-player)
|
||||||
|
let main_block = Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.title(title.clone())
|
||||||
|
.style(Style::default().fg(Theme::border()).bg(Theme::background()));
|
||||||
|
|
||||||
|
let inner_area = main_block.inner(area);
|
||||||
|
frame.render_widget(main_block, area);
|
||||||
|
|
||||||
|
// Render appropriate content based on active tab
|
||||||
|
if self.focus_hosts {
|
||||||
|
// Render hosts list (no additional borders)
|
||||||
|
let localhost = self.localhost.clone();
|
||||||
|
let current_host = self.current_host.as_deref();
|
||||||
|
self.hosts_widget.render(
|
||||||
|
frame,
|
||||||
|
inner_area,
|
||||||
|
&self.available_hosts,
|
||||||
|
&localhost,
|
||||||
|
current_host,
|
||||||
|
metric_store,
|
||||||
|
|hostname, store| {
|
||||||
|
// Inline calculate_host_status logic
|
||||||
|
if store.get_agent_data(hostname).is_some() {
|
||||||
|
Status::Ok
|
||||||
|
} else {
|
||||||
|
Status::Offline
|
||||||
|
}
|
||||||
|
},
|
||||||
|
true, // Always focused when visible
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Render services for current host (no additional borders - just content!)
|
||||||
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
|
let is_focused = true;
|
||||||
|
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
||||||
|
host_widgets.services_widget.render_content(frame, inner_area, is_focused);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Render offline host message with wake-up option
|
/// Render offline host message with wake-up option
|
||||||
fn render_offline_host_message(&self, frame: &mut Frame, area: Rect) {
|
fn render_offline_host_message(&self, frame: &mut Frame, area: Rect) {
|
||||||
use ratatui::layout::Alignment;
|
use ratatui::layout::Alignment;
|
||||||
|
|||||||
229
dashboard/src/ui/widgets/hosts.rs
Normal file
229
dashboard/src/ui/widgets/hosts.rs
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
use ratatui::{
|
||||||
|
layout::Rect,
|
||||||
|
style::{Modifier, Style},
|
||||||
|
text::{Line, Span},
|
||||||
|
widgets::{List, ListItem},
|
||||||
|
Frame,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::metrics::MetricStore;
|
||||||
|
use crate::ui::theme::Theme;
|
||||||
|
use cm_dashboard_shared::Status;
|
||||||
|
|
||||||
|
/// Hosts widget displaying all available hosts with selector bar navigation
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct HostsWidget {
|
||||||
|
/// Currently selected host index (for blue selector bar)
|
||||||
|
pub selected_index: usize,
|
||||||
|
/// Scroll offset for viewport
|
||||||
|
pub scroll_offset: usize,
|
||||||
|
/// Last rendered viewport height for scroll calculations
|
||||||
|
last_viewport_height: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HostsWidget {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
selected_index: 0,
|
||||||
|
scroll_offset: 0,
|
||||||
|
last_viewport_height: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move selection up
|
||||||
|
pub fn select_previous(&mut self) {
|
||||||
|
if self.selected_index > 0 {
|
||||||
|
self.selected_index -= 1;
|
||||||
|
self.ensure_selected_visible();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move selection down
|
||||||
|
pub fn select_next(&mut self, total_hosts: usize) {
|
||||||
|
if total_hosts > 0 && self.selected_index < total_hosts.saturating_sub(1) {
|
||||||
|
self.selected_index += 1;
|
||||||
|
self.ensure_selected_visible();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ensure selected item is visible in viewport (auto-scroll)
|
||||||
|
fn ensure_selected_visible(&mut self) {
|
||||||
|
if self.last_viewport_height == 0 {
|
||||||
|
return; // Can't calculate without viewport height
|
||||||
|
}
|
||||||
|
|
||||||
|
let viewport_height = self.last_viewport_height;
|
||||||
|
|
||||||
|
// If selection is above viewport, scroll up to show it
|
||||||
|
if self.selected_index < self.scroll_offset {
|
||||||
|
self.scroll_offset = self.selected_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If selection is below viewport, scroll down to show it
|
||||||
|
if self.selected_index >= self.scroll_offset + viewport_height {
|
||||||
|
self.scroll_offset = self.selected_index.saturating_sub(viewport_height.saturating_sub(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scroll down manually
|
||||||
|
pub fn scroll_down(&mut self, total_hosts: usize) {
|
||||||
|
if self.last_viewport_height == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let viewport_height = self.last_viewport_height;
|
||||||
|
let max_scroll = total_hosts.saturating_sub(viewport_height);
|
||||||
|
|
||||||
|
if self.scroll_offset < max_scroll {
|
||||||
|
self.scroll_offset += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scroll up manually
|
||||||
|
pub fn scroll_up(&mut self) {
|
||||||
|
if self.scroll_offset > 0 {
|
||||||
|
self.scroll_offset -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the currently selected host index
|
||||||
|
pub fn get_selected_index(&self) -> usize {
|
||||||
|
self.selected_index
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set selected index (used when switching to host via mouse)
|
||||||
|
pub fn set_selected_index(&mut self, index: usize, total_hosts: usize) {
|
||||||
|
if index < total_hosts {
|
||||||
|
self.selected_index = index;
|
||||||
|
self.ensure_selected_visible();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert y coordinate to host index (accounting for scroll)
|
||||||
|
pub fn y_to_host_index(&self, relative_y: usize) -> usize {
|
||||||
|
self.scroll_offset + relative_y
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render hosts list with selector bar
|
||||||
|
pub fn render<F>(
|
||||||
|
&mut self,
|
||||||
|
frame: &mut Frame,
|
||||||
|
area: Rect,
|
||||||
|
available_hosts: &[String],
|
||||||
|
localhost: &str,
|
||||||
|
current_host: Option<&str>,
|
||||||
|
metric_store: &MetricStore,
|
||||||
|
mut calculate_host_status: F,
|
||||||
|
is_focused: bool,
|
||||||
|
) where F: FnMut(&str, &MetricStore) -> Status {
|
||||||
|
use crate::ui::theme::{StatusIcons, Typography};
|
||||||
|
use ratatui::widgets::Paragraph;
|
||||||
|
|
||||||
|
// Split area for header and list
|
||||||
|
let chunks = ratatui::layout::Layout::default()
|
||||||
|
.direction(ratatui::layout::Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
ratatui::layout::Constraint::Length(1), // Header
|
||||||
|
ratatui::layout::Constraint::Min(0), // List
|
||||||
|
])
|
||||||
|
.split(area);
|
||||||
|
|
||||||
|
// Render header
|
||||||
|
let header = Paragraph::new("Hosts:").style(Typography::muted());
|
||||||
|
frame.render_widget(header, chunks[0]);
|
||||||
|
|
||||||
|
// Store viewport height for scroll calculations (minus header)
|
||||||
|
self.last_viewport_height = chunks[1].height as usize;
|
||||||
|
|
||||||
|
// Validate scroll offset
|
||||||
|
if self.scroll_offset >= available_hosts.len() && !available_hosts.is_empty() {
|
||||||
|
self.scroll_offset = available_hosts.len().saturating_sub(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create list items for visible hosts
|
||||||
|
let items: Vec<ListItem> = available_hosts
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.skip(self.scroll_offset)
|
||||||
|
.take(chunks[1].height as usize)
|
||||||
|
.map(|(idx, hostname)| {
|
||||||
|
let host_status = calculate_host_status(hostname, metric_store);
|
||||||
|
let status_icon = StatusIcons::get_icon(host_status);
|
||||||
|
let status_color = Theme::status_color(host_status);
|
||||||
|
|
||||||
|
// Check if this is the selected host (for blue selector bar)
|
||||||
|
let is_selected = is_focused && idx == self.selected_index;
|
||||||
|
|
||||||
|
// Check if this is the current (active) host
|
||||||
|
let is_current = current_host == Some(hostname.as_str());
|
||||||
|
|
||||||
|
// Check if this is localhost
|
||||||
|
let is_localhost = hostname == localhost;
|
||||||
|
|
||||||
|
// Build the line with icon and hostname
|
||||||
|
let mut spans = vec![Span::styled(
|
||||||
|
format!("{} ", status_icon),
|
||||||
|
if is_selected {
|
||||||
|
Style::default()
|
||||||
|
.fg(Theme::background())
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(status_color)
|
||||||
|
},
|
||||||
|
)];
|
||||||
|
|
||||||
|
// Add arrow indicator if this is the current host (like cm-player)
|
||||||
|
if is_current {
|
||||||
|
spans.push(Span::styled(
|
||||||
|
"▸ ",
|
||||||
|
if is_selected {
|
||||||
|
Style::default()
|
||||||
|
.fg(Theme::background())
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
.fg(Theme::primary_text())
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add hostname with appropriate styling
|
||||||
|
let hostname_text = if is_localhost {
|
||||||
|
format!("{} (localhost)", hostname)
|
||||||
|
} else {
|
||||||
|
hostname.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
spans.push(Span::styled(
|
||||||
|
hostname_text,
|
||||||
|
if is_selected {
|
||||||
|
Style::default()
|
||||||
|
.fg(Theme::background())
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else if is_current {
|
||||||
|
Style::default()
|
||||||
|
.fg(Theme::primary_text())
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(Theme::primary_text())
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
let line = Line::from(spans);
|
||||||
|
|
||||||
|
// Apply blue background to selected row
|
||||||
|
let base_style = if is_selected {
|
||||||
|
Style::default().bg(Theme::highlight()) // Blue background
|
||||||
|
} else {
|
||||||
|
Style::default().bg(Theme::background())
|
||||||
|
};
|
||||||
|
|
||||||
|
ListItem::new(line).style(base_style)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let hosts_list = List::new(items);
|
||||||
|
frame.render_widget(hosts_list, chunks[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
use cm_dashboard_shared::AgentData;
|
use cm_dashboard_shared::AgentData;
|
||||||
|
|
||||||
|
pub mod hosts;
|
||||||
pub mod services;
|
pub mod services;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
|
|
||||||
|
pub use hosts::HostsWidget;
|
||||||
pub use services::ServicesWidget;
|
pub use services::ServicesWidget;
|
||||||
pub use system::SystemWidget;
|
pub use system::SystemWidget;
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,6 @@ pub struct ServicesWidget {
|
|||||||
struct ServiceInfo {
|
struct ServiceInfo {
|
||||||
metrics: Vec<(String, f32, Option<String>)>, // (label, value, unit)
|
metrics: Vec<(String, f32, Option<String>)>, // (label, value, unit)
|
||||||
widget_status: Status,
|
widget_status: Status,
|
||||||
service_type: String, // "nginx_site", "container", "image", or empty for parent services
|
|
||||||
memory_bytes: Option<u64>,
|
memory_bytes: Option<u64>,
|
||||||
restart_count: Option<u32>,
|
restart_count: Option<u32>,
|
||||||
uptime_seconds: Option<u64>,
|
uptime_seconds: Option<u64>,
|
||||||
@@ -344,6 +343,7 @@ impl ServicesWidget {
|
|||||||
pub fn select_previous(&mut self) {
|
pub fn select_previous(&mut self) {
|
||||||
if self.selected_index > 0 {
|
if self.selected_index > 0 {
|
||||||
self.selected_index -= 1;
|
self.selected_index -= 1;
|
||||||
|
self.ensure_selected_visible();
|
||||||
}
|
}
|
||||||
debug!("Service selection moved up to: {}", self.selected_index);
|
debug!("Service selection moved up to: {}", self.selected_index);
|
||||||
}
|
}
|
||||||
@@ -352,10 +352,77 @@ impl ServicesWidget {
|
|||||||
pub fn select_next(&mut self, total_services: usize) {
|
pub fn select_next(&mut self, total_services: usize) {
|
||||||
if total_services > 0 && self.selected_index < total_services.saturating_sub(1) {
|
if total_services > 0 && self.selected_index < total_services.saturating_sub(1) {
|
||||||
self.selected_index += 1;
|
self.selected_index += 1;
|
||||||
|
self.ensure_selected_visible();
|
||||||
}
|
}
|
||||||
debug!("Service selection: {}/{}", self.selected_index, total_services);
|
debug!("Service selection: {}/{}", self.selected_index, total_services);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert parent service index to display line index
|
||||||
|
fn parent_index_to_display_line(&self, parent_index: usize) -> usize {
|
||||||
|
let mut parent_services: Vec<_> = self.parent_services.iter().collect();
|
||||||
|
parent_services.sort_by(|(a, _), (b, _)| a.cmp(b));
|
||||||
|
|
||||||
|
let mut display_line = 0;
|
||||||
|
for (idx, (parent_name, _)) in parent_services.iter().enumerate() {
|
||||||
|
if idx == parent_index {
|
||||||
|
return display_line;
|
||||||
|
}
|
||||||
|
display_line += 1; // Parent service line
|
||||||
|
|
||||||
|
// Add sub-service lines
|
||||||
|
if let Some(sub_list) = self.sub_services.get(*parent_name) {
|
||||||
|
display_line += sub_list.len();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
display_line
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ensure the currently selected service is visible in the viewport
|
||||||
|
fn ensure_selected_visible(&mut self) {
|
||||||
|
if self.last_viewport_height == 0 {
|
||||||
|
return; // Can't adjust without knowing viewport size
|
||||||
|
}
|
||||||
|
|
||||||
|
let display_line = self.parent_index_to_display_line(self.selected_index);
|
||||||
|
let total_display_lines = self.get_total_display_lines();
|
||||||
|
let viewport_height = self.last_viewport_height;
|
||||||
|
|
||||||
|
// Check if selected line is above visible area
|
||||||
|
if display_line < self.scroll_offset {
|
||||||
|
self.scroll_offset = display_line;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate current effective viewport (accounting for "more below" if present)
|
||||||
|
let current_remaining = total_display_lines.saturating_sub(self.scroll_offset);
|
||||||
|
let current_has_more = current_remaining > viewport_height;
|
||||||
|
let current_effective = if current_has_more {
|
||||||
|
viewport_height.saturating_sub(1)
|
||||||
|
} else {
|
||||||
|
viewport_height
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check if selected line is below current visible area
|
||||||
|
if display_line >= self.scroll_offset + current_effective {
|
||||||
|
// Need to scroll down. Position selected line so there's room for "more below" if needed
|
||||||
|
// Strategy: if there are lines below the selected line, don't put it at the very bottom
|
||||||
|
let has_content_below = display_line < total_display_lines - 1;
|
||||||
|
|
||||||
|
if has_content_below {
|
||||||
|
// Leave room for "... X more below" message by positioning selected line
|
||||||
|
// one position higher than the last line
|
||||||
|
let target_position = viewport_height.saturating_sub(2);
|
||||||
|
self.scroll_offset = display_line.saturating_sub(target_position);
|
||||||
|
} else {
|
||||||
|
// This is the last line, can put it at the bottom
|
||||||
|
self.scroll_offset = display_line.saturating_sub(viewport_height - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Auto-scroll: selected={}, display_line={}, scroll_offset={}, viewport={}, total={}",
|
||||||
|
self.selected_index, display_line, self.scroll_offset, viewport_height, total_display_lines);
|
||||||
|
}
|
||||||
|
|
||||||
/// Get currently selected service name (for actions)
|
/// Get currently selected service name (for actions)
|
||||||
/// Only returns parent service names since only parent services can be selected
|
/// Only returns parent service names since only parent services can be selected
|
||||||
pub fn get_selected_service(&self) -> Option<String> {
|
pub fn get_selected_service(&self) -> Option<String> {
|
||||||
@@ -488,7 +555,6 @@ impl Widget for ServicesWidget {
|
|||||||
let parent_info = ServiceInfo {
|
let parent_info = ServiceInfo {
|
||||||
metrics: Vec::new(), // Parent services don't have custom metrics
|
metrics: Vec::new(), // Parent services don't have custom metrics
|
||||||
widget_status: service.service_status,
|
widget_status: service.service_status,
|
||||||
service_type: String::new(), // Parent services have no type
|
|
||||||
memory_bytes: service.memory_bytes,
|
memory_bytes: service.memory_bytes,
|
||||||
restart_count: service.restart_count,
|
restart_count: service.restart_count,
|
||||||
uptime_seconds: service.uptime_seconds,
|
uptime_seconds: service.uptime_seconds,
|
||||||
@@ -507,7 +573,6 @@ impl Widget for ServicesWidget {
|
|||||||
let sub_info = ServiceInfo {
|
let sub_info = ServiceInfo {
|
||||||
metrics,
|
metrics,
|
||||||
widget_status: sub_service.service_status,
|
widget_status: sub_service.service_status,
|
||||||
service_type: sub_service.service_type.clone(),
|
|
||||||
memory_bytes: None, // Sub-services don't have individual metrics yet
|
memory_bytes: None, // Sub-services don't have individual metrics yet
|
||||||
restart_count: None,
|
restart_count: None,
|
||||||
uptime_seconds: None,
|
uptime_seconds: None,
|
||||||
@@ -552,7 +617,6 @@ impl ServicesWidget {
|
|||||||
.or_insert(ServiceInfo {
|
.or_insert(ServiceInfo {
|
||||||
metrics: Vec::new(),
|
metrics: Vec::new(),
|
||||||
widget_status: Status::Unknown,
|
widget_status: Status::Unknown,
|
||||||
service_type: String::new(),
|
|
||||||
memory_bytes: None,
|
memory_bytes: None,
|
||||||
restart_count: None,
|
restart_count: None,
|
||||||
uptime_seconds: None,
|
uptime_seconds: None,
|
||||||
@@ -581,7 +645,6 @@ impl ServicesWidget {
|
|||||||
ServiceInfo {
|
ServiceInfo {
|
||||||
metrics: Vec::new(),
|
metrics: Vec::new(),
|
||||||
widget_status: Status::Unknown,
|
widget_status: Status::Unknown,
|
||||||
service_type: String::new(), // Unknown type in legacy path
|
|
||||||
memory_bytes: None,
|
memory_bytes: None,
|
||||||
restart_count: None,
|
restart_count: None,
|
||||||
uptime_seconds: None,
|
uptime_seconds: None,
|
||||||
@@ -650,7 +713,11 @@ impl ServicesWidget {
|
|||||||
|
|
||||||
/// Render with focus
|
/// Render with focus
|
||||||
pub fn render(&mut self, frame: &mut Frame, area: Rect, is_focused: bool) {
|
pub fn render(&mut self, frame: &mut Frame, area: Rect, is_focused: bool) {
|
||||||
let services_block = Components::widget_block("services");
|
self.render_with_title(frame, area, is_focused, "services");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render_with_title(&mut self, frame: &mut Frame, area: Rect, is_focused: bool, title: &str) {
|
||||||
|
let services_block = Components::widget_block(title);
|
||||||
let inner_area = services_block.inner(area);
|
let inner_area = services_block.inner(area);
|
||||||
frame.render_widget(services_block, area);
|
frame.render_widget(services_block, area);
|
||||||
|
|
||||||
@@ -695,6 +762,49 @@ impl ServicesWidget {
|
|||||||
self.render_services(frame, content_chunks[1], is_focused, columns);
|
self.render_services(frame, content_chunks[1], is_focused, columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Render services content WITHOUT block (for tab mode like cm-player)
|
||||||
|
pub fn render_content(&mut self, frame: &mut Frame, area: Rect, is_focused: bool) {
|
||||||
|
let content_chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([Constraint::Length(1), Constraint::Min(0)])
|
||||||
|
.split(area);
|
||||||
|
|
||||||
|
// Determine which columns to show based on available width
|
||||||
|
let columns = ColumnVisibility::from_width(area.width);
|
||||||
|
|
||||||
|
// Build header based on visible columns
|
||||||
|
let mut header_parts = Vec::new();
|
||||||
|
if columns.show_name {
|
||||||
|
header_parts.push(format!("{:<width$}", "Service:", width = ColumnVisibility::NAME_WIDTH as usize));
|
||||||
|
}
|
||||||
|
if columns.show_status {
|
||||||
|
header_parts.push(format!("{:<width$}", "Status:", width = ColumnVisibility::STATUS_WIDTH as usize));
|
||||||
|
}
|
||||||
|
if columns.show_ram {
|
||||||
|
header_parts.push(format!("{:<width$}", "RAM:", width = ColumnVisibility::RAM_WIDTH as usize));
|
||||||
|
}
|
||||||
|
if columns.show_uptime {
|
||||||
|
header_parts.push(format!("{:<width$}", "Uptime:", width = ColumnVisibility::UPTIME_WIDTH as usize));
|
||||||
|
}
|
||||||
|
if columns.show_restarts {
|
||||||
|
header_parts.push(format!("{:<width$}", "↻:", width = ColumnVisibility::RESTARTS_WIDTH as usize));
|
||||||
|
}
|
||||||
|
let header = header_parts.join(" ");
|
||||||
|
|
||||||
|
let header_para = Paragraph::new(header).style(Typography::muted());
|
||||||
|
frame.render_widget(header_para, content_chunks[0]);
|
||||||
|
|
||||||
|
// Check if we have any services to display
|
||||||
|
if self.parent_services.is_empty() && self.sub_services.is_empty() {
|
||||||
|
let empty_text = Paragraph::new("No process data").style(Typography::muted());
|
||||||
|
frame.render_widget(empty_text, content_chunks[1]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the services list
|
||||||
|
self.render_services(frame, content_chunks[1], is_focused, columns);
|
||||||
|
}
|
||||||
|
|
||||||
/// Render services list
|
/// Render services list
|
||||||
fn render_services(&mut self, frame: &mut Frame, area: Rect, is_focused: bool, columns: ColumnVisibility) {
|
fn render_services(&mut self, frame: &mut Frame, area: Rect, is_focused: bool, columns: ColumnVisibility) {
|
||||||
// Build hierarchical service list for display
|
// Build hierarchical service list for display
|
||||||
|
|||||||
@@ -44,9 +44,9 @@ pub struct SystemWidget {
|
|||||||
storage_pools: Vec<StoragePool>,
|
storage_pools: Vec<StoragePool>,
|
||||||
|
|
||||||
// Backup metrics
|
// Backup metrics
|
||||||
backup_repositories: Vec<String>,
|
backup_last_time: Option<String>,
|
||||||
backup_repository_status: Status,
|
backup_status: Status,
|
||||||
backup_disks: Vec<cm_dashboard_shared::BackupDiskData>,
|
backup_repositories: Vec<cm_dashboard_shared::BackupRepositoryData>,
|
||||||
|
|
||||||
// Overall status
|
// Overall status
|
||||||
has_data: bool,
|
has_data: bool,
|
||||||
@@ -112,9 +112,9 @@ impl SystemWidget {
|
|||||||
tmp_status: Status::Unknown,
|
tmp_status: Status::Unknown,
|
||||||
tmpfs_mounts: Vec::new(),
|
tmpfs_mounts: Vec::new(),
|
||||||
storage_pools: Vec::new(),
|
storage_pools: Vec::new(),
|
||||||
|
backup_last_time: None,
|
||||||
|
backup_status: Status::Unknown,
|
||||||
backup_repositories: Vec::new(),
|
backup_repositories: Vec::new(),
|
||||||
backup_repository_status: Status::Unknown,
|
|
||||||
backup_disks: Vec::new(),
|
|
||||||
has_data: false,
|
has_data: false,
|
||||||
scroll_offset: 0,
|
scroll_offset: 0,
|
||||||
last_viewport_height: 0,
|
last_viewport_height: 0,
|
||||||
@@ -221,9 +221,9 @@ impl Widget for SystemWidget {
|
|||||||
|
|
||||||
// Extract backup data
|
// Extract backup data
|
||||||
let backup = &agent_data.backup;
|
let backup = &agent_data.backup;
|
||||||
|
self.backup_last_time = backup.last_backup_time.clone();
|
||||||
|
self.backup_status = backup.backup_status;
|
||||||
self.backup_repositories = backup.repositories.clone();
|
self.backup_repositories = backup.repositories.clone();
|
||||||
self.backup_repository_status = backup.repository_status;
|
|
||||||
self.backup_disks = backup.disks.clone();
|
|
||||||
|
|
||||||
// Clamp scroll offset to valid range after update
|
// Clamp scroll offset to valid range after update
|
||||||
// This prevents scroll issues when switching between hosts
|
// This prevents scroll issues when switching between hosts
|
||||||
@@ -533,79 +533,42 @@ impl SystemWidget {
|
|||||||
fn render_backup(&self) -> Vec<Line<'_>> {
|
fn render_backup(&self) -> Vec<Line<'_>> {
|
||||||
let mut lines = Vec::new();
|
let mut lines = Vec::new();
|
||||||
|
|
||||||
// First section: Repository status and list
|
if self.backup_repositories.is_empty() {
|
||||||
if !self.backup_repositories.is_empty() {
|
return lines;
|
||||||
let repo_text = format!("Repo: {}", self.backup_repositories.len());
|
}
|
||||||
let repo_spans = StatusIcons::create_status_spans(self.backup_repository_status, &repo_text);
|
|
||||||
|
// Format backup time (use complete timestamp)
|
||||||
|
let time_display = if let Some(ref time_str) = self.backup_last_time {
|
||||||
|
time_str.clone()
|
||||||
|
} else {
|
||||||
|
"unknown".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Header: just the timestamp
|
||||||
|
let repo_spans = StatusIcons::create_status_spans(self.backup_status, &time_display);
|
||||||
lines.push(Line::from(repo_spans));
|
lines.push(Line::from(repo_spans));
|
||||||
|
|
||||||
// List all repositories (sorted for consistent display)
|
// List all repositories with archive count and size
|
||||||
let mut sorted_repos = self.backup_repositories.clone();
|
let repo_count = self.backup_repositories.len();
|
||||||
sorted_repos.sort();
|
for (idx, repo) in self.backup_repositories.iter().enumerate() {
|
||||||
let repo_count = sorted_repos.len();
|
|
||||||
for (idx, repo) in sorted_repos.iter().enumerate() {
|
|
||||||
let tree_char = if idx == repo_count - 1 { "└─" } else { "├─" };
|
let tree_char = if idx == repo_count - 1 { "└─" } else { "├─" };
|
||||||
lines.push(Line::from(vec![
|
|
||||||
|
// Format size: use kB for < 1MB, MB for < 1GB, otherwise GB
|
||||||
|
let size_display = if repo.repo_size_gb < 0.001 {
|
||||||
|
format!("{:.0}kB", repo.repo_size_gb * 1024.0 * 1024.0)
|
||||||
|
} else if repo.repo_size_gb < 1.0 {
|
||||||
|
format!("{:.0}MB", repo.repo_size_gb * 1024.0)
|
||||||
|
} else {
|
||||||
|
format!("{:.1}GB", repo.repo_size_gb)
|
||||||
|
};
|
||||||
|
|
||||||
|
let repo_text = format!("{} ({}) {}", repo.name, repo.archive_count, size_display);
|
||||||
|
|
||||||
|
let mut repo_spans = vec![
|
||||||
Span::styled(format!(" {} ", tree_char), Typography::tree()),
|
Span::styled(format!(" {} ", tree_char), Typography::tree()),
|
||||||
Span::styled(repo.clone(), Typography::secondary()),
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Second section: Per-disk backup information (sorted by serial for consistent display)
|
|
||||||
let mut sorted_disks = self.backup_disks.clone();
|
|
||||||
sorted_disks.sort_by(|a, b| a.serial.cmp(&b.serial));
|
|
||||||
for disk in &sorted_disks {
|
|
||||||
let truncated_serial = truncate_serial(&disk.serial);
|
|
||||||
let mut details = Vec::new();
|
|
||||||
|
|
||||||
if let Some(temp) = disk.temperature_celsius {
|
|
||||||
details.push(format!("T: {}°C", temp as i32));
|
|
||||||
}
|
|
||||||
if let Some(wear) = disk.wear_percent {
|
|
||||||
details.push(format!("W: {}%", wear as i32));
|
|
||||||
}
|
|
||||||
|
|
||||||
let disk_text = if !details.is_empty() {
|
|
||||||
format!("{} {}", truncated_serial, details.join(" "))
|
|
||||||
} else {
|
|
||||||
truncated_serial
|
|
||||||
};
|
|
||||||
|
|
||||||
// Overall disk status (worst of backup and usage)
|
|
||||||
let disk_status = disk.backup_status.max(disk.usage_status);
|
|
||||||
let disk_spans = StatusIcons::create_status_spans(disk_status, &disk_text);
|
|
||||||
lines.push(Line::from(disk_spans));
|
|
||||||
|
|
||||||
// Show backup time with status
|
|
||||||
if let Some(backup_time) = &disk.last_backup_time {
|
|
||||||
let time_text = format!("Backup: {}", backup_time);
|
|
||||||
let mut time_spans = vec![
|
|
||||||
Span::styled(" ├─ ", Typography::tree()),
|
|
||||||
];
|
];
|
||||||
time_spans.extend(StatusIcons::create_status_spans(disk.backup_status, &time_text));
|
repo_spans.extend(StatusIcons::create_status_spans(repo.status, &repo_text));
|
||||||
lines.push(Line::from(time_spans));
|
lines.push(Line::from(repo_spans));
|
||||||
}
|
|
||||||
|
|
||||||
// Show usage with status and archive count
|
|
||||||
let archive_display = if disk.archives_min == disk.archives_max {
|
|
||||||
format!("{}", disk.archives_min)
|
|
||||||
} else {
|
|
||||||
format!("{}-{}", disk.archives_min, disk.archives_max)
|
|
||||||
};
|
|
||||||
|
|
||||||
let usage_text = format!(
|
|
||||||
"Usage: ({}) {:.0}% {:.0}GB/{:.0}GB",
|
|
||||||
archive_display,
|
|
||||||
disk.disk_usage_percent,
|
|
||||||
disk.disk_used_gb,
|
|
||||||
disk.disk_total_gb
|
|
||||||
);
|
|
||||||
let mut usage_spans = vec![
|
|
||||||
Span::styled(" └─ ", Typography::tree()),
|
|
||||||
];
|
|
||||||
usage_spans.extend(StatusIcons::create_status_spans(disk.usage_status, &usage_text));
|
|
||||||
lines.push(Line::from(usage_spans));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lines
|
lines
|
||||||
@@ -876,13 +839,10 @@ impl SystemWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Backup section
|
// Backup section
|
||||||
if !self.backup_repositories.is_empty() || !self.backup_disks.is_empty() {
|
|
||||||
count += 1; // Header
|
|
||||||
if !self.backup_repositories.is_empty() {
|
if !self.backup_repositories.is_empty() {
|
||||||
count += 1; // Repo header
|
count += 1; // Header: "Backup:"
|
||||||
count += self.backup_repositories.len();
|
count += 1; // Repo count and timestamp header
|
||||||
}
|
count += self.backup_repositories.len(); // Individual repos
|
||||||
count += self.backup_disks.len() * 3; // Each disk has 3 lines
|
|
||||||
}
|
}
|
||||||
|
|
||||||
count
|
count
|
||||||
@@ -988,7 +948,7 @@ impl SystemWidget {
|
|||||||
lines.extend(storage_lines);
|
lines.extend(storage_lines);
|
||||||
|
|
||||||
// Backup section (if available)
|
// Backup section (if available)
|
||||||
if !self.backup_repositories.is_empty() || !self.backup_disks.is_empty() {
|
if !self.backup_repositories.is_empty() {
|
||||||
lines.push(Line::from(vec![
|
lines.push(Line::from(vec![
|
||||||
Span::styled("Backup:", Typography::widget_title())
|
Span::styled("Backup:", Typography::widget_title())
|
||||||
]));
|
]));
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.261"
|
version = "0.1.275"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -182,27 +182,18 @@ pub struct SubServiceMetric {
|
|||||||
/// Backup system data
|
/// Backup system data
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct BackupData {
|
pub struct BackupData {
|
||||||
pub repositories: Vec<String>,
|
|
||||||
pub repository_status: Status,
|
|
||||||
pub disks: Vec<BackupDiskData>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Backup repository disk information
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct BackupDiskData {
|
|
||||||
pub serial: String,
|
|
||||||
pub product_name: Option<String>,
|
|
||||||
pub wear_percent: Option<f32>,
|
|
||||||
pub temperature_celsius: Option<f32>,
|
|
||||||
pub last_backup_time: Option<String>,
|
pub last_backup_time: Option<String>,
|
||||||
pub backup_status: Status,
|
pub backup_status: Status,
|
||||||
pub disk_usage_percent: f32,
|
pub repositories: Vec<BackupRepositoryData>,
|
||||||
pub disk_used_gb: f32,
|
}
|
||||||
pub disk_total_gb: f32,
|
|
||||||
pub usage_status: Status,
|
/// Individual backup repository information
|
||||||
pub services: Vec<String>,
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub archives_min: i64,
|
pub struct BackupRepositoryData {
|
||||||
pub archives_max: i64,
|
pub name: String,
|
||||||
|
pub archive_count: i64,
|
||||||
|
pub repo_size_gb: f32,
|
||||||
|
pub status: Status,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AgentData {
|
impl AgentData {
|
||||||
@@ -245,9 +236,9 @@ impl AgentData {
|
|||||||
},
|
},
|
||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
backup: BackupData {
|
backup: BackupData {
|
||||||
|
last_backup_time: None,
|
||||||
|
backup_status: Status::Unknown,
|
||||||
repositories: Vec::new(),
|
repositories: Vec::new(),
|
||||||
repository_status: Status::Unknown,
|
|
||||||
disks: Vec::new(),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user