Compare commits
49 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1656f20e96 | |||
| dcd350ec2c | |||
| a34b095857 | |||
| 7362464b46 | |||
| c8b79576fa | |||
| f53df5440b | |||
| d1b0e2c431 | |||
| b1719a60fc | |||
| d922e8d6f3 | |||
| 407bc9dbc2 | |||
| 3c278351c9 | |||
| 8da4522d85 | |||
| 5b1e39cfca | |||
| ffecbc3166 | |||
| 49f9504429 | |||
| bc9015e96b | |||
| aaec8e691c | |||
| 4a8cfbbde4 | |||
| d93260529b | |||
| 41e1be451e | |||
| 2863526ec8 | |||
| 5da9213da6 | |||
| a7755f02ae | |||
| b886fb2045 | |||
| cfb02e1763 | |||
| 5b53ca3d52 | |||
| 92a30913b4 | |||
| a288a8ef9a | |||
| c65d596099 | |||
| 98ed17947d | |||
| 1cb6abf58a | |||
| 477724b4f4 | |||
| 7a3ed17952 | |||
| 7e1962a168 | |||
| 5bb7d6cf57 | |||
| 7a0dc27846 | |||
| 5bc250a738 | |||
| 5c3ac8b15e | |||
| bdfff942f7 | |||
| 47ab1e387d | |||
| 966ba27b1e | |||
| 6c6c9144bd | |||
| 3fdcec8047 | |||
| 1fcaf4a670 | |||
| 885e19f7fd | |||
| a7b69b8ae7 | |||
| 2d290f40b2 | |||
| ad1fcaa27b | |||
| 60ab4d4f9e |
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.226"
|
version = "0.1.274"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
@ -301,7 +301,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.226"
|
version = "0.1.274"
|
||||||
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.226"
|
version = "0.1.274"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.227"
|
version = "0.1.275"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use gethostname::gethostname;
|
use gethostname::gethostname;
|
||||||
use std::time::Duration;
|
use std::time::{Duration, Instant};
|
||||||
use tokio::time::interval;
|
use tokio::time::interval;
|
||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
@ -19,13 +19,22 @@ use crate::collectors::{
|
|||||||
use crate::notifications::NotificationManager;
|
use crate::notifications::NotificationManager;
|
||||||
use cm_dashboard_shared::AgentData;
|
use cm_dashboard_shared::AgentData;
|
||||||
|
|
||||||
|
/// Wrapper for collectors with timing information
|
||||||
|
struct TimedCollector {
|
||||||
|
collector: Box<dyn Collector>,
|
||||||
|
interval: Duration,
|
||||||
|
last_collection: Option<Instant>,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
hostname: String,
|
hostname: String,
|
||||||
config: AgentConfig,
|
config: AgentConfig,
|
||||||
zmq_handler: ZmqHandler,
|
zmq_handler: ZmqHandler,
|
||||||
collectors: Vec<Box<dyn Collector>>,
|
collectors: Vec<TimedCollector>,
|
||||||
notification_manager: NotificationManager,
|
notification_manager: NotificationManager,
|
||||||
previous_status: Option<SystemStatus>,
|
previous_status: Option<SystemStatus>,
|
||||||
|
cached_agent_data: AgentData,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Track system component status for change detection
|
/// Track system component status for change detection
|
||||||
@ -55,36 +64,78 @@ impl Agent {
|
|||||||
config.zmq.publisher_port
|
config.zmq.publisher_port
|
||||||
);
|
);
|
||||||
|
|
||||||
// Initialize collectors
|
// Initialize collectors with timing information
|
||||||
let mut collectors: Vec<Box<dyn Collector>> = Vec::new();
|
let mut collectors: Vec<TimedCollector> = Vec::new();
|
||||||
|
|
||||||
// Add enabled collectors
|
// Add enabled collectors
|
||||||
if config.collectors.cpu.enabled {
|
if config.collectors.cpu.enabled {
|
||||||
collectors.push(Box::new(CpuCollector::new(config.collectors.cpu.clone())));
|
collectors.push(TimedCollector {
|
||||||
|
collector: Box::new(CpuCollector::new(config.collectors.cpu.clone())),
|
||||||
|
interval: Duration::from_secs(config.collectors.cpu.interval_seconds),
|
||||||
|
last_collection: None,
|
||||||
|
name: "CPU".to_string(),
|
||||||
|
});
|
||||||
|
info!("CPU collector initialized with {}s interval", config.collectors.cpu.interval_seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.collectors.memory.enabled {
|
if config.collectors.memory.enabled {
|
||||||
collectors.push(Box::new(MemoryCollector::new(config.collectors.memory.clone())));
|
collectors.push(TimedCollector {
|
||||||
|
collector: Box::new(MemoryCollector::new(config.collectors.memory.clone())),
|
||||||
|
interval: Duration::from_secs(config.collectors.memory.interval_seconds),
|
||||||
|
last_collection: None,
|
||||||
|
name: "Memory".to_string(),
|
||||||
|
});
|
||||||
|
info!("Memory collector initialized with {}s interval", config.collectors.memory.interval_seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.collectors.disk.enabled {
|
if config.collectors.disk.enabled {
|
||||||
collectors.push(Box::new(DiskCollector::new(config.collectors.disk.clone())));
|
collectors.push(TimedCollector {
|
||||||
|
collector: Box::new(DiskCollector::new(config.collectors.disk.clone())),
|
||||||
|
interval: Duration::from_secs(config.collectors.disk.interval_seconds),
|
||||||
|
last_collection: None,
|
||||||
|
name: "Disk".to_string(),
|
||||||
|
});
|
||||||
|
info!("Disk collector initialized with {}s interval", config.collectors.disk.interval_seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.collectors.systemd.enabled {
|
if config.collectors.systemd.enabled {
|
||||||
collectors.push(Box::new(SystemdCollector::new(config.collectors.systemd.clone())));
|
collectors.push(TimedCollector {
|
||||||
|
collector: Box::new(SystemdCollector::new(config.collectors.systemd.clone())),
|
||||||
|
interval: Duration::from_secs(config.collectors.systemd.interval_seconds),
|
||||||
|
last_collection: None,
|
||||||
|
name: "Systemd".to_string(),
|
||||||
|
});
|
||||||
|
info!("Systemd collector initialized with {}s interval", config.collectors.systemd.interval_seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.collectors.backup.enabled {
|
if config.collectors.backup.enabled {
|
||||||
collectors.push(Box::new(BackupCollector::new()));
|
collectors.push(TimedCollector {
|
||||||
|
collector: Box::new(BackupCollector::new()),
|
||||||
|
interval: Duration::from_secs(config.collectors.backup.interval_seconds),
|
||||||
|
last_collection: None,
|
||||||
|
name: "Backup".to_string(),
|
||||||
|
});
|
||||||
|
info!("Backup collector initialized with {}s interval", config.collectors.backup.interval_seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.collectors.network.enabled {
|
if config.collectors.network.enabled {
|
||||||
collectors.push(Box::new(NetworkCollector::new(config.collectors.network.clone())));
|
collectors.push(TimedCollector {
|
||||||
|
collector: Box::new(NetworkCollector::new(config.collectors.network.clone())),
|
||||||
|
interval: Duration::from_secs(config.collectors.network.interval_seconds),
|
||||||
|
last_collection: None,
|
||||||
|
name: "Network".to_string(),
|
||||||
|
});
|
||||||
|
info!("Network collector initialized with {}s interval", config.collectors.network.interval_seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.collectors.nixos.enabled {
|
if config.collectors.nixos.enabled {
|
||||||
collectors.push(Box::new(NixOSCollector::new(config.collectors.nixos.clone())));
|
collectors.push(TimedCollector {
|
||||||
|
collector: Box::new(NixOSCollector::new(config.collectors.nixos.clone())),
|
||||||
|
interval: Duration::from_secs(config.collectors.nixos.interval_seconds),
|
||||||
|
last_collection: None,
|
||||||
|
name: "NixOS".to_string(),
|
||||||
|
});
|
||||||
|
info!("NixOS collector initialized with {}s interval", config.collectors.nixos.interval_seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Initialized {} collectors", collectors.len());
|
info!("Initialized {} collectors", collectors.len());
|
||||||
@ -93,6 +144,9 @@ impl Agent {
|
|||||||
let notification_manager = NotificationManager::new(&config.notifications, &hostname)?;
|
let notification_manager = NotificationManager::new(&config.notifications, &hostname)?;
|
||||||
info!("Notification manager initialized");
|
info!("Notification manager initialized");
|
||||||
|
|
||||||
|
// Initialize cached agent data
|
||||||
|
let cached_agent_data = AgentData::new(hostname.clone(), env!("CARGO_PKG_VERSION").to_string());
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
hostname,
|
hostname,
|
||||||
config,
|
config,
|
||||||
@ -100,6 +154,7 @@ impl Agent {
|
|||||||
collectors,
|
collectors,
|
||||||
notification_manager,
|
notification_manager,
|
||||||
previous_status: None,
|
previous_status: None,
|
||||||
|
cached_agent_data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,24 +204,47 @@ impl Agent {
|
|||||||
async fn collect_and_broadcast(&mut self) -> Result<()> {
|
async fn collect_and_broadcast(&mut self) -> Result<()> {
|
||||||
debug!("Starting structured data collection");
|
debug!("Starting structured data collection");
|
||||||
|
|
||||||
// Initialize empty AgentData
|
// Collect data from collectors whose intervals have elapsed
|
||||||
let mut agent_data = AgentData::new(self.hostname.clone(), env!("CARGO_PKG_VERSION").to_string());
|
// Update cached_agent_data with new data
|
||||||
|
let now = Instant::now();
|
||||||
|
for timed_collector in &mut self.collectors {
|
||||||
|
let should_collect = match timed_collector.last_collection {
|
||||||
|
None => true, // First collection
|
||||||
|
Some(last_time) => now.duration_since(last_time) >= timed_collector.interval,
|
||||||
|
};
|
||||||
|
|
||||||
// Collect data from all collectors
|
if should_collect {
|
||||||
for collector in &self.collectors {
|
if let Err(e) = timed_collector.collector.collect_structured(&mut self.cached_agent_data).await {
|
||||||
if let Err(e) = collector.collect_structured(&mut agent_data).await {
|
error!("Collector {} failed: {}", timed_collector.name, e);
|
||||||
error!("Collector failed: {}", e);
|
// Update last_collection time even on failure to prevent immediate retries
|
||||||
// Continue with other collectors even if one fails
|
timed_collector.last_collection = Some(now);
|
||||||
|
} else {
|
||||||
|
timed_collector.last_collection = Some(now);
|
||||||
|
debug!(
|
||||||
|
"Collected from {} ({}s interval)",
|
||||||
|
timed_collector.name,
|
||||||
|
timed_collector.interval.as_secs()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update timestamp on cached data
|
||||||
|
self.cached_agent_data.timestamp = std::time::SystemTime::now()
|
||||||
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_secs();
|
||||||
|
|
||||||
|
// Clone for notification check (to avoid borrow issues)
|
||||||
|
let agent_data_snapshot = self.cached_agent_data.clone();
|
||||||
|
|
||||||
// Check for status changes and send notifications
|
// Check for status changes and send notifications
|
||||||
if let Err(e) = self.check_status_changes_and_notify(&agent_data).await {
|
if let Err(e) = self.check_status_changes_and_notify(&agent_data_snapshot).await {
|
||||||
error!("Failed to check status changes: {}", e);
|
error!("Failed to check status changes: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Broadcast the structured data via ZMQ
|
// Broadcast the cached structured data via ZMQ
|
||||||
if let Err(e) = self.zmq_handler.publish_agent_data(&agent_data).await {
|
if let Err(e) = self.zmq_handler.publish_agent_data(&agent_data_snapshot).await {
|
||||||
error!("Failed to broadcast agent data: {}", e);
|
error!("Failed to broadcast agent data: {}", e);
|
||||||
} else {
|
} else {
|
||||||
debug!("Successfully broadcast structured agent data");
|
debug!("Successfully broadcast structured agent data");
|
||||||
|
|||||||
@ -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(())
|
||||||
|
|||||||
@ -119,6 +119,71 @@ impl CpuCollector {
|
|||||||
utils::parse_u64(content.trim())
|
utils::parse_u64(content.trim())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Collect static CPU information from /proc/cpuinfo (only once at startup)
|
||||||
|
async fn collect_cpu_info(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||||
|
let content = utils::read_proc_file("/proc/cpuinfo")?;
|
||||||
|
|
||||||
|
let mut model_name: Option<String> = None;
|
||||||
|
let mut core_count: u32 = 0;
|
||||||
|
|
||||||
|
for line in content.lines() {
|
||||||
|
if line.starts_with("model name") {
|
||||||
|
if let Some(colon_pos) = line.find(':') {
|
||||||
|
let full_name = line[colon_pos + 1..].trim();
|
||||||
|
// Extract just the model number (e.g., "i7-9700" from "Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz")
|
||||||
|
let model = Self::extract_cpu_model(full_name);
|
||||||
|
if model_name.is_none() {
|
||||||
|
model_name = Some(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if line.starts_with("processor") {
|
||||||
|
core_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
agent_data.system.cpu.model_name = model_name;
|
||||||
|
if core_count > 0 {
|
||||||
|
agent_data.system.cpu.core_count = Some(core_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract CPU model number from full model name
|
||||||
|
/// Examples:
|
||||||
|
/// - "Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz" -> "i7-9700"
|
||||||
|
/// - "12th Gen Intel(R) Core(TM) i7-12700K" -> "i7-12700K"
|
||||||
|
/// - "AMD Ryzen 9 5950X 16-Core Processor" -> "Ryzen 9 5950X"
|
||||||
|
fn extract_cpu_model(full_name: &str) -> String {
|
||||||
|
// Look for Intel Core patterns (both old and new gen): i3, i5, i7, i9
|
||||||
|
// Match pattern like "i7-12700K" or "i7-9700"
|
||||||
|
for prefix in &["i3-", "i5-", "i7-", "i9-"] {
|
||||||
|
if let Some(pos) = full_name.find(prefix) {
|
||||||
|
// Find end of model number (until space or end of string)
|
||||||
|
let after_prefix = &full_name[pos..];
|
||||||
|
let end = after_prefix.find(' ').unwrap_or(after_prefix.len());
|
||||||
|
return after_prefix[..end].to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for AMD Ryzen pattern
|
||||||
|
if let Some(pos) = full_name.find("Ryzen") {
|
||||||
|
// Extract "Ryzen X XXXX" pattern
|
||||||
|
let after_ryzen = &full_name[pos..];
|
||||||
|
let parts: Vec<&str> = after_ryzen.split_whitespace().collect();
|
||||||
|
if parts.len() >= 3 {
|
||||||
|
return format!("{} {} {}", parts[0], parts[1], parts[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: return first 15 characters or full name if shorter
|
||||||
|
if full_name.len() > 15 {
|
||||||
|
full_name[..15].to_string()
|
||||||
|
} else {
|
||||||
|
full_name.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Collect CPU C-state (idle depth) and populate AgentData with top 3 C-states by usage
|
/// Collect CPU C-state (idle depth) and populate AgentData with top 3 C-states by usage
|
||||||
async fn collect_cstate(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
async fn collect_cstate(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||||
// Read C-state usage from first CPU (representative of overall system)
|
// Read C-state usage from first CPU (representative of overall system)
|
||||||
@ -192,6 +257,11 @@ impl Collector for CpuCollector {
|
|||||||
debug!("Collecting CPU metrics");
|
debug!("Collecting CPU metrics");
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
|
|
||||||
|
// Collect static CPU info (only once at startup)
|
||||||
|
if agent_data.system.cpu.model_name.is_none() || agent_data.system.cpu.core_count.is_none() {
|
||||||
|
self.collect_cpu_info(agent_data).await?;
|
||||||
|
}
|
||||||
|
|
||||||
// Collect load averages (always available)
|
// Collect load averages (always available)
|
||||||
self.collect_load_averages(agent_data).await?;
|
self.collect_load_averages(agent_data).await?;
|
||||||
|
|
||||||
@ -212,8 +282,8 @@ impl Collector for CpuCollector {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate status using thresholds
|
// Calculate status using thresholds (use 5-minute average for stability)
|
||||||
agent_data.system.cpu.load_status = self.calculate_load_status(agent_data.system.cpu.load_1min);
|
agent_data.system.cpu.load_status = self.calculate_load_status(agent_data.system.cpu.load_5min);
|
||||||
agent_data.system.cpu.temperature_status = if let Some(temp) = agent_data.system.cpu.temperature_celsius {
|
agent_data.system.cpu.temperature_status = if let Some(temp) = agent_data.system.cpu.temperature_celsius {
|
||||||
self.calculate_temperature_status(temp)
|
self.calculate_temperature_status(temp)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -66,6 +66,10 @@ impl DiskCollector {
|
|||||||
|
|
||||||
/// Collect all storage data and populate AgentData
|
/// Collect all storage data and populate AgentData
|
||||||
async fn collect_storage_data(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
async fn collect_storage_data(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||||
|
// Clear drives and pools to prevent duplicates when updating cached data
|
||||||
|
agent_data.system.storage.drives.clear();
|
||||||
|
agent_data.system.storage.pools.clear();
|
||||||
|
|
||||||
// Step 1: Get mount points and their backing devices
|
// Step 1: Get mount points and their backing devices
|
||||||
let mount_devices = self.get_mount_devices().await?;
|
let mount_devices = self.get_mount_devices().await?;
|
||||||
|
|
||||||
@ -110,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(),
|
||||||
@ -180,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),
|
||||||
@ -429,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(),
|
||||||
@ -768,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))?;
|
||||||
|
|
||||||
|
|||||||
@ -200,6 +200,9 @@ impl Collector for MemoryCollector {
|
|||||||
debug!("Collecting memory metrics");
|
debug!("Collecting memory metrics");
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
|
|
||||||
|
// Clear tmpfs list to prevent duplicates when updating cached data
|
||||||
|
agent_data.system.memory.tmpfs.clear();
|
||||||
|
|
||||||
// Parse memory info from /proc/meminfo
|
// Parse memory info from /proc/meminfo
|
||||||
let info = self.parse_meminfo().await?;
|
let info = self.parse_meminfo().await?;
|
||||||
|
|
||||||
|
|||||||
@ -181,6 +181,7 @@ impl NetworkCollector {
|
|||||||
link_status,
|
link_status,
|
||||||
parent_interface,
|
parent_interface,
|
||||||
vlan_id,
|
vlan_id,
|
||||||
|
connection_method: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ use cm_dashboard_shared::{AgentData, ServiceData, SubServiceData, SubServiceMetr
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use tracing::debug;
|
use tracing::{debug, info};
|
||||||
|
|
||||||
use super::{Collector, CollectorError};
|
use super::{Collector, CollectorError};
|
||||||
use crate::config::SystemdConfig;
|
use crate::config::SystemdConfig;
|
||||||
@ -142,23 +142,126 @@ impl SystemdCollector {
|
|||||||
|
|
||||||
// Add Docker images
|
// Add Docker images
|
||||||
let docker_images = self.get_docker_images();
|
let docker_images = self.get_docker_images();
|
||||||
for (image_name, image_status, image_size_mb) in docker_images {
|
for (image_name, _image_status, image_size_mb) in docker_images {
|
||||||
let mut metrics = Vec::new();
|
let metrics = Vec::new();
|
||||||
metrics.push(SubServiceMetric {
|
|
||||||
label: "size".to_string(),
|
|
||||||
value: image_size_mb,
|
|
||||||
unit: Some("MB".to_string()),
|
|
||||||
});
|
|
||||||
|
|
||||||
sub_services.push(SubServiceData {
|
sub_services.push(SubServiceData {
|
||||||
name: image_name.to_string(),
|
name: format!("{} size: {:.1} MB", image_name, image_size_mb),
|
||||||
service_status: self.calculate_service_status(&image_name, &image_status),
|
service_status: Status::Info, // Informational only, no status icon
|
||||||
metrics,
|
metrics,
|
||||||
service_type: "image".to_string(),
|
service_type: "image".to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if service_name == "openvpn-vpn-download" && status_info.active_state == "active" {
|
||||||
|
// Add VPN route
|
||||||
|
if let Some(external_ip) = self.get_vpn_external_ip() {
|
||||||
|
let metrics = Vec::new();
|
||||||
|
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("route: {}", external_ip),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics,
|
||||||
|
service_type: "vpn_route".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add torrent stats
|
||||||
|
if let Some((active_count, download_mbps, upload_mbps)) = self.get_qbittorrent_stats() {
|
||||||
|
let metrics = Vec::new();
|
||||||
|
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("{} active, ↓ {:.1} MB/s, ↑ {:.1} MB/s", active_count, download_mbps, upload_mbps),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics,
|
||||||
|
service_type: "torrent_stats".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add active torrent copy status for each copy operation
|
||||||
|
for torrent_name in self.get_active_torrent_copies() {
|
||||||
|
let metrics = Vec::new();
|
||||||
|
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("Copy: {}", torrent_name),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics,
|
||||||
|
service_type: "torrent_copy".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if service_name == "nftables" && status_info.active_state == "active" {
|
||||||
|
let (tcp_ports, udp_ports) = self.get_nftables_open_ports();
|
||||||
|
|
||||||
|
if !tcp_ports.is_empty() {
|
||||||
|
let metrics = Vec::new();
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("wan tcp: {}", tcp_ports),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics,
|
||||||
|
service_type: "firewall_port".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if !udp_ports.is_empty() {
|
||||||
|
let metrics = Vec::new();
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("wan udp: {}", udp_ports),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics,
|
||||||
|
service_type: "firewall_port".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if service_name == "tailscaled" && status_info.active_state == "active" {
|
||||||
|
// Add Tailscale peers with their connection methods as sub-services
|
||||||
|
let peers = self.get_tailscale_peers();
|
||||||
|
for (peer_name, conn_method) in peers {
|
||||||
|
let metrics = Vec::new();
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("{}: {}", peer_name, conn_method),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics,
|
||||||
|
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) in shares {
|
||||||
|
let metrics = Vec::new();
|
||||||
|
sub_services.push(SubServiceData {
|
||||||
|
name: format!("{}: {}", share_name, share_path),
|
||||||
|
service_status: Status::Info,
|
||||||
|
metrics,
|
||||||
|
service_type: "smb_share".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create complete service data
|
// Create complete service data
|
||||||
let service_data = ServiceData {
|
let service_data = ServiceData {
|
||||||
name: service_name.clone(),
|
name: service_name.clone(),
|
||||||
@ -836,11 +939,462 @@ impl SystemdCollector {
|
|||||||
_ => value, // Assume bytes if no unit
|
_ => value, // Assume bytes if no unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get VPN external IP by querying through the vpn namespace
|
||||||
|
fn get_vpn_external_ip(&self) -> Option<String> {
|
||||||
|
let output = Command::new("timeout")
|
||||||
|
.args(&[
|
||||||
|
"5",
|
||||||
|
"sudo",
|
||||||
|
"ip",
|
||||||
|
"netns",
|
||||||
|
"exec",
|
||||||
|
"vpn",
|
||||||
|
"curl",
|
||||||
|
"-s",
|
||||||
|
"--max-time",
|
||||||
|
"4",
|
||||||
|
"https://ifconfig.me"
|
||||||
|
])
|
||||||
|
.output()
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
if output.status.success() {
|
||||||
|
let ip = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||||
|
if !ip.is_empty() && ip.contains('.') {
|
||||||
|
return Some(ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get Tailscale connected peers with their connection methods
|
||||||
|
/// Returns a list of (device_name, connection_method) tuples
|
||||||
|
fn get_tailscale_peers(&self) -> Vec<(String, String)> {
|
||||||
|
match Command::new("timeout")
|
||||||
|
.args(["2", "tailscale", "status"])
|
||||||
|
.output()
|
||||||
|
{
|
||||||
|
Ok(output) if output.status.success() => {
|
||||||
|
let status_output = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let mut peers = Vec::new();
|
||||||
|
|
||||||
|
// Get current hostname to filter it out
|
||||||
|
let current_hostname = gethostname::gethostname()
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
// Parse tailscale status output
|
||||||
|
// Format: IP hostname user os status
|
||||||
|
// Example: 100.110.98.3 wslbox cm@ linux active; direct 192.168.30.227:53757
|
||||||
|
// Note: First line is always the current host, skip it
|
||||||
|
for (idx, line) in status_output.lines().enumerate() {
|
||||||
|
if idx == 0 {
|
||||||
|
continue; // Skip first line (current host)
|
||||||
|
}
|
||||||
|
|
||||||
|
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||||
|
if parts.len() < 5 {
|
||||||
|
continue; // Skip invalid lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build display strings: "path: mode [networks]"
|
||||||
|
let mut exports: Vec<(String, String)> = exports_map
|
||||||
|
.into_iter()
|
||||||
|
.map(|(path, mut entries)| {
|
||||||
|
if entries.is_empty() {
|
||||||
|
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) tuples
|
||||||
|
fn get_smb_shares(&self) -> Vec<(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;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start new share
|
||||||
|
let share_name = line[1..line.len()-1].trim().to_string();
|
||||||
|
current_share = Some(share_name);
|
||||||
|
current_path = None;
|
||||||
|
}
|
||||||
|
// 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shares
|
||||||
|
}
|
||||||
|
_ => Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get nftables open ports grouped by protocol
|
||||||
|
/// Returns: (tcp_ports_string, udp_ports_string)
|
||||||
|
fn get_nftables_open_ports(&self) -> (String, String) {
|
||||||
|
let output = Command::new("sudo")
|
||||||
|
.args(&["/run/current-system/sw/bin/nft", "list", "ruleset"])
|
||||||
|
.output();
|
||||||
|
|
||||||
|
let output = match output {
|
||||||
|
Ok(out) if out.status.success() => out,
|
||||||
|
Ok(out) => {
|
||||||
|
info!("nft command failed with status: {:?}, stderr: {}",
|
||||||
|
out.status, String::from_utf8_lossy(&out.stderr));
|
||||||
|
return (String::new(), String::new());
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
info!("Failed to execute nft command: {}", e);
|
||||||
|
return (String::new(), String::new());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let output_str = match String::from_utf8(output.stdout) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(_) => {
|
||||||
|
info!("Failed to parse nft output as UTF-8");
|
||||||
|
return (String::new(), String::new());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut tcp_ports = std::collections::HashSet::new();
|
||||||
|
let mut udp_ports = std::collections::HashSet::new();
|
||||||
|
|
||||||
|
// Parse nftables output for WAN incoming accept rules with dport
|
||||||
|
// Looking for patterns like: tcp dport 22 accept or tcp dport { 22, 80, 443 } accept
|
||||||
|
// Only include rules in input_wan chain
|
||||||
|
let mut in_wan_chain = false;
|
||||||
|
|
||||||
|
for line in output_str.lines() {
|
||||||
|
let line = line.trim();
|
||||||
|
|
||||||
|
// Track if we're in the input_wan chain
|
||||||
|
if line.contains("chain input_wan") {
|
||||||
|
in_wan_chain = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset when exiting chain (closing brace) or entering other chains
|
||||||
|
if line == "}" || (line.starts_with("chain ") && !line.contains("input_wan")) {
|
||||||
|
in_wan_chain = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only process rules in input_wan chain
|
||||||
|
if !in_wan_chain {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if not an accept rule
|
||||||
|
if !line.contains("accept") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse TCP ports
|
||||||
|
if line.contains("tcp dport") {
|
||||||
|
for port in self.extract_ports_from_nft_rule(line) {
|
||||||
|
tcp_ports.insert(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse UDP ports
|
||||||
|
if line.contains("udp dport") {
|
||||||
|
for port in self.extract_ports_from_nft_rule(line) {
|
||||||
|
udp_ports.insert(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort and format
|
||||||
|
let mut tcp_vec: Vec<u16> = tcp_ports.into_iter().collect();
|
||||||
|
let mut udp_vec: Vec<u16> = udp_ports.into_iter().collect();
|
||||||
|
tcp_vec.sort();
|
||||||
|
udp_vec.sort();
|
||||||
|
|
||||||
|
let tcp_str = tcp_vec.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(", ");
|
||||||
|
let udp_str = udp_vec.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(", ");
|
||||||
|
|
||||||
|
info!("nftables WAN ports - TCP: '{}', UDP: '{}'", tcp_str, udp_str);
|
||||||
|
|
||||||
|
(tcp_str, udp_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract port numbers from nftables rule line
|
||||||
|
/// Returns vector of ports (handles both single ports and sets)
|
||||||
|
fn extract_ports_from_nft_rule(&self, line: &str) -> Vec<u16> {
|
||||||
|
let mut ports = Vec::new();
|
||||||
|
|
||||||
|
// Pattern: "tcp dport 22 accept" or "tcp dport { 22, 80, 443 } accept"
|
||||||
|
if let Some(dport_pos) = line.find("dport") {
|
||||||
|
let after_dport = &line[dport_pos + 5..].trim();
|
||||||
|
|
||||||
|
// Handle port sets like { 22, 80, 443 }
|
||||||
|
if after_dport.starts_with('{') {
|
||||||
|
if let Some(end_brace) = after_dport.find('}') {
|
||||||
|
let ports_str = &after_dport[1..end_brace];
|
||||||
|
// Parse each port in the set
|
||||||
|
for port_str in ports_str.split(',') {
|
||||||
|
if let Ok(port) = port_str.trim().parse::<u16>() {
|
||||||
|
ports.push(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Single port
|
||||||
|
if let Some(port_str) = after_dport.split_whitespace().next() {
|
||||||
|
if let Ok(port) = port_str.parse::<u16>() {
|
||||||
|
ports.push(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ports
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get aggregate qBittorrent torrent statistics
|
||||||
|
/// Returns: (active_count, download_mbps, upload_mbps)
|
||||||
|
fn get_qbittorrent_stats(&self) -> Option<(u32, f32, f32)> {
|
||||||
|
// Query qBittorrent API through VPN namespace
|
||||||
|
let output = Command::new("timeout")
|
||||||
|
.args(&[
|
||||||
|
"5",
|
||||||
|
"sudo",
|
||||||
|
"ip",
|
||||||
|
"netns",
|
||||||
|
"exec",
|
||||||
|
"vpn",
|
||||||
|
"curl",
|
||||||
|
"-s",
|
||||||
|
"--max-time",
|
||||||
|
"4",
|
||||||
|
"http://localhost:8080/api/v2/torrents/info"
|
||||||
|
])
|
||||||
|
.output()
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let torrents: Vec<serde_json::Value> = serde_json::from_str(&output_str).ok()?;
|
||||||
|
|
||||||
|
let mut active_count = 0u32;
|
||||||
|
let mut total_download_bps = 0.0f64;
|
||||||
|
let mut total_upload_bps = 0.0f64;
|
||||||
|
|
||||||
|
for torrent in torrents {
|
||||||
|
let state = torrent["state"].as_str().unwrap_or("");
|
||||||
|
let dlspeed = torrent["dlspeed"].as_f64().unwrap_or(0.0);
|
||||||
|
let upspeed = torrent["upspeed"].as_f64().unwrap_or(0.0);
|
||||||
|
|
||||||
|
// States: downloading, uploading, stalledDL, stalledUP, queuedDL, queuedUP, pausedDL, pausedUP
|
||||||
|
// Count as active if downloading or uploading (seeding)
|
||||||
|
if state.contains("downloading") || state.contains("uploading") ||
|
||||||
|
state == "stalledDL" || state == "stalledUP" {
|
||||||
|
active_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
total_download_bps += dlspeed;
|
||||||
|
total_upload_bps += upspeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// qBittorrent returns bytes/s, convert to MB/s
|
||||||
|
let download_mbps = (total_download_bps / 1024.0 / 1024.0) as f32;
|
||||||
|
let upload_mbps = (total_upload_bps / 1024.0 / 1024.0) as f32;
|
||||||
|
|
||||||
|
Some((active_count, download_mbps, upload_mbps))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check for active torrent copy operations
|
||||||
|
/// Returns: Vec of filenames currently being copied
|
||||||
|
fn get_active_torrent_copies(&self) -> Vec<String> {
|
||||||
|
let marker_dir = "/tmp/torrent-copy";
|
||||||
|
let mut active_copies = Vec::new();
|
||||||
|
|
||||||
|
// Read all marker files from directory
|
||||||
|
if let Ok(entries) = std::fs::read_dir(marker_dir) {
|
||||||
|
for entry in entries.flatten() {
|
||||||
|
if let Ok(file_type) = entry.file_type() {
|
||||||
|
if file_type.is_file() {
|
||||||
|
// Filename is the marker (sanitized torrent name)
|
||||||
|
if let Some(filename) = entry.file_name().to_str() {
|
||||||
|
// Convert sanitized name back (replace _ with /)
|
||||||
|
let display_name = filename.replace('_', "/");
|
||||||
|
active_copies.push(display_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
active_copies
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Collector for SystemdCollector {
|
impl Collector for SystemdCollector {
|
||||||
async fn collect_structured(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
async fn collect_structured(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||||
|
// Clear services to prevent duplicates when updating cached data
|
||||||
|
agent_data.services.clear();
|
||||||
|
|
||||||
// Use cached complete data if available and fresh
|
// Use cached complete data if available and fresh
|
||||||
if let Some(cached_complete_services) = self.get_cached_complete_services() {
|
if let Some(cached_complete_services) = self.get_cached_complete_services() {
|
||||||
for service_data in cached_complete_services {
|
for service_data in cached_complete_services {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.227"
|
version = "0.1.275"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
event::{self},
|
event::{self, EnableMouseCapture, DisableMouseCapture, Event, MouseEvent, MouseEventKind, MouseButton},
|
||||||
execute,
|
execute,
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
};
|
};
|
||||||
use ratatui::{backend::CrosstermBackend, Terminal};
|
use ratatui::{backend::CrosstermBackend, Terminal, layout::Rect};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
@ -22,6 +22,9 @@ 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
|
||||||
|
services_area: Rect, // Store services area for mouse event handling
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dashboard {
|
impl Dashboard {
|
||||||
@ -92,7 +95,7 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
if let Err(e) = execute!(stdout, EnterAlternateScreen) {
|
if let Err(e) = execute!(stdout, EnterAlternateScreen, EnableMouseCapture) {
|
||||||
error!("Failed to enter alternate screen: {}", e);
|
error!("Failed to enter alternate screen: {}", e);
|
||||||
let _ = disable_raw_mode();
|
let _ = disable_raw_mode();
|
||||||
return Err(e.into());
|
return Err(e.into());
|
||||||
@ -121,6 +124,9 @@ 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(),
|
||||||
|
services_area: Rect::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,18 +138,22 @@ 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 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) => {
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
// Handle input
|
match event {
|
||||||
|
Event::Key(_) => {
|
||||||
|
// 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");
|
||||||
@ -155,6 +165,20 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Event::Mouse(mouse_event) => {
|
||||||
|
// Handle mouse events
|
||||||
|
if let Err(e) = self.handle_mouse_event(mouse_event) {
|
||||||
|
error!("Error handling mouse event: {}", e);
|
||||||
|
}
|
||||||
|
needs_render = true;
|
||||||
|
}
|
||||||
|
Event::Resize(_width, _height) => {
|
||||||
|
// Terminal was resized - mark for re-render
|
||||||
|
needs_render = true;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Error reading terminal event: {}", e);
|
error!("Error reading terminal event: {}", e);
|
||||||
@ -168,17 +192,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 {
|
|
||||||
if let Err(e) = terminal.draw(|frame| {
|
|
||||||
tui_app.render(frame, &self.metric_store);
|
|
||||||
}) {
|
|
||||||
error!("Error rendering TUI after input: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for new metrics
|
// Check for new metrics
|
||||||
@ -217,6 +230,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
|
||||||
@ -245,31 +260,416 @@ 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 {
|
||||||
if let Err(e) = terminal.draw(|frame| {
|
// Clear and autoresize terminal to handle any resize events
|
||||||
tui_app.render(frame, &self.metric_store);
|
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: {}", e);
|
error!("Error rendering TUI: {}", e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handle mouse events
|
||||||
|
fn handle_mouse_event(&mut self, mouse: MouseEvent) -> Result<()> {
|
||||||
|
let x = mouse.column;
|
||||||
|
let y = mouse.row;
|
||||||
|
|
||||||
|
// Handle popup menu if open
|
||||||
|
let popup_info = if let Some(ref tui_app) = self.tui_app {
|
||||||
|
tui_app.popup_menu.clone().map(|popup| {
|
||||||
|
let hostname = tui_app.current_host.clone();
|
||||||
|
(popup, hostname)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some((popup, hostname)) = popup_info {
|
||||||
|
// Calculate popup bounds using screen coordinates
|
||||||
|
let popup_width = 20;
|
||||||
|
let popup_height = 5; // 3 items + 2 borders
|
||||||
|
|
||||||
|
// Get terminal size
|
||||||
|
let (screen_width, screen_height) = if let Some(ref terminal) = self.terminal {
|
||||||
|
let size = terminal.size().unwrap_or_default();
|
||||||
|
(size.width, size.height)
|
||||||
|
} else {
|
||||||
|
(80, 24) // fallback
|
||||||
|
};
|
||||||
|
|
||||||
|
let popup_x = if popup.x + popup_width < screen_width {
|
||||||
|
popup.x
|
||||||
|
} else {
|
||||||
|
screen_width.saturating_sub(popup_width)
|
||||||
|
};
|
||||||
|
|
||||||
|
let popup_y = if popup.y + popup_height < screen_height {
|
||||||
|
popup.y
|
||||||
|
} else {
|
||||||
|
screen_height.saturating_sub(popup_height)
|
||||||
|
};
|
||||||
|
|
||||||
|
let popup_area = Rect {
|
||||||
|
x: popup_x,
|
||||||
|
y: popup_y,
|
||||||
|
width: popup_width,
|
||||||
|
height: popup_height,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update selected index on mouse move
|
||||||
|
if matches!(mouse.kind, MouseEventKind::Moved) {
|
||||||
|
if is_in_area(x, y, &popup_area) {
|
||||||
|
let relative_y = y.saturating_sub(popup_y + 1) as usize; // +1 for top border
|
||||||
|
if relative_y < 3 {
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
if let Some(ref mut popup) = tui_app.popup_menu {
|
||||||
|
popup.selected_index = relative_y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
|
||||||
|
if is_in_area(x, y, &popup_area) {
|
||||||
|
// Click inside popup - execute action
|
||||||
|
let relative_y = y.saturating_sub(popup_y + 1) as usize; // +1 for top border
|
||||||
|
if relative_y < 3 {
|
||||||
|
// Execute the selected action
|
||||||
|
self.execute_service_action(relative_y, &popup.service_name, hostname.as_deref())?;
|
||||||
|
}
|
||||||
|
// Close popup after action
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
tui_app.popup_menu = None;
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
// Click outside popup - close it
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
tui_app.popup_menu = None;
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any other event while popup is open - don't process panels
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for title bar clicks (host selection)
|
||||||
|
if matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
|
||||||
|
if is_in_area(x, y, &self.title_area) {
|
||||||
|
// Click in title bar - check if it's on a hostname
|
||||||
|
// The title bar has "cm-dashboard vX.X.X" on the left (22 chars)
|
||||||
|
// 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 {
|
||||||
|
tui_app.switch_to_host(&host);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine which panel the mouse is over
|
||||||
|
let in_system_area = is_in_area(x, y, &self.system_area);
|
||||||
|
let in_services_area = is_in_area(x, y, &self.services_area);
|
||||||
|
|
||||||
|
if !in_system_area && !in_services_area {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle mouse events
|
||||||
|
match mouse.kind {
|
||||||
|
MouseEventKind::ScrollDown => {
|
||||||
|
if in_system_area {
|
||||||
|
// Scroll down in system panel
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
if let Some(hostname) = tui_app.current_host.clone() {
|
||||||
|
let host_widgets = tui_app.get_or_create_host_widgets(&hostname);
|
||||||
|
let visible_height = self.system_area.height as usize;
|
||||||
|
let total_lines = host_widgets.system_widget.get_total_lines();
|
||||||
|
host_widgets.system_widget.scroll_down(visible_height, total_lines);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if in_services_area {
|
||||||
|
// Scroll down in services panel
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
if let Some(hostname) = tui_app.current_host.clone() {
|
||||||
|
let host_widgets = tui_app.get_or_create_host_widgets(&hostname);
|
||||||
|
// Calculate visible height (panel height - borders and header)
|
||||||
|
let visible_height = self.services_area.height.saturating_sub(3) as usize;
|
||||||
|
host_widgets.services_widget.scroll_down(visible_height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MouseEventKind::ScrollUp => {
|
||||||
|
if in_system_area {
|
||||||
|
// Scroll up in system panel
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
if let Some(hostname) = tui_app.current_host.clone() {
|
||||||
|
let host_widgets = tui_app.get_or_create_host_widgets(&hostname);
|
||||||
|
host_widgets.system_widget.scroll_up();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if in_services_area {
|
||||||
|
// Scroll up in services panel
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
if let Some(hostname) = tui_app.current_host.clone() {
|
||||||
|
let host_widgets = tui_app.get_or_create_host_widgets(&hostname);
|
||||||
|
host_widgets.services_widget.scroll_up();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MouseEventKind::Down(button) => {
|
||||||
|
// Only handle clicks in services area (not system area)
|
||||||
|
if !in_services_area {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate which service was clicked
|
||||||
|
// 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
|
||||||
|
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
if let Some(hostname) = tui_app.current_host.clone() {
|
||||||
|
let host_widgets = tui_app.get_or_create_host_widgets(&hostname);
|
||||||
|
|
||||||
|
// Account for scroll offset - the clicked line is relative to viewport
|
||||||
|
let display_line_index = host_widgets.services_widget.scroll_offset + relative_y;
|
||||||
|
|
||||||
|
// Map display line to parent service index
|
||||||
|
if let Some(parent_index) = host_widgets.services_widget.display_line_to_parent_index(display_line_index) {
|
||||||
|
// Set the selected index to the clicked parent service
|
||||||
|
host_widgets.services_widget.selected_index = parent_index;
|
||||||
|
|
||||||
|
match button {
|
||||||
|
MouseButton::Left => {
|
||||||
|
// Left click just selects the service
|
||||||
|
debug!("Left-clicked service at display line {} (parent index: {})", display_line_index, parent_index);
|
||||||
|
}
|
||||||
|
MouseButton::Right => {
|
||||||
|
// Right click opens context menu
|
||||||
|
debug!("Right-clicked service at display line {} (parent index: {})", display_line_index, parent_index);
|
||||||
|
|
||||||
|
// Get the service name for the popup
|
||||||
|
if let Some(service_name) = host_widgets.services_widget.get_selected_service() {
|
||||||
|
tui_app.popup_menu = Some(crate::ui::PopupMenu {
|
||||||
|
service_name,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
selected_index: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Execute service action from popup menu
|
||||||
|
fn execute_service_action(&self, action_index: usize, service_name: &str, hostname: Option<&str>) -> Result<()> {
|
||||||
|
let Some(hostname) = hostname else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
let connection_ip = self.get_connection_ip(hostname);
|
||||||
|
|
||||||
|
match action_index {
|
||||||
|
0 => {
|
||||||
|
// Start Service
|
||||||
|
let service_start_command = format!(
|
||||||
|
"echo 'Starting service: {} on {}' && ssh -tt {}@{} \"bash -ic '{} start {}'\"",
|
||||||
|
service_name,
|
||||||
|
hostname,
|
||||||
|
self.config.ssh.rebuild_user,
|
||||||
|
connection_ip,
|
||||||
|
self.config.ssh.service_manage_cmd,
|
||||||
|
service_name
|
||||||
|
);
|
||||||
|
|
||||||
|
std::process::Command::new("tmux")
|
||||||
|
.arg("split-window")
|
||||||
|
.arg("-v")
|
||||||
|
.arg("-p")
|
||||||
|
.arg("30")
|
||||||
|
.arg(&service_start_command)
|
||||||
|
.spawn()
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
// Stop Service
|
||||||
|
let service_stop_command = format!(
|
||||||
|
"echo 'Stopping service: {} on {}' && ssh -tt {}@{} \"bash -ic '{} stop {}'\"",
|
||||||
|
service_name,
|
||||||
|
hostname,
|
||||||
|
self.config.ssh.rebuild_user,
|
||||||
|
connection_ip,
|
||||||
|
self.config.ssh.service_manage_cmd,
|
||||||
|
service_name
|
||||||
|
);
|
||||||
|
|
||||||
|
std::process::Command::new("tmux")
|
||||||
|
.arg("split-window")
|
||||||
|
.arg("-v")
|
||||||
|
.arg("-p")
|
||||||
|
.arg("30")
|
||||||
|
.arg(&service_stop_command)
|
||||||
|
.spawn()
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
// View Logs
|
||||||
|
let logs_command = format!(
|
||||||
|
"ssh -tt {}@{} '{} logs {}'",
|
||||||
|
self.config.ssh.rebuild_user,
|
||||||
|
connection_ip,
|
||||||
|
self.config.ssh.service_manage_cmd,
|
||||||
|
service_name
|
||||||
|
);
|
||||||
|
|
||||||
|
std::process::Command::new("tmux")
|
||||||
|
.arg("split-window")
|
||||||
|
.arg("-v")
|
||||||
|
.arg("-p")
|
||||||
|
.arg("30")
|
||||||
|
.arg(&logs_command)
|
||||||
|
.spawn()
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get connection IP for a host
|
||||||
|
fn get_connection_ip(&self, hostname: &str) -> String {
|
||||||
|
self.config
|
||||||
|
.hosts
|
||||||
|
.get(hostname)
|
||||||
|
.and_then(|h| h.ip.clone())
|
||||||
|
.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
|
||||||
|
fn is_in_area(x: u16, y: u16, area: &Rect) -> bool {
|
||||||
|
x >= area.x && x < area.x + area.width
|
||||||
|
&& y >= area.y && y < area.y + area.height
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Dashboard {
|
impl Drop for Dashboard {
|
||||||
@ -278,7 +678,7 @@ impl Drop for Dashboard {
|
|||||||
if !self.headless {
|
if !self.headless {
|
||||||
let _ = disable_raw_mode();
|
let _ = disable_raw_mode();
|
||||||
if let Some(ref mut terminal) = self.terminal {
|
if let Some(ref mut terminal) = self.terminal {
|
||||||
let _ = execute!(terminal.backend_mut(), LeaveAlternateScreen);
|
let _ = execute!(terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture);
|
||||||
let _ = terminal.show_cursor();
|
let _ = terminal.show_cursor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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();
|
||||||
|
|||||||
@ -17,7 +17,7 @@ pub mod widgets;
|
|||||||
use crate::config::DashboardConfig;
|
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, Typography};
|
use theme::{Components, Layout as ThemeLayout, Theme};
|
||||||
use widgets::{ServicesWidget, SystemWidget, Widget};
|
use widgets::{ServicesWidget, SystemWidget, Widget};
|
||||||
|
|
||||||
|
|
||||||
@ -47,12 +47,21 @@ impl HostWidgets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Popup menu state
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct PopupMenu {
|
||||||
|
pub service_name: String,
|
||||||
|
pub x: u16,
|
||||||
|
pub y: u16,
|
||||||
|
pub selected_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
/// Main TUI application
|
/// Main TUI application
|
||||||
pub struct TuiApp {
|
pub struct TuiApp {
|
||||||
/// Widget states per host (hostname -> HostWidgets)
|
/// Widget states per host (hostname -> HostWidgets)
|
||||||
host_widgets: HashMap<String, HostWidgets>,
|
host_widgets: HashMap<String, HostWidgets>,
|
||||||
/// Current active host
|
/// Current active host
|
||||||
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 for navigation
|
||||||
@ -65,6 +74,8 @@ pub struct TuiApp {
|
|||||||
config: DashboardConfig,
|
config: DashboardConfig,
|
||||||
/// Cached localhost hostname to avoid repeated system calls
|
/// Cached localhost hostname to avoid repeated system calls
|
||||||
localhost: String,
|
localhost: String,
|
||||||
|
/// Active popup menu (if any)
|
||||||
|
pub popup_menu: Option<PopupMenu>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TuiApp {
|
impl TuiApp {
|
||||||
@ -79,6 +90,7 @@ impl TuiApp {
|
|||||||
user_navigated_away: false,
|
user_navigated_away: false,
|
||||||
config,
|
config,
|
||||||
localhost,
|
localhost,
|
||||||
|
popup_menu: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sort predefined hosts
|
// Sort predefined hosts
|
||||||
@ -93,7 +105,7 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get or create host widgets for the given hostname
|
/// Get or create host widgets for the given hostname
|
||||||
fn get_or_create_host_widgets(&mut self, hostname: &str) -> &mut HostWidgets {
|
pub fn get_or_create_host_widgets(&mut self, hostname: &str) -> &mut HostWidgets {
|
||||||
self.host_widgets
|
self.host_widgets
|
||||||
.entry(hostname.to_string())
|
.entry(hostname.to_string())
|
||||||
.or_insert_with(HostWidgets::new)
|
.or_insert_with(HostWidgets::new)
|
||||||
@ -110,14 +122,6 @@ impl TuiApp {
|
|||||||
host_widgets.system_widget.update_from_agent_data(agent_data);
|
host_widgets.system_widget.update_from_agent_data(agent_data);
|
||||||
host_widgets.services_widget.update_from_agent_data(agent_data);
|
host_widgets.services_widget.update_from_agent_data(agent_data);
|
||||||
|
|
||||||
// Update ZMQ stats
|
|
||||||
if let Some(zmq_stats) = metric_store.get_zmq_stats(&hostname) {
|
|
||||||
host_widgets.system_widget.update_zmq_stats(
|
|
||||||
zmq_stats.packets_received,
|
|
||||||
zmq_stats.last_packet_age_secs
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
host_widgets.last_update = Some(Instant::now());
|
host_widgets.last_update = Some(Instant::now());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,6 +171,14 @@ impl TuiApp {
|
|||||||
/// Handle keyboard input
|
/// Handle keyboard input
|
||||||
pub fn handle_input(&mut self, event: Event) -> Result<()> {
|
pub fn handle_input(&mut self, event: Event) -> Result<()> {
|
||||||
if let Event::Key(key) = event {
|
if let Event::Key(key) = event {
|
||||||
|
// Close popup on Escape
|
||||||
|
if matches!(key.code, KeyCode::Esc) {
|
||||||
|
if self.popup_menu.is_some() {
|
||||||
|
self.popup_menu = None;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char('q') => {
|
KeyCode::Char('q') => {
|
||||||
self.should_quit = true;
|
self.should_quit = true;
|
||||||
@ -371,6 +383,23 @@ impl TuiApp {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Switch to a specific host by name
|
||||||
|
pub fn switch_to_host(&mut self, hostname: &str) {
|
||||||
|
if let Some(index) = self.available_hosts.iter().position(|h| h == hostname) {
|
||||||
|
self.host_index = index;
|
||||||
|
self.current_host = Some(hostname.to_string());
|
||||||
|
|
||||||
|
// Check if user navigated away from localhost
|
||||||
|
if hostname != &self.localhost {
|
||||||
|
self.user_navigated_away = true;
|
||||||
|
} else {
|
||||||
|
self.user_navigated_away = false; // User navigated back to localhost
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Switched to host: {}", hostname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Navigate between hosts
|
/// Navigate between hosts
|
||||||
fn navigate_host(&mut self, direction: i32) {
|
fn navigate_host(&mut self, direction: i32) {
|
||||||
if self.available_hosts.is_empty() {
|
if self.available_hosts.is_empty() {
|
||||||
@ -416,6 +445,10 @@ impl TuiApp {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the list of available hosts
|
||||||
|
pub fn get_available_hosts(&self) -> &Vec<String> {
|
||||||
|
&self.available_hosts
|
||||||
|
}
|
||||||
|
|
||||||
/// Should quit application
|
/// Should quit application
|
||||||
pub fn should_quit(&self) -> bool {
|
pub fn should_quit(&self) -> bool {
|
||||||
@ -429,7 +462,7 @@ impl TuiApp {
|
|||||||
|
|
||||||
|
|
||||||
/// Render the dashboard (real btop-style multi-panel layout)
|
/// Render the dashboard (real btop-style multi-panel layout)
|
||||||
pub fn render(&mut self, frame: &mut Frame, metric_store: &MetricStore) {
|
pub fn render(&mut self, frame: &mut Frame, metric_store: &MetricStore) -> (Rect, Rect, Rect) {
|
||||||
let size = frame.size();
|
let size = frame.size();
|
||||||
|
|
||||||
// Clear background to true black like btop
|
// Clear background to true black like btop
|
||||||
@ -469,8 +502,8 @@ impl TuiApp {
|
|||||||
if current_host_offline {
|
if current_host_offline {
|
||||||
self.render_offline_host_message(frame, main_chunks[1]);
|
self.render_offline_host_message(frame, main_chunks[1]);
|
||||||
self.render_btop_title(frame, main_chunks[0], metric_store);
|
self.render_btop_title(frame, main_chunks[0], metric_store);
|
||||||
self.render_statusbar(frame, main_chunks[2]);
|
self.render_statusbar(frame, main_chunks[2], metric_store);
|
||||||
return;
|
return (main_chunks[0], Rect::default(), Rect::default()); // Return title area and empty areas when offline
|
||||||
}
|
}
|
||||||
|
|
||||||
// Left side: system panel only (full height)
|
// Left side: system panel only (full height)
|
||||||
@ -483,20 +516,29 @@ impl TuiApp {
|
|||||||
self.render_btop_title(frame, main_chunks[0], metric_store);
|
self.render_btop_title(frame, main_chunks[0], metric_store);
|
||||||
|
|
||||||
// Render system panel
|
// Render system panel
|
||||||
self.render_system_panel(frame, left_chunks[0], metric_store);
|
let system_area = left_chunks[0];
|
||||||
|
self.render_system_panel(frame, system_area, metric_store);
|
||||||
|
|
||||||
// Render services widget for current host
|
// Render services widget for current host
|
||||||
|
let services_area = content_chunks[1];
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
let is_focused = true; // Always show service selection
|
let is_focused = true; // Always show service selection
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
||||||
host_widgets
|
host_widgets
|
||||||
.services_widget
|
.services_widget
|
||||||
.render(frame, content_chunks[1], is_focused); // Services takes full right side
|
.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]); // main_chunks[2] is the statusbar area
|
self.render_statusbar(frame, main_chunks[2], metric_store);
|
||||||
|
|
||||||
|
// Render popup menu on top of everything if active
|
||||||
|
if let Some(ref popup) = self.popup_menu {
|
||||||
|
self.render_popup_menu(frame, popup);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return all areas for mouse event handling
|
||||||
|
(main_chunks[0], system_area, services_area)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render btop-style minimal title with host status colors
|
/// Render btop-style minimal title with host status colors
|
||||||
@ -564,7 +606,14 @@ impl TuiApp {
|
|||||||
));
|
));
|
||||||
|
|
||||||
if Some(host) == self.current_host.as_ref() {
|
if Some(host) == self.current_host.as_ref() {
|
||||||
// Selected host in bold background color against status background
|
// 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_spans.push(Span::styled(
|
||||||
host.clone(),
|
host.clone(),
|
||||||
Style::default()
|
Style::default()
|
||||||
@ -572,6 +621,13 @@ impl TuiApp {
|
|||||||
.bg(background_color)
|
.bg(background_color)
|
||||||
.add_modifier(Modifier::BOLD),
|
.add_modifier(Modifier::BOLD),
|
||||||
));
|
));
|
||||||
|
host_spans.push(Span::styled(
|
||||||
|
"]",
|
||||||
|
Style::default()
|
||||||
|
.fg(Theme::background())
|
||||||
|
.bg(background_color)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
// Other hosts in normal background color against status background
|
// Other hosts in normal background color against status background
|
||||||
host_spans.push(Span::styled(
|
host_spans.push(Span::styled(
|
||||||
@ -605,36 +661,137 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render dynamic statusbar with context-aware shortcuts
|
/// Render popup menu for service actions
|
||||||
fn render_statusbar(&self, frame: &mut Frame, area: Rect) {
|
fn render_popup_menu(&self, frame: &mut Frame, popup: &PopupMenu) {
|
||||||
let shortcuts = self.get_context_shortcuts();
|
use ratatui::widgets::{Block, Borders, Clear, List, ListItem};
|
||||||
let statusbar_text = shortcuts.join(" • ");
|
use ratatui::style::{Color, Modifier};
|
||||||
|
|
||||||
let statusbar = Paragraph::new(statusbar_text)
|
// Menu items
|
||||||
.style(Typography::secondary())
|
let items = vec![
|
||||||
.alignment(ratatui::layout::Alignment::Center);
|
"Start Service",
|
||||||
|
"Stop Service",
|
||||||
|
"View Logs",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Calculate popup size
|
||||||
|
let width = 20;
|
||||||
|
let height = items.len() as u16 + 2; // +2 for borders
|
||||||
|
|
||||||
|
// Position popup near click location, but keep it on screen
|
||||||
|
let screen_width = frame.size().width;
|
||||||
|
let screen_height = frame.size().height;
|
||||||
|
|
||||||
|
let x = if popup.x + width < screen_width {
|
||||||
|
popup.x
|
||||||
|
} else {
|
||||||
|
screen_width.saturating_sub(width)
|
||||||
|
};
|
||||||
|
|
||||||
|
let y = if popup.y + height < screen_height {
|
||||||
|
popup.y
|
||||||
|
} else {
|
||||||
|
screen_height.saturating_sub(height)
|
||||||
|
};
|
||||||
|
|
||||||
|
let popup_area = Rect {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create menu items with selection highlight
|
||||||
|
let menu_items: Vec<ListItem> = items
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, item)| {
|
||||||
|
let style = if i == popup.selected_index {
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::Black)
|
||||||
|
.bg(Color::White)
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(Theme::primary_text())
|
||||||
|
};
|
||||||
|
ListItem::new(*item).style(style)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let menu_list = List::new(menu_items)
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.style(Style::default().bg(Theme::background()).fg(Theme::primary_text()))
|
||||||
|
);
|
||||||
|
|
||||||
|
// Clear the area and render menu
|
||||||
|
frame.render_widget(Clear, popup_area);
|
||||||
|
frame.render_widget(menu_list, popup_area);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render statusbar with host and client IPs
|
||||||
|
fn render_statusbar(&self, frame: &mut Frame, area: Rect, _metric_store: &MetricStore) {
|
||||||
|
use ratatui::text::{Line, Span};
|
||||||
|
use ratatui::widgets::Paragraph;
|
||||||
|
|
||||||
|
// Get current host info
|
||||||
|
let (hostname_str, host_ip, build_version, agent_version) = if let Some(hostname) = &self.current_host {
|
||||||
|
// Get the connection IP (the IP dashboard uses to connect to the agent)
|
||||||
|
let ip = if let Some(host_details) = self.config.hosts.get(hostname) {
|
||||||
|
host_details.get_connection_ip(hostname)
|
||||||
|
} else {
|
||||||
|
hostname.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get build and agent versions from system widget
|
||||||
|
let (build, agent) = if let Some(host_widgets) = self.host_widgets.get(hostname) {
|
||||||
|
let build = host_widgets.system_widget.get_build_version().unwrap_or("N/A".to_string());
|
||||||
|
let agent = host_widgets.system_widget.get_agent_version().unwrap_or("N/A".to_string());
|
||||||
|
(build, agent)
|
||||||
|
} else {
|
||||||
|
("N/A".to_string(), "N/A".to_string())
|
||||||
|
};
|
||||||
|
|
||||||
|
(hostname.clone(), ip, build, agent)
|
||||||
|
} else {
|
||||||
|
("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);
|
||||||
|
|
||||||
|
// Get dashboard local IP
|
||||||
|
let dashboard_ip = Self::get_local_ip();
|
||||||
|
let right_text = format!("Dashboard: {}", dashboard_ip);
|
||||||
|
|
||||||
|
// 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![
|
||||||
|
Span::raw(" "), // 1 char left padding
|
||||||
|
Span::styled(left_text, Style::default().fg(Theme::border())),
|
||||||
|
Span::raw(spacing_str),
|
||||||
|
Span::styled(right_text, Style::default().fg(Theme::border())),
|
||||||
|
]);
|
||||||
|
|
||||||
|
let statusbar = Paragraph::new(line);
|
||||||
frame.render_widget(statusbar, area);
|
frame.render_widget(statusbar, area);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get context-aware shortcuts based on focused panel
|
/// Get local IP address of the dashboard
|
||||||
fn get_context_shortcuts(&self) -> Vec<String> {
|
fn get_local_ip() -> String {
|
||||||
let mut shortcuts = Vec::new();
|
use std::net::UdpSocket;
|
||||||
|
|
||||||
// Global shortcuts
|
// Try to get local IP by creating a UDP socket
|
||||||
shortcuts.push("Tab: Host".to_string());
|
// This doesn't actually send data, just determines routing
|
||||||
shortcuts.push("↑↓/jk: Select".to_string());
|
if let Ok(socket) = UdpSocket::bind("0.0.0.0:0") {
|
||||||
shortcuts.push("r: Rebuild".to_string());
|
if socket.connect("8.8.8.8:80").is_ok() {
|
||||||
shortcuts.push("B: Backup".to_string());
|
if let Ok(addr) = socket.local_addr() {
|
||||||
shortcuts.push("s/S: Start/Stop".to_string());
|
return addr.ip().to_string();
|
||||||
shortcuts.push("L: Logs".to_string());
|
}
|
||||||
shortcuts.push("t: Terminal".to_string());
|
}
|
||||||
shortcuts.push("w: Wake".to_string());
|
}
|
||||||
|
"N/A".to_string()
|
||||||
// Always show quit
|
|
||||||
shortcuts.push("q: Quit".to_string());
|
|
||||||
|
|
||||||
shortcuts
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_system_panel(&mut self, frame: &mut Frame, area: Rect, _metric_store: &MetricStore) {
|
fn render_system_panel(&mut self, frame: &mut Frame, area: Rect, _metric_store: &MetricStore) {
|
||||||
|
|||||||
@ -142,6 +142,7 @@ impl Theme {
|
|||||||
/// Get color for status level
|
/// Get color for status level
|
||||||
pub fn status_color(status: Status) -> Color {
|
pub fn status_color(status: Status) -> Color {
|
||||||
match status {
|
match status {
|
||||||
|
Status::Info => Self::muted_text(), // Gray for informational data
|
||||||
Status::Ok => Self::success(),
|
Status::Ok => Self::success(),
|
||||||
Status::Inactive => Self::muted_text(), // Gray for inactive services in service list
|
Status::Inactive => Self::muted_text(), // Gray for inactive services in service list
|
||||||
Status::Pending => Self::highlight(), // Blue for pending
|
Status::Pending => Self::highlight(), // Blue for pending
|
||||||
@ -240,6 +241,7 @@ impl StatusIcons {
|
|||||||
/// Get status icon symbol
|
/// Get status icon symbol
|
||||||
pub fn get_icon(status: Status) -> &'static str {
|
pub fn get_icon(status: Status) -> &'static str {
|
||||||
match status {
|
match status {
|
||||||
|
Status::Info => "", // No icon for informational data
|
||||||
Status::Ok => "●",
|
Status::Ok => "●",
|
||||||
Status::Inactive => "○", // Empty circle for inactive services
|
Status::Inactive => "○", // Empty circle for inactive services
|
||||||
Status::Pending => "◉", // Hollow circle for pending
|
Status::Pending => "◉", // Hollow circle for pending
|
||||||
@ -254,6 +256,7 @@ impl StatusIcons {
|
|||||||
pub fn create_status_spans(status: Status, text: &str) -> Vec<ratatui::text::Span<'static>> {
|
pub fn create_status_spans(status: Status, text: &str) -> Vec<ratatui::text::Span<'static>> {
|
||||||
let icon = Self::get_icon(status);
|
let icon = Self::get_icon(status);
|
||||||
let status_color = match status {
|
let status_color = match status {
|
||||||
|
Status::Info => Theme::muted_text(), // Gray for info
|
||||||
Status::Ok => Theme::success(), // Green
|
Status::Ok => Theme::success(), // Green
|
||||||
Status::Inactive => Theme::muted_text(), // Gray for inactive services
|
Status::Inactive => Theme::muted_text(), // Gray for inactive services
|
||||||
Status::Pending => Theme::highlight(), // Blue
|
Status::Pending => Theme::highlight(), // Blue
|
||||||
|
|||||||
@ -22,10 +22,25 @@ struct ColumnVisibility {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ColumnVisibility {
|
impl ColumnVisibility {
|
||||||
|
/// Calculate actual width needed for all columns
|
||||||
|
const NAME_WIDTH: u16 = 23;
|
||||||
|
const STATUS_WIDTH: u16 = 10;
|
||||||
|
const RAM_WIDTH: u16 = 8;
|
||||||
|
const UPTIME_WIDTH: u16 = 8;
|
||||||
|
const RESTARTS_WIDTH: u16 = 5;
|
||||||
|
const COLUMN_SPACING: u16 = 1; // Space between columns
|
||||||
|
|
||||||
/// Determine which columns to show based on available width
|
/// Determine which columns to show based on available width
|
||||||
|
/// Priority order: Name > Status > RAM > Uptime > Restarts
|
||||||
fn from_width(width: u16) -> Self {
|
fn from_width(width: u16) -> Self {
|
||||||
if width >= 80 {
|
// Calculate cumulative widths for each configuration
|
||||||
// Full layout: Name (25) + Status (10) + RAM (8) + Uptime (8) + Restarts (5) = 56 chars
|
let minimal = Self::NAME_WIDTH + Self::COLUMN_SPACING + Self::STATUS_WIDTH; // 34
|
||||||
|
let with_ram = minimal + Self::COLUMN_SPACING + Self::RAM_WIDTH; // 43
|
||||||
|
let with_uptime = with_ram + Self::COLUMN_SPACING + Self::UPTIME_WIDTH; // 52
|
||||||
|
let full = with_uptime + Self::COLUMN_SPACING + Self::RESTARTS_WIDTH; // 58
|
||||||
|
|
||||||
|
if width >= full {
|
||||||
|
// Show all columns
|
||||||
Self {
|
Self {
|
||||||
show_name: true,
|
show_name: true,
|
||||||
show_status: true,
|
show_status: true,
|
||||||
@ -33,8 +48,8 @@ impl ColumnVisibility {
|
|||||||
show_uptime: true,
|
show_uptime: true,
|
||||||
show_restarts: true,
|
show_restarts: true,
|
||||||
}
|
}
|
||||||
} else if width >= 60 {
|
} else if width >= with_uptime {
|
||||||
// Hide restarts: Name (25) + Status (10) + RAM (8) + Uptime (8) = 51 chars
|
// Hide restarts
|
||||||
Self {
|
Self {
|
||||||
show_name: true,
|
show_name: true,
|
||||||
show_status: true,
|
show_status: true,
|
||||||
@ -42,8 +57,8 @@ impl ColumnVisibility {
|
|||||||
show_uptime: true,
|
show_uptime: true,
|
||||||
show_restarts: false,
|
show_restarts: false,
|
||||||
}
|
}
|
||||||
} else if width >= 45 {
|
} else if width >= with_ram {
|
||||||
// Hide uptime and restarts: Name (25) + Status (10) + RAM (8) = 43 chars
|
// Hide uptime and restarts
|
||||||
Self {
|
Self {
|
||||||
show_name: true,
|
show_name: true,
|
||||||
show_status: true,
|
show_status: true,
|
||||||
@ -52,7 +67,7 @@ impl ColumnVisibility {
|
|||||||
show_restarts: false,
|
show_restarts: false,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Minimal: Name (25) + Status (10) = 35 chars
|
// Minimal: Name + Status only
|
||||||
Self {
|
Self {
|
||||||
show_name: true,
|
show_name: true,
|
||||||
show_status: true,
|
show_status: true,
|
||||||
@ -76,14 +91,17 @@ pub struct ServicesWidget {
|
|||||||
/// Last update indicator
|
/// Last update indicator
|
||||||
has_data: bool,
|
has_data: bool,
|
||||||
/// Currently selected service index (for navigation cursor)
|
/// Currently selected service index (for navigation cursor)
|
||||||
selected_index: usize,
|
pub selected_index: usize,
|
||||||
|
/// Scroll offset for viewport (which display line is at the top)
|
||||||
|
pub scroll_offset: usize,
|
||||||
|
/// Last rendered viewport height (for accurate scroll bounds)
|
||||||
|
last_viewport_height: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
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>,
|
||||||
@ -97,6 +115,8 @@ impl ServicesWidget {
|
|||||||
status: Status::Unknown,
|
status: Status::Unknown,
|
||||||
has_data: false,
|
has_data: false,
|
||||||
selected_index: 0,
|
selected_index: 0,
|
||||||
|
scroll_offset: 0,
|
||||||
|
last_viewport_height: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,15 +150,18 @@ impl ServicesWidget {
|
|||||||
|
|
||||||
/// Format parent service line - returns text without icon for span formatting
|
/// Format parent service line - returns text without icon for span formatting
|
||||||
fn format_parent_service_line(&self, name: &str, info: &ServiceInfo, columns: ColumnVisibility) -> String {
|
fn format_parent_service_line(&self, name: &str, info: &ServiceInfo, columns: ColumnVisibility) -> String {
|
||||||
// Truncate long service names to fit layout (account for icon space)
|
// Truncate long service names to fit layout
|
||||||
let short_name = if name.len() > 22 {
|
// NAME_WIDTH - 3 chars for "..." = max displayable chars
|
||||||
format!("{}...", &name[..19])
|
let max_name_len = (ColumnVisibility::NAME_WIDTH - 3) as usize;
|
||||||
|
let short_name = if name.len() > max_name_len {
|
||||||
|
format!("{}...", &name[..max_name_len.saturating_sub(3)])
|
||||||
} else {
|
} else {
|
||||||
name.to_string()
|
name.to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Convert Status enum to display text
|
// Convert Status enum to display text
|
||||||
let status_str = match info.widget_status {
|
let status_str = match info.widget_status {
|
||||||
|
Status::Info => "", // Shouldn't happen for parent services
|
||||||
Status::Ok => "active",
|
Status::Ok => "active",
|
||||||
Status::Inactive => "inactive",
|
Status::Inactive => "inactive",
|
||||||
Status::Critical => "failed",
|
Status::Critical => "failed",
|
||||||
@ -185,19 +208,19 @@ impl ServicesWidget {
|
|||||||
// Build format string based on column visibility
|
// Build format string based on column visibility
|
||||||
let mut parts = Vec::new();
|
let mut parts = Vec::new();
|
||||||
if columns.show_name {
|
if columns.show_name {
|
||||||
parts.push(format!("{:<23}", short_name));
|
parts.push(format!("{:<width$}", short_name, width = ColumnVisibility::NAME_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_status {
|
if columns.show_status {
|
||||||
parts.push(format!("{:<10}", status_str));
|
parts.push(format!("{:<width$}", status_str, width = ColumnVisibility::STATUS_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_ram {
|
if columns.show_ram {
|
||||||
parts.push(format!("{:<8}", memory_str));
|
parts.push(format!("{:<width$}", memory_str, width = ColumnVisibility::RAM_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_uptime {
|
if columns.show_uptime {
|
||||||
parts.push(format!("{:<8}", uptime_str));
|
parts.push(format!("{:<width$}", uptime_str, width = ColumnVisibility::UPTIME_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_restarts {
|
if columns.show_restarts {
|
||||||
parts.push(format!("{:<5}", restart_str));
|
parts.push(format!("{:<width$}", restart_str, width = ColumnVisibility::RESTARTS_WIDTH as usize));
|
||||||
}
|
}
|
||||||
|
|
||||||
parts.join(" ")
|
parts.join(" ")
|
||||||
@ -212,9 +235,12 @@ impl ServicesWidget {
|
|||||||
info: &ServiceInfo,
|
info: &ServiceInfo,
|
||||||
is_last: bool,
|
is_last: bool,
|
||||||
) -> Vec<ratatui::text::Span<'static>> {
|
) -> Vec<ratatui::text::Span<'static>> {
|
||||||
|
// Informational sub-services (Status::Info) can use more width since they don't show columns
|
||||||
|
let max_width = if info.widget_status == Status::Info { 50 } else { 18 };
|
||||||
|
|
||||||
// Truncate long sub-service names to fit layout (accounting for indentation)
|
// Truncate long sub-service names to fit layout (accounting for indentation)
|
||||||
let short_name = if name.len() > 18 {
|
let short_name = if name.len() > max_width {
|
||||||
format!("{}...", &name[..15])
|
format!("{}...", &name[..(max_width.saturating_sub(3))])
|
||||||
} else {
|
} else {
|
||||||
name.to_string()
|
name.to_string()
|
||||||
};
|
};
|
||||||
@ -222,6 +248,7 @@ impl ServicesWidget {
|
|||||||
// Get status icon and text
|
// Get status icon and text
|
||||||
let icon = StatusIcons::get_icon(info.widget_status);
|
let icon = StatusIcons::get_icon(info.widget_status);
|
||||||
let status_color = match info.widget_status {
|
let status_color = match info.widget_status {
|
||||||
|
Status::Info => Theme::muted_text(),
|
||||||
Status::Ok => Theme::success(),
|
Status::Ok => Theme::success(),
|
||||||
Status::Inactive => Theme::muted_text(),
|
Status::Inactive => Theme::muted_text(),
|
||||||
Status::Pending => Theme::highlight(),
|
Status::Pending => Theme::highlight(),
|
||||||
@ -242,6 +269,7 @@ impl ServicesWidget {
|
|||||||
} else {
|
} else {
|
||||||
// Convert Status enum to display text for sub-services
|
// Convert Status enum to display text for sub-services
|
||||||
match info.widget_status {
|
match info.widget_status {
|
||||||
|
Status::Info => "",
|
||||||
Status::Ok => "active",
|
Status::Ok => "active",
|
||||||
Status::Inactive => "inactive",
|
Status::Inactive => "inactive",
|
||||||
Status::Critical => "failed",
|
Status::Critical => "failed",
|
||||||
@ -253,34 +281,34 @@ impl ServicesWidget {
|
|||||||
};
|
};
|
||||||
let tree_symbol = if is_last { "└─" } else { "├─" };
|
let tree_symbol = if is_last { "└─" } else { "├─" };
|
||||||
|
|
||||||
// Docker images use docker whale icon
|
if info.widget_status == Status::Info {
|
||||||
if info.service_type == "image" {
|
// Informational data - no status icon, show metrics if available
|
||||||
vec![
|
let mut spans = vec![
|
||||||
// Indentation and tree prefix
|
// Indentation and tree prefix
|
||||||
ratatui::text::Span::styled(
|
ratatui::text::Span::styled(
|
||||||
format!(" {} ", tree_symbol),
|
format!(" {} ", tree_symbol),
|
||||||
Typography::tree(),
|
Typography::tree(),
|
||||||
),
|
),
|
||||||
// Docker icon (simple character for performance)
|
// Service name (no icon) - no fixed width padding for Info status
|
||||||
ratatui::text::Span::styled(
|
ratatui::text::Span::styled(
|
||||||
"D ".to_string(),
|
short_name,
|
||||||
Style::default().fg(Theme::highlight()).bg(Theme::background()),
|
|
||||||
),
|
|
||||||
// Service name
|
|
||||||
ratatui::text::Span::styled(
|
|
||||||
format!("{:<18} ", short_name),
|
|
||||||
Style::default()
|
Style::default()
|
||||||
.fg(Theme::secondary_text())
|
.fg(Theme::secondary_text())
|
||||||
.bg(Theme::background()),
|
.bg(Theme::background()),
|
||||||
),
|
),
|
||||||
// Status/metrics text
|
];
|
||||||
ratatui::text::Span::styled(
|
|
||||||
|
// Add metrics if available (e.g., Docker image size)
|
||||||
|
if !status_str.is_empty() {
|
||||||
|
spans.push(ratatui::text::Span::styled(
|
||||||
status_str,
|
status_str,
|
||||||
Style::default()
|
Style::default()
|
||||||
.fg(Theme::secondary_text())
|
.fg(Theme::secondary_text())
|
||||||
.bg(Theme::background()),
|
.bg(Theme::background()),
|
||||||
),
|
));
|
||||||
]
|
}
|
||||||
|
|
||||||
|
spans
|
||||||
} else {
|
} else {
|
||||||
vec![
|
vec![
|
||||||
// Indentation and tree prefix
|
// Indentation and tree prefix
|
||||||
@ -315,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);
|
||||||
}
|
}
|
||||||
@ -323,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> {
|
||||||
@ -343,6 +439,81 @@ impl ServicesWidget {
|
|||||||
self.parent_services.len()
|
self.parent_services.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get total display lines (parent services + sub-services)
|
||||||
|
pub fn get_total_display_lines(&self) -> usize {
|
||||||
|
let mut total = self.parent_services.len();
|
||||||
|
for sub_list in self.sub_services.values() {
|
||||||
|
total += sub_list.len();
|
||||||
|
}
|
||||||
|
total
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scroll down by one line
|
||||||
|
pub fn scroll_down(&mut self, _visible_height: usize) {
|
||||||
|
let total_lines = self.get_total_display_lines();
|
||||||
|
|
||||||
|
// Use last_viewport_height if available (more accurate), otherwise can't scroll
|
||||||
|
let viewport_height = if self.last_viewport_height > 0 {
|
||||||
|
self.last_viewport_height
|
||||||
|
} else {
|
||||||
|
return; // Can't scroll without knowing viewport size
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calculate exact max scroll to match render logic
|
||||||
|
// Stop scrolling when all remaining content fits in viewport
|
||||||
|
// At scroll_offset N: remaining = total_lines - N
|
||||||
|
// We can show all when: remaining <= viewport_height
|
||||||
|
// So max_scroll is when: total_lines - max_scroll = viewport_height
|
||||||
|
// Therefore: max_scroll = total_lines - viewport_height (but at least 0)
|
||||||
|
let max_scroll = total_lines.saturating_sub(viewport_height);
|
||||||
|
|
||||||
|
debug!("Scroll down: total={}, viewport={}, offset={}, max={}", total_lines, viewport_height, self.scroll_offset, max_scroll);
|
||||||
|
|
||||||
|
if self.scroll_offset < max_scroll {
|
||||||
|
self.scroll_offset += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scroll up by one line
|
||||||
|
pub fn scroll_up(&mut self) {
|
||||||
|
if self.scroll_offset > 0 {
|
||||||
|
self.scroll_offset -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Map a display line index to a parent service index (returns None if clicked on sub-service)
|
||||||
|
pub fn display_line_to_parent_index(&self, display_line_index: usize) -> Option<usize> {
|
||||||
|
// Build the same display list to map line index to parent service index
|
||||||
|
let mut parent_index = 0;
|
||||||
|
let mut line_index = 0;
|
||||||
|
|
||||||
|
let mut parent_services: Vec<_> = self.parent_services.iter().collect();
|
||||||
|
parent_services.sort_by(|(a, _), (b, _)| a.cmp(b));
|
||||||
|
|
||||||
|
for (parent_name, _) in parent_services {
|
||||||
|
// Check if this line index matches a parent service
|
||||||
|
if line_index == display_line_index {
|
||||||
|
return Some(parent_index);
|
||||||
|
}
|
||||||
|
line_index += 1;
|
||||||
|
|
||||||
|
// Add sub-services for this parent (if any)
|
||||||
|
if let Some(sub_list) = self.sub_services.get(parent_name) {
|
||||||
|
for _ in sub_list {
|
||||||
|
if line_index == display_line_index {
|
||||||
|
// Clicked on a sub-service - return None (can't select sub-services)
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
line_index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parent_index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Calculate which parent service index corresponds to a display line index
|
/// Calculate which parent service index corresponds to a display line index
|
||||||
fn calculate_parent_service_index(&self, display_line_index: &usize) -> usize {
|
fn calculate_parent_service_index(&self, display_line_index: &usize) -> usize {
|
||||||
@ -384,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,
|
||||||
@ -403,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,
|
||||||
@ -448,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,
|
||||||
@ -477,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,
|
||||||
@ -519,12 +686,23 @@ impl ServicesWidget {
|
|||||||
self.selected_index = total_count - 1;
|
self.selected_index = total_count - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clamp scroll offset to valid range after update
|
||||||
|
// This prevents scroll issues when switching between hosts or when service count changes
|
||||||
|
let total_display_lines = self.get_total_display_lines();
|
||||||
|
if total_display_lines == 0 {
|
||||||
|
self.scroll_offset = 0;
|
||||||
|
} else if self.scroll_offset >= total_display_lines {
|
||||||
|
// Clamp to max valid value, not reset to 0
|
||||||
|
self.scroll_offset = total_display_lines.saturating_sub(1);
|
||||||
|
}
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"Services widget updated: {} parent services, {} sub-service groups, total={}, selected={}, status={:?}",
|
"Services widget updated: {} parent services, {} sub-service groups, total={}, selected={}, scroll={}, status={:?}",
|
||||||
self.parent_services.len(),
|
self.parent_services.len(),
|
||||||
self.sub_services.len(),
|
self.sub_services.len(),
|
||||||
total_count,
|
total_count,
|
||||||
self.selected_index,
|
self.selected_index,
|
||||||
|
self.scroll_offset,
|
||||||
self.status
|
self.status
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -550,19 +728,19 @@ impl ServicesWidget {
|
|||||||
// Build header based on visible columns
|
// Build header based on visible columns
|
||||||
let mut header_parts = Vec::new();
|
let mut header_parts = Vec::new();
|
||||||
if columns.show_name {
|
if columns.show_name {
|
||||||
header_parts.push(format!("{:<25}", "Service:"));
|
header_parts.push(format!("{:<width$}", "Service:", width = ColumnVisibility::NAME_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_status {
|
if columns.show_status {
|
||||||
header_parts.push(format!("{:<10}", "Status:"));
|
header_parts.push(format!("{:<width$}", "Status:", width = ColumnVisibility::STATUS_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_ram {
|
if columns.show_ram {
|
||||||
header_parts.push(format!("{:<8}", "RAM:"));
|
header_parts.push(format!("{:<width$}", "RAM:", width = ColumnVisibility::RAM_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_uptime {
|
if columns.show_uptime {
|
||||||
header_parts.push(format!("{:<8}", "Uptime:"));
|
header_parts.push(format!("{:<width$}", "Uptime:", width = ColumnVisibility::UPTIME_WIDTH as usize));
|
||||||
}
|
}
|
||||||
if columns.show_restarts {
|
if columns.show_restarts {
|
||||||
header_parts.push(format!("{:<5}", "↻:"));
|
header_parts.push(format!("{:<width$}", "↻:", width = ColumnVisibility::RESTARTS_WIDTH as usize));
|
||||||
}
|
}
|
||||||
let header = header_parts.join(" ");
|
let header = header_parts.join(" ");
|
||||||
|
|
||||||
@ -617,19 +795,45 @@ impl ServicesWidget {
|
|||||||
let available_lines = area.height as usize;
|
let available_lines = area.height as usize;
|
||||||
let total_lines = display_lines.len();
|
let total_lines = display_lines.len();
|
||||||
|
|
||||||
// Reserve one line for "X more below" if needed
|
// Store viewport height for accurate scroll calculations
|
||||||
let lines_for_content = if total_lines > available_lines {
|
self.last_viewport_height = available_lines;
|
||||||
|
|
||||||
|
// Clamp scroll_offset to valid range based on current viewport and content
|
||||||
|
// This handles dynamic viewport size changes
|
||||||
|
let max_valid_scroll = total_lines.saturating_sub(available_lines);
|
||||||
|
if self.scroll_offset > max_valid_scroll {
|
||||||
|
self.scroll_offset = max_valid_scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate how many lines remain after scroll offset
|
||||||
|
let remaining_lines = total_lines.saturating_sub(self.scroll_offset);
|
||||||
|
|
||||||
|
debug!("Render: total={}, viewport={}, offset={}, max={}, remaining={}",
|
||||||
|
total_lines, available_lines, self.scroll_offset, max_valid_scroll, remaining_lines);
|
||||||
|
|
||||||
|
// Check if all remaining content fits in viewport
|
||||||
|
let will_show_more_below = remaining_lines > available_lines;
|
||||||
|
|
||||||
|
// Reserve one line for "X more below" only if we can't fit everything
|
||||||
|
let lines_for_content = if will_show_more_below {
|
||||||
available_lines.saturating_sub(1)
|
available_lines.saturating_sub(1)
|
||||||
} else {
|
} else {
|
||||||
available_lines
|
available_lines.min(remaining_lines)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Apply scroll offset
|
||||||
let visible_lines: Vec<_> = display_lines
|
let visible_lines: Vec<_> = display_lines
|
||||||
.iter()
|
.iter()
|
||||||
|
.skip(self.scroll_offset)
|
||||||
.take(lines_for_content)
|
.take(lines_for_content)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let hidden_below = total_lines.saturating_sub(lines_for_content);
|
// Only calculate hidden_below if we actually reserved space for the message
|
||||||
|
let hidden_below = if will_show_more_below {
|
||||||
|
remaining_lines.saturating_sub(lines_for_content)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
let lines_to_show = visible_lines.len();
|
let lines_to_show = visible_lines.len();
|
||||||
|
|
||||||
@ -643,7 +847,7 @@ impl ServicesWidget {
|
|||||||
|
|
||||||
for (i, (line_text, line_status, is_sub, sub_info)) in visible_lines.iter().enumerate()
|
for (i, (line_text, line_status, is_sub, sub_info)) in visible_lines.iter().enumerate()
|
||||||
{
|
{
|
||||||
let actual_index = i; // Simple index since we're not scrolling
|
let actual_index = self.scroll_offset + i; // Account for scroll offset
|
||||||
|
|
||||||
// Only parent services can be selected - calculate parent service index
|
// Only parent services can be selected - calculate parent service index
|
||||||
let is_selected = if !*is_sub {
|
let is_selected = if !*is_sub {
|
||||||
@ -689,7 +893,7 @@ impl ServicesWidget {
|
|||||||
// Show "X more below" message if content was truncated
|
// Show "X more below" message if content was truncated
|
||||||
if hidden_below > 0 {
|
if hidden_below > 0 {
|
||||||
let more_text = format!("... {} more below", hidden_below);
|
let more_text = format!("... {} more below", hidden_below);
|
||||||
let more_para = Paragraph::new(more_text).style(Typography::muted());
|
let more_para = Paragraph::new(more_text).style(Style::default().fg(Theme::border()));
|
||||||
frame.render_widget(more_para, service_chunks[lines_to_show]);
|
frame.render_widget(more_para, service_chunks[lines_to_show]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,13 @@
|
|||||||
use cm_dashboard_shared::Status;
|
use cm_dashboard_shared::Status;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::Rect,
|
layout::Rect,
|
||||||
|
style::Style,
|
||||||
text::{Line, Span, Text},
|
text::{Line, Span, Text},
|
||||||
widgets::Paragraph,
|
widgets::Paragraph,
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::ui::theme::{StatusIcons, Typography};
|
use crate::ui::theme::{StatusIcons, Theme, Typography};
|
||||||
|
|
||||||
/// System widget displaying NixOS info, Network, CPU, RAM, and Storage in unified layout
|
/// System widget displaying NixOS info, Network, CPU, RAM, and Storage in unified layout
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -15,10 +16,6 @@ pub struct SystemWidget {
|
|||||||
nixos_build: Option<String>,
|
nixos_build: Option<String>,
|
||||||
agent_hash: Option<String>,
|
agent_hash: Option<String>,
|
||||||
|
|
||||||
// ZMQ communication stats
|
|
||||||
zmq_packets_received: Option<u64>,
|
|
||||||
zmq_last_packet_age: Option<f64>,
|
|
||||||
|
|
||||||
// Network interfaces
|
// Network interfaces
|
||||||
network_interfaces: Vec<cm_dashboard_shared::NetworkInterfaceData>,
|
network_interfaces: Vec<cm_dashboard_shared::NetworkInterfaceData>,
|
||||||
|
|
||||||
@ -27,6 +24,8 @@ pub struct SystemWidget {
|
|||||||
cpu_load_5min: Option<f32>,
|
cpu_load_5min: Option<f32>,
|
||||||
cpu_load_15min: Option<f32>,
|
cpu_load_15min: Option<f32>,
|
||||||
cpu_cstates: Vec<cm_dashboard_shared::CStateInfo>,
|
cpu_cstates: Vec<cm_dashboard_shared::CStateInfo>,
|
||||||
|
cpu_model_name: Option<String>,
|
||||||
|
cpu_core_count: Option<u32>,
|
||||||
cpu_status: Status,
|
cpu_status: Status,
|
||||||
|
|
||||||
// Memory metrics
|
// Memory metrics
|
||||||
@ -45,12 +44,17 @@ 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,
|
||||||
|
|
||||||
|
// Scroll offset for viewport
|
||||||
|
pub scroll_offset: usize,
|
||||||
|
/// Last rendered viewport height (for accurate scroll bounds)
|
||||||
|
last_viewport_height: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -90,13 +94,13 @@ impl SystemWidget {
|
|||||||
Self {
|
Self {
|
||||||
nixos_build: None,
|
nixos_build: None,
|
||||||
agent_hash: None,
|
agent_hash: None,
|
||||||
zmq_packets_received: None,
|
|
||||||
zmq_last_packet_age: None,
|
|
||||||
network_interfaces: Vec::new(),
|
network_interfaces: Vec::new(),
|
||||||
cpu_load_1min: None,
|
cpu_load_1min: None,
|
||||||
cpu_load_5min: None,
|
cpu_load_5min: None,
|
||||||
cpu_load_15min: None,
|
cpu_load_15min: None,
|
||||||
cpu_cstates: Vec::new(),
|
cpu_cstates: Vec::new(),
|
||||||
|
cpu_model_name: None,
|
||||||
|
cpu_core_count: None,
|
||||||
cpu_status: Status::Unknown,
|
cpu_status: Status::Unknown,
|
||||||
memory_usage_percent: None,
|
memory_usage_percent: None,
|
||||||
memory_used_gb: None,
|
memory_used_gb: None,
|
||||||
@ -108,10 +112,12 @@ 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,
|
||||||
|
last_viewport_height: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,10 +162,14 @@ impl SystemWidget {
|
|||||||
self.agent_hash.as_ref()
|
self.agent_hash.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update ZMQ communication statistics
|
/// Get the build version
|
||||||
pub fn update_zmq_stats(&mut self, packets_received: u64, last_packet_age_secs: f64) {
|
pub fn get_build_version(&self) -> Option<String> {
|
||||||
self.zmq_packets_received = Some(packets_received);
|
self.nixos_build.clone()
|
||||||
self.zmq_last_packet_age = Some(last_packet_age_secs);
|
}
|
||||||
|
|
||||||
|
/// Get the agent version
|
||||||
|
pub fn get_agent_version(&self) -> Option<String> {
|
||||||
|
self.agent_hash.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,6 +194,8 @@ impl Widget for SystemWidget {
|
|||||||
self.cpu_load_5min = Some(cpu.load_5min);
|
self.cpu_load_5min = Some(cpu.load_5min);
|
||||||
self.cpu_load_15min = Some(cpu.load_15min);
|
self.cpu_load_15min = Some(cpu.load_15min);
|
||||||
self.cpu_cstates = cpu.cstates.clone();
|
self.cpu_cstates = cpu.cstates.clone();
|
||||||
|
self.cpu_model_name = cpu.model_name.clone();
|
||||||
|
self.cpu_core_count = cpu.core_count;
|
||||||
self.cpu_status = Status::Ok;
|
self.cpu_status = Status::Ok;
|
||||||
|
|
||||||
// Extract memory data directly
|
// Extract memory data directly
|
||||||
@ -209,9 +221,19 @@ 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
|
||||||
|
// This prevents scroll issues when switching between hosts
|
||||||
|
let total_lines = self.get_total_lines();
|
||||||
|
if total_lines == 0 {
|
||||||
|
self.scroll_offset = 0;
|
||||||
|
} else if self.scroll_offset >= total_lines {
|
||||||
|
// Clamp to max valid value, not reset to 0
|
||||||
|
self.scroll_offset = total_lines.saturating_sub(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -511,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
|
||||||
@ -787,35 +772,87 @@ impl SystemWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Render system widget
|
/// Render system widget
|
||||||
pub fn render(&mut self, frame: &mut Frame, area: Rect, hostname: &str, _config: Option<&crate::config::DashboardConfig>) {
|
/// Scroll down by one line
|
||||||
let mut lines = Vec::new();
|
pub fn scroll_down(&mut self, _visible_height: usize, _total_lines: usize) {
|
||||||
|
let total_lines = self.get_total_lines();
|
||||||
|
|
||||||
// NixOS section
|
// Use last_viewport_height if available (more accurate), otherwise can't scroll
|
||||||
lines.push(Line::from(vec![
|
let viewport_height = if self.last_viewport_height > 0 {
|
||||||
Span::styled(format!("NixOS {}:", hostname), Typography::widget_title())
|
self.last_viewport_height
|
||||||
]));
|
|
||||||
|
|
||||||
let build_text = self.nixos_build.as_deref().unwrap_or("unknown");
|
|
||||||
lines.push(Line::from(vec![
|
|
||||||
Span::styled(format!("Build: {}", build_text), Typography::secondary())
|
|
||||||
]));
|
|
||||||
|
|
||||||
let agent_version_text = self.agent_hash.as_deref().unwrap_or("unknown");
|
|
||||||
lines.push(Line::from(vec![
|
|
||||||
Span::styled(format!("Agent: {}", agent_version_text), Typography::secondary())
|
|
||||||
]));
|
|
||||||
|
|
||||||
// ZMQ communication stats
|
|
||||||
if let (Some(packets), Some(age)) = (self.zmq_packets_received, self.zmq_last_packet_age) {
|
|
||||||
let age_text = if age < 1.0 {
|
|
||||||
format!("{:.0}ms ago", age * 1000.0)
|
|
||||||
} else {
|
} else {
|
||||||
format!("{:.1}s ago", age)
|
return; // Can't scroll without knowing viewport size
|
||||||
};
|
};
|
||||||
lines.push(Line::from(vec![
|
|
||||||
Span::styled(format!("ZMQ: {} pkts, last {}", packets, age_text), Typography::secondary())
|
// Max scroll should allow us to see all remaining content
|
||||||
]));
|
// When scroll_offset + viewport_height >= total_lines, we can see everything
|
||||||
|
let max_scroll = if total_lines > viewport_height {
|
||||||
|
total_lines - viewport_height
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.scroll_offset < max_scroll {
|
||||||
|
self.scroll_offset += 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scroll up by one line
|
||||||
|
pub fn scroll_up(&mut self) {
|
||||||
|
if self.scroll_offset > 0 {
|
||||||
|
self.scroll_offset -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get total line count (needs to be calculated before rendering)
|
||||||
|
pub fn get_total_lines(&self) -> usize {
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
// CPU section (2+ lines for load/cstate, +1 if has model/cores)
|
||||||
|
count += 2;
|
||||||
|
if self.cpu_model_name.is_some() || self.cpu_core_count.is_some() {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// RAM section (1 + tmpfs mounts)
|
||||||
|
count += 2;
|
||||||
|
count += self.tmpfs_mounts.len();
|
||||||
|
|
||||||
|
// Network section
|
||||||
|
if !self.network_interfaces.is_empty() {
|
||||||
|
count += 1; // Header
|
||||||
|
// Count network lines (would need to mirror render_network logic)
|
||||||
|
for iface in &self.network_interfaces {
|
||||||
|
count += 1; // Interface name
|
||||||
|
count += iface.ipv4_addresses.len();
|
||||||
|
count += iface.ipv6_addresses.len();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Storage section
|
||||||
|
count += 1; // Header
|
||||||
|
for pool in &self.storage_pools {
|
||||||
|
count += 1; // Pool header
|
||||||
|
count += pool.drives.len();
|
||||||
|
count += pool.data_drives.len();
|
||||||
|
count += pool.parity_drives.len();
|
||||||
|
count += pool.filesystems.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backup section
|
||||||
|
if !self.backup_repositories.is_empty() {
|
||||||
|
count += 1; // Header: "Backup:"
|
||||||
|
count += 1; // Repo count and timestamp header
|
||||||
|
count += self.backup_repositories.len(); // Individual repos
|
||||||
|
}
|
||||||
|
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&mut self, frame: &mut Frame, area: Rect, _hostname: &str, _config: Option<&crate::config::DashboardConfig>) {
|
||||||
|
// Store viewport height for accurate scroll calculations
|
||||||
|
self.last_viewport_height = area.height as usize;
|
||||||
|
|
||||||
|
let mut lines = Vec::new();
|
||||||
|
|
||||||
// CPU section
|
// CPU section
|
||||||
lines.push(Line::from(vec![
|
lines.push(Line::from(vec![
|
||||||
@ -830,11 +867,31 @@ impl SystemWidget {
|
|||||||
lines.push(Line::from(cpu_spans));
|
lines.push(Line::from(cpu_spans));
|
||||||
|
|
||||||
let cstate_text = self.format_cpu_cstate();
|
let cstate_text = self.format_cpu_cstate();
|
||||||
|
let has_cpu_info = self.cpu_model_name.is_some() || self.cpu_core_count.is_some();
|
||||||
|
let cstate_tree = if has_cpu_info { " ├─ " } else { " └─ " };
|
||||||
lines.push(Line::from(vec![
|
lines.push(Line::from(vec![
|
||||||
Span::styled(" └─ ", Typography::tree()),
|
Span::styled(cstate_tree, Typography::tree()),
|
||||||
Span::styled(format!("C-state: {}", cstate_text), Typography::secondary())
|
Span::styled(format!("C-state: {}", cstate_text), Typography::secondary())
|
||||||
]));
|
]));
|
||||||
|
|
||||||
|
// CPU model and core count (if available)
|
||||||
|
if let (Some(model), Some(cores)) = (&self.cpu_model_name, self.cpu_core_count) {
|
||||||
|
lines.push(Line::from(vec![
|
||||||
|
Span::styled(" └─ ", Typography::tree()),
|
||||||
|
Span::styled(format!("{} ({} cores)", model, cores), Typography::secondary())
|
||||||
|
]));
|
||||||
|
} else if let Some(model) = &self.cpu_model_name {
|
||||||
|
lines.push(Line::from(vec![
|
||||||
|
Span::styled(" └─ ", Typography::tree()),
|
||||||
|
Span::styled(model.clone(), Typography::secondary())
|
||||||
|
]));
|
||||||
|
} else if let Some(cores) = self.cpu_core_count {
|
||||||
|
lines.push(Line::from(vec![
|
||||||
|
Span::styled(" └─ ", Typography::tree()),
|
||||||
|
Span::styled(format!("{} cores", cores), Typography::secondary())
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
// RAM section
|
// RAM section
|
||||||
lines.push(Line::from(vec![
|
lines.push(Line::from(vec![
|
||||||
Span::styled("RAM:", Typography::widget_title())
|
Span::styled("RAM:", Typography::widget_title())
|
||||||
@ -891,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())
|
||||||
]));
|
]));
|
||||||
@ -904,28 +961,50 @@ impl SystemWidget {
|
|||||||
let total_lines = lines.len();
|
let total_lines = lines.len();
|
||||||
let available_height = area.height as usize;
|
let available_height = area.height as usize;
|
||||||
|
|
||||||
// Show only what fits, with "X more below" if needed
|
// Clamp scroll_offset to valid range based on current viewport and content
|
||||||
if total_lines > available_height {
|
// This handles dynamic viewport size changes
|
||||||
let lines_for_content = available_height.saturating_sub(1); // Reserve one line for "more below"
|
let max_valid_scroll = total_lines.saturating_sub(available_height);
|
||||||
|
let clamped_scroll = self.scroll_offset.min(max_valid_scroll);
|
||||||
|
|
||||||
|
// Calculate how many lines remain after scroll offset
|
||||||
|
let remaining_lines = total_lines.saturating_sub(clamped_scroll);
|
||||||
|
|
||||||
|
// Check if all remaining content fits in viewport
|
||||||
|
let will_show_more_below = remaining_lines > available_height;
|
||||||
|
|
||||||
|
// Reserve one line for "X more below" only if we can't fit everything
|
||||||
|
let lines_for_content = if will_show_more_below {
|
||||||
|
available_height.saturating_sub(1)
|
||||||
|
} else {
|
||||||
|
available_height.min(remaining_lines)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Apply clamped scroll offset and take only what fits
|
||||||
let mut visible_lines: Vec<Line> = lines
|
let mut visible_lines: Vec<Line> = lines
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.skip(clamped_scroll)
|
||||||
.take(lines_for_content)
|
.take(lines_for_content)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let hidden_below = total_lines.saturating_sub(lines_for_content);
|
// Note: we don't update self.scroll_offset here due to borrow checker constraints
|
||||||
|
// It will be clamped on next render if still out of bounds
|
||||||
|
|
||||||
|
// Only calculate hidden_below if we actually reserved space for the message
|
||||||
|
let hidden_below = if will_show_more_below {
|
||||||
|
remaining_lines.saturating_sub(lines_for_content)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add "more below" message if needed
|
||||||
if hidden_below > 0 {
|
if hidden_below > 0 {
|
||||||
let more_line = Line::from(vec![
|
let more_line = Line::from(vec![
|
||||||
Span::styled(format!("... {} more below", hidden_below), Typography::muted())
|
Span::styled(format!("... {} more below", hidden_below), Style::default().fg(Theme::border()))
|
||||||
]);
|
]);
|
||||||
visible_lines.push(more_line);
|
visible_lines.push(more_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
let paragraph = Paragraph::new(Text::from(visible_lines));
|
let paragraph = Paragraph::new(Text::from(visible_lines));
|
||||||
frame.render_widget(paragraph, area);
|
frame.render_widget(paragraph, area);
|
||||||
} else {
|
|
||||||
// All content fits and no scroll offset, render normally
|
|
||||||
let paragraph = Paragraph::new(Text::from(lines));
|
|
||||||
frame.render_widget(paragraph, area);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.227"
|
version = "0.1.275"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -38,6 +38,7 @@ pub struct NetworkInterfaceData {
|
|||||||
pub link_status: Status,
|
pub link_status: Status,
|
||||||
pub parent_interface: Option<String>,
|
pub parent_interface: Option<String>,
|
||||||
pub vlan_id: Option<u16>,
|
pub vlan_id: Option<u16>,
|
||||||
|
pub connection_method: Option<String>, // For Tailscale: "direct", "relay", or "proxy"
|
||||||
}
|
}
|
||||||
|
|
||||||
/// CPU C-state usage information
|
/// CPU C-state usage information
|
||||||
@ -57,6 +58,11 @@ pub struct CpuData {
|
|||||||
pub temperature_celsius: Option<f32>,
|
pub temperature_celsius: Option<f32>,
|
||||||
pub load_status: Status,
|
pub load_status: Status,
|
||||||
pub temperature_status: Status,
|
pub temperature_status: Status,
|
||||||
|
// Static CPU information (collected once at startup)
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub model_name: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub core_count: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Memory monitoring data
|
/// Memory monitoring data
|
||||||
@ -176,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 {
|
||||||
@ -219,6 +216,8 @@ impl AgentData {
|
|||||||
temperature_celsius: None,
|
temperature_celsius: None,
|
||||||
load_status: Status::Unknown,
|
load_status: Status::Unknown,
|
||||||
temperature_status: Status::Unknown,
|
temperature_status: Status::Unknown,
|
||||||
|
model_name: None,
|
||||||
|
core_count: None,
|
||||||
},
|
},
|
||||||
memory: MemoryData {
|
memory: MemoryData {
|
||||||
usage_percent: 0.0,
|
usage_percent: 0.0,
|
||||||
@ -237,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(),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -82,11 +82,12 @@ impl MetricValue {
|
|||||||
/// Health status for metrics
|
/// Health status for metrics
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum Status {
|
pub enum Status {
|
||||||
Inactive, // Lowest priority
|
Info, // Lowest priority - informational data with no status (no icon)
|
||||||
|
Inactive, //
|
||||||
Unknown, //
|
Unknown, //
|
||||||
Offline, //
|
Offline, //
|
||||||
Pending, //
|
Pending, //
|
||||||
Ok, // 5th place - good status has higher priority than unknown states
|
Ok, // Good status has higher priority than unknown states
|
||||||
Warning, //
|
Warning, //
|
||||||
Critical, // Highest priority
|
Critical, // Highest priority
|
||||||
}
|
}
|
||||||
@ -223,6 +224,17 @@ impl HysteresisThresholds {
|
|||||||
Status::Ok
|
Status::Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Status::Info => {
|
||||||
|
// Informational data shouldn't be used with hysteresis calculations
|
||||||
|
// Treat like Unknown if it somehow ends up here
|
||||||
|
if value >= self.critical_high {
|
||||||
|
Status::Critical
|
||||||
|
} else if value >= self.warning_high {
|
||||||
|
Status::Warning
|
||||||
|
} else {
|
||||||
|
Status::Ok
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user