Skip to content

Commit 3c40e67

Browse files
committed
feat: [#56] console command to import tracker stats for all torrents
1 parent 364c115 commit 3c40e67

File tree

6 files changed

+111
-2
lines changed

6 files changed

+111
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Import Tracker Statistics command.
2+
//! It imports the number of seeders and leechers for all torrent from the linked tracker.
3+
//! You can execute it with: `cargo run --bin import_tracker_statistics`
4+
5+
use torrust_index_backend::console::commands::import_tracker_statistics::run_importer;
6+
7+
#[actix_web::main]
8+
async fn main() {
9+
run_importer().await;
10+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! It imports statistics for all torrents from the linked tracker.
2+
3+
use std::env;
4+
use std::sync::Arc;
5+
6+
use derive_more::{Display, Error};
7+
use text_colorizer::*;
8+
9+
use crate::config::Configuration;
10+
use crate::databases::database::connect_database;
11+
use crate::tracker::TrackerService;
12+
13+
const NUMBER_OF_ARGUMENTS: usize = 0;
14+
15+
#[derive(Debug)]
16+
pub struct Arguments {}
17+
18+
#[derive(Debug, Display, PartialEq, Error)]
19+
#[allow(dead_code)]
20+
pub enum ImportError {
21+
#[display(fmt = "internal server error")]
22+
WrongNumberOfArgumentsError,
23+
}
24+
25+
fn parse_args() -> Result<Arguments, ImportError> {
26+
let args: Vec<String> = env::args().skip(1).collect();
27+
28+
if args.len() != NUMBER_OF_ARGUMENTS {
29+
eprintln!(
30+
"{} wrong number of arguments: expected {}, got {}",
31+
"Error".red().bold(),
32+
NUMBER_OF_ARGUMENTS,
33+
args.len()
34+
);
35+
print_usage();
36+
return Err(ImportError::WrongNumberOfArgumentsError);
37+
}
38+
39+
Ok(Arguments {})
40+
}
41+
42+
fn print_usage() {
43+
eprintln!(
44+
"{} - imports torrents statistics from linked tracker.
45+
46+
cargo run --bin upgrade SOURCE_DB_FILE DESTINY_DB_FILE TORRENT_UPLOAD_DIR
47+
48+
For example:
49+
50+
cargo run --bin import_tracker_statistics
51+
52+
",
53+
"Upgrader".green()
54+
);
55+
}
56+
57+
pub async fn run_importer() {
58+
import(&parse_args().unwrap()).await;
59+
}
60+
61+
pub async fn import(_args: &Arguments) {
62+
println!("Importing statistics from linked tracker ...");
63+
64+
let cfg = match Configuration::load_from_file().await {
65+
Ok(config) => Arc::new(config),
66+
Err(error) => {
67+
panic!("{}", error)
68+
}
69+
};
70+
71+
let settings = cfg.settings.read().await;
72+
73+
let tracker_url = settings.tracker.url.clone();
74+
75+
eprintln!("Tracker url: {}", tracker_url.green());
76+
77+
let database = Arc::new(
78+
connect_database(&settings.database.connect_url)
79+
.await
80+
.expect("Database error."),
81+
);
82+
83+
let tracker_service = Arc::new(TrackerService::new(cfg.clone(), database.clone()));
84+
85+
tracker_service.update_torrents().await.unwrap();
86+
}

src/console/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod import_tracker_statistics;

src/console/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod commands;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod auth;
22
pub mod common;
33
pub mod config;
4+
pub mod console;
45
pub mod databases;
56
pub mod errors;
67
pub mod mailer;

src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::torrent_transferrer::t
2323
use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::tracker_key_transferrer::transfer_tracker_keys;
2424
use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::user_transferrer::transfer_users;
2525

26-
const NUMBER_OF_ARGUMENTS: i64 = 3;
26+
const NUMBER_OF_ARGUMENTS: usize = 3;
2727

2828
#[derive(Debug)]
2929
pub struct Arguments {
@@ -50,7 +50,7 @@ fn print_usage() {
5050
fn parse_args() -> Arguments {
5151
let args: Vec<String> = env::args().skip(1).collect();
5252

53-
if args.len() != 3 {
53+
if args.len() != NUMBER_OF_ARGUMENTS {
5454
eprintln!(
5555
"{} wrong number of arguments: expected {}, got {}",
5656
"Error".red().bold(),
@@ -88,6 +88,16 @@ pub async fn upgrade(args: &Arguments, date_imported: &str) {
8888
transfer_users(source_database.clone(), target_database.clone(), date_imported).await;
8989
transfer_tracker_keys(source_database.clone(), target_database.clone()).await;
9090
transfer_torrents(source_database.clone(), target_database.clone(), &args.upload_path).await;
91+
92+
println!("Upgrade data from version v1.0.0 to v2.0.0 finished!\n");
93+
94+
eprintln!(
95+
"{}\nWe recommend you to run the command to import torrent statistics for all torrents manually. \
96+
If you do not do it the statistics will be imported anyway during the normal execution of the program. \
97+
You can import statistics manually with:\n {}",
98+
"SUGGESTION: \n".yellow(),
99+
"cargo run --bin import_tracker_statistics".yellow()
100+
);
91101
}
92102

93103
/// Current datetime in ISO8601 without time zone.

0 commit comments

Comments
 (0)