|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use log::{error, info}; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +use super::api::{Client, ConnectionInfo}; |
| 7 | +use crate::config::Configuration; |
| 8 | +use crate::databases::database::{Database, DatabaseError}; |
| 9 | +use crate::errors::ServiceError; |
| 10 | + |
| 11 | +// If `TorrentInfo` struct is used in the future for other purposes, it should |
| 12 | +// be moved to a separate file. Maybe a `ClientWrapper` struct which returns |
| 13 | +// `TorrentInfo` and `TrackerKey` structs instead of `Response` structs. |
| 14 | + |
| 15 | +#[derive(Debug, Serialize, Deserialize)] |
| 16 | +pub struct TorrentInfo { |
| 17 | + pub info_hash: String, |
| 18 | + pub seeders: i64, |
| 19 | + pub completed: i64, |
| 20 | + pub leechers: i64, |
| 21 | + pub peers: Vec<Peer>, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Serialize, Deserialize)] |
| 25 | +pub struct Peer { |
| 26 | + pub peer_id: Option<PeerId>, |
| 27 | + pub peer_addr: Option<String>, |
| 28 | + pub updated: Option<i64>, |
| 29 | + pub uploaded: Option<i64>, |
| 30 | + pub downloaded: Option<i64>, |
| 31 | + pub left: Option<i64>, |
| 32 | + pub event: Option<String>, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Serialize, Deserialize)] |
| 36 | +pub struct PeerId { |
| 37 | + pub id: Option<String>, |
| 38 | + pub client: Option<String>, |
| 39 | +} |
| 40 | + |
| 41 | +pub struct StatisticsImporter { |
| 42 | + database: Arc<Box<dyn Database>>, |
| 43 | + api_client: Client, |
| 44 | + tracker_url: String, |
| 45 | +} |
| 46 | + |
| 47 | +impl StatisticsImporter { |
| 48 | + pub async fn new(cfg: Arc<Configuration>, database: Arc<Box<dyn Database>>) -> Self { |
| 49 | + let settings = cfg.settings.read().await; |
| 50 | + let api_client = Client::new(ConnectionInfo::new( |
| 51 | + settings.tracker.api_url.clone(), |
| 52 | + settings.tracker.token.clone(), |
| 53 | + )); |
| 54 | + let tracker_url = settings.tracker.url.clone(); |
| 55 | + drop(settings); |
| 56 | + Self { |
| 57 | + database, |
| 58 | + api_client, |
| 59 | + tracker_url, |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Import torrents statistics from tracker and update them in database. |
| 64 | + /// |
| 65 | + /// # Errors |
| 66 | + /// |
| 67 | + /// Will return an error if the database query failed. |
| 68 | + pub async fn import_all_torrents_statistics(&self) -> Result<(), DatabaseError> { |
| 69 | + info!("Importing torrents statistics from tracker ..."); |
| 70 | + let torrents = self.database.get_all_torrents_compact().await?; |
| 71 | + |
| 72 | + for torrent in torrents { |
| 73 | + info!("Updating torrent {} ...", torrent.torrent_id); |
| 74 | + |
| 75 | + let ret = self.import_torrent_statistics(torrent.torrent_id, &torrent.info_hash).await; |
| 76 | + |
| 77 | + // code-review: should we treat differently for each case?. The |
| 78 | + // tracker API could be temporarily offline, or there could be a |
| 79 | + // tracker misconfiguration. |
| 80 | + // |
| 81 | + // This is the log when the torrent is not found in the tracker: |
| 82 | + // |
| 83 | + // ``` |
| 84 | + // 2023-05-09T13:31:24.497465723+00:00 [torrust_index_backend::tracker::statistics_importer][ERROR] Error updating torrent tracker stats for torrent with id 140: TorrentNotFound |
| 85 | + // ``` |
| 86 | + |
| 87 | + if let Some(err) = ret.err() { |
| 88 | + error!( |
| 89 | + "Error updating torrent tracker stats for torrent with id {}: {:?}", |
| 90 | + torrent.torrent_id, err |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | + |
| 98 | + /// Import torrent statistics from tracker and update them in database. |
| 99 | + /// |
| 100 | + /// # Errors |
| 101 | + /// |
| 102 | + /// Will return an error if the HTTP request failed or the torrent is not |
| 103 | + /// found. |
| 104 | + pub async fn import_torrent_statistics(&self, torrent_id: i64, info_hash: &str) -> Result<TorrentInfo, ServiceError> { |
| 105 | + let response = self |
| 106 | + .api_client |
| 107 | + .get_torrent_info(info_hash) |
| 108 | + .await |
| 109 | + .map_err(|_| ServiceError::InternalServerError)?; |
| 110 | + |
| 111 | + if let Ok(torrent_info) = response.json::<TorrentInfo>().await { |
| 112 | + let _ = self |
| 113 | + .database |
| 114 | + .update_tracker_info(torrent_id, &self.tracker_url, torrent_info.seeders, torrent_info.leechers) |
| 115 | + .await; |
| 116 | + Ok(torrent_info) |
| 117 | + } else { |
| 118 | + let _ = self.database.update_tracker_info(torrent_id, &self.tracker_url, 0, 0).await; |
| 119 | + Err(ServiceError::TorrentNotFound) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments