151 lines
4.4 KiB
Rust
151 lines
4.4 KiB
Rust
use ratatui::layout::Rect;
|
|
use ratatui::Frame;
|
|
|
|
use crate::app::HostDisplayData;
|
|
use crate::data::metrics::ServiceStatus;
|
|
use crate::ui::widget::{render_placeholder, render_widget_data, status_level_from_agent_status, connection_status_message, WidgetData, WidgetStatus, StatusLevel};
|
|
use crate::app::ConnectionStatus;
|
|
|
|
pub fn render(frame: &mut Frame, host: Option<&HostDisplayData>, area: Rect) {
|
|
match host {
|
|
Some(data) => {
|
|
match (&data.connection_status, data.services.as_ref()) {
|
|
(ConnectionStatus::Connected, Some(metrics)) => {
|
|
render_metrics(frame, data, metrics, area);
|
|
}
|
|
(ConnectionStatus::Connected, None) => {
|
|
render_placeholder(
|
|
frame,
|
|
area,
|
|
"Services",
|
|
&format!("Host {} has no service metrics yet", data.name),
|
|
);
|
|
}
|
|
(status, _) => {
|
|
render_placeholder(
|
|
frame,
|
|
area,
|
|
"Services",
|
|
&format!("Host {}: {}", data.name, connection_status_message(status, &data.last_error)),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
None => render_placeholder(frame, area, "Services", "No hosts configured"),
|
|
}
|
|
}
|
|
|
|
fn render_metrics(
|
|
frame: &mut Frame,
|
|
_host: &HostDisplayData,
|
|
metrics: &crate::data::metrics::ServiceMetrics,
|
|
area: Rect,
|
|
) {
|
|
let summary = &metrics.summary;
|
|
let title = "Services".to_string();
|
|
|
|
// Use agent-calculated services status
|
|
let widget_status = status_level_from_agent_status(summary.services_status.as_ref());
|
|
|
|
let mut data = WidgetData::new(
|
|
title,
|
|
Some(WidgetStatus::new(widget_status)),
|
|
vec!["Service".to_string(), "Memory (GB)".to_string(), "CPU".to_string(), "Disk".to_string()]
|
|
);
|
|
|
|
|
|
if metrics.services.is_empty() {
|
|
data.add_row(
|
|
None,
|
|
vec![],
|
|
vec![
|
|
"No services reported".to_string(),
|
|
"".to_string(),
|
|
"".to_string(),
|
|
"".to_string(),
|
|
],
|
|
);
|
|
render_widget_data(frame, area, data);
|
|
return;
|
|
}
|
|
|
|
let mut services = metrics.services.clone();
|
|
services.sort_by(|a, b| {
|
|
status_weight(&a.status)
|
|
.cmp(&status_weight(&b.status))
|
|
.then_with(|| a.name.cmp(&b.name))
|
|
});
|
|
|
|
for svc in services {
|
|
let status_level = match svc.status {
|
|
ServiceStatus::Running => StatusLevel::Ok,
|
|
ServiceStatus::Degraded => StatusLevel::Warning,
|
|
ServiceStatus::Restarting => StatusLevel::Warning,
|
|
ServiceStatus::Stopped => StatusLevel::Error,
|
|
};
|
|
|
|
// Service row with optional description(s)
|
|
let description = if let Some(desc_vec) = &svc.description {
|
|
desc_vec.clone()
|
|
} else {
|
|
vec![]
|
|
};
|
|
|
|
data.add_row(
|
|
Some(WidgetStatus::new(status_level)),
|
|
description,
|
|
vec![
|
|
svc.name.clone(),
|
|
format_memory_value(svc.memory_used_mb, svc.memory_quota_mb),
|
|
format_cpu_value(svc.cpu_percent),
|
|
format_disk_value(svc.disk_used_gb),
|
|
],
|
|
);
|
|
}
|
|
|
|
render_widget_data(frame, area, data);
|
|
}
|
|
|
|
fn status_weight(status: &ServiceStatus) -> i32 {
|
|
match status {
|
|
ServiceStatus::Stopped => 0,
|
|
ServiceStatus::Degraded => 1,
|
|
ServiceStatus::Restarting => 2,
|
|
ServiceStatus::Running => 3,
|
|
}
|
|
}
|
|
|
|
|
|
fn format_memory_value(used: f32, quota: f32) -> String {
|
|
let used_gb = used / 1000.0;
|
|
let quota_gb = quota / 1000.0;
|
|
|
|
if quota > 0.05 {
|
|
format!("{:.1}/{:.1} GB", used_gb, quota_gb)
|
|
} else if used > 0.05 {
|
|
format!("{:.1} GB", used_gb)
|
|
} else {
|
|
"0.0 GB".to_string()
|
|
}
|
|
}
|
|
|
|
fn format_cpu_value(cpu_percent: f32) -> String {
|
|
if cpu_percent >= 0.1 {
|
|
format!("{:.1}%", cpu_percent)
|
|
} else {
|
|
"0.0%".to_string()
|
|
}
|
|
}
|
|
|
|
fn format_disk_value(used: f32) -> String {
|
|
if used >= 1.0 {
|
|
format!("{:.1} GB", used)
|
|
} else if used >= 0.001 {
|
|
// 1 MB or more
|
|
format!("{:.0} MB", used * 1000.0)
|
|
} else {
|
|
"<1 MB".to_string()
|
|
}
|
|
}
|
|
|