11use std:: sync:: Arc ;
22
3+ use bittorrent_tracker_core:: statistics:: TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL ;
34use bittorrent_tracker_core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
45use bittorrent_udp_tracker_core:: services:: banning:: BanService ;
56use bittorrent_udp_tracker_core:: { self } ;
67use tokio:: sync:: RwLock ;
8+ use torrust_tracker_configuration:: Core ;
9+ use torrust_tracker_metrics:: label:: LabelSet ;
710use torrust_tracker_metrics:: metric_collection:: MetricCollection ;
11+ use torrust_tracker_metrics:: metric_name;
812use torrust_tracker_primitives:: swarm_metadata:: AggregateActiveSwarmMetadata ;
13+ use torrust_tracker_torrent_repository:: statistics:: TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL ;
914use torrust_udp_tracker_server:: statistics as udp_server_statistics;
1015
1116use crate :: statistics:: metrics:: Metrics ;
@@ -27,16 +32,50 @@ pub struct TrackerMetrics {
2732/// It returns all the [`TrackerMetrics`]
2833#[ allow( deprecated) ]
2934pub async fn get_metrics (
35+ core_config : Arc < Core > ,
3036 in_memory_torrent_repository : Arc < InMemoryTorrentRepository > ,
3137 ban_service : Arc < RwLock < BanService > > ,
38+ torrent_repository_stats_repository : Arc < torrust_tracker_torrent_repository:: statistics:: repository:: Repository > ,
39+ tracker_core_stats_repository : Arc < bittorrent_tracker_core:: statistics:: repository:: Repository > ,
3240 http_stats_repository : Arc < bittorrent_http_tracker_core:: statistics:: repository:: Repository > ,
3341 udp_server_stats_repository : Arc < udp_server_statistics:: repository:: Repository > ,
3442) -> TrackerMetrics {
35- let torrents_metrics = in_memory_torrent_repository. get_aggregate_swarm_metadata ( ) . await ;
43+ let aggregate_active_swarm_metadata = in_memory_torrent_repository. get_aggregate_swarm_metadata ( ) . await ;
3644 let udp_banned_ips_total = ban_service. read ( ) . await . get_banned_ips_total ( ) ;
3745 let http_stats = http_stats_repository. get_stats ( ) . await ;
3846 let udp_server_stats = udp_server_stats_repository. get_stats ( ) . await ;
3947
48+ let total_downloaded = if core_config. tracker_policy . persistent_torrent_completed_stat {
49+ let metrics = tracker_core_stats_repository. get_metrics ( ) . await ;
50+
51+ let downloads = metrics. metric_collection . get_counter_value (
52+ & metric_name ! ( TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL ) ,
53+ & LabelSet :: default ( ) ,
54+ ) ;
55+
56+ if let Some ( downloads) = downloads {
57+ downloads. value ( )
58+ } else {
59+ 0
60+ }
61+ } else {
62+ let metrics = torrent_repository_stats_repository. get_metrics ( ) . await ;
63+
64+ let downloads = metrics. metric_collection . get_counter_value (
65+ & metric_name ! ( TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL ) ,
66+ & LabelSet :: default ( ) ,
67+ ) ;
68+
69+ if let Some ( downloads) = downloads {
70+ downloads. value ( )
71+ } else {
72+ 0
73+ }
74+ } ;
75+
76+ let mut torrents_metrics = aggregate_active_swarm_metadata;
77+ torrents_metrics. total_downloaded = total_downloaded;
78+
4079 // For backward compatibility we keep the `tcp4_connections_handled` and
4180 // `tcp6_connections_handled` metrics. They don't make sense for the HTTP
4281 // tracker, but we keep them for now. In new major versions we should remove
@@ -138,14 +177,16 @@ mod tests {
138177 use bittorrent_http_tracker_core:: event:: sender:: Broadcaster ;
139178 use bittorrent_http_tracker_core:: statistics:: event:: listener:: run_event_listener;
140179 use bittorrent_http_tracker_core:: statistics:: repository:: Repository ;
141- use bittorrent_tracker_core:: torrent :: repository :: in_memory :: InMemoryTorrentRepository ;
180+ use bittorrent_tracker_core:: container :: TrackerCoreContainer ;
142181 use bittorrent_tracker_core:: { self } ;
143182 use bittorrent_udp_tracker_core:: services:: banning:: BanService ;
144183 use bittorrent_udp_tracker_core:: MAX_CONNECTION_ID_ERRORS_PER_IP ;
145184 use tokio:: sync:: RwLock ;
146185 use torrust_tracker_configuration:: Configuration ;
186+ use torrust_tracker_events:: bus:: SenderStatus ;
147187 use torrust_tracker_primitives:: swarm_metadata:: AggregateActiveSwarmMetadata ;
148188 use torrust_tracker_test_helpers:: configuration;
189+ use torrust_tracker_torrent_repository:: container:: TorrentRepositoryContainer ;
149190
150191 use crate :: statistics:: metrics:: Metrics ;
151192 use crate :: statistics:: services:: { get_metrics, TrackerMetrics } ;
@@ -157,8 +198,12 @@ mod tests {
157198 #[ tokio:: test]
158199 async fn the_statistics_service_should_return_the_tracker_metrics ( ) {
159200 let config = tracker_configuration ( ) ;
201+ let core_config = Arc :: new ( config. core . clone ( ) ) ;
202+
203+ let torrent_repository_container = Arc :: new ( TorrentRepositoryContainer :: initialize ( SenderStatus :: Enabled ) ) ;
204+
205+ let tracker_core_container = TrackerCoreContainer :: initialize_from ( & core_config, & torrent_repository_container. clone ( ) ) ;
160206
161- let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
162207 let ban_service = Arc :: new ( RwLock :: new ( BanService :: new ( MAX_CONNECTION_ID_ERRORS_PER_IP ) ) ) ;
163208
164209 // HTTP core stats
@@ -177,8 +222,11 @@ mod tests {
177222 let udp_server_stats_repository = Arc :: new ( torrust_udp_tracker_server:: statistics:: repository:: Repository :: new ( ) ) ;
178223
179224 let tracker_metrics = get_metrics (
180- in_memory_torrent_repository. clone ( ) ,
225+ core_config,
226+ tracker_core_container. in_memory_torrent_repository . clone ( ) ,
181227 ban_service. clone ( ) ,
228+ torrent_repository_container. stats_repository . clone ( ) ,
229+ tracker_core_container. stats_repository . clone ( ) ,
182230 http_stats_repository. clone ( ) ,
183231 udp_server_stats_repository. clone ( ) ,
184232 )
0 commit comments