@@ -8,10 +8,11 @@ use torrust_tracker_primitives::DurationSinceUnixEpoch;
88use crate :: event:: Event ;
99use crate :: statistics:: repository:: Repository ;
1010use crate :: statistics:: {
11- SWARM_COORDINATION_REGISTRY_PEERS_ADDED_TOTAL , SWARM_COORDINATION_REGISTRY_PEERS_REMOVED_TOTAL ,
12- SWARM_COORDINATION_REGISTRY_PEERS_UPDATED_TOTAL , SWARM_COORDINATION_REGISTRY_PEER_CONNECTIONS_TOTAL ,
13- SWARM_COORDINATION_REGISTRY_TORRENTS_ADDED_TOTAL , SWARM_COORDINATION_REGISTRY_TORRENTS_DOWNLOADS_TOTAL ,
14- SWARM_COORDINATION_REGISTRY_TORRENTS_REMOVED_TOTAL , SWARM_COORDINATION_REGISTRY_TORRENTS_TOTAL ,
11+ SWARM_COORDINATION_REGISTRY_PEERS_ADDED_TOTAL , SWARM_COORDINATION_REGISTRY_PEERS_COMPLETED_STATE_REVERTED_TOTAL ,
12+ SWARM_COORDINATION_REGISTRY_PEERS_REMOVED_TOTAL , SWARM_COORDINATION_REGISTRY_PEERS_UPDATED_TOTAL ,
13+ SWARM_COORDINATION_REGISTRY_PEER_CONNECTIONS_TOTAL , SWARM_COORDINATION_REGISTRY_TORRENTS_ADDED_TOTAL ,
14+ SWARM_COORDINATION_REGISTRY_TORRENTS_DOWNLOADS_TOTAL , SWARM_COORDINATION_REGISTRY_TORRENTS_REMOVED_TOTAL ,
15+ SWARM_COORDINATION_REGISTRY_TORRENTS_TOTAL ,
1516} ;
1617
1718#[ allow( clippy:: too_many_lines) ]
@@ -103,6 +104,8 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
103104 } => {
104105 tracing:: debug!( info_hash = ?info_hash, old_peer = ?old_peer, new_peer = ?new_peer, "Peer updated" , ) ;
105106
107+ // If the peer's role has changed, we need to adjust the number of
108+ // connections
106109 if old_peer. role ( ) != new_peer. role ( ) {
107110 let _unused = stats_repository
108111 . increment_gauge (
@@ -121,6 +124,20 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
121124 . await ;
122125 }
123126
127+ // If the peer reverted from a completed state to any other state,
128+ // we need to increment the counter for reverted completed.
129+ if old_peer. is_completed ( ) && !new_peer. is_completed ( ) {
130+ let _unused = stats_repository
131+ . increment_counter (
132+ & metric_name ! ( SWARM_COORDINATION_REGISTRY_PEERS_COMPLETED_STATE_REVERTED_TOTAL ) ,
133+ & LabelSet :: default ( ) ,
134+ now,
135+ )
136+ . await ;
137+ }
138+
139+ // Regardless of the role change, we still need to increment the
140+ // counter for updated peers.
124141 let label_set = label_set_for_peer ( & new_peer) ;
125142
126143 let _unused = stats_repository
@@ -134,7 +151,7 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
134151 Event :: PeerDownloadCompleted { info_hash, peer } => {
135152 tracing:: debug!( info_hash = ?info_hash, peer = ?peer, "Peer download completed" , ) ;
136153
137- let _unused = stats_repository
154+ let _unused: Result < ( ) , torrust_tracker_metrics :: metric_collection :: Error > = stats_repository
138155 . increment_counter (
139156 & metric_name ! ( SWARM_COORDINATION_REGISTRY_TORRENTS_DOWNLOADS_TOTAL ) ,
140157 & label_set_for_peer ( & peer) ,
0 commit comments