From feffd09a03c80620cd78cff6f0f6d6deba0bc700 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 12 Mar 2024 15:59:59 +0000 Subject: [PATCH] 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. --- ...130530_torrust_add_update_data_to_tracker_stats.sql | 4 ++++ ...130530_torrust_add_update_data_to_tracker_stats.sql | 2 ++ src/databases/mysql.rs | 5 +++-- src/databases/sqlite.rs | 5 +++-- src/utils/clock.rs | 10 ++++++++++ 5 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 migrations/mysql/20240312130530_torrust_add_update_data_to_tracker_stats.sql create mode 100644 migrations/sqlite3/20240312130530_torrust_add_update_data_to_tracker_stats.sql diff --git a/migrations/mysql/20240312130530_torrust_add_update_data_to_tracker_stats.sql b/migrations/mysql/20240312130530_torrust_add_update_data_to_tracker_stats.sql new file mode 100644 index 00000000..76b306de --- /dev/null +++ b/migrations/mysql/20240312130530_torrust_add_update_data_to_tracker_stats.sql @@ -0,0 +1,4 @@ +-- New field to track when stats were updated from the tracker +ALTER TABLE torrust_torrent_tracker_stats ADD COLUMN updated_at DATETIME DEFAULT NULL; +UPDATE torrust_torrent_tracker_stats SET updated_at = '1000-01-01 00:00:00'; +ALTER TABLE torrust_torrent_tracker_stats MODIFY COLUMN updated_at DATETIME NOT NULL; \ No newline at end of file diff --git a/migrations/sqlite3/20240312130530_torrust_add_update_data_to_tracker_stats.sql b/migrations/sqlite3/20240312130530_torrust_add_update_data_to_tracker_stats.sql new file mode 100644 index 00000000..b376d945 --- /dev/null +++ b/migrations/sqlite3/20240312130530_torrust_add_update_data_to_tracker_stats.sql @@ -0,0 +1,2 @@ +-- New field to track when stats were updated from the tracker +ALTER TABLE torrust_torrent_tracker_stats ADD COLUMN updated_at TEXT DEFAULT "1000-01-01 00:00:00"; diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index b964a1e8..5ee723cd 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -20,7 +20,7 @@ use crate::models::torrent_tag::{TagId, TorrentTag}; use crate::models::tracker_key::TrackerKey; use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile}; use crate::services::torrent::{CanonicalInfoHashGroup, DbTorrentInfoHash}; -use crate::utils::clock; +use crate::utils::clock::{self, datetime_now}; use crate::utils::hex::from_bytes; pub struct Mysql { @@ -1055,11 +1055,12 @@ impl Database for Mysql { seeders: i64, leechers: i64, ) -> Result<(), database::Error> { - query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers) VALUES (?, ?, ?, ?)") + query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers, updated_at) VALUES (?, ?, ?, ?, ?)") .bind(torrent_id) .bind(tracker_url) .bind(seeders) .bind(leechers) + .bind(datetime_now()) .execute(&self.pool) .await .map(|_| ()) diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index 18b799f3..26d90eea 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -20,7 +20,7 @@ use crate::models::torrent_tag::{TagId, TorrentTag}; use crate::models::tracker_key::TrackerKey; use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile}; use crate::services::torrent::{CanonicalInfoHashGroup, DbTorrentInfoHash}; -use crate::utils::clock; +use crate::utils::clock::{self, datetime_now}; use crate::utils::hex::from_bytes; pub struct Sqlite { @@ -1047,11 +1047,12 @@ impl Database for Sqlite { seeders: i64, leechers: i64, ) -> Result<(), database::Error> { - query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers) VALUES ($1, $2, $3, $4)") + query("REPLACE INTO torrust_torrent_tracker_stats (torrent_id, tracker_url, seeders, leechers, updated_at) VALUES ($1, $2, $3, $4, $5)") .bind(torrent_id) .bind(tracker_url) .bind(seeders) .bind(leechers) + .bind(datetime_now()) .execute(&self.pool) .await .map(|_| ()) diff --git a/src/utils/clock.rs b/src/utils/clock.rs index b17ee48b..338a5a61 100644 --- a/src/utils/clock.rs +++ b/src/utils/clock.rs @@ -1,3 +1,5 @@ +use chrono::Utc; + /// Returns the current timestamp in seconds. /// /// # Panics @@ -8,3 +10,11 @@ pub fn now() -> u64 { u64::try_from(chrono::prelude::Utc::now().timestamp()).expect("timestamp should be positive") } + +/// Returns the current time in database format. +/// +/// For example: `2024-03-12 15:56:24`. +#[must_use] +pub fn datetime_now() -> String { + Utc::now().format("%Y-%m-%d %H:%M:%S").to_string() +}