From 43242debcea0dcfe6e8540a91b589a9bd2e03002 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Tue, 28 Oct 2025 12:16:31 +0100 Subject: [PATCH] Update version to 0.1.21 and fix dashboard data caching - Separate dashboard updates from email notifications for immediate status aggregation - Add metric caching to MetricCollectionManager for instant dashboard updates - Dashboard now receives cached data every 1 second instead of waiting for collection intervals - Fix transmission to use cached metrics rather than triggering fresh collection - Email notifications maintain separate 60-second batching interval - Update configurable email notification aggregation interval --- Cargo.lock | 6 +++--- agent/Cargo.toml | 2 +- agent/src/agent.rs | 18 ++++++++++-------- agent/src/config/mod.rs | 2 ++ agent/src/metrics/mod.rs | 22 ++++++++++++++++++++++ dashboard/Cargo.toml | 2 +- dashboard/src/main.rs | 2 +- shared/Cargo.toml | 2 +- 8 files changed, 41 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0bdc604..d5ddabf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -270,7 +270,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" [[package]] name = "cm-dashboard" -version = "0.1.19" +version = "0.1.20" dependencies = [ "anyhow", "chrono", @@ -291,7 +291,7 @@ dependencies = [ [[package]] name = "cm-dashboard-agent" -version = "0.1.19" +version = "0.1.20" dependencies = [ "anyhow", "async-trait", @@ -314,7 +314,7 @@ dependencies = [ [[package]] name = "cm-dashboard-shared" -version = "0.1.19" +version = "0.1.20" dependencies = [ "chrono", "serde", diff --git a/agent/Cargo.toml b/agent/Cargo.toml index b5273e4..13678cd 100644 --- a/agent/Cargo.toml +++ b/agent/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard-agent" -version = "0.1.20" +version = "0.1.21" edition = "2021" [dependencies] diff --git a/agent/src/agent.rs b/agent/src/agent.rs index 04d3fbc..9235463 100644 --- a/agent/src/agent.rs +++ b/agent/src/agent.rs @@ -71,10 +71,11 @@ impl Agent { info!("Initial metric collection completed - all data cached and ready"); } - // Separate intervals for collection and transmission + // Separate intervals for collection, transmission, and email notifications let mut collection_interval = 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 notification_interval = interval(Duration::from_secs(self.config.notifications.aggregation_interval_seconds)); loop { tokio::select! { @@ -85,12 +86,13 @@ impl Agent { } } _ = transmission_interval.tick() => { - // Send all metrics via ZMQ and process notifications immediately + // Send all metrics via ZMQ (dashboard updates only) if let Err(e) = self.broadcast_all_metrics().await { error!("Failed to broadcast metrics: {}", e); } - - // Process notifications immediately with each transmission + } + _ = notification_interval.tick() => { + // Process batched email notifications (separate from dashboard updates) if let Err(e) = self.host_status_manager.process_pending_notifications(&mut self.notification_manager).await { error!("Failed to process pending notifications: {}", e); } @@ -158,10 +160,10 @@ impl Agent { } async fn broadcast_all_metrics(&mut self) -> Result<()> { - debug!("Broadcasting all metrics via ZMQ"); + debug!("Broadcasting cached metrics via ZMQ"); - // Get all current metrics from collectors - let mut metrics = self.metric_manager.collect_all_metrics().await?; + // Get cached metrics (no fresh collection) + let mut metrics = self.metric_manager.get_cached_metrics(); // Add the host status summary metric from status manager let host_status_metric = self.host_status_manager.get_host_status_metric(); @@ -176,7 +178,7 @@ impl Agent { return Ok(()); } - debug!("Broadcasting {} metrics (including host status summary)", metrics.len()); + debug!("Broadcasting {} cached metrics (including host status summary)", metrics.len()); // Create and send message with all current data let message = MetricMessage::new(self.hostname.clone(), metrics); diff --git a/agent/src/config/mod.rs b/agent/src/config/mod.rs index 56d4e97..ed56f87 100644 --- a/agent/src/config/mod.rs +++ b/agent/src/config/mod.rs @@ -143,6 +143,8 @@ pub struct NotificationConfig { pub from_email: String, pub to_email: String, pub rate_limit_minutes: u64, + /// Email notification batching interval in seconds (default: 60) + pub aggregation_interval_seconds: u64, } diff --git a/agent/src/metrics/mod.rs b/agent/src/metrics/mod.rs index ebb219b..e94cf08 100644 --- a/agent/src/metrics/mod.rs +++ b/agent/src/metrics/mod.rs @@ -21,6 +21,7 @@ struct TimedCollector { pub struct MetricCollectionManager { collectors: Vec, status_tracker: StatusTracker, + cached_metrics: Vec, } impl MetricCollectionManager { @@ -177,6 +178,7 @@ impl MetricCollectionManager { Ok(Self { collectors, status_tracker: StatusTracker::new(), + cached_metrics: Vec::new(), }) } @@ -198,6 +200,9 @@ impl MetricCollectionManager { } } } + + // Cache the collected metrics + self.cached_metrics = all_metrics.clone(); Ok(all_metrics) } @@ -231,6 +236,18 @@ impl MetricCollectionManager { } } } + + // Update cache with newly collected metrics + if !all_metrics.is_empty() { + // Merge new metrics with cached metrics (replace by name) + for new_metric in &all_metrics { + // Remove any existing metric with the same name + self.cached_metrics.retain(|cached| cached.name != new_metric.name); + // Add the new metric + self.cached_metrics.push(new_metric.clone()); + } + } + Ok(all_metrics) } @@ -238,5 +255,10 @@ impl MetricCollectionManager { pub async fn collect_all_metrics(&mut self) -> Result> { self.collect_metrics_timed().await } + + /// Get cached metrics without triggering fresh collection + pub fn get_cached_metrics(&self) -> Vec { + self.cached_metrics.clone() + } } diff --git a/dashboard/Cargo.toml b/dashboard/Cargo.toml index 8ce6949..7acd532 100644 --- a/dashboard/Cargo.toml +++ b/dashboard/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard" -version = "0.1.20" +version = "0.1.21" edition = "2021" [dependencies] diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index adfe80f..a497d87 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -14,7 +14,7 @@ use app::Dashboard; /// Get hardcoded version fn get_version() -> &'static str { - "v0.1.20" + "v0.1.21" } /// Check if running inside tmux session diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 9890e42..67be62f 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard-shared" -version = "0.1.20" +version = "0.1.21" edition = "2021" [dependencies]