diff --git a/crates/core/src/fold_db_core/event_monitor.rs b/crates/core/src/fold_db_core/event_monitor.rs index 338f56af..554c3a5e 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 94e53e0b..20fb6c79 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(); } }