Skip to content

Commit feffd09

Browse files
committed
feat: [#469] add update datetime for tracker stasts importation
Add a new column to the table `torrust_torrent_tracker_stats` with the datetime of the update date, which means the date when the stats (leechers and seeders) were synchronized from the tracker API.
1 parent 1769bf1 commit feffd09

5 files changed

+22
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- New field to track when stats were updated from the tracker
2+
ALTER TABLE torrust_torrent_tracker_stats ADD COLUMN updated_at DATETIME DEFAULT NULL;
3+
UPDATE torrust_torrent_tracker_stats SET updated_at = '1000-01-01 00:00:00';
4+
ALTER TABLE torrust_torrent_tracker_stats MODIFY COLUMN updated_at DATETIME NOT NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- New field to track when stats were updated from the tracker
2+
ALTER TABLE torrust_torrent_tracker_stats ADD COLUMN updated_at TEXT DEFAULT "1000-01-01 00:00:00";

src/databases/mysql.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::models::torrent_tag::{TagId, TorrentTag};
2020
use crate::models::tracker_key::TrackerKey;
2121
use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile};
2222
use crate::services::torrent::{CanonicalInfoHashGroup, DbTorrentInfoHash};
23-
use crate::utils::clock;
23+
use crate::utils::clock::{self, datetime_now};
2424
use crate::utils::hex::from_bytes;
2525

2626
pub struct Mysql {
@@ -1055,11 +1055,12 @@ impl Database for Mysql {
10551055
seeders: i64,
10561056
leechers: i64,
10571057
) -> Result<(), database::Error> {
1058-
query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers) VALUES (?, ?, ?, ?)")
1058+
query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers, updated_at) VALUES (?, ?, ?, ?, ?)")
10591059
.bind(torrent_id)
10601060
.bind(tracker_url)
10611061
.bind(seeders)
10621062
.bind(leechers)
1063+
.bind(datetime_now())
10631064
.execute(&self.pool)
10641065
.await
10651066
.map(|_| ())

src/databases/sqlite.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::models::torrent_tag::{TagId, TorrentTag};
2020
use crate::models::tracker_key::TrackerKey;
2121
use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile};
2222
use crate::services::torrent::{CanonicalInfoHashGroup, DbTorrentInfoHash};
23-
use crate::utils::clock;
23+
use crate::utils::clock::{self, datetime_now};
2424
use crate::utils::hex::from_bytes;
2525

2626
pub struct Sqlite {
@@ -1047,11 +1047,12 @@ impl Database for Sqlite {
10471047
seeders: i64,
10481048
leechers: i64,
10491049
) -> Result<(), database::Error> {
1050-
query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers) VALUES ($1, $2, $3, $4)")
1050+
query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers, updated_at) VALUES ($1, $2, $3, $4, $5)")
10511051
.bind(torrent_id)
10521052
.bind(tracker_url)
10531053
.bind(seeders)
10541054
.bind(leechers)
1055+
.bind(datetime_now())
10551056
.execute(&self.pool)
10561057
.await
10571058
.map(|_| ())

src/utils/clock.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use chrono::Utc;
2+
13
/// Returns the current timestamp in seconds.
24
///
35
/// # Panics
@@ -8,3 +10,11 @@
810
pub fn now() -> u64 {
911
u64::try_from(chrono::prelude::Utc::now().timestamp()).expect("timestamp should be positive")
1012
}
13+
14+
/// Returns the current time in database format.
15+
///
16+
/// For example: `2024-03-12 15:56:24`.
17+
#[must_use]
18+
pub fn datetime_now() -> String {
19+
Utc::now().format("%Y-%m-%d %H:%M:%S").to_string()
20+
}

0 commit comments

Comments
 (0)