Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 61dd686fb9 | |||
| c0f7a97a6f | |||
| 9575077045 | |||
| 34a1f7b9dc | |||
| d11aa11f99 | |||
| 0ca06d2507 | |||
| 6693f3a05f | |||
| de252d27b9 | |||
| db0e41a7d3 | |||
| ec460496d8 | |||
| 33e700529e | |||
| d644b7d40a | |||
| f635ba9c75 | |||
| 76b6e3373e | |||
| 0a13cab897 | |||
| d33ec5d225 | |||
| d31c2384df | |||
| c8db463204 | |||
| e8e50ef9bb | |||
| 0faed9309e | |||
| c980346d05 |
@@ -113,13 +113,13 @@ jobs:
|
|||||||
NIX_HASH="sha256-$(python3 -c "import base64, binascii; print(base64.b64encode(binascii.unhexlify('$NEW_HASH')).decode())")"
|
NIX_HASH="sha256-$(python3 -c "import base64, binascii; print(base64.b64encode(binascii.unhexlify('$NEW_HASH')).decode())")"
|
||||||
|
|
||||||
# Update the NixOS configuration
|
# Update the NixOS configuration
|
||||||
sed -i "s|version = \"v[^\"]*\"|version = \"$VERSION\"|" hosts/common/cm-dashboard.nix
|
sed -i "s|version = \"v[^\"]*\"|version = \"$VERSION\"|" hosts/services/cm-dashboard.nix
|
||||||
sed -i "s|sha256 = \"sha256-[^\"]*\"|sha256 = \"$NIX_HASH\"|" hosts/common/cm-dashboard.nix
|
sed -i "s|sha256 = \"sha256-[^\"]*\"|sha256 = \"$NIX_HASH\"|" hosts/services/cm-dashboard.nix
|
||||||
|
|
||||||
# Commit and push changes
|
# Commit and push changes
|
||||||
git config user.name "Gitea Actions"
|
git config user.name "Gitea Actions"
|
||||||
git config user.email "actions@gitea.cmtec.se"
|
git config user.email "actions@gitea.cmtec.se"
|
||||||
git add hosts/common/cm-dashboard.nix
|
git add hosts/services/cm-dashboard.nix
|
||||||
git commit -m "Auto-update cm-dashboard to $VERSION
|
git commit -m "Auto-update cm-dashboard to $VERSION
|
||||||
|
|
||||||
- Update version to $VERSION with automated release
|
- Update version to $VERSION with automated release
|
||||||
|
|||||||
@@ -49,8 +49,12 @@ hostname2 = [
|
|||||||
### Navigation
|
### Navigation
|
||||||
- **Tab**: Switch between hosts
|
- **Tab**: Switch between hosts
|
||||||
- **↑↓ or j/k**: Select services
|
- **↑↓ or j/k**: Select services
|
||||||
|
- **s**: Start selected service (UserStart)
|
||||||
|
- **S**: Stop selected service (UserStop)
|
||||||
- **J**: Show service logs (journalctl)
|
- **J**: Show service logs (journalctl)
|
||||||
- **L**: Show custom log files
|
- **L**: Show custom log files
|
||||||
|
- **R**: Rebuild current host
|
||||||
|
- **B**: Run backup on current host
|
||||||
- **q**: Quit dashboard
|
- **q**: Quit dashboard
|
||||||
|
|
||||||
## Core Architecture Principles
|
## Core Architecture Principles
|
||||||
@@ -115,7 +119,7 @@ This automatically:
|
|||||||
- Uploads binaries via Gitea API
|
- Uploads binaries via Gitea API
|
||||||
|
|
||||||
### NixOS Configuration Updates
|
### NixOS Configuration Updates
|
||||||
Edit `~/projects/nixosbox/hosts/common/cm-dashboard.nix`:
|
Edit `~/projects/nixosbox/hosts/services/cm-dashboard.nix`:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
version = "v0.1.X";
|
version = "v0.1.X";
|
||||||
|
|||||||
6
Cargo.lock
generated
6
Cargo.lock
generated
@@ -270,7 +270,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.60"
|
version = "0.1.80"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
@@ -292,7 +292,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.60"
|
version = "0.1.80"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@@ -315,7 +315,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.60"
|
version = "0.1.80"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -88,7 +88,9 @@ cm-dashboard • ● cmbox ● srv01 ● srv02 ● steambox
|
|||||||
- **s**: Start selected service (UserStart)
|
- **s**: Start selected service (UserStart)
|
||||||
- **S**: Stop selected service (UserStop)
|
- **S**: Stop selected service (UserStop)
|
||||||
- **J**: Show service logs (journalctl in tmux popup)
|
- **J**: Show service logs (journalctl in tmux popup)
|
||||||
|
- **L**: Show custom log files (tail -f custom paths in tmux popup)
|
||||||
- **R**: Rebuild current host
|
- **R**: Rebuild current host
|
||||||
|
- **B**: Run backup on current host
|
||||||
- **q**: Quit
|
- **q**: Quit
|
||||||
|
|
||||||
### Status Indicators
|
### Status Indicators
|
||||||
@@ -173,9 +175,10 @@ subscriber_ports = [6130]
|
|||||||
[hosts]
|
[hosts]
|
||||||
predefined_hosts = ["cmbox", "srv01", "srv02"]
|
predefined_hosts = ["cmbox", "srv01", "srv02"]
|
||||||
|
|
||||||
[ui]
|
[ssh]
|
||||||
ssh_user = "cm"
|
rebuild_user = "cm"
|
||||||
rebuild_alias = "nixos-rebuild-cmtec"
|
rebuild_alias = "nixos-rebuild-cmtec"
|
||||||
|
backup_alias = "cm-backup-run"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Technical Implementation
|
## Technical Implementation
|
||||||
@@ -329,7 +332,7 @@ This triggers automated:
|
|||||||
- Tarball upload to Gitea
|
- Tarball upload to Gitea
|
||||||
|
|
||||||
### NixOS Integration
|
### NixOS Integration
|
||||||
Update `~/projects/nixosbox/hosts/common/cm-dashboard.nix`:
|
Update `~/projects/nixosbox/hosts/services/cm-dashboard.nix`:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
version = "v0.1.43";
|
version = "v0.1.43";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.61"
|
version = "0.1.81"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use std::time::Duration;
|
|||||||
use tokio::time::interval;
|
use tokio::time::interval;
|
||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
use crate::communication::{AgentCommand, ServiceAction, ZmqHandler};
|
use crate::communication::{AgentCommand, ZmqHandler};
|
||||||
use crate::config::AgentConfig;
|
use crate::config::AgentConfig;
|
||||||
use crate::metrics::MetricCollectionManager;
|
use crate::metrics::MetricCollectionManager;
|
||||||
use crate::notifications::NotificationManager;
|
use crate::notifications::NotificationManager;
|
||||||
@@ -78,10 +78,11 @@ impl Agent {
|
|||||||
info!("Initial metric collection completed - all data cached and ready");
|
info!("Initial metric collection completed - all data cached and ready");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Separate intervals for collection, transmission, and email notifications
|
// Separate intervals for collection, transmission, heartbeat, and email notifications
|
||||||
let mut collection_interval =
|
let mut collection_interval =
|
||||||
interval(Duration::from_secs(self.config.collection_interval_seconds));
|
interval(Duration::from_secs(self.config.collection_interval_seconds));
|
||||||
let mut transmission_interval = interval(Duration::from_secs(self.config.zmq.transmission_interval_seconds));
|
let mut transmission_interval = interval(Duration::from_secs(self.config.zmq.transmission_interval_seconds));
|
||||||
|
let mut heartbeat_interval = interval(Duration::from_secs(self.config.zmq.heartbeat_interval_seconds));
|
||||||
let mut notification_interval = interval(Duration::from_secs(self.config.notifications.aggregation_interval_seconds));
|
let mut notification_interval = interval(Duration::from_secs(self.config.notifications.aggregation_interval_seconds));
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -98,6 +99,12 @@ impl Agent {
|
|||||||
error!("Failed to broadcast metrics: {}", e);
|
error!("Failed to broadcast metrics: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ = heartbeat_interval.tick() => {
|
||||||
|
// Send standalone heartbeat for host connectivity detection
|
||||||
|
if let Err(e) = self.send_heartbeat().await {
|
||||||
|
error!("Failed to send heartbeat: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
_ = notification_interval.tick() => {
|
_ = notification_interval.tick() => {
|
||||||
// Process batched email notifications (separate from dashboard updates)
|
// Process batched email notifications (separate from dashboard updates)
|
||||||
if let Err(e) = self.host_status_manager.process_pending_notifications(&mut self.notification_manager).await {
|
if let Err(e) = self.host_status_manager.process_pending_notifications(&mut self.notification_manager).await {
|
||||||
@@ -206,7 +213,7 @@ impl Agent {
|
|||||||
let mut status_changed = false;
|
let mut status_changed = false;
|
||||||
for metric in metrics {
|
for metric in metrics {
|
||||||
// Filter excluded metrics from email notification processing only
|
// Filter excluded metrics from email notification processing only
|
||||||
if self.config.exclude_email_metrics.contains(&metric.name) {
|
if self.config.notifications.exclude_email_metrics.contains(&metric.name) {
|
||||||
debug!("Excluding metric '{}' from email notification processing", metric.name);
|
debug!("Excluding metric '{}' from email notification processing", metric.name);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -252,6 +259,19 @@ impl Agent {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send standalone heartbeat for connectivity detection
|
||||||
|
async fn send_heartbeat(&mut self) -> Result<()> {
|
||||||
|
let heartbeat_metric = self.get_heartbeat_metric();
|
||||||
|
let message = MetricMessage::new(
|
||||||
|
self.hostname.clone(),
|
||||||
|
vec![heartbeat_metric],
|
||||||
|
);
|
||||||
|
|
||||||
|
self.zmq_handler.publish_metrics(&message).await?;
|
||||||
|
debug!("Sent standalone heartbeat for connectivity detection");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn handle_commands(&mut self) -> Result<()> {
|
async fn handle_commands(&mut self) -> Result<()> {
|
||||||
// Try to receive commands (non-blocking)
|
// Try to receive commands (non-blocking)
|
||||||
match self.zmq_handler.try_receive_command() {
|
match self.zmq_handler.try_receive_command() {
|
||||||
@@ -295,75 +315,10 @@ impl Agent {
|
|||||||
info!("Processing Ping command - agent is alive");
|
info!("Processing Ping command - agent is alive");
|
||||||
// Could send a response back via ZMQ if needed
|
// Could send a response back via ZMQ if needed
|
||||||
}
|
}
|
||||||
AgentCommand::ServiceControl { service_name, action } => {
|
|
||||||
info!("Processing ServiceControl command: {} {:?}", service_name, action);
|
|
||||||
if let Err(e) = self.handle_service_control(&service_name, &action).await {
|
|
||||||
error!("Failed to execute service control: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle systemd service control commands
|
|
||||||
async fn handle_service_control(&mut self, service_name: &str, action: &ServiceAction) -> Result<()> {
|
|
||||||
let (action_str, is_user_action) = match action {
|
|
||||||
ServiceAction::Start => ("start", false),
|
|
||||||
ServiceAction::Stop => ("stop", false),
|
|
||||||
ServiceAction::Status => ("status", false),
|
|
||||||
ServiceAction::UserStart => ("start", true),
|
|
||||||
ServiceAction::UserStop => ("stop", true),
|
|
||||||
};
|
|
||||||
|
|
||||||
info!("Executing systemctl {} {} (user action: {})", action_str, service_name, is_user_action);
|
|
||||||
|
|
||||||
// Handle user-stopped service tracking before systemctl execution (stop only)
|
|
||||||
match action {
|
|
||||||
ServiceAction::UserStop => {
|
|
||||||
info!("Marking service '{}' as user-stopped", service_name);
|
|
||||||
if let Err(e) = self.service_tracker.mark_user_stopped(service_name) {
|
|
||||||
error!("Failed to mark service as user-stopped: {}", e);
|
|
||||||
} else {
|
|
||||||
// Sync to global tracker
|
|
||||||
UserStoppedServiceTracker::update_global(&self.service_tracker);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let output = tokio::process::Command::new("sudo")
|
|
||||||
.arg("systemctl")
|
|
||||||
.arg(action_str)
|
|
||||||
.arg(format!("{}.service", service_name))
|
|
||||||
.output()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if output.status.success() {
|
|
||||||
info!("Service {} {} completed successfully", service_name, action_str);
|
|
||||||
if !output.stdout.is_empty() {
|
|
||||||
debug!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: User-stopped flag will be cleared by systemd collector
|
|
||||||
// when service actually reaches 'active' state, not here
|
|
||||||
} else {
|
|
||||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
||||||
error!("Service {} {} failed: {}", service_name, action_str, stderr);
|
|
||||||
return Err(anyhow::anyhow!("systemctl {} {} failed: {}", action_str, service_name, stderr));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Force refresh metrics after service control to update service status
|
|
||||||
if matches!(action, ServiceAction::Start | ServiceAction::Stop | ServiceAction::UserStart | ServiceAction::UserStop) {
|
|
||||||
info!("Triggering immediate metric refresh after service control");
|
|
||||||
if let Err(e) = self.collect_metrics_only().await {
|
|
||||||
error!("Failed to refresh metrics after service control: {}", e);
|
|
||||||
} else {
|
|
||||||
info!("Service status refreshed immediately after {} {}", action_str, service_name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check metrics for user-stopped services that are now active and clear their flags
|
/// Check metrics for user-stopped services that are now active and clear their flags
|
||||||
fn clear_user_stopped_flags_for_active_services(&mut self, metrics: &[Metric]) {
|
fn clear_user_stopped_flags_for_active_services(&mut self, metrics: &[Metric]) {
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ impl Collector for BackupCollector {
|
|||||||
name: "backup_overall_status".to_string(),
|
name: "backup_overall_status".to_string(),
|
||||||
value: MetricValue::String(match overall_status {
|
value: MetricValue::String(match overall_status {
|
||||||
Status::Ok => "ok".to_string(),
|
Status::Ok => "ok".to_string(),
|
||||||
|
Status::Inactive => "inactive".to_string(),
|
||||||
Status::Pending => "pending".to_string(),
|
Status::Pending => "pending".to_string(),
|
||||||
Status::Warning => "warning".to_string(),
|
Status::Warning => "warning".to_string(),
|
||||||
Status::Critical => "critical".to_string(),
|
Status::Critical => "critical".to_string(),
|
||||||
@@ -199,6 +200,7 @@ impl Collector for BackupCollector {
|
|||||||
name: format!("backup_service_{}_status", service_name),
|
name: format!("backup_service_{}_status", service_name),
|
||||||
value: MetricValue::String(match service_status {
|
value: MetricValue::String(match service_status {
|
||||||
Status::Ok => "ok".to_string(),
|
Status::Ok => "ok".to_string(),
|
||||||
|
Status::Inactive => "inactive".to_string(),
|
||||||
Status::Pending => "pending".to_string(),
|
Status::Pending => "pending".to_string(),
|
||||||
Status::Warning => "warning".to_string(),
|
Status::Warning => "warning".to_string(),
|
||||||
Status::Critical => "critical".to_string(),
|
Status::Critical => "critical".to_string(),
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use tracing::debug;
|
|||||||
|
|
||||||
use super::{Collector, CollectorError};
|
use super::{Collector, CollectorError};
|
||||||
use crate::config::SystemdConfig;
|
use crate::config::SystemdConfig;
|
||||||
use crate::service_tracker::UserStoppedServiceTracker;
|
|
||||||
|
|
||||||
/// Systemd collector for monitoring systemd services
|
/// Systemd collector for monitoring systemd services
|
||||||
pub struct SystemdCollector {
|
pub struct SystemdCollector {
|
||||||
@@ -357,33 +356,15 @@ impl SystemdCollector {
|
|||||||
/// Calculate service status, taking user-stopped services into account
|
/// Calculate service status, taking user-stopped services into account
|
||||||
fn calculate_service_status(&self, service_name: &str, active_status: &str) -> Status {
|
fn calculate_service_status(&self, service_name: &str, active_status: &str) -> Status {
|
||||||
match active_status.to_lowercase().as_str() {
|
match active_status.to_lowercase().as_str() {
|
||||||
"active" => {
|
"active" => Status::Ok,
|
||||||
// If service is now active and was marked as user-stopped, clear the flag
|
|
||||||
if UserStoppedServiceTracker::is_service_user_stopped(service_name) {
|
|
||||||
debug!("Service '{}' is now active - clearing user-stopped flag", service_name);
|
|
||||||
// Note: We can't directly clear here because this is a read-only context
|
|
||||||
// The agent will need to handle this differently
|
|
||||||
}
|
|
||||||
Status::Ok
|
|
||||||
},
|
|
||||||
"inactive" | "dead" => {
|
"inactive" | "dead" => {
|
||||||
// Check if this service was stopped by user action
|
debug!("Service '{}' is inactive - treating as Inactive status", service_name);
|
||||||
if UserStoppedServiceTracker::is_service_user_stopped(service_name) {
|
Status::Inactive
|
||||||
debug!("Service '{}' is inactive but marked as user-stopped - treating as OK", service_name);
|
|
||||||
Status::Ok
|
|
||||||
} else {
|
|
||||||
Status::Warning
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"failed" | "error" => Status::Critical,
|
"failed" | "error" => Status::Critical,
|
||||||
"activating" | "deactivating" | "reloading" | "start" | "stop" | "restart" => {
|
"activating" | "deactivating" | "reloading" | "start" | "stop" | "restart" => {
|
||||||
// For user-stopped services that are transitioning, keep them as OK during transition
|
debug!("Service '{}' is transitioning - treating as Pending", service_name);
|
||||||
if UserStoppedServiceTracker::is_service_user_stopped(service_name) {
|
Status::Pending
|
||||||
debug!("Service '{}' is transitioning but was user-stopped - treating as OK", service_name);
|
|
||||||
Status::Ok
|
|
||||||
} else {
|
|
||||||
Status::Pending
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => Status::Unknown,
|
_ => Status::Unknown,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,19 +98,4 @@ pub enum AgentCommand {
|
|||||||
ToggleCollector { name: String, enabled: bool },
|
ToggleCollector { name: String, enabled: bool },
|
||||||
/// Request status/health check
|
/// Request status/health check
|
||||||
Ping,
|
Ping,
|
||||||
/// Control systemd service
|
|
||||||
ServiceControl {
|
|
||||||
service_name: String,
|
|
||||||
action: ServiceAction,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Service control actions
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub enum ServiceAction {
|
|
||||||
Start,
|
|
||||||
Stop,
|
|
||||||
Status,
|
|
||||||
UserStart, // User-initiated start (clears user-stopped flag)
|
|
||||||
UserStop, // User-initiated stop (marks as user-stopped)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ pub struct AgentConfig {
|
|||||||
pub notifications: NotificationConfig,
|
pub notifications: NotificationConfig,
|
||||||
pub status_aggregation: HostStatusConfig,
|
pub status_aggregation: HostStatusConfig,
|
||||||
pub collection_interval_seconds: u64,
|
pub collection_interval_seconds: u64,
|
||||||
/// List of metric names to exclude from email notifications
|
|
||||||
#[serde(default)]
|
|
||||||
pub exclude_email_metrics: Vec<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ZMQ communication configuration
|
/// ZMQ communication configuration
|
||||||
@@ -29,6 +26,9 @@ pub struct ZmqConfig {
|
|||||||
pub command_port: u16,
|
pub command_port: u16,
|
||||||
pub bind_address: String,
|
pub bind_address: String,
|
||||||
pub transmission_interval_seconds: u64,
|
pub transmission_interval_seconds: u64,
|
||||||
|
/// Heartbeat transmission interval in seconds for host connectivity detection
|
||||||
|
#[serde(default = "default_heartbeat_interval_seconds")]
|
||||||
|
pub heartbeat_interval_seconds: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collector configuration
|
/// Collector configuration
|
||||||
@@ -147,9 +147,23 @@ pub struct NotificationConfig {
|
|||||||
pub rate_limit_minutes: u64,
|
pub rate_limit_minutes: u64,
|
||||||
/// Email notification batching interval in seconds (default: 60)
|
/// Email notification batching interval in seconds (default: 60)
|
||||||
pub aggregation_interval_seconds: u64,
|
pub aggregation_interval_seconds: u64,
|
||||||
|
/// List of metric names to exclude from email notifications
|
||||||
|
#[serde(default)]
|
||||||
|
pub exclude_email_metrics: Vec<String>,
|
||||||
|
/// Path to maintenance mode file that suppresses email notifications when present
|
||||||
|
#[serde(default = "default_maintenance_mode_file")]
|
||||||
|
pub maintenance_mode_file: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn default_heartbeat_interval_seconds() -> u64 {
|
||||||
|
5
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_maintenance_mode_file() -> String {
|
||||||
|
"/tmp/cm-maintenance".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
impl AgentConfig {
|
impl AgentConfig {
|
||||||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
|
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||||
loader::load_config(path)
|
loader::load_config(path)
|
||||||
|
|||||||
@@ -59,6 +59,6 @@ impl NotificationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_maintenance_mode(&self) -> bool {
|
fn is_maintenance_mode(&self) -> bool {
|
||||||
std::fs::metadata("/tmp/cm-maintenance").is_ok()
|
std::fs::metadata(&self.config.maintenance_mode_file).is_ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,14 +90,6 @@ impl UserStoppedServiceTracker {
|
|||||||
tracker
|
tracker
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mark a service as user-stopped
|
|
||||||
pub fn mark_user_stopped(&mut self, service_name: &str) -> Result<()> {
|
|
||||||
info!("Marking service '{}' as user-stopped", service_name);
|
|
||||||
self.user_stopped_services.insert(service_name.to_string());
|
|
||||||
self.save_to_storage()?;
|
|
||||||
debug!("Service '{}' marked as user-stopped and saved to storage", service_name);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clear user-stopped flag for a service (when user starts it)
|
/// Clear user-stopped flag for a service (when user starts it)
|
||||||
pub fn clear_user_stopped(&mut self, service_name: &str) -> Result<()> {
|
pub fn clear_user_stopped(&mut self, service_name: &str) -> Result<()> {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.61"
|
version = "0.1.81"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -9,14 +9,13 @@ 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};
|
||||||
|
|
||||||
use crate::communication::{AgentCommand, ServiceAction, ZmqCommandSender, ZmqConsumer};
|
use crate::communication::{ZmqConsumer};
|
||||||
use crate::config::DashboardConfig;
|
use crate::config::DashboardConfig;
|
||||||
use crate::metrics::MetricStore;
|
use crate::metrics::MetricStore;
|
||||||
use crate::ui::{TuiApp, UiCommand};
|
use crate::ui::TuiApp;
|
||||||
|
|
||||||
pub struct Dashboard {
|
pub struct Dashboard {
|
||||||
zmq_consumer: ZmqConsumer,
|
zmq_consumer: ZmqConsumer,
|
||||||
zmq_command_sender: ZmqCommandSender,
|
|
||||||
metric_store: MetricStore,
|
metric_store: MetricStore,
|
||||||
tui_app: Option<TuiApp>,
|
tui_app: Option<TuiApp>,
|
||||||
terminal: Option<Terminal<CrosstermBackend<io::Stdout>>>,
|
terminal: Option<Terminal<CrosstermBackend<io::Stdout>>>,
|
||||||
@@ -58,20 +57,9 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize ZMQ command sender
|
|
||||||
let zmq_command_sender = match ZmqCommandSender::new(&config.zmq) {
|
|
||||||
Ok(sender) => sender,
|
|
||||||
Err(e) => {
|
|
||||||
error!("Failed to initialize ZMQ command sender: {}", e);
|
|
||||||
return Err(e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Connect to configured hosts from configuration
|
|
||||||
let hosts: Vec<String> = config.hosts.keys().cloned().collect();
|
|
||||||
|
|
||||||
// Try to connect to hosts but don't fail if none are available
|
// Try to connect to hosts but don't fail if none are available
|
||||||
match zmq_consumer.connect_to_predefined_hosts(&hosts).await {
|
match zmq_consumer.connect_to_predefined_hosts(&config.hosts).await {
|
||||||
Ok(_) => info!("Successfully connected to ZMQ hosts"),
|
Ok(_) => info!("Successfully connected to ZMQ hosts"),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!(
|
warn!(
|
||||||
@@ -127,7 +115,6 @@ impl Dashboard {
|
|||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
zmq_consumer,
|
zmq_consumer,
|
||||||
zmq_command_sender,
|
|
||||||
metric_store,
|
metric_store,
|
||||||
tui_app,
|
tui_app,
|
||||||
terminal,
|
terminal,
|
||||||
@@ -137,18 +124,14 @@ impl Dashboard {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a command to a specific agent
|
|
||||||
pub async fn send_command(&mut self, hostname: &str, command: AgentCommand) -> Result<()> {
|
|
||||||
self.zmq_command_sender
|
|
||||||
.send_command(hostname, command)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn run(&mut self) -> Result<()> {
|
pub async fn run(&mut self) -> Result<()> {
|
||||||
info!("Starting dashboard main loop");
|
info!("Starting dashboard main loop");
|
||||||
|
|
||||||
let mut last_metrics_check = Instant::now();
|
let mut last_metrics_check = Instant::now();
|
||||||
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 heartbeat_check_interval = Duration::from_secs(1); // Check for host connectivity every 1 second
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Handle terminal events (keyboard input) only if not headless
|
// Handle terminal events (keyboard input) only if not headless
|
||||||
@@ -158,16 +141,10 @@ impl Dashboard {
|
|||||||
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 and check for commands
|
// Handle input
|
||||||
match tui_app.handle_input(event) {
|
match tui_app.handle_input(event) {
|
||||||
Ok(Some(command)) => {
|
Ok(_) => {
|
||||||
// Execute the command
|
// Check if we should quit
|
||||||
if let Err(e) = self.execute_ui_command(command).await {
|
|
||||||
error!("Failed to execute UI command: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(None) => {
|
|
||||||
// No command, 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");
|
||||||
break;
|
break;
|
||||||
@@ -213,34 +190,18 @@ impl Dashboard {
|
|||||||
metric_message.metrics.len()
|
metric_message.metrics.len()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check if this is the first time we've seen this host
|
// Track first contact with host (no command needed - agent sends data every 2s)
|
||||||
let is_new_host = !self
|
let is_new_host = !self
|
||||||
.initial_commands_sent
|
.initial_commands_sent
|
||||||
.contains(&metric_message.hostname);
|
.contains(&metric_message.hostname);
|
||||||
|
|
||||||
if is_new_host {
|
if is_new_host {
|
||||||
info!(
|
info!(
|
||||||
"First contact with host {}, sending initial CollectNow command",
|
"First contact with host {} - data will update automatically",
|
||||||
metric_message.hostname
|
metric_message.hostname
|
||||||
);
|
);
|
||||||
|
self.initial_commands_sent
|
||||||
// Send CollectNow command for immediate refresh
|
.insert(metric_message.hostname.clone());
|
||||||
if let Err(e) = self
|
|
||||||
.send_command(&metric_message.hostname, AgentCommand::CollectNow)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
error!(
|
|
||||||
"Failed to send initial CollectNow command to {}: {}",
|
|
||||||
metric_message.hostname, e
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
info!(
|
|
||||||
"✓ Sent initial CollectNow command to {}",
|
|
||||||
metric_message.hostname
|
|
||||||
);
|
|
||||||
self.initial_commands_sent
|
|
||||||
.insert(metric_message.hostname.clone());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update metric store
|
// Update metric store
|
||||||
@@ -254,14 +215,8 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update TUI with new hosts and metrics (only if not headless)
|
// Update TUI with new metrics (only if not headless)
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
let connected_hosts = self
|
|
||||||
.metric_store
|
|
||||||
.get_connected_hosts(Duration::from_secs(self.config.zmq.heartbeat_timeout_seconds));
|
|
||||||
|
|
||||||
|
|
||||||
tui_app.update_hosts(connected_hosts);
|
|
||||||
tui_app.update_metrics(&self.metric_store);
|
tui_app.update_metrics(&self.metric_store);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -280,6 +235,20 @@ impl Dashboard {
|
|||||||
last_metrics_check = Instant::now();
|
last_metrics_check = Instant::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for host connectivity changes (heartbeat timeouts) periodically
|
||||||
|
if last_heartbeat_check.elapsed() >= heartbeat_check_interval {
|
||||||
|
let timeout = Duration::from_secs(self.config.zmq.heartbeat_timeout_seconds);
|
||||||
|
|
||||||
|
// Clean up metrics for offline hosts
|
||||||
|
self.metric_store.cleanup_offline_hosts(timeout);
|
||||||
|
|
||||||
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
|
let connected_hosts = self.metric_store.get_connected_hosts(timeout);
|
||||||
|
tui_app.update_hosts(connected_hosts);
|
||||||
|
}
|
||||||
|
last_heartbeat_check = Instant::now();
|
||||||
|
}
|
||||||
|
|
||||||
// Render TUI (only if not headless)
|
// Render TUI (only if not headless)
|
||||||
if !self.headless {
|
if !self.headless {
|
||||||
if let Some(ref mut terminal) = self.terminal {
|
if let Some(ref mut terminal) = self.terminal {
|
||||||
@@ -302,33 +271,6 @@ impl Dashboard {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a UI command by sending it to the appropriate agent
|
|
||||||
async fn execute_ui_command(&self, command: UiCommand) -> Result<()> {
|
|
||||||
match command {
|
|
||||||
UiCommand::ServiceStart { hostname, service_name } => {
|
|
||||||
info!("Sending user start command for service {} on {}", service_name, hostname);
|
|
||||||
let agent_command = AgentCommand::ServiceControl {
|
|
||||||
service_name: service_name.clone(),
|
|
||||||
action: ServiceAction::UserStart,
|
|
||||||
};
|
|
||||||
self.zmq_command_sender.send_command(&hostname, agent_command).await?;
|
|
||||||
}
|
|
||||||
UiCommand::ServiceStop { hostname, service_name } => {
|
|
||||||
info!("Sending user stop command for service {} on {}", service_name, hostname);
|
|
||||||
let agent_command = AgentCommand::ServiceControl {
|
|
||||||
service_name: service_name.clone(),
|
|
||||||
action: ServiceAction::UserStop,
|
|
||||||
};
|
|
||||||
self.zmq_command_sender.send_command(&hostname, agent_command).await?;
|
|
||||||
}
|
|
||||||
UiCommand::TriggerBackup { hostname } => {
|
|
||||||
info!("Trigger backup requested for {}", hostname);
|
|
||||||
// TODO: Implement backup trigger command
|
|
||||||
info!("Backup trigger not yet implemented");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,40 +5,6 @@ use zmq::{Context, Socket, SocketType};
|
|||||||
|
|
||||||
use crate::config::ZmqConfig;
|
use crate::config::ZmqConfig;
|
||||||
|
|
||||||
/// Commands that can be sent to agents
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub enum AgentCommand {
|
|
||||||
/// Request immediate metric collection
|
|
||||||
CollectNow,
|
|
||||||
/// Change collection interval
|
|
||||||
SetInterval { seconds: u64 },
|
|
||||||
/// Enable/disable a collector
|
|
||||||
ToggleCollector { name: String, enabled: bool },
|
|
||||||
/// Request status/health check
|
|
||||||
Ping,
|
|
||||||
/// Control systemd service
|
|
||||||
ServiceControl {
|
|
||||||
service_name: String,
|
|
||||||
action: ServiceAction,
|
|
||||||
},
|
|
||||||
/// Rebuild NixOS system
|
|
||||||
SystemRebuild {
|
|
||||||
git_url: String,
|
|
||||||
git_branch: String,
|
|
||||||
working_dir: String,
|
|
||||||
api_key_file: Option<String>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Service control actions
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub enum ServiceAction {
|
|
||||||
Start,
|
|
||||||
Stop,
|
|
||||||
Status,
|
|
||||||
UserStart, // User-initiated start (clears user-stopped flag)
|
|
||||||
UserStop, // User-initiated stop (marks as user-stopped)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ZMQ consumer for receiving metrics from agents
|
/// ZMQ consumer for receiving metrics from agents
|
||||||
pub struct ZmqConsumer {
|
pub struct ZmqConsumer {
|
||||||
@@ -84,13 +50,14 @@ impl ZmqConsumer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Connect to predefined hosts
|
|
||||||
pub async fn connect_to_predefined_hosts(&mut self, hosts: &[String]) -> Result<()> {
|
/// Connect to predefined hosts using their configuration
|
||||||
|
pub async fn connect_to_predefined_hosts(&mut self, hosts: &std::collections::HashMap<String, crate::config::HostDetails>) -> Result<()> {
|
||||||
let default_port = self.config.subscriber_ports[0];
|
let default_port = self.config.subscriber_ports[0];
|
||||||
|
|
||||||
for hostname in hosts {
|
for (hostname, host_details) in hosts {
|
||||||
// Try to connect, but don't fail if some hosts are unreachable
|
// Try to connect using configured IP, but don't fail if some hosts are unreachable
|
||||||
if let Err(e) = self.connect_to_host(hostname, default_port).await {
|
if let Err(e) = self.connect_to_host_with_details(hostname, host_details, default_port).await {
|
||||||
warn!("Could not connect to {}: {}", hostname, e);
|
warn!("Could not connect to {}: {}", hostname, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,6 +71,15 @@ impl ZmqConsumer {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Connect to a host using its configuration details
|
||||||
|
pub async fn connect_to_host_with_details(&mut self, hostname: &str, host_details: &crate::config::HostDetails, port: u16) -> Result<()> {
|
||||||
|
// Get primary connection IP only - no fallbacks
|
||||||
|
let primary_ip = host_details.get_connection_ip(hostname);
|
||||||
|
|
||||||
|
// Connect directly without fallback attempts
|
||||||
|
self.connect_to_host(&primary_ip, port).await
|
||||||
|
}
|
||||||
|
|
||||||
/// Receive command output from any connected agent (non-blocking)
|
/// Receive command output from any connected agent (non-blocking)
|
||||||
pub async fn receive_command_output(&mut self) -> Result<Option<CommandOutputMessage>> {
|
pub async fn receive_command_output(&mut self) -> Result<Option<CommandOutputMessage>> {
|
||||||
match self.subscriber.recv_bytes(zmq::DONTWAIT) {
|
match self.subscriber.recv_bytes(zmq::DONTWAIT) {
|
||||||
@@ -192,42 +168,3 @@ impl ZmqConsumer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ZMQ command sender for sending commands to agents
|
|
||||||
pub struct ZmqCommandSender {
|
|
||||||
context: Context,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ZmqCommandSender {
|
|
||||||
pub fn new(_config: &ZmqConfig) -> Result<Self> {
|
|
||||||
let context = Context::new();
|
|
||||||
|
|
||||||
info!("ZMQ command sender initialized");
|
|
||||||
|
|
||||||
Ok(Self { context })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send a command to a specific agent
|
|
||||||
pub async fn send_command(&self, hostname: &str, command: AgentCommand) -> Result<()> {
|
|
||||||
// Create a new PUSH socket for this command (ZMQ best practice)
|
|
||||||
let socket = self.context.socket(SocketType::PUSH)?;
|
|
||||||
|
|
||||||
// Set socket options
|
|
||||||
socket.set_linger(1000)?; // Wait up to 1 second on close
|
|
||||||
socket.set_sndtimeo(5000)?; // 5 second send timeout
|
|
||||||
|
|
||||||
// Connect to agent's command port (6131)
|
|
||||||
let address = format!("tcp://{}:6131", hostname);
|
|
||||||
socket.connect(&address)?;
|
|
||||||
|
|
||||||
// Serialize command
|
|
||||||
let serialized = serde_json::to_vec(&command)?;
|
|
||||||
|
|
||||||
// Send command
|
|
||||||
socket.send(&serialized, 0)?;
|
|
||||||
|
|
||||||
info!("Sent command {:?} to agent at {}", command, hostname);
|
|
||||||
|
|
||||||
// Socket will be automatically closed when dropped
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -29,6 +29,17 @@ fn default_heartbeat_timeout_seconds() -> u64 {
|
|||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct HostDetails {
|
pub struct HostDetails {
|
||||||
pub mac_address: Option<String>,
|
pub mac_address: Option<String>,
|
||||||
|
/// Primary IP address (local network)
|
||||||
|
pub ip: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl HostDetails {
|
||||||
|
/// Get the IP address for connection (uses ip field or hostname as fallback)
|
||||||
|
pub fn get_connection_ip(&self, hostname: &str) -> String {
|
||||||
|
self.ip.as_ref().unwrap_or(&hostname.to_string()).clone()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// System configuration
|
/// System configuration
|
||||||
@@ -40,11 +51,12 @@ pub struct SystemConfig {
|
|||||||
pub nixos_config_api_key_file: Option<String>,
|
pub nixos_config_api_key_file: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SSH configuration for rebuild operations
|
/// SSH configuration for rebuild and backup operations
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct SshConfig {
|
pub struct SshConfig {
|
||||||
pub rebuild_user: String,
|
pub rebuild_user: String,
|
||||||
pub rebuild_alias: String,
|
pub rebuild_alias: String,
|
||||||
|
pub backup_alias: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Service log file configuration per host
|
/// Service log file configuration per host
|
||||||
|
|||||||
@@ -109,6 +109,28 @@ impl MetricStore {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Clean up data for offline hosts
|
||||||
|
pub fn cleanup_offline_hosts(&mut self, timeout: Duration) {
|
||||||
|
let now = Instant::now();
|
||||||
|
let mut hosts_to_cleanup = Vec::new();
|
||||||
|
|
||||||
|
// Find hosts that are offline (no recent heartbeat)
|
||||||
|
for (hostname, &last_heartbeat) in &self.last_heartbeat {
|
||||||
|
if now.duration_since(last_heartbeat) > timeout {
|
||||||
|
hosts_to_cleanup.push(hostname.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear metrics for offline hosts
|
||||||
|
for hostname in hosts_to_cleanup {
|
||||||
|
if let Some(metrics) = self.current_metrics.remove(&hostname) {
|
||||||
|
info!("Cleared {} metrics for offline host: {}", metrics.len(), hostname);
|
||||||
|
}
|
||||||
|
// Keep heartbeat timestamp for reconnection detection
|
||||||
|
// Don't remove from last_heartbeat to track when host was last seen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Cleanup old data and enforce limits
|
/// Cleanup old data and enforce limits
|
||||||
fn cleanup_host_data(&mut self, hostname: &str) {
|
fn cleanup_host_data(&mut self, hostname: &str) {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|||||||
@@ -16,26 +16,12 @@ pub mod widgets;
|
|||||||
|
|
||||||
use crate::config::DashboardConfig;
|
use crate::config::DashboardConfig;
|
||||||
use crate::metrics::MetricStore;
|
use crate::metrics::MetricStore;
|
||||||
use cm_dashboard_shared::{Metric, Status};
|
use cm_dashboard_shared::Status;
|
||||||
use theme::{Components, Layout as ThemeLayout, Theme, Typography};
|
use theme::{Components, Layout as ThemeLayout, Theme, Typography};
|
||||||
use widgets::{BackupWidget, ServicesWidget, SystemWidget, Widget};
|
use widgets::{BackupWidget, ServicesWidget, SystemWidget, Widget};
|
||||||
|
|
||||||
/// Commands that can be triggered from the UI
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum UiCommand {
|
|
||||||
ServiceStart { hostname: String, service_name: String },
|
|
||||||
ServiceStop { hostname: String, service_name: String },
|
|
||||||
TriggerBackup { hostname: String },
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Types of commands for status tracking
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum CommandType {
|
|
||||||
ServiceStart,
|
|
||||||
ServiceStop,
|
|
||||||
BackupTrigger,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Panel types for focus management
|
/// Panel types for focus management
|
||||||
|
|
||||||
@@ -48,14 +34,8 @@ pub struct HostWidgets {
|
|||||||
pub services_widget: ServicesWidget,
|
pub services_widget: ServicesWidget,
|
||||||
/// Backup widget state
|
/// Backup widget state
|
||||||
pub backup_widget: BackupWidget,
|
pub backup_widget: BackupWidget,
|
||||||
/// Scroll offsets for each panel
|
|
||||||
pub system_scroll_offset: usize,
|
|
||||||
pub services_scroll_offset: usize,
|
|
||||||
pub backup_scroll_offset: usize,
|
|
||||||
/// Last update time for this host
|
/// Last update time for this host
|
||||||
pub last_update: Option<Instant>,
|
pub last_update: Option<Instant>,
|
||||||
/// Pending service transitions for immediate visual feedback
|
|
||||||
pub pending_service_transitions: HashMap<String, (CommandType, String, Instant)>, // service_name -> (command_type, original_status, start_time)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HostWidgets {
|
impl HostWidgets {
|
||||||
@@ -64,11 +44,7 @@ impl HostWidgets {
|
|||||||
system_widget: SystemWidget::new(),
|
system_widget: SystemWidget::new(),
|
||||||
services_widget: ServicesWidget::new(),
|
services_widget: ServicesWidget::new(),
|
||||||
backup_widget: BackupWidget::new(),
|
backup_widget: BackupWidget::new(),
|
||||||
system_scroll_offset: 0,
|
|
||||||
services_scroll_offset: 0,
|
|
||||||
backup_scroll_offset: 0,
|
|
||||||
last_update: None,
|
last_update: None,
|
||||||
pending_service_transitions: HashMap::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -161,8 +137,6 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear completed transitions first
|
|
||||||
self.clear_completed_transitions(&hostname, &service_metrics);
|
|
||||||
|
|
||||||
// Now get host widgets and update them
|
// Now get host widgets and update them
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
||||||
@@ -198,14 +172,6 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep hosts that have pending transitions even if they're offline
|
|
||||||
for (hostname, host_widgets) in &self.host_widgets {
|
|
||||||
if !host_widgets.pending_service_transitions.is_empty() {
|
|
||||||
if !all_hosts.contains(hostname) {
|
|
||||||
all_hosts.push(hostname.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
all_hosts.sort();
|
all_hosts.sort();
|
||||||
self.available_hosts = all_hosts;
|
self.available_hosts = all_hosts;
|
||||||
@@ -236,7 +202,7 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Handle keyboard input
|
/// Handle keyboard input
|
||||||
pub fn handle_input(&mut self, event: Event) -> Result<Option<UiCommand>> {
|
pub fn handle_input(&mut self, event: Event) -> Result<()> {
|
||||||
if let Event::Key(key) = event {
|
if let Event::Key(key) = event {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char('q') => {
|
KeyCode::Char('q') => {
|
||||||
@@ -251,12 +217,14 @@ impl TuiApp {
|
|||||||
KeyCode::Char('r') => {
|
KeyCode::Char('r') => {
|
||||||
// System rebuild command - works on any panel for current host
|
// System rebuild command - works on any panel for current host
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
|
let connection_ip = self.get_connection_ip(&hostname);
|
||||||
// Create command that shows logo, rebuilds, and waits for user input
|
// Create command that shows logo, rebuilds, and waits for user input
|
||||||
let logo_and_rebuild = format!(
|
let logo_and_rebuild = format!(
|
||||||
"bash -c 'cat << \"EOF\"\nNixOS System Rebuild\nTarget: {}\n\nEOF\nssh -tt {}@{} \"bash -ic {}\"\necho\necho \"========================================\"\necho \"Rebuild completed. Press any key to close...\"\necho \"========================================\"\nread -n 1 -s\nexit'",
|
"bash -c 'cat << \"EOF\"\nNixOS System Rebuild\nTarget: {} ({})\n\nEOF\nssh -tt {}@{} \"bash -ic {}\"\necho\necho \"========================================\"\necho \"Rebuild completed. Press any key to close...\"\necho \"========================================\"\nread -n 1 -s\nexit'",
|
||||||
hostname,
|
hostname,
|
||||||
|
connection_ip,
|
||||||
self.config.ssh.rebuild_user,
|
self.config.ssh.rebuild_user,
|
||||||
hostname,
|
connection_ip,
|
||||||
self.config.ssh.rebuild_alias
|
self.config.ssh.rebuild_alias
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -270,29 +238,89 @@ impl TuiApp {
|
|||||||
.ok(); // Ignore errors, tmux will handle them
|
.ok(); // Ignore errors, tmux will handle them
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('B') => {
|
||||||
|
// Backup command - works on any panel for current host
|
||||||
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
|
let connection_ip = self.get_connection_ip(&hostname);
|
||||||
|
// Create command that shows logo, runs backup, and waits for user input
|
||||||
|
let logo_and_backup = format!(
|
||||||
|
"bash -c 'cat << \"EOF\"\nBackup Operation\nTarget: {} ({})\n\nEOF\nssh -tt {}@{} \"bash -ic {}\"\necho\necho \"========================================\"\necho \"Backup completed. Press any key to close...\"\necho \"========================================\"\nread -n 1 -s\nexit'",
|
||||||
|
hostname,
|
||||||
|
connection_ip,
|
||||||
|
self.config.ssh.rebuild_user,
|
||||||
|
connection_ip,
|
||||||
|
self.config.ssh.backup_alias
|
||||||
|
);
|
||||||
|
|
||||||
|
std::process::Command::new("tmux")
|
||||||
|
.arg("split-window")
|
||||||
|
.arg("-v")
|
||||||
|
.arg("-p")
|
||||||
|
.arg("30")
|
||||||
|
.arg(&logo_and_backup)
|
||||||
|
.spawn()
|
||||||
|
.ok(); // Ignore errors, tmux will handle them
|
||||||
|
}
|
||||||
|
}
|
||||||
KeyCode::Char('s') => {
|
KeyCode::Char('s') => {
|
||||||
// Service start command
|
// Service start command via SSH with progress display
|
||||||
if let (Some(service_name), Some(hostname)) = (self.get_selected_service(), self.current_host.clone()) {
|
if let (Some(service_name), Some(hostname)) = (self.get_selected_service(), self.current_host.clone()) {
|
||||||
if self.start_command(&hostname, CommandType::ServiceStart, service_name.clone()) {
|
let connection_ip = self.get_connection_ip(&hostname);
|
||||||
return Ok(Some(UiCommand::ServiceStart { hostname, service_name }));
|
let service_start_command = format!(
|
||||||
}
|
"bash -c 'cat << \"EOF\"\nService Start: {}\nTarget: {} ({})\n\nEOF\nssh -tt {}@{} \"echo \\\"Starting service...\\\" && sudo systemctl start {} && echo \\\"Following logs (Ctrl+C to stop):\\\" && echo \\\"========================================\\\" && sudo journalctl -fu {} --no-pager\"\necho\necho \"========================================\"\necho \"Operation completed. Press any key to close...\"\necho \"========================================\"\nread -n 1 -s\nexit'",
|
||||||
|
service_name,
|
||||||
|
hostname,
|
||||||
|
connection_ip,
|
||||||
|
self.config.ssh.rebuild_user,
|
||||||
|
connection_ip,
|
||||||
|
service_name,
|
||||||
|
service_name
|
||||||
|
);
|
||||||
|
|
||||||
|
std::process::Command::new("tmux")
|
||||||
|
.arg("split-window")
|
||||||
|
.arg("-v")
|
||||||
|
.arg("-p")
|
||||||
|
.arg("30")
|
||||||
|
.arg(&service_start_command)
|
||||||
|
.spawn()
|
||||||
|
.ok(); // Ignore errors, tmux will handle them
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Char('S') => {
|
KeyCode::Char('S') => {
|
||||||
// Service stop command
|
// Service stop command via SSH with progress display
|
||||||
if let (Some(service_name), Some(hostname)) = (self.get_selected_service(), self.current_host.clone()) {
|
if let (Some(service_name), Some(hostname)) = (self.get_selected_service(), self.current_host.clone()) {
|
||||||
if self.start_command(&hostname, CommandType::ServiceStop, service_name.clone()) {
|
let connection_ip = self.get_connection_ip(&hostname);
|
||||||
return Ok(Some(UiCommand::ServiceStop { hostname, service_name }));
|
let service_stop_command = format!(
|
||||||
}
|
"bash -c 'cat << \"EOF\"\nService Stop: {}.service\nTarget: {} ({})\n\nEOF\nssh -tt {}@{} \"echo \\\"Stopping service...\\\" && sudo systemctl stop {}.service && echo \\\"Service stopped! Final logs:\\\" && echo \\\"========================================\\\" && sudo journalctl -u {}.service --no-pager -n 10 && echo \\\"========================================\\\" && sudo systemctl status {}.service --no-pager -l\"\necho\necho \"========================================\"\necho \"Operation completed. Press any key to close...\"\necho \"========================================\"\nread -n 1 -s\nexit'",
|
||||||
|
service_name,
|
||||||
|
hostname,
|
||||||
|
connection_ip,
|
||||||
|
self.config.ssh.rebuild_user,
|
||||||
|
connection_ip,
|
||||||
|
service_name,
|
||||||
|
service_name,
|
||||||
|
service_name
|
||||||
|
);
|
||||||
|
|
||||||
|
std::process::Command::new("tmux")
|
||||||
|
.arg("split-window")
|
||||||
|
.arg("-v")
|
||||||
|
.arg("-p")
|
||||||
|
.arg("30")
|
||||||
|
.arg(&service_stop_command)
|
||||||
|
.spawn()
|
||||||
|
.ok(); // Ignore errors, tmux will handle them
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Char('J') => {
|
KeyCode::Char('J') => {
|
||||||
// Show service logs via journalctl in tmux split window
|
// Show service logs via journalctl in tmux split window
|
||||||
if let (Some(service_name), Some(hostname)) = (self.get_selected_service(), self.current_host.clone()) {
|
if let (Some(service_name), Some(hostname)) = (self.get_selected_service(), self.current_host.clone()) {
|
||||||
|
let connection_ip = self.get_connection_ip(&hostname);
|
||||||
let journalctl_command = format!(
|
let journalctl_command = format!(
|
||||||
"bash -c \"ssh -tt {}@{} 'sudo journalctl -u {}.service -f --no-pager -n 50'; exit\"",
|
"bash -c \"ssh -tt {}@{} 'sudo journalctl -u {}.service -f --no-pager -n 50'; exit\"",
|
||||||
self.config.ssh.rebuild_user,
|
self.config.ssh.rebuild_user,
|
||||||
hostname,
|
connection_ip,
|
||||||
service_name
|
service_name
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -312,10 +340,11 @@ impl TuiApp {
|
|||||||
// Check if this service has a custom log file configured
|
// Check if this service has a custom log file configured
|
||||||
if let Some(host_logs) = self.config.service_logs.get(&hostname) {
|
if let Some(host_logs) = self.config.service_logs.get(&hostname) {
|
||||||
if let Some(log_config) = host_logs.iter().find(|config| config.service_name == service_name) {
|
if let Some(log_config) = host_logs.iter().find(|config| config.service_name == service_name) {
|
||||||
|
let connection_ip = self.get_connection_ip(&hostname);
|
||||||
let tail_command = format!(
|
let tail_command = format!(
|
||||||
"bash -c \"ssh -tt {}@{} 'sudo tail -n 50 -f {}'; exit\"",
|
"bash -c \"ssh -tt {}@{} 'sudo tail -n 50 -f {}'; exit\"",
|
||||||
self.config.ssh.rebuild_user,
|
self.config.ssh.rebuild_user,
|
||||||
hostname,
|
connection_ip,
|
||||||
log_config.log_file_path
|
log_config.log_file_path
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -331,13 +360,6 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Char('b') => {
|
|
||||||
// Trigger backup
|
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
|
||||||
self.start_command(&hostname, CommandType::BackupTrigger, hostname.clone());
|
|
||||||
return Ok(Some(UiCommand::TriggerBackup { hostname }));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
KeyCode::Char('w') => {
|
KeyCode::Char('w') => {
|
||||||
// Wake on LAN for offline hosts
|
// Wake on LAN for offline hosts
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
@@ -365,6 +387,26 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('t') => {
|
||||||
|
// Open SSH terminal session in tmux window
|
||||||
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
|
let connection_ip = self.get_connection_ip(&hostname);
|
||||||
|
let ssh_command = format!(
|
||||||
|
"ssh -tt {}@{}",
|
||||||
|
self.config.ssh.rebuild_user,
|
||||||
|
connection_ip
|
||||||
|
);
|
||||||
|
|
||||||
|
std::process::Command::new("tmux")
|
||||||
|
.arg("split-window")
|
||||||
|
.arg("-v")
|
||||||
|
.arg("-p")
|
||||||
|
.arg("30") // Use 30% like other commands
|
||||||
|
.arg(&ssh_command)
|
||||||
|
.spawn()
|
||||||
|
.ok(); // Ignore errors, tmux will handle them
|
||||||
|
}
|
||||||
|
}
|
||||||
KeyCode::Tab => {
|
KeyCode::Tab => {
|
||||||
// Tab cycles to next host
|
// Tab cycles to next host
|
||||||
self.navigate_host(1);
|
self.navigate_host(1);
|
||||||
@@ -390,7 +432,7 @@ impl TuiApp {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Navigate between hosts
|
/// Navigate between hosts
|
||||||
@@ -444,86 +486,8 @@ impl TuiApp {
|
|||||||
self.should_quit
|
self.should_quit
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current service status for state-aware command validation
|
|
||||||
fn get_current_service_status(&self, hostname: &str, service_name: &str) -> Option<String> {
|
|
||||||
if let Some(host_widgets) = self.host_widgets.get(hostname) {
|
|
||||||
return host_widgets.services_widget.get_service_status(service_name);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Start command execution with immediate visual feedback
|
|
||||||
pub fn start_command(&mut self, hostname: &str, command_type: CommandType, target: String) -> bool {
|
|
||||||
// Get current service status to validate command
|
|
||||||
let current_status = self.get_current_service_status(hostname, &target);
|
|
||||||
|
|
||||||
// Validate if command makes sense for current state
|
|
||||||
let should_execute = match (&command_type, current_status.as_deref()) {
|
|
||||||
(CommandType::ServiceStart, Some("inactive") | Some("failed") | Some("dead")) => true,
|
|
||||||
(CommandType::ServiceStop, Some("active")) => true,
|
|
||||||
(CommandType::ServiceStart, Some("active")) => {
|
|
||||||
// Already running - don't execute
|
|
||||||
false
|
|
||||||
},
|
|
||||||
(CommandType::ServiceStop, Some("inactive") | Some("failed") | Some("dead")) => {
|
|
||||||
// Already stopped - don't execute
|
|
||||||
false
|
|
||||||
},
|
|
||||||
(_, None) => {
|
|
||||||
// Unknown service state - allow command to proceed
|
|
||||||
true
|
|
||||||
},
|
|
||||||
_ => true, // Default: allow other combinations
|
|
||||||
};
|
|
||||||
|
|
||||||
// ALWAYS store the pending transition for immediate visual feedback, even if we don't execute
|
|
||||||
if let Some(host_widgets) = self.host_widgets.get_mut(hostname) {
|
|
||||||
host_widgets.pending_service_transitions.insert(
|
|
||||||
target.clone(),
|
|
||||||
(command_type, current_status.unwrap_or_else(|| "unknown".to_string()), Instant::now())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
should_execute
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clear pending transitions when real status updates arrive or timeout
|
|
||||||
fn clear_completed_transitions(&mut self, hostname: &str, service_metrics: &[&Metric]) {
|
|
||||||
if let Some(host_widgets) = self.host_widgets.get_mut(hostname) {
|
|
||||||
let mut completed_services = Vec::new();
|
|
||||||
|
|
||||||
// Check each pending transition to see if real status has changed
|
|
||||||
for (service_name, (command_type, original_status, _start_time)) in &host_widgets.pending_service_transitions {
|
|
||||||
|
|
||||||
// Look for status metric for this service
|
|
||||||
for metric in service_metrics {
|
|
||||||
if metric.name == format!("service_{}_status", service_name) {
|
|
||||||
let new_status = metric.value.as_string();
|
|
||||||
|
|
||||||
// Check if status has changed from original (command completed)
|
|
||||||
if &new_status != original_status {
|
|
||||||
// Verify it changed in the expected direction
|
|
||||||
let expected_change = match command_type {
|
|
||||||
CommandType::ServiceStart => &new_status == "active",
|
|
||||||
CommandType::ServiceStop => &new_status != "active",
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
if expected_change {
|
|
||||||
completed_services.push(service_name.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove completed transitions
|
|
||||||
for service_name in completed_services {
|
|
||||||
host_widgets.pending_service_transitions.remove(&service_name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -558,6 +522,21 @@ impl TuiApp {
|
|||||||
])
|
])
|
||||||
.split(main_chunks[1]); // main_chunks[1] is now the content area (between title and statusbar)
|
.split(main_chunks[1]); // main_chunks[1] is now the content area (between title and statusbar)
|
||||||
|
|
||||||
|
// Check if current host is offline
|
||||||
|
let current_host_offline = if let Some(hostname) = self.current_host.clone() {
|
||||||
|
self.calculate_host_status(&hostname, metric_store) == Status::Offline
|
||||||
|
} else {
|
||||||
|
true // No host selected is considered offline
|
||||||
|
};
|
||||||
|
|
||||||
|
// If host is offline, render wake-up message instead of panels
|
||||||
|
if current_host_offline {
|
||||||
|
self.render_offline_host_message(frame, main_chunks[1]);
|
||||||
|
self.render_btop_title(frame, main_chunks[0], metric_store);
|
||||||
|
self.render_statusbar(frame, main_chunks[2]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if backup panel should be shown
|
// Check if backup panel should be shown
|
||||||
let show_backup = if let Some(hostname) = self.current_host.clone() {
|
let show_backup = if let Some(hostname) = self.current_host.clone() {
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
||||||
@@ -596,14 +575,10 @@ impl TuiApp {
|
|||||||
// Render services widget for current host
|
// Render services widget for current host
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
let is_focused = true; // Always show service selection
|
let is_focused = true; // Always show service selection
|
||||||
let (scroll_offset, pending_transitions) = {
|
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
|
||||||
(host_widgets.services_scroll_offset, host_widgets.pending_service_transitions.clone())
|
|
||||||
};
|
|
||||||
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_with_transitions(frame, content_chunks[1], is_focused, scroll_offset, &pending_transitions); // Services takes full right side
|
.render(frame, content_chunks[1], is_focused); // Services takes full right side
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render statusbar at the bottom
|
// Render statusbar at the bottom
|
||||||
@@ -730,6 +705,7 @@ impl TuiApp {
|
|||||||
Status::Warning => has_warning = true,
|
Status::Warning => has_warning = true,
|
||||||
Status::Pending => has_pending = true,
|
Status::Pending => has_pending = true,
|
||||||
Status::Ok => ok_count += 1,
|
Status::Ok => ok_count += 1,
|
||||||
|
Status::Inactive => ok_count += 1, // Treat inactive as OK for aggregation
|
||||||
Status::Unknown => {}, // Ignore unknown for aggregation
|
Status::Unknown => {}, // Ignore unknown for aggregation
|
||||||
Status::Offline => {}, // Ignore offline for aggregation
|
Status::Offline => {}, // Ignore offline for aggregation
|
||||||
}
|
}
|
||||||
@@ -786,12 +762,10 @@ impl TuiApp {
|
|||||||
frame.render_widget(system_block, area);
|
frame.render_widget(system_block, area);
|
||||||
// Get current host widgets, create if none exist
|
// Get current host widgets, create if none exist
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
let scroll_offset = {
|
// Clone the config to avoid borrowing issues
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
let config = self.config.clone();
|
||||||
host_widgets.system_scroll_offset
|
|
||||||
};
|
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
||||||
host_widgets.system_widget.render_with_scroll(frame, inner_area, scroll_offset, &hostname);
|
host_widgets.system_widget.render(frame, inner_area, &hostname, Some(&config));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -802,16 +776,92 @@ impl TuiApp {
|
|||||||
|
|
||||||
// Get current host widgets for backup widget
|
// Get current host widgets for backup widget
|
||||||
if let Some(hostname) = self.current_host.clone() {
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
let scroll_offset = {
|
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
|
||||||
host_widgets.backup_scroll_offset
|
|
||||||
};
|
|
||||||
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
let host_widgets = self.get_or_create_host_widgets(&hostname);
|
||||||
host_widgets.backup_widget.render_with_scroll(frame, inner_area, scroll_offset);
|
host_widgets.backup_widget.render(frame, inner_area);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Render offline host message with wake-up option
|
||||||
|
fn render_offline_host_message(&self, frame: &mut Frame, area: Rect) {
|
||||||
|
use ratatui::layout::Alignment;
|
||||||
|
use ratatui::style::Modifier;
|
||||||
|
use ratatui::text::{Line, Span};
|
||||||
|
use ratatui::widgets::{Block, Borders, Paragraph};
|
||||||
|
|
||||||
|
// Get hostname for message
|
||||||
|
let hostname = self.current_host.as_ref()
|
||||||
|
.map(|h| h.as_str())
|
||||||
|
.unwrap_or("Unknown");
|
||||||
|
|
||||||
|
// Check if host has MAC address for wake-on-LAN
|
||||||
|
let has_mac = self.current_host.as_ref()
|
||||||
|
.and_then(|hostname| self.config.hosts.get(hostname))
|
||||||
|
.and_then(|details| details.mac_address.as_ref())
|
||||||
|
.is_some();
|
||||||
|
|
||||||
|
// Create message content
|
||||||
|
let mut lines = vec![
|
||||||
|
Line::from(Span::styled(
|
||||||
|
format!("Host '{}' is offline", hostname),
|
||||||
|
Style::default().fg(Theme::muted_text()).add_modifier(Modifier::BOLD),
|
||||||
|
)),
|
||||||
|
Line::from(""),
|
||||||
|
];
|
||||||
|
|
||||||
|
if has_mac {
|
||||||
|
lines.push(Line::from(Span::styled(
|
||||||
|
"Press 'w' to wake up host",
|
||||||
|
Style::default().fg(Theme::primary_text()).add_modifier(Modifier::BOLD),
|
||||||
|
)));
|
||||||
|
} else {
|
||||||
|
lines.push(Line::from(Span::styled(
|
||||||
|
"No MAC address configured - cannot wake up",
|
||||||
|
Style::default().fg(Theme::muted_text()),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create centered message
|
||||||
|
let message = Paragraph::new(lines)
|
||||||
|
.block(Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(Style::default().fg(Theme::muted_text()))
|
||||||
|
.title(" Offline Host ")
|
||||||
|
.title_style(Style::default().fg(Theme::muted_text()).add_modifier(Modifier::BOLD)))
|
||||||
|
.style(Style::default().bg(Theme::background()).fg(Theme::primary_text()))
|
||||||
|
.alignment(Alignment::Center);
|
||||||
|
|
||||||
|
// Center the message in the available area
|
||||||
|
let popup_area = ratatui::layout::Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(40),
|
||||||
|
Constraint::Length(6),
|
||||||
|
Constraint::Percentage(40),
|
||||||
|
])
|
||||||
|
.split(area)[1];
|
||||||
|
|
||||||
|
let popup_area = ratatui::layout::Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(25),
|
||||||
|
Constraint::Percentage(50),
|
||||||
|
Constraint::Percentage(25),
|
||||||
|
])
|
||||||
|
.split(popup_area)[1];
|
||||||
|
|
||||||
|
frame.render_widget(message, popup_area);
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse MAC address string (e.g., "AA:BB:CC:DD:EE:FF") to [u8; 6]
|
/// Parse MAC address string (e.g., "AA:BB:CC:DD:EE:FF") to [u8; 6]
|
||||||
|
/// Get the connection IP for a hostname based on host configuration
|
||||||
|
fn get_connection_ip(&self, hostname: &str) -> String {
|
||||||
|
if let Some(host_details) = self.config.hosts.get(hostname) {
|
||||||
|
host_details.get_connection_ip(hostname)
|
||||||
|
} else {
|
||||||
|
hostname.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_mac_address(mac_str: &str) -> Result<[u8; 6], &'static str> {
|
fn parse_mac_address(mac_str: &str) -> Result<[u8; 6], &'static str> {
|
||||||
let parts: Vec<&str> = mac_str.split(':').collect();
|
let parts: Vec<&str> = mac_str.split(':').collect();
|
||||||
if parts.len() != 6 {
|
if parts.len() != 6 {
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ impl Theme {
|
|||||||
pub fn status_color(status: Status) -> Color {
|
pub fn status_color(status: Status) -> Color {
|
||||||
match status {
|
match status {
|
||||||
Status::Ok => Self::success(),
|
Status::Ok => Self::success(),
|
||||||
|
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
|
||||||
Status::Warning => Self::warning(),
|
Status::Warning => Self::warning(),
|
||||||
Status::Critical => Self::error(),
|
Status::Critical => Self::error(),
|
||||||
@@ -243,6 +244,7 @@ impl StatusIcons {
|
|||||||
pub fn get_icon(status: Status) -> &'static str {
|
pub fn get_icon(status: Status) -> &'static str {
|
||||||
match status {
|
match status {
|
||||||
Status::Ok => "●",
|
Status::Ok => "●",
|
||||||
|
Status::Inactive => "○", // Empty circle for inactive services
|
||||||
Status::Pending => "◉", // Hollow circle for pending
|
Status::Pending => "◉", // Hollow circle for pending
|
||||||
Status::Warning => "◐",
|
Status::Warning => "◐",
|
||||||
Status::Critical => "!",
|
Status::Critical => "!",
|
||||||
@@ -256,6 +258,7 @@ impl StatusIcons {
|
|||||||
let icon = Self::get_icon(status);
|
let icon = Self::get_icon(status);
|
||||||
let status_color = match status {
|
let status_color = match status {
|
||||||
Status::Ok => Theme::success(), // Green
|
Status::Ok => Theme::success(), // Green
|
||||||
|
Status::Inactive => Theme::muted_text(), // Gray for inactive services
|
||||||
Status::Pending => Theme::highlight(), // Blue
|
Status::Pending => Theme::highlight(), // Blue
|
||||||
Status::Warning => Theme::warning(), // Yellow
|
Status::Warning => Theme::warning(), // Yellow
|
||||||
Status::Critical => Theme::error(), // Red
|
Status::Critical => Theme::error(), // Red
|
||||||
|
|||||||
@@ -285,8 +285,8 @@ impl Widget for BackupWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BackupWidget {
|
impl BackupWidget {
|
||||||
/// Render with scroll offset support
|
/// Render backup widget
|
||||||
pub fn render_with_scroll(&mut self, frame: &mut Frame, area: Rect, scroll_offset: usize) {
|
pub fn render(&mut self, frame: &mut Frame, area: Rect) {
|
||||||
let mut lines = Vec::new();
|
let mut lines = Vec::new();
|
||||||
|
|
||||||
// Latest backup section
|
// Latest backup section
|
||||||
@@ -366,42 +366,20 @@ impl BackupWidget {
|
|||||||
let total_lines = lines.len();
|
let total_lines = lines.len();
|
||||||
let available_height = area.height as usize;
|
let available_height = area.height as usize;
|
||||||
|
|
||||||
// Calculate scroll boundaries
|
// Show only what fits, with "X more below" if needed
|
||||||
let max_scroll = if total_lines > available_height {
|
if total_lines > available_height {
|
||||||
total_lines - available_height
|
let lines_for_content = available_height.saturating_sub(1); // Reserve one line for "more below"
|
||||||
} else {
|
|
||||||
total_lines.saturating_sub(1)
|
|
||||||
};
|
|
||||||
let effective_scroll = scroll_offset.min(max_scroll);
|
|
||||||
|
|
||||||
// Apply scrolling if needed
|
|
||||||
if scroll_offset > 0 || total_lines > available_height {
|
|
||||||
let mut visible_lines: Vec<_> = lines
|
let mut visible_lines: Vec<_> = lines
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.skip(effective_scroll)
|
.take(lines_for_content)
|
||||||
.take(available_height)
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Add scroll indicator if there are hidden lines
|
let hidden_below = total_lines.saturating_sub(lines_for_content);
|
||||||
if total_lines > available_height {
|
if hidden_below > 0 {
|
||||||
let hidden_above = effective_scroll;
|
let more_line = ratatui::text::Line::from(vec![
|
||||||
let hidden_below = total_lines.saturating_sub(effective_scroll + available_height);
|
ratatui::text::Span::styled(format!("... {} more below", hidden_below), Typography::muted())
|
||||||
|
]);
|
||||||
if (hidden_above > 0 || hidden_below > 0) && !visible_lines.is_empty() {
|
visible_lines.push(more_line);
|
||||||
let scroll_text = if hidden_above > 0 && hidden_below > 0 {
|
|
||||||
format!("... {} above, {} below", hidden_above, hidden_below)
|
|
||||||
} else if hidden_above > 0 {
|
|
||||||
format!("... {} more above", hidden_above)
|
|
||||||
} else {
|
|
||||||
format!("... {} more below", hidden_below)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Replace last line with scroll indicator
|
|
||||||
visible_lines.pop();
|
|
||||||
visible_lines.push(ratatui::text::Line::from(vec![
|
|
||||||
ratatui::text::Span::styled(scroll_text, Typography::muted())
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let paragraph = Paragraph::new(ratatui::text::Text::from(visible_lines));
|
let paragraph = Paragraph::new(ratatui::text::Text::from(visible_lines));
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ use tracing::debug;
|
|||||||
|
|
||||||
use super::Widget;
|
use super::Widget;
|
||||||
use crate::ui::theme::{Components, StatusIcons, Theme, Typography};
|
use crate::ui::theme::{Components, StatusIcons, Theme, Typography};
|
||||||
use crate::ui::CommandType;
|
|
||||||
use ratatui::style::Style;
|
use ratatui::style::Style;
|
||||||
|
|
||||||
/// Services widget displaying hierarchical systemd service statuses
|
/// Services widget displaying hierarchical systemd service statuses
|
||||||
@@ -125,41 +124,14 @@ impl ServicesWidget {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get status icon for service, considering pending transitions for visual feedback
|
|
||||||
fn get_service_icon_and_status(&self, service_name: &str, info: &ServiceInfo, pending_transitions: &HashMap<String, (CommandType, String, std::time::Instant)>) -> (String, String, ratatui::prelude::Color) {
|
|
||||||
// Check if this service has a pending transition
|
|
||||||
if let Some((command_type, _original_status, _start_time)) = pending_transitions.get(service_name) {
|
|
||||||
// Show transitional icons for pending commands
|
|
||||||
let (icon, status_text) = match command_type {
|
|
||||||
CommandType::ServiceStart => ("↑", "starting"),
|
|
||||||
CommandType::ServiceStop => ("↓", "stopping"),
|
|
||||||
_ => return (StatusIcons::get_icon(info.widget_status).to_string(), info.status.clone(), Theme::status_color(info.widget_status)), // Not a service command
|
|
||||||
};
|
|
||||||
return (icon.to_string(), status_text.to_string(), Theme::highlight());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normal status display
|
|
||||||
let icon = StatusIcons::get_icon(info.widget_status);
|
|
||||||
let status_color = match info.widget_status {
|
|
||||||
Status::Ok => Theme::success(),
|
|
||||||
Status::Pending => Theme::highlight(),
|
|
||||||
Status::Warning => Theme::warning(),
|
|
||||||
Status::Critical => Theme::error(),
|
|
||||||
Status::Unknown => Theme::muted_text(),
|
|
||||||
Status::Offline => Theme::muted_text(),
|
|
||||||
};
|
|
||||||
|
|
||||||
(icon.to_string(), info.status.clone(), status_color)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Create spans for sub-service with icon next to name, considering pending transitions
|
/// Create spans for sub-service with icon next to name
|
||||||
fn create_sub_service_spans_with_transitions(
|
fn create_sub_service_spans(
|
||||||
&self,
|
&self,
|
||||||
name: &str,
|
name: &str,
|
||||||
info: &ServiceInfo,
|
info: &ServiceInfo,
|
||||||
is_last: bool,
|
is_last: bool,
|
||||||
pending_transitions: &HashMap<String, (CommandType, String, std::time::Instant)>,
|
|
||||||
) -> Vec<ratatui::text::Span<'static>> {
|
) -> Vec<ratatui::text::Span<'static>> {
|
||||||
// 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() > 18 {
|
||||||
@@ -168,19 +140,28 @@ impl ServicesWidget {
|
|||||||
name.to_string()
|
name.to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get status icon and text, considering pending transitions
|
// Get status icon and text
|
||||||
let (icon, mut status_str, status_color) = self.get_service_icon_and_status(name, info, pending_transitions);
|
let icon = StatusIcons::get_icon(info.widget_status);
|
||||||
|
let status_color = match info.widget_status {
|
||||||
|
Status::Ok => Theme::success(),
|
||||||
|
Status::Inactive => Theme::muted_text(),
|
||||||
|
Status::Pending => Theme::highlight(),
|
||||||
|
Status::Warning => Theme::warning(),
|
||||||
|
Status::Critical => Theme::error(),
|
||||||
|
Status::Unknown => Theme::muted_text(),
|
||||||
|
Status::Offline => Theme::muted_text(),
|
||||||
|
};
|
||||||
|
|
||||||
// For sub-services, prefer latency if available (unless transition is pending)
|
// For sub-services, prefer latency if available
|
||||||
if !pending_transitions.contains_key(name) {
|
let status_str = if let Some(latency) = info.latency_ms {
|
||||||
if let Some(latency) = info.latency_ms {
|
if latency < 0.0 {
|
||||||
status_str = if latency < 0.0 {
|
"timeout".to_string()
|
||||||
"timeout".to_string()
|
} else {
|
||||||
} else {
|
format!("{:.0}ms", latency)
|
||||||
format!("{:.0}ms", latency)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
info.status.clone()
|
||||||
|
};
|
||||||
let tree_symbol = if is_last { "└─" } else { "├─" };
|
let tree_symbol = if is_last { "└─" } else { "├─" };
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
@@ -266,25 +247,6 @@ impl ServicesWidget {
|
|||||||
self.parent_services.len()
|
self.parent_services.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current status of a specific service by name
|
|
||||||
pub fn get_service_status(&self, service_name: &str) -> Option<String> {
|
|
||||||
// Check if it's a parent service
|
|
||||||
if let Some(parent_info) = self.parent_services.get(service_name) {
|
|
||||||
return Some(parent_info.status.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check sub-services (format: parent_sub)
|
|
||||||
for (parent_name, sub_list) in &self.sub_services {
|
|
||||||
for (sub_name, sub_info) in sub_list {
|
|
||||||
let full_sub_name = format!("{}_{}", parent_name, sub_name);
|
|
||||||
if full_sub_name == service_name {
|
|
||||||
return Some(sub_info.status.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
@@ -439,8 +401,8 @@ impl Widget for ServicesWidget {
|
|||||||
|
|
||||||
impl ServicesWidget {
|
impl ServicesWidget {
|
||||||
|
|
||||||
/// Render with focus, scroll, and pending transitions for visual feedback
|
/// Render with focus
|
||||||
pub fn render_with_transitions(&mut self, frame: &mut Frame, area: Rect, is_focused: bool, scroll_offset: usize, pending_transitions: &HashMap<String, (CommandType, String, std::time::Instant)>) {
|
pub fn render(&mut self, frame: &mut Frame, area: Rect, is_focused: bool) {
|
||||||
let services_block = Components::widget_block("services");
|
let services_block = Components::widget_block("services");
|
||||||
let inner_area = services_block.inner(area);
|
let inner_area = services_block.inner(area);
|
||||||
frame.render_widget(services_block, area);
|
frame.render_widget(services_block, area);
|
||||||
@@ -465,14 +427,14 @@ impl ServicesWidget {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the existing render logic but with pending transitions
|
// Render the services list
|
||||||
self.render_services_with_transitions(frame, content_chunks[1], is_focused, scroll_offset, pending_transitions);
|
self.render_services(frame, content_chunks[1], is_focused);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render services list with pending transitions awareness
|
/// Render services list
|
||||||
fn render_services_with_transitions(&mut self, frame: &mut Frame, area: Rect, is_focused: bool, scroll_offset: usize, pending_transitions: &HashMap<String, (CommandType, String, std::time::Instant)>) {
|
fn render_services(&mut self, frame: &mut Frame, area: Rect, is_focused: bool) {
|
||||||
// Build hierarchical service list for display - include raw service name for pending transition lookups
|
// Build hierarchical service list for display
|
||||||
let mut display_lines: Vec<(String, Status, bool, Option<(ServiceInfo, bool)>, String)> = Vec::new(); // Added raw service name
|
let mut display_lines: Vec<(String, Status, bool, Option<(ServiceInfo, bool)>)> = Vec::new();
|
||||||
|
|
||||||
// Sort parent services alphabetically for consistent order
|
// Sort parent services alphabetically for consistent order
|
||||||
let mut parent_services: Vec<_> = self.parent_services.iter().collect();
|
let mut parent_services: Vec<_> = self.parent_services.iter().collect();
|
||||||
@@ -481,7 +443,7 @@ impl ServicesWidget {
|
|||||||
for (parent_name, parent_info) in parent_services {
|
for (parent_name, parent_info) in parent_services {
|
||||||
// Add parent service line
|
// Add parent service line
|
||||||
let parent_line = self.format_parent_service_line(parent_name, parent_info);
|
let parent_line = self.format_parent_service_line(parent_name, parent_info);
|
||||||
display_lines.push((parent_line, parent_info.widget_status, false, None, parent_name.clone())); // Include raw name
|
display_lines.push((parent_line, parent_info.widget_status, false, None));
|
||||||
|
|
||||||
// Add sub-services for this parent (if any)
|
// Add sub-services for this parent (if any)
|
||||||
if let Some(sub_list) = self.sub_services.get(parent_name) {
|
if let Some(sub_list) = self.sub_services.get(parent_name) {
|
||||||
@@ -491,49 +453,48 @@ impl ServicesWidget {
|
|||||||
|
|
||||||
for (i, (sub_name, sub_info)) in sorted_subs.iter().enumerate() {
|
for (i, (sub_name, sub_info)) in sorted_subs.iter().enumerate() {
|
||||||
let is_last_sub = i == sorted_subs.len() - 1;
|
let is_last_sub = i == sorted_subs.len() - 1;
|
||||||
let full_sub_name = format!("{}_{}", parent_name, sub_name);
|
|
||||||
// Store sub-service info for custom span rendering
|
// Store sub-service info for custom span rendering
|
||||||
display_lines.push((
|
display_lines.push((
|
||||||
sub_name.clone(),
|
sub_name.clone(),
|
||||||
sub_info.widget_status,
|
sub_info.widget_status,
|
||||||
true,
|
true,
|
||||||
Some((sub_info.clone(), is_last_sub)),
|
Some((sub_info.clone(), is_last_sub)),
|
||||||
full_sub_name, // Raw service name for pending transition lookup
|
|
||||||
)); // true = sub-service, with is_last info
|
)); // true = sub-service, with is_last info
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply scroll offset and render visible lines (same as existing logic)
|
// Show only what fits, with "X more below" if needed
|
||||||
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();
|
||||||
|
|
||||||
// Calculate scroll boundaries
|
// Reserve one line for "X more below" if needed
|
||||||
let max_scroll = if total_lines > available_lines {
|
let lines_for_content = if total_lines > available_lines {
|
||||||
total_lines - available_lines
|
available_lines.saturating_sub(1)
|
||||||
} else {
|
} else {
|
||||||
total_lines.saturating_sub(1)
|
available_lines
|
||||||
};
|
};
|
||||||
let effective_scroll = scroll_offset.min(max_scroll);
|
|
||||||
|
|
||||||
// Get visible lines after scrolling
|
|
||||||
let visible_lines: Vec<_> = display_lines
|
let visible_lines: Vec<_> = display_lines
|
||||||
.iter()
|
.iter()
|
||||||
.skip(effective_scroll)
|
.take(lines_for_content)
|
||||||
.take(available_lines)
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let hidden_below = total_lines.saturating_sub(lines_for_content);
|
||||||
|
|
||||||
let lines_to_show = visible_lines.len();
|
let lines_to_show = visible_lines.len();
|
||||||
|
|
||||||
if lines_to_show > 0 {
|
if lines_to_show > 0 {
|
||||||
|
// Add space for "X more below" message if needed
|
||||||
|
let total_chunks_needed = if hidden_below > 0 { lines_to_show + 1 } else { lines_to_show };
|
||||||
let service_chunks = Layout::default()
|
let service_chunks = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
.constraints(vec![Constraint::Length(1); lines_to_show])
|
.constraints(vec![Constraint::Length(1); total_chunks_needed])
|
||||||
.split(area);
|
.split(area);
|
||||||
|
|
||||||
for (i, (line_text, line_status, is_sub, sub_info, raw_service_name)) in visible_lines.iter().enumerate()
|
for (i, (line_text, line_status, is_sub, sub_info)) in visible_lines.iter().enumerate()
|
||||||
{
|
{
|
||||||
let actual_index = effective_scroll + i; // Real index in the full list
|
let actual_index = i; // Simple index since we're not scrolling
|
||||||
|
|
||||||
// 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 {
|
||||||
@@ -545,41 +506,16 @@ impl ServicesWidget {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut spans = if *is_sub && sub_info.is_some() {
|
let mut spans = if *is_sub && sub_info.is_some() {
|
||||||
// Use custom sub-service span creation WITH pending transitions
|
// Use custom sub-service span creation
|
||||||
let (service_info, is_last) = sub_info.as_ref().unwrap();
|
let (service_info, is_last) = sub_info.as_ref().unwrap();
|
||||||
self.create_sub_service_spans_with_transitions(line_text, service_info, *is_last, pending_transitions)
|
self.create_sub_service_spans(line_text, service_info, *is_last)
|
||||||
} else {
|
} else {
|
||||||
// Parent services - check if this parent service has a pending transition using RAW service name
|
// Parent services - use normal status spans
|
||||||
if pending_transitions.contains_key(raw_service_name) {
|
StatusIcons::create_status_spans(*line_status, line_text)
|
||||||
// Create spans with transitional status
|
|
||||||
let (icon, status_text, _) = self.get_service_icon_and_status(raw_service_name, &ServiceInfo {
|
|
||||||
status: "".to_string(),
|
|
||||||
memory_mb: None,
|
|
||||||
disk_gb: None,
|
|
||||||
latency_ms: None,
|
|
||||||
widget_status: *line_status
|
|
||||||
}, pending_transitions);
|
|
||||||
|
|
||||||
// Use blue for transitional icons when not selected, background color when selected
|
|
||||||
let icon_color = if is_selected && !*is_sub && is_focused {
|
|
||||||
Theme::background() // Dark background color for visibility against blue selection
|
|
||||||
} else {
|
|
||||||
Theme::highlight() // Blue for normal case
|
|
||||||
};
|
|
||||||
|
|
||||||
vec![
|
|
||||||
ratatui::text::Span::styled(format!("{} ", icon), Style::default().fg(icon_color)),
|
|
||||||
ratatui::text::Span::styled(line_text.clone(), Style::default().fg(Theme::primary_text())),
|
|
||||||
ratatui::text::Span::styled(format!(" {}", status_text), Style::default().fg(icon_color)),
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
StatusIcons::create_status_spans(*line_status, line_text)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Apply selection highlighting to parent services only, making icons background color when selected
|
// Apply selection highlighting to parent services only
|
||||||
// Only show selection when Services panel is focused
|
// Only show selection when Services panel is focused
|
||||||
// Show selection highlighting even when transitional icons are present
|
|
||||||
if is_selected && !*is_sub && is_focused {
|
if is_selected && !*is_sub && is_focused {
|
||||||
for (i, span) in spans.iter_mut().enumerate() {
|
for (i, span) in spans.iter_mut().enumerate() {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
@@ -600,33 +536,12 @@ impl ServicesWidget {
|
|||||||
|
|
||||||
frame.render_widget(service_para, service_chunks[i]);
|
frame.render_widget(service_para, service_chunks[i]);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Show scroll indicator if there are more services than we can display (same as existing)
|
|
||||||
if total_lines > available_lines {
|
|
||||||
let hidden_above = effective_scroll;
|
|
||||||
let hidden_below = total_lines.saturating_sub(effective_scroll + available_lines);
|
|
||||||
|
|
||||||
if hidden_above > 0 || hidden_below > 0 {
|
// Show "X more below" message if content was truncated
|
||||||
let scroll_text = if hidden_above > 0 && hidden_below > 0 {
|
if hidden_below > 0 {
|
||||||
format!("... {} above, {} below", hidden_above, hidden_below)
|
let more_text = format!("... {} more below", hidden_below);
|
||||||
} else if hidden_above > 0 {
|
let more_para = Paragraph::new(more_text).style(Typography::muted());
|
||||||
format!("... {} more above", hidden_above)
|
frame.render_widget(more_para, service_chunks[lines_to_show]);
|
||||||
} else {
|
|
||||||
format!("... {} more below", hidden_below)
|
|
||||||
};
|
|
||||||
|
|
||||||
if available_lines > 0 && lines_to_show > 0 {
|
|
||||||
let last_line_area = Rect {
|
|
||||||
x: area.x,
|
|
||||||
y: area.y + (lines_to_show - 1) as u16,
|
|
||||||
width: area.width,
|
|
||||||
height: 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
let scroll_para = Paragraph::new(scroll_text).style(Typography::muted());
|
|
||||||
frame.render_widget(scroll_para, last_line_area);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -438,8 +438,8 @@ impl Widget for SystemWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SystemWidget {
|
impl SystemWidget {
|
||||||
/// Render with scroll offset support
|
/// Render system widget
|
||||||
pub fn render_with_scroll(&mut self, frame: &mut Frame, area: Rect, scroll_offset: usize, hostname: &str) {
|
pub fn render(&mut self, frame: &mut Frame, area: Rect, hostname: &str, config: Option<&crate::config::DashboardConfig>) {
|
||||||
let mut lines = Vec::new();
|
let mut lines = Vec::new();
|
||||||
|
|
||||||
// NixOS section
|
// NixOS section
|
||||||
@@ -457,6 +457,16 @@ impl SystemWidget {
|
|||||||
Span::styled(format!("Agent: {}", agent_version_text), Typography::secondary())
|
Span::styled(format!("Agent: {}", agent_version_text), Typography::secondary())
|
||||||
]));
|
]));
|
||||||
|
|
||||||
|
// Display detected connection IP
|
||||||
|
if let Some(config) = config {
|
||||||
|
if let Some(host_details) = config.hosts.get(hostname) {
|
||||||
|
let detected_ip = host_details.get_connection_ip(hostname);
|
||||||
|
lines.push(Line::from(vec![
|
||||||
|
Span::styled(format!("IP: {}", detected_ip), Typography::secondary())
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// CPU section
|
// CPU section
|
||||||
lines.push(Line::from(vec![
|
lines.push(Line::from(vec![
|
||||||
@@ -550,22 +560,22 @@ 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;
|
||||||
|
|
||||||
// Always apply scrolling if scroll_offset > 0, even if content fits
|
// Show only what fits, with "X more below" if needed
|
||||||
if scroll_offset > 0 || total_lines > available_height {
|
if total_lines > available_height {
|
||||||
let max_scroll = if total_lines > available_height {
|
let lines_for_content = available_height.saturating_sub(1); // Reserve one line for "more below"
|
||||||
total_lines - available_height
|
let mut visible_lines: Vec<Line> = lines
|
||||||
} else {
|
|
||||||
total_lines.saturating_sub(1)
|
|
||||||
};
|
|
||||||
let effective_scroll = scroll_offset.min(max_scroll);
|
|
||||||
|
|
||||||
// Take only the visible portion after scrolling
|
|
||||||
let visible_lines: Vec<Line> = lines
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.skip(effective_scroll)
|
.take(lines_for_content)
|
||||||
.take(available_height)
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let hidden_below = total_lines.saturating_sub(lines_for_content);
|
||||||
|
if hidden_below > 0 {
|
||||||
|
let more_line = Line::from(vec![
|
||||||
|
Span::styled(format!("... {} more below", hidden_below), Typography::muted())
|
||||||
|
]);
|
||||||
|
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 {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.61"
|
version = "0.1.81"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -82,12 +82,13 @@ 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 {
|
||||||
Ok,
|
Inactive, // Lowest priority - treated as good
|
||||||
|
Ok, // Second lowest - also good
|
||||||
|
Unknown,
|
||||||
|
Offline,
|
||||||
Pending,
|
Pending,
|
||||||
Warning,
|
Warning,
|
||||||
Critical,
|
Critical,
|
||||||
Unknown,
|
|
||||||
Offline,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Status {
|
impl Status {
|
||||||
@@ -181,6 +182,16 @@ impl HysteresisThresholds {
|
|||||||
Status::Ok
|
Status::Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Status::Inactive => {
|
||||||
|
// Inactive services use normal thresholds like first measurement
|
||||||
|
if value >= self.critical_high {
|
||||||
|
Status::Critical
|
||||||
|
} else if value >= self.warning_high {
|
||||||
|
Status::Warning
|
||||||
|
} else {
|
||||||
|
Status::Ok
|
||||||
|
}
|
||||||
|
}
|
||||||
Status::Pending => {
|
Status::Pending => {
|
||||||
// Service transitioning, use normal thresholds like first measurement
|
// Service transitioning, use normal thresholds like first measurement
|
||||||
if value >= self.critical_high {
|
if value >= self.critical_high {
|
||||||
|
|||||||
Reference in New Issue
Block a user