Skip to content

Commit

Permalink
Merge #170: Documentation: statistics importer console command
Browse files Browse the repository at this point in the history
5485589 refactor: remove unneeded code (Jose Celano)
93d1b64 docs: [#168] statistics importer console command (Jose Celano)

Pull request description:

Top commit has no ACKs.

Tree-SHA512: 1d5f8b787ae94b418aaea50bee045fa080f8b200cac1f22e514c8a9a4c3548d504f9c92a1eabbc4658530522d798a139ea07d6f012d3167cc70728f0e921f322
  • Loading branch information
josecelano committed Jun 6, 2023
2 parents 29d87de + 5485589 commit 2626f9f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
36 changes: 26 additions & 10 deletions src/console/commands/import_tracker_statistics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
//! It imports statistics for all torrents from the linked tracker.
//!
//! It imports the number of seeders and leechers for all torrent from the linked tracker.
//! It imports the number of seeders and leechers for all torrents from the
//! associated tracker.
//!
//! You can execute it with: `cargo run --bin import_tracker_statistics`
//! You can execute it with: `cargo run --bin import_tracker_statistics`.
//!
//! After running it you will see the following output:
//!
//! ```text
//! Importing statistics from linked tracker ...
//! Loading configuration from config file `./config.toml`
//! Tracker url: udp://localhost:6969
//! ```
//!
//! Statistics are also imported:
//!
//! - Periodically by the importer job. The importer job is executed every hour
//! by default. See [`TrackerStatisticsImporter`](crate::config::TrackerStatisticsImporter)
//! for more details.
//! - When a new torrent is added.
//! - When the API returns data about a torrent statistics are collected from
//! the tracker in real time.
use std::env;
use std::sync::Arc;

Expand All @@ -17,17 +35,14 @@ use crate::tracker::statistics_importer::StatisticsImporter;

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> {
fn parse_args() -> Result<(), ImportError> {
let args: Vec<String> = env::args().skip(1).collect();

if args.len() != NUMBER_OF_ARGUMENTS {
Expand All @@ -41,7 +56,7 @@ fn parse_args() -> Result<Arguments, ImportError> {
return Err(ImportError::WrongNumberOfArgumentsError);
}

Ok(Arguments {})
Ok(())
}

fn print_usage() {
Expand All @@ -56,15 +71,16 @@ fn print_usage() {
}

pub async fn run_importer() {
import(&parse_args().expect("unable to parse command arguments")).await;
parse_args().expect("unable to parse command arguments");
import().await;
}

/// Import Command Arguments
///
/// # Panics
///
/// Panics if `Configuration::load_from_file` has any error.
pub async fn import(_args: &Arguments) {
pub async fn import() {
println!("Importing statistics from linked tracker ...");

let configuration = init_configuration().await;
Expand Down Expand Up @@ -92,5 +108,5 @@ pub async fn import(_args: &Arguments) {
tracker_statistics_importer
.import_all_torrents_statistics()
.await
.expect("variable `tracker_service` is unable to `update_torrents`");
.expect("should import all torrents statistics");
}
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
//! - [Development](#development)
//! - [Configuration](#configuration)
//! - [Usage](#usage)
//! - [API](#api)
//! - [Tracker Statistics Importer](#tracker-statistics-importer)
//! - [Upgrader](#upgrader)
//! - [Contributing](#contributing)
//! - [Documentation](#documentation)
//!
Expand Down Expand Up @@ -218,12 +221,24 @@
//!
//! # Usage
//!
//! ## API
//!
//! Running the tracker with the default configuration will expose the REST API on port 3000: <http://localhost:3000>
//!
//! You can also run console commands:
//! ## Tracker Statistics Importer
//!
//! This console command allows you to manually import the tracker statistics.
//!
//! For more information about this command you can visit the documentation for
//! the [`Import tracker statistics`](crate::console::commands::import_tracker_statistics) module.
//!
//! ## Upgrader
//!
//! This console command allows you to manually upgrade the application from one
//! version to another.
//!
//! - [`Import tracker statistics`](crate::console::commands::import_tracker_statistics).
//! - [`Upgrade app from version 1.0.0 to 2.0.0`](crate::upgrades::from_v1_0_0_to_v2_0_0::upgrader).
//! For more information about this command you can visit the documentation for
//! the [`Upgrade app from version 1.0.0 to 2.0.0`](crate::upgrades::from_v1_0_0_to_v2_0_0::upgrader) module.
//!
//! Refer to the documentation of each command for more information.
//!
Expand Down
3 changes: 2 additions & 1 deletion src/tracker/statistics_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ impl StatisticsImporter {
// ```

if let Some(err) = ret.err() {
error!(
let message = format!(
"Error updating torrent tracker stats for torrent with id {}: {:?}",
torrent.torrent_id, err
);
error!("{}", message);
}
}

Expand Down

0 comments on commit 2626f9f

Please sign in to comment.