From 6d6beb207df130e46314bcadf4c5e7e23c72d569 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Thu, 27 Nov 2025 15:57:38 +0100 Subject: [PATCH] Parse Docker image sizes to MB and sort services alphabetically --- Cargo.lock | 6 ++-- agent/Cargo.toml | 2 +- agent/src/collectors/systemd.rs | 50 ++++++++++++++++++++++++++++----- dashboard/Cargo.toml | 2 +- shared/Cargo.toml | 2 +- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 386c7f1..9c8e0ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,7 +279,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" [[package]] name = "cm-dashboard" -version = "0.1.183" +version = "0.1.184" dependencies = [ "anyhow", "chrono", @@ -301,7 +301,7 @@ dependencies = [ [[package]] name = "cm-dashboard-agent" -version = "0.1.183" +version = "0.1.184" dependencies = [ "anyhow", "async-trait", @@ -324,7 +324,7 @@ dependencies = [ [[package]] name = "cm-dashboard-shared" -version = "0.1.183" +version = "0.1.184" dependencies = [ "chrono", "serde", diff --git a/agent/Cargo.toml b/agent/Cargo.toml index c33e14a..fb2f12f 100644 --- a/agent/Cargo.toml +++ b/agent/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard-agent" -version = "0.1.184" +version = "0.1.185" edition = "2021" [dependencies] diff --git a/agent/src/collectors/systemd.rs b/agent/src/collectors/systemd.rs index 5da3bed..4309bad 100644 --- a/agent/src/collectors/systemd.rs +++ b/agent/src/collectors/systemd.rs @@ -133,16 +133,16 @@ impl SystemdCollector { // Add Docker images let docker_images = self.get_docker_images(); - for (image_name, image_status, image_size) in docker_images { + for (image_name, image_status, image_size_str, image_size_mb) in docker_images { let mut metrics = Vec::new(); metrics.push(SubServiceMetric { label: "size".to_string(), - value: 0.0, // Size as string in name instead - unit: None, + value: image_size_mb, + unit: Some("MB".to_string()), }); sub_services.push(SubServiceData { - name: format!("{} ({})", image_name, image_size), + name: format!("{} ({})", image_name, image_size_str), service_status: self.calculate_service_status(&image_name, &image_status), metrics, }); @@ -169,6 +169,10 @@ impl SystemdCollector { } } + // Sort services alphabetically by name + agent_data.services.sort_by(|a, b| a.name.cmp(&b.name)); + complete_service_data.sort_by(|a, b| a.name.cmp(&b.name)); + // Update cached state { let mut state = self.state.write().unwrap(); @@ -815,7 +819,7 @@ impl SystemdCollector { } /// Get docker images as sub-services - fn get_docker_images(&self) -> Vec<(String, String, String)> { + fn get_docker_images(&self) -> Vec<(String, String, String, f32)> { let mut images = Vec::new(); // Check if docker is available (cm-agent user is in docker group) let output = Command::new("docker") @@ -845,23 +849,55 @@ impl SystemdCollector { let parts: Vec<&str> = line.split(',').collect(); if parts.len() >= 2 { let image_name = parts[0].trim(); - let size = parts[1].trim(); + let size_str = parts[1].trim(); // Skip : images (dangling images) if image_name.contains("") { continue; } + // Parse size to MB (sizes come as "142MB", "1.5GB", "512kB", etc.) + let size_mb = self.parse_docker_size(size_str); + images.push(( format!("image_{}", image_name), "active".to_string(), // Images are always "active" (present) - size.to_string() + size_str.to_string(), + size_mb )); } } images } + + /// Parse Docker size string to MB + fn parse_docker_size(&self, size_str: &str) -> f32 { + let size_upper = size_str.to_uppercase(); + + // Extract numeric part and unit + let mut num_str = String::new(); + let mut unit = String::new(); + + for ch in size_upper.chars() { + if ch.is_ascii_digit() || ch == '.' { + num_str.push(ch); + } else if ch.is_alphabetic() { + unit.push(ch); + } + } + + let value: f32 = num_str.parse().unwrap_or(0.0); + + // Convert to MB + match unit.as_str() { + "KB" | "K" => value / 1024.0, + "MB" | "M" => value, + "GB" | "G" => value * 1024.0, + "TB" | "T" => value * 1024.0 * 1024.0, + _ => value, // Assume bytes if no unit + } + } } #[async_trait] diff --git a/dashboard/Cargo.toml b/dashboard/Cargo.toml index be8cfee..494a115 100644 --- a/dashboard/Cargo.toml +++ b/dashboard/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard" -version = "0.1.184" +version = "0.1.185" edition = "2021" [dependencies] diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 742e246..6681447 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard-shared" -version = "0.1.184" +version = "0.1.185" edition = "2021" [dependencies]