11use crate :: network:: state:: NodeType ;
22use common_utils:: PacketTypeHeader ;
3+ use log:: error;
34use serde:: { Deserialize , Serialize } ;
45use std:: collections:: HashMap ;
56use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
67use wg_2024:: network:: NodeId ;
78use wg_2024:: packet:: { Packet , PacketType } ;
89
10+ const ROLLING_WINDOW_SIZE : usize = 100 ;
11+
912// =============================================================================
1013#[ derive( Debug , Clone , Copy , PartialEq , Serialize , Deserialize , Eq , Hash ) ]
1114pub enum PacketTypeLabel {
@@ -55,6 +58,8 @@ pub struct DroneMetrics {
5558
5659 pub current_pdr : f32 ,
5760
61+ pub rolling_window : Vec < bool > ,
62+
5863 /// Number of shortcuts used by the drone
5964 pub shortcuts : u64 ,
6065
@@ -77,15 +82,28 @@ impl DroneMetrics {
7782 }
7883
7984 pub fn update_pdr ( & mut self , successful : bool ) {
80- self . current_pdr = self . current_pdr * 0.99 + if successful { 0.00 } else { 0.1 } ;
85+ // Add success/failure to rolling window
86+ self . rolling_window . push ( successful) ;
87+ if self . rolling_window . len ( ) > ROLLING_WINDOW_SIZE {
88+ self . rolling_window . remove ( 0 ) ;
89+ }
90+
91+ // Compute PDR based on recent `ROLLING_WINDOW_SIZE` packets
92+ let failed = self . rolling_window . iter ( ) . filter ( |& & s| !s) . count ( ) as f32 ;
93+ self . current_pdr = if self . rolling_window . is_empty ( ) {
94+ 0.0
95+ } else {
96+ failed / self . rolling_window . len ( ) as f32
97+ } ;
8198
8299 let sent = self . number_of_msg_fragments_sent ( ) ;
83100 let dropped = self . drops ;
84-
85101 let timestamp = SystemTime :: now ( )
86102 . duration_since ( UNIX_EPOCH )
87103 . map ( |d| d. as_secs ( ) )
88104 . unwrap_or_default ( ) ;
105+
106+ // Maintain rolling window for time series
89107 self . time_series . push ( MetricsTimePoint {
90108 timestamp,
91109 sent,
@@ -109,6 +127,13 @@ impl DroneMetrics {
109127// 5. Statistiche per gli host (client/server)
110128// =============================================================================
111129
130+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
131+ pub struct HostMetricsTimePoint {
132+ pub timestamp : u64 ,
133+ pub sent : u64 ,
134+ pub acked : u64 ,
135+ }
136+
112137#[ derive( Debug , Default ) ]
113138pub struct HostMetrics {
114139 /// For each destination: (sent, acked)
@@ -120,20 +145,40 @@ pub struct HostMetrics {
120145 /// Latency for each message sent by the host. It could be use to compute number of Message sent
121146 pub latencies : Vec < Duration > ,
122147 /// Time series for the number of packets sent and dropped
123- pub time_series : Vec < MetricsTimePoint > ,
148+ pub time_series : Vec < HostMetricsTimePoint > ,
124149}
125150
126151impl HostMetrics {
127152 pub fn record_packet ( & mut self , dest : NodeId , packet_type : PacketTypeLabel ) {
128153 let entry = self . dest_stats . entry ( dest) . or_insert ( ( 0 , 0 ) ) ;
129154 entry. 0 += 1 ;
130155 * self . packet_type_counts . entry ( packet_type) . or_insert ( 0 ) += 1 ;
156+
157+ if packet_type == PacketTypeLabel :: MsgFragment {
158+ self . update_time_series ( ) ;
159+ }
131160 }
132161
133162 /// Record an ack received by the host from another host.
134163 pub fn record_ack ( & mut self , src : NodeId ) {
135164 let entry = self . dest_stats . entry ( src) . or_insert ( ( 0 , 0 ) ) ;
136165 entry. 1 += 1 ;
166+
167+ self . update_time_series ( ) ;
168+ }
169+
170+ fn update_time_series ( & mut self ) {
171+ let timestamp = SystemTime :: now ( )
172+ . duration_since ( UNIX_EPOCH )
173+ . map ( |d| d. as_secs ( ) )
174+ . unwrap_or_default ( ) ;
175+ let sent = self . dest_stats . values ( ) . map ( |( s, _) | s) . sum ( ) ;
176+ let acked = self . dest_stats . values ( ) . map ( |( _, a) | a) . sum ( ) ;
177+ self . time_series . push ( HostMetricsTimePoint {
178+ timestamp,
179+ sent,
180+ acked,
181+ } ) ;
137182 }
138183
139184 /// Record a shortcut used by the host.
@@ -252,6 +297,12 @@ impl Metrics {
252297 host_metrics. record_packet ( destination, packet_type) ;
253298 }
254299 }
300+
301+ if let Some ( current_hop) = packet_header. routing_header . current_hop ( ) {
302+ if matches ! ( packet_header. pack_type, PacketTypeHeader :: MsgFragment ) {
303+ self . update_global_heatmap ( node_id, current_hop) ;
304+ }
305+ }
255306 }
256307
257308 /// Update the global heatmap with a packet sent from `src` to `dest`.
0 commit comments