diff --git a/Cargo.lock b/Cargo.lock index ccdf7f8..615740f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,7 +279,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" [[package]] name = "cm-dashboard" -version = "0.1.229" +version = "0.1.230" dependencies = [ "anyhow", "chrono", @@ -301,7 +301,7 @@ dependencies = [ [[package]] name = "cm-dashboard-agent" -version = "0.1.229" +version = "0.1.230" dependencies = [ "anyhow", "async-trait", @@ -325,7 +325,7 @@ dependencies = [ [[package]] name = "cm-dashboard-shared" -version = "0.1.229" +version = "0.1.230" dependencies = [ "chrono", "serde", diff --git a/agent/Cargo.toml b/agent/Cargo.toml index 1ee89f7..dc0bdee 100644 --- a/agent/Cargo.toml +++ b/agent/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard-agent" -version = "0.1.229" +version = "0.1.230" edition = "2021" [dependencies] diff --git a/agent/src/agent.rs b/agent/src/agent.rs index d57d607..bafa6ff 100644 --- a/agent/src/agent.rs +++ b/agent/src/agent.rs @@ -34,6 +34,7 @@ pub struct Agent { collectors: Vec, notification_manager: NotificationManager, previous_status: Option, + cached_agent_data: AgentData, } /// Track system component status for change detection @@ -143,6 +144,9 @@ impl Agent { let notification_manager = NotificationManager::new(&config.notifications, &hostname)?; info!("Notification manager initialized"); + // Initialize cached agent data + let cached_agent_data = AgentData::new(hostname.clone(), env!("CARGO_PKG_VERSION").to_string()); + Ok(Self { hostname, config, @@ -150,6 +154,7 @@ impl Agent { collectors, notification_manager, previous_status: None, + cached_agent_data, }) } @@ -199,10 +204,8 @@ impl Agent { async fn collect_and_broadcast(&mut self) -> Result<()> { debug!("Starting structured data collection"); - // Initialize empty AgentData - let mut agent_data = AgentData::new(self.hostname.clone(), env!("CARGO_PKG_VERSION").to_string()); - // Collect data from collectors whose intervals have elapsed + // Update cached_agent_data with new data let now = Instant::now(); for timed_collector in &mut self.collectors { let should_collect = match timed_collector.last_collection { @@ -211,7 +214,7 @@ impl Agent { }; if should_collect { - if let Err(e) = timed_collector.collector.collect_structured(&mut agent_data).await { + if let Err(e) = timed_collector.collector.collect_structured(&mut self.cached_agent_data).await { error!("Collector {} failed: {}", timed_collector.name, e); // Update last_collection time even on failure to prevent immediate retries timed_collector.last_collection = Some(now); @@ -226,13 +229,22 @@ impl Agent { } } + // Update timestamp on cached data + self.cached_agent_data.timestamp = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + + // Clone for notification check (to avoid borrow issues) + let agent_data_snapshot = self.cached_agent_data.clone(); + // Check for status changes and send notifications - if let Err(e) = self.check_status_changes_and_notify(&agent_data).await { + if let Err(e) = self.check_status_changes_and_notify(&agent_data_snapshot).await { error!("Failed to check status changes: {}", e); } - // Broadcast the structured data via ZMQ - if let Err(e) = self.zmq_handler.publish_agent_data(&agent_data).await { + // Broadcast the cached structured data via ZMQ + if let Err(e) = self.zmq_handler.publish_agent_data(&agent_data_snapshot).await { error!("Failed to broadcast agent data: {}", e); } else { debug!("Successfully broadcast structured agent data"); diff --git a/dashboard/Cargo.toml b/dashboard/Cargo.toml index 4b6aae2..a9b75ff 100644 --- a/dashboard/Cargo.toml +++ b/dashboard/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard" -version = "0.1.229" +version = "0.1.230" edition = "2021" [dependencies] diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 31354d4..924589c 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cm-dashboard-shared" -version = "0.1.229" +version = "0.1.230" edition = "2021" [dependencies]