From 3c1194a02ac285eb87d402935d76411fd552e432 Mon Sep 17 00:00:00 2001 From: Tom Tang <4220945+shiba4life@users.noreply.github.com> Date: Wed, 13 May 2026 08:16:46 +0800 Subject: [PATCH] refactor(fold_db_core): dedupe 4 SystemTime now() blocks via now_secs() helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Four identical SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() chains across event_monitor.rs (2 sites) and event_statistics.rs (2 sites) collapsed into a single pub(super) now_secs() helper defined in event_statistics.rs and reused from event_monitor.rs. Behavior-preserving: helper uses .unwrap() so panic-on-clock-before-epoch semantics are unchanged at all four call sites. Same dedupe pattern as #705 for progress.rs, but .unwrap() preserved (not .unwrap_or_default()) because that's what these files already used. event_monitor.rs no longer needs std::time::{SystemTime, UNIX_EPOCH} — import dropped. --- crates/core/src/fold_db_core/event_monitor.rs | 13 +++---------- .../core/src/fold_db_core/event_statistics.rs | 17 +++++++++-------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/crates/core/src/fold_db_core/event_monitor.rs b/crates/core/src/fold_db_core/event_monitor.rs index 338f56af6..554c3a5e7 100644 --- a/crates/core/src/fold_db_core/event_monitor.rs +++ b/crates/core/src/fold_db_core/event_monitor.rs @@ -5,10 +5,10 @@ //! with a single component that can see all system activity. use std::sync::{Arc, Mutex}; -use std::time::{SystemTime, UNIX_EPOCH}; use tracing::info; +use super::event_statistics::now_secs; pub use super::event_statistics::{EventStatistics, MutationStats, QueryStats}; use crate::messaging::{AsyncMessageBus, Event}; @@ -21,10 +21,7 @@ impl EventMonitor { /// Create a new EventMonitor that subscribes to all event types pub async fn new(message_bus: Arc) -> Self { let statistics = Arc::new(Mutex::new(EventStatistics { - monitoring_start_time: SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs(), + monitoring_start_time: now_secs(), ..Default::default() })); @@ -124,11 +121,7 @@ impl EventMonitor { /// Log a summary of all activity since monitoring started pub fn log_summary(&self) { let stats = self.get_statistics(); - let runtime = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs() - - stats.monitoring_start_time; + let runtime = now_secs() - stats.monitoring_start_time; info!("📊 EventMonitor Summary ({}s runtime):", runtime); info!(" 📝 Field Value Sets: {}", stats.field_value_sets); diff --git a/crates/core/src/fold_db_core/event_statistics.rs b/crates/core/src/fold_db_core/event_statistics.rs index 94e53e0b8..20fb6c799 100644 --- a/crates/core/src/fold_db_core/event_statistics.rs +++ b/crates/core/src/fold_db_core/event_statistics.rs @@ -4,6 +4,13 @@ use std::time::{SystemTime, UNIX_EPOCH}; +pub(super) fn now_secs() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() +} + /// Statistics about system activity tracked by the event monitor #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct EventStatistics { @@ -77,10 +84,7 @@ impl EventStatistics { stats.total_execution_time_ms as f64 / stats.executions as f64; stats.total_results += result_count; stats.avg_result_count = stats.total_results as f64 / stats.executions as f64; - stats.last_execution_time = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs(); + stats.last_execution_time = now_secs(); } pub fn increment_mutation_executions( @@ -102,9 +106,6 @@ impl EventStatistics { stats.total_execution_time_ms as f64 / stats.executions as f64; stats.total_fields_affected += fields_affected; stats.avg_fields_affected = stats.total_fields_affected as f64 / stats.executions as f64; - stats.last_execution_time = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs(); + stats.last_execution_time = now_secs(); } }