@@ -11,26 +11,28 @@ pub struct NetworkHistory {
1111 pub last_tx_bytes : u64 ,
1212 pub max_history : usize ,
1313 pub current_interface : String ,
14+ pub counter_wrapped : bool ,
1415}
1516
1617impl NetworkHistory {
1718 pub fn new ( ) -> Self {
1819 Self {
19- rx_history : VecDeque :: new ( ) ,
20- tx_history : VecDeque :: new ( ) ,
21- rx_rates : VecDeque :: new ( ) ,
22- tx_rates : VecDeque :: new ( ) ,
20+ rx_history : VecDeque :: with_capacity ( NETWORK_HISTORY_SIZE ) ,
21+ tx_history : VecDeque :: with_capacity ( NETWORK_HISTORY_SIZE ) ,
22+ rx_rates : VecDeque :: with_capacity ( NETWORK_HISTORY_SIZE ) ,
23+ tx_rates : VecDeque :: with_capacity ( NETWORK_HISTORY_SIZE ) ,
2324 last_rx_bytes : 0 ,
2425 last_tx_bytes : 0 ,
25- max_history : NETWORK_HISTORY_SIZE , // Keep 60 data points (2 minutes at 2-second intervals)
26+ max_history : NETWORK_HISTORY_SIZE ,
2627 current_interface : String :: new ( ) ,
28+ counter_wrapped : false ,
2729 }
2830 }
2931
3032 pub fn update ( & mut self , networks : & Networks , selected_interface : & str ) {
3133 // Find the selected network interface or use the first available one
32- let network_list: Vec < _ > = networks. list ( ) . iter ( ) . take ( 100 ) . collect ( ) ; // Limit to 100 network interfaces
33- let ( interface_name, network_data) = if let Some ( item) = network_list. get ( 0 ) {
34+ let network_list: Vec < _ > = networks. list ( ) . iter ( ) . take ( 100 ) . collect ( ) ;
35+ let ( interface_name, network_data) = if let Some ( item) = network_list. first ( ) {
3436 // If we have a specific interface selected, try to find it
3537 if !selected_interface. is_empty ( ) {
3638 network_list. iter ( )
@@ -49,20 +51,17 @@ impl NetworkHistory {
4951 let total_rx = network_data. total_received ( ) ;
5052 let total_tx = network_data. total_transmitted ( ) ;
5153
52- if self . last_rx_bytes > 0 && self . last_tx_bytes > 0 {
53- // Calculate rate (bytes per 2 seconds) with overflow detection
54- let rx_rate = if total_rx >= self . last_rx_bytes {
55- total_rx. saturating_sub ( self . last_rx_bytes )
56- } else {
57- // Network counter reset or overflow - skip this measurement
58- 0
59- } ;
60- let tx_rate = if total_tx >= self . last_tx_bytes {
61- total_tx. saturating_sub ( self . last_tx_bytes )
62- } else {
63- // Network counter reset or overflow - skip this measurement
64- 0
65- } ;
54+ // Detect counter wraparound or interface reset
55+ if total_rx < self . last_rx_bytes || total_tx < self . last_tx_bytes {
56+ self . counter_wrapped = true ;
57+ self . last_rx_bytes = total_rx;
58+ self . last_tx_bytes = total_tx;
59+ return ; // Skip this measurement entirely
60+ }
61+
62+ if self . last_rx_bytes > 0 && self . last_tx_bytes > 0 && !self . counter_wrapped {
63+ let rx_rate = total_rx. saturating_sub ( self . last_rx_bytes ) ;
64+ let tx_rate = total_tx. saturating_sub ( self . last_tx_bytes ) ;
6665
6766 self . rx_rates . push_back ( rx_rate) ;
6867 self . tx_rates . push_back ( tx_rate) ;
@@ -87,5 +86,16 @@ impl NetworkHistory {
8786
8887 self . last_rx_bytes = total_rx;
8988 self . last_tx_bytes = total_tx;
89+ self . counter_wrapped = false ;
90+ }
91+
92+ pub fn clear ( & mut self ) {
93+ self . rx_history . clear ( ) ;
94+ self . tx_history . clear ( ) ;
95+ self . rx_rates . clear ( ) ;
96+ self . tx_rates . clear ( ) ;
97+ self . last_rx_bytes = 0 ;
98+ self . last_tx_bytes = 0 ;
99+ self . counter_wrapped = false ;
90100 }
91101}
0 commit comments