mod_events/
metrics.rs

1//! Event dispatch metrics and monitoring
2
3use crate::Event;
4use std::any::TypeId;
5use std::time::Instant;
6
7/// Event metadata for debugging and monitoring
8///
9/// Contains information about event dispatch history and performance.
10#[derive(Debug, Clone)]
11pub struct EventMetadata {
12    /// The name of the event type
13    pub event_name: &'static str,
14    /// Type ID of the event
15    pub type_id: TypeId,
16    /// Timestamp of the last dispatch
17    pub last_dispatch: Instant,
18    /// Total number of times this event has been dispatched
19    pub dispatch_count: usize,
20    /// Number of listeners currently subscribed to this event
21    pub listener_count: usize,
22}
23
24impl EventMetadata {
25    pub(crate) fn new<T: Event>() -> Self {
26        Self {
27            event_name: std::any::type_name::<T>(),
28            type_id: TypeId::of::<T>(),
29            last_dispatch: Instant::now(),
30            dispatch_count: 0,
31            listener_count: 0,
32        }
33    }
34
35    pub(crate) fn increment_dispatch(&mut self) {
36        self.dispatch_count += 1;
37        self.last_dispatch = Instant::now();
38    }
39
40    pub(crate) fn update_listener_count(&mut self, count: usize) {
41        self.listener_count = count;
42    }
43
44    /// Get the time since the last dispatch
45    pub fn time_since_last_dispatch(&self) -> std::time::Duration {
46        self.last_dispatch.elapsed()
47    }
48}