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}
13
14/// Acoustic metrics
15#[derive(Debug, Clone)]
16pub struct AcousticMetrics {
17    pub nodes_discovered: u32,
18    pub messages_sent: u64,
19    pub messages_received: u64,
20    pub delivery_rate: f64,
21    pub latency: Duration,
22    pub throughput: f64,
23    pub packet_loss_rate: f64,
24}
25
26/// BLE metrics
27#[derive(Debug, Clone)]
28pub struct BleMetrics {
29    pub nodes_discovered: u32,
30    pub connections_established: u32,
31    pub messages_sent: u64,
32    pub messages_received: u64,
33    pub delivery_rate: f64,
34    pub latency: Duration,
35    pub throughput: f64,
36}
37
38/// Routing metrics
39#[derive(Debug, Clone)]
40pub struct RoutingMetrics {
41    pub routes_discovered: u32,
42    pub route_discovery_time: Duration,
43    pub forwarding_efficiency: f64,
44    pub congestion_events: u32,
45    pub route_optimizations: u32,
46}
47
48/// Global metrics
49#[derive(Debug, Clone)]
50pub struct MeshGlobalMetrics {
51    pub total_nodes: u32,
52    pub total_messages: u64,
53    pub network_uptime: Duration,
54    pub average_latency: Duration,
55    pub overall_throughput: f64,
56    pub reliability: f64,
57}
58
59impl MeshPerformanceMonitor {
60    pub fn new() -> Self {
61        Self {
62            acoustic_metrics: AcousticMetrics::new(),
63            ble_metrics: BleMetrics::new(),
64            routing_metrics: RoutingMetrics::new(),
65            global_metrics: MeshGlobalMetrics::new(),
66        }
67    }
68
69    pub fn update_receive_metrics(&mut self, message: &StoredMessage) {
70        self.global_metrics.total_messages += 1;
71        // Route to the appropriate metrics based on message source.
72        if message.source.starts_with("acoustic_") {
73            self.acoustic_metrics.messages_received += 1;
74        } else {
75            self.ble_metrics.messages_received += 1;
76        }
77    }
78
79    pub fn acoustic_metrics(&self) -> &AcousticMetrics {
80        &self.acoustic_metrics
81    }
82
83    pub fn ble_metrics(&self) -> &BleMetrics {
84        &self.ble_metrics
85    }
86
87    pub fn routing_metrics(&self) -> &RoutingMetrics {
88        &self.routing_metrics
89    }
90
91    pub fn get_uptime(&self) -> Duration {
92        Duration::from_secs(3600) // 1 hour uptime (dummy)
93    }
94
95    pub fn get_global_stats(&self) -> MeshGlobalMetrics {
96        self.global_metrics.clone()
97    }
98}
99
100impl AcousticMetrics {
101    pub fn new() -> Self {
102        Self {
103            nodes_discovered: 0,
104            messages_sent: 0,
105            messages_received: 0,
106            delivery_rate: 0.0,
107            latency: Duration::from_millis(0),
108            throughput: 0.0,
109            packet_loss_rate: 0.0,
110        }
111    }
112}
113
114impl BleMetrics {
115    pub fn new() -> Self {
116        Self {
117            nodes_discovered: 0,
118            connections_established: 0,
119            messages_sent: 0,
120            messages_received: 0,
121            delivery_rate: 0.0,
122            latency: Duration::from_millis(0),
123            throughput: 0.0,
124        }
125    }
126}
127
128impl RoutingMetrics {
129    pub fn new() -> Self {
130        Self {
131            routes_discovered: 0,
132            route_discovery_time: Duration::from_millis(0),
133            forwarding_efficiency: 0.0,
134            congestion_events: 0,
135            route_optimizations: 0,
136        }
137    }
138}
139
140impl MeshGlobalMetrics {
141    pub fn new() -> Self {
142        Self {
143            total_nodes: 0,
144            total_messages: 0,
145            network_uptime: Duration::from_secs(0),
146            average_latency: Duration::from_millis(0),
147            overall_throughput: 0.0,
148            reliability: 0.0,
149        }
150    }
151}