Update services widget memory display and formatting

Improve services widget to show consistent usage/total format for both
RAM and Disk columns, using system totals when no service quotas exist.

Changes:
- Change column header from "Memory (GB)" to "RAM (GB)"
- Remove "GB" units from memory values (units now in header)
- Add system memory total detection from /proc/meminfo
- Use system memory total as default quota for services without limits
- Services now show "5.2/32.0" format for both RAM and disk

Both RAM and Disk columns now consistently display usage/quota format
where quota is either service-specific limit or system total capacity.
This commit is contained in:
Christoffer Martinsson 2025-10-14 10:23:16 +02:00
parent 630d2ff674
commit 4be1223a8d
2 changed files with 35 additions and 5 deletions

View File

@ -286,6 +286,30 @@ impl ServiceCollector {
}
async fn get_system_memory_total(&self) -> Result<f32, CollectorError> {
// Read /proc/meminfo to get total system memory
let meminfo = fs::read_to_string("/proc/meminfo")
.await
.map_err(|e| CollectorError::IoError {
message: e.to_string(),
})?;
for line in meminfo.lines() {
if let Some(mem_total_line) = line.strip_prefix("MemTotal:") {
let parts: Vec<&str> = mem_total_line.trim().split_whitespace().collect();
if let Some(mem_kb_str) = parts.first() {
if let Ok(mem_kb) = mem_kb_str.parse::<f32>() {
return Ok(mem_kb / 1024.0); // Convert KB to MB
}
}
}
}
Err(CollectorError::ParseError {
message: "Could not parse total memory".to_string(),
})
}
async fn get_disk_usage(&self) -> Result<DiskUsage, CollectorError> {
let output = Command::new("/run/current-system/sw/bin/df")
.args(["-BG", "--output=size,used,avail", "/"])
@ -1185,12 +1209,18 @@ impl Collector for ServiceCollector {
used_gb: 0.0,
});
// Set disk quota to system total capacity for services that don't have specific quotas
// Get system memory total for services without memory quotas
let system_memory_total_mb = self.get_system_memory_total().await.unwrap_or(8192.0); // Default 8GB
// Set quotas to system totals for services that don't have specific quotas
let system_disk_capacity_gb = disk_usage.total_capacity_gb;
for service in &mut services {
if service.disk_quota_gb == 0.0 {
service.disk_quota_gb = system_disk_capacity_gb;
}
if service.memory_quota_mb == 0.0 {
service.memory_quota_mb = system_memory_total_mb;
}
}
// Calculate overall services status

View File

@ -50,7 +50,7 @@ fn render_metrics(
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()]
vec!["Service".to_string(), "RAM (GB)".to_string(), "CPU".to_string(), "Disk".to_string()]
);
@ -142,11 +142,11 @@ fn format_memory_value(used: f32, quota: f32) -> String {
let quota_gb = quota / 1000.0;
if quota > 0.05 {
format!("{:.1}/{:.1} GB", used_gb, quota_gb)
format!("{:.1}/{:.1}", used_gb, quota_gb)
} else if used > 0.05 {
format!("{:.1} GB", used_gb)
format!("{:.1}", used_gb)
} else {
"0.0 GB".to_string()
"0.0".to_string()
}
}