Skip to content

Commit

Permalink
feat: [torrust#469] add update datetime for tracker stasts importation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
josecelano committed Mar 13, 2024
1 parent 1769bf1 commit feffd09
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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";
5 changes: 3 additions & 2 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(|_| ())
Expand Down
5 changes: 3 additions & 2 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(|_| ())
Expand Down
10 changes: 10 additions & 0 deletions src/utils/clock.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use chrono::Utc;

/// Returns the current timestamp in seconds.
///
/// # Panics
Expand All @@ -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()
}

0 comments on commit feffd09

Please sign in to comment.