Skip to content

Commit

Permalink
feat: [torrust#56] console command to import tracker stats for all to…
Browse files Browse the repository at this point in the history
…rrents
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 19d054e commit 5a7d875
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/bin/import_tracker_statistics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Import Tracker Statistics command.
//! It imports the number of seeders and leechers for all torrent from the linked tracker.
//! You can execute it with: `cargo run --bin import_tracker_statistics`

use torrust_index_backend::console::commands::import_tracker_statistics::run_importer;

#[actix_web::main]
async fn main() {
run_importer().await;
}
86 changes: 86 additions & 0 deletions src/console/commands/import_tracker_statistics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! It imports statistics for all torrents from the linked tracker.

use std::env;
use std::sync::Arc;

use derive_more::{Display, Error};
use text_colorizer::*;

use crate::config::Configuration;
use crate::databases::database::connect_database;
use crate::tracker::TrackerService;

const NUMBER_OF_ARGUMENTS: usize = 0;

#[derive(Debug)]
pub struct Arguments {}

#[derive(Debug, Display, PartialEq, Error)]
#[allow(dead_code)]
pub enum ImportError {
#[display(fmt = "internal server error")]
WrongNumberOfArgumentsError,
}

fn parse_args() -> Result<Arguments, ImportError> {
let args: Vec<String> = env::args().skip(1).collect();

if args.len() != NUMBER_OF_ARGUMENTS {
eprintln!(
"{} wrong number of arguments: expected {}, got {}",
"Error".red().bold(),
NUMBER_OF_ARGUMENTS,
args.len()
);
print_usage();
return Err(ImportError::WrongNumberOfArgumentsError);
}

Ok(Arguments {})
}

fn print_usage() {
eprintln!(
"{} - imports torrents statistics from linked tracker.
cargo run --bin upgrade SOURCE_DB_FILE DESTINY_DB_FILE TORRENT_UPLOAD_DIR
For example:
cargo run --bin import_tracker_statistics
",
"Upgrader".green()
);
}

pub async fn run_importer() {
import(&parse_args().unwrap()).await;
}

pub async fn import(_args: &Arguments) {
println!("Importing statistics from linked tracker ...");

let cfg = match Configuration::load_from_file().await {
Ok(config) => Arc::new(config),
Err(error) => {
panic!("{}", error)
}
};

let settings = cfg.settings.read().await;

let tracker_url = settings.tracker.url.clone();

eprintln!("Tracker url: {}", tracker_url.green());

let database = Arc::new(
connect_database(&settings.database.connect_url)
.await
.expect("Database error."),
);

let tracker_service = Arc::new(TrackerService::new(cfg.clone(), database.clone()));

tracker_service.update_torrents().await.unwrap();
}
1 change: 1 addition & 0 deletions src/console/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod import_tracker_statistics;
1 change: 1 addition & 0 deletions src/console/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod commands;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod auth;
pub mod common;
pub mod config;
pub mod console;
pub mod databases;
pub mod errors;
pub mod mailer;
Expand Down
14 changes: 12 additions & 2 deletions src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::torrent_transferrer::t
use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::tracker_key_transferrer::transfer_tracker_keys;
use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::user_transferrer::transfer_users;

const NUMBER_OF_ARGUMENTS: i64 = 3;
const NUMBER_OF_ARGUMENTS: usize = 3;

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

if args.len() != 3 {
if args.len() != NUMBER_OF_ARGUMENTS {
eprintln!(
"{} wrong number of arguments: expected {}, got {}",
"Error".red().bold(),
Expand Down Expand Up @@ -88,6 +88,16 @@ pub async fn upgrade(args: &Arguments, date_imported: &str) {
transfer_users(source_database.clone(), target_database.clone(), date_imported).await;
transfer_tracker_keys(source_database.clone(), target_database.clone()).await;
transfer_torrents(source_database.clone(), target_database.clone(), &args.upload_path).await;

println!("Upgrade data from version v1.0.0 to v2.0.0 finished!\n");

eprintln!(
"{}\nWe recommend you to run the command to import torrent statistics for all torrents manually. \
If you do not do it the statistics will be imported anyway during the normal execution of the program. \
You can import statistics manually with:\n {}",
"SUGGESTION: \n".yellow(),
"cargo run --bin import_tracker_statistics".yellow()
);
}

/// Current datetime in ISO8601 without time zone.
Expand Down

0 comments on commit 5a7d875

Please sign in to comment.