Skip to content

Commit 9301e58

Browse files
committed
feat: [#1539] save global downloads counter in DB
The total number of dowloads (for all torrents) is saved in the DB, but not loaded yet. todo: load the initial value when the tracker starts.
1 parent 6f11534 commit 9301e58

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

packages/tracker-core/src/statistics/event/handler.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ pub async fn handle_event(
4141
Event::PeerDownloadCompleted { info_hash, peer } => {
4242
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer download completed", );
4343

44-
// Increment the number of downloads for the torrent
45-
match db_torrent_repository.increase_number_of_downloads(&info_hash) {
46-
Ok(()) => {
47-
tracing::debug!(info_hash = ?info_hash, "Number of downloads increased");
48-
}
49-
Err(err) => {
50-
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads");
51-
}
52-
}
53-
54-
// Increment the number of downloads for all the torrents
44+
// Increment the number of downloads for all the torrents in memory
5545
let _unused = stats_repository
5646
.increment_counter(
5747
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
@@ -60,9 +50,25 @@ pub async fn handle_event(
6050
)
6151
.await;
6252

63-
// todo:
64-
// - Persist the metric into the database.
65-
// - Load the metric from the database.
53+
// Increment the number of downloads for the torrent in the database
54+
match db_torrent_repository.increase_number_of_downloads(&info_hash) {
55+
Ok(()) => {
56+
tracing::debug!(info_hash = ?info_hash, "Number of torrent downloads increased");
57+
}
58+
Err(err) => {
59+
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads for the torrent");
60+
}
61+
}
62+
63+
// Increment the global number of downloads (for all torrents) in the database
64+
match db_torrent_repository.increase_global_number_of_downloads() {
65+
Ok(()) => {
66+
tracing::debug!("Global number of downloads increased");
67+
}
68+
Err(err) => {
69+
tracing::error!(error = ?err, "Failed to increase global number of downloads");
70+
}
71+
}
6672
}
6773
}
6874
}

packages/tracker-core/src/torrent/repository/persisted.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ impl DatabasePersistentTorrentRepository {
6767
}
6868
}
6969

70+
/// Increases the global number of downloads for all torrent.
71+
///
72+
/// If the metric is not found, it creates it.
73+
///
74+
/// # Errors
75+
///
76+
/// Returns an [`Error`] if the database operation fails.
77+
pub(crate) fn increase_global_number_of_downloads(&self) -> Result<(), Error> {
78+
let torrent = self.database.load_global_number_of_downloads()?;
79+
80+
match torrent {
81+
Some(_number_of_downloads) => self.database.increase_global_number_of_downloads(),
82+
None => self.database.save_global_number_of_downloads(1),
83+
}
84+
}
85+
7086
/// Loads all persistent torrent metrics from the database.
7187
///
7288
/// This function retrieves the torrent metrics (e.g., download counts) from the persistent store

0 commit comments

Comments
 (0)