Skip to main content

qualia_core_db/net/acoustic_ble_mesh/
metrics.rs

1//! Mesh performance monitoring: per-interface (acoustic / BLE), routing, and
2//! global network metrics.
3
4use super::*;
5
6/// Mesh performance monitor
7pub struct MeshPerformanceMonitor {
8    acoustic_metrics: AcousticMetrics,
9    ble_metrics: BleMetrics,
10    routing_metrics: RoutingMetrics,
11    global_metrics: MeshGlobalMetrics,
12    start_time: std::time::Instant,
13}
14
15/// Acoustic metrics
16#[derive(Debug, Clone)]
17pub struct AcousticMetrics {
18    pub nodes_discovered: u32,
19    pub messages_sent: u64,
20    pub messages_received: u64,
21    pub delivery_rate: f64,
22    pub latency: Duration,
23    pub throughput: f64,
24    pub packet_loss_rate: f64,
25}
26
27/// BLE metrics
28#[derive(Debug, Clone)]
29pub struct BleMetrics {
30    pub nodes_discovered: u32,
31    pub connections_established: u32,
32    pub messages_sent: u64,
33    pub messages_received: u64,
34    pub delivery_rate: f64,
35    pub latency: Duration,
36    pub throughput: f64,
37}
38
39/// Routing metrics
40#[derive(Debug, Clone)]
41pub struct RoutingMetrics {
42    pub routes_discovered: u32,
43    pub route_discovery_time: Duration,
44    pub forwarding_efficiency: f64,
45    pub congestion_events: u32,
46    pub route_optimizations: u32,
47}
48
49/// Global metrics
50#[derive(Debug, Clone)]
51pub struct MeshGlobalMetrics {
52    pub total_nodes: u32,
53    pub total_messages: u64,
54    pub network_uptime: Duration,
55    pub average_latency: Duration,
56    pub overall_throughput: f64,
57    pub reliability: f64,
58}
59
60impl MeshPerformanceMonitor {
61    /// Creates a monitor with zeroed per-domain metrics and a fresh start time.
62    pub fn new() -> Self {
63        Self {
64            acoustic_metrics: AcousticMetrics::new(),
65            ble_metrics: BleMetrics::new(),
66            routing_metrics: RoutingMetrics::new(),
67            global_metrics: MeshGlobalMetrics::new(),
68            start_time: std::time::Instant::now(),
69        }
70    }
71
72    /// Records one received message — latency, hop count, route stats.
73    pub fn update_receive_metrics(&mut self, message: &StoredMessage) {
74        self.global_metrics.total_messages += 1;
75        // Route to the appropriate metrics based on message source.
76        if message.source.starts_with("acoustic_") {
77            self.acoustic_metrics.messages_received += 1;
78        } else {
79            self.ble_metrics.messages_received += 1;
80        }
81    }
82
83    /// Acoustic-channel metrics.
84    pub fn acoustic_metrics(&self) -> &AcousticMetrics {
85        &self.acoustic_metrics
86    }
87
88    /// BLE-channel metrics.
89    pub fn ble_metrics(&self) -> &BleMetrics {
90        &self.ble_metrics
91    }
92
93    /// Routing metrics.
94    pub fn routing_metrics(&self) -> &RoutingMetrics {
95        &self.routing_metrics
96    }
97
98    /// Elapsed time since monitor creation.
99    pub fn get_uptime(&self) -> Duration {
100        self.start_time.elapsed()
101    }
102
103    /// Aggregated cross-channel metrics snapshot.
104    pub fn get_global_stats(&self) -> MeshGlobalMetrics {
105        let mut stats = self.global_metrics.clone();
106        stats.network_uptime = self.get_uptime();
107        stats
108    }
109}
110
111impl AcousticMetrics {
112    /// Creates zeroed acoustic metrics.
113    pub fn new() -> Self {
114        Self {
115            nodes_discovered: 0,
116            messages_sent: 0,
117            messages_received: 0,
118            delivery_rate: 0.0,
119            latency: Duration::from_millis(0),
120            throughput: 0.0,
121            packet_loss_rate: 0.0,
122        }
123    }
124}
125
126impl BleMetrics {
127    /// Creates zeroed BLE metrics.
128    pub fn new() -> Self {
129        Self {
130            nodes_discovered: 0,
131            connections_established: 0,
132            messages_sent: 0,
133            messages_received: 0,
134            delivery_rate: 0.0,
135            latency: Duration::from_millis(0),
136            throughput: 0.0,
137        }
138    }
139}
140
141impl RoutingMetrics {
142    /// Creates zeroed routing metrics.
143    pub fn new() -> Self {
144        Self {
145            routes_discovered: 0,
146            route_discovery_time: Duration::from_millis(0),
147            forwarding_efficiency: 0.0,
148            congestion_events: 0,
149            route_optimizations: 0,
150        }
151    }
152}
153
154impl MeshGlobalMetrics {
155    /// Creates zeroed global metrics.
156    pub fn new() -> Self {
157        Self {
158            total_nodes: 0,
159            total_messages: 0,
160            network_uptime: Duration::from_secs(0),
161            average_latency: Duration::from_millis(0),
162            overall_throughput: 0.0,
163            reliability: 0.0,
164        }
165    }
166}