Skip to content

Commit

Permalink
feat(api): [#143] axum api, GET /stats endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 2, 2023
1 parent 5ee3f93 commit 6a9e2d5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 6 deletions.
58 changes: 58 additions & 0 deletions src/apis/routes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,65 @@
use std::sync::Arc;

use axum::extract::State;
use axum::response::Json;
use serde_json::{json, Value};

use crate::api::resource::stats::Stats;
use crate::tracker::Tracker;

#[allow(clippy::unused_async)]
pub async fn root() -> Json<Value> {
Json(json!({ "data": 42 }))
}

#[allow(clippy::unused_async)]
pub async fn get_stats(State(tracker): State<Arc<Tracker>>) -> Json<Value> {
let mut results = Stats {
torrents: 0,
seeders: 0,
completed: 0,
leechers: 0,
tcp4_connections_handled: 0,
tcp4_announces_handled: 0,
tcp4_scrapes_handled: 0,
tcp6_connections_handled: 0,
tcp6_announces_handled: 0,
tcp6_scrapes_handled: 0,
udp4_connections_handled: 0,
udp4_announces_handled: 0,
udp4_scrapes_handled: 0,
udp6_connections_handled: 0,
udp6_announces_handled: 0,
udp6_scrapes_handled: 0,
};

let db = tracker.get_torrents().await;

db.values().for_each(|torrent_entry| {
let (seeders, completed, leechers) = torrent_entry.get_stats();
results.seeders += seeders;
results.completed += completed;
results.leechers += leechers;
results.torrents += 1;
});

let stats = tracker.get_stats().await;

#[allow(clippy::cast_possible_truncation)]
{
results.tcp4_connections_handled = stats.tcp4_connections_handled as u32;
results.tcp4_announces_handled = stats.tcp4_announces_handled as u32;
results.tcp4_scrapes_handled = stats.tcp4_scrapes_handled as u32;
results.tcp6_connections_handled = stats.tcp6_connections_handled as u32;
results.tcp6_announces_handled = stats.tcp6_announces_handled as u32;
results.tcp6_scrapes_handled = stats.tcp6_scrapes_handled as u32;
results.udp4_connections_handled = stats.udp4_connections_handled as u32;
results.udp4_announces_handled = stats.udp4_announces_handled as u32;
results.udp4_scrapes_handled = stats.udp4_scrapes_handled as u32;
results.udp6_connections_handled = stats.udp6_connections_handled as u32;
results.udp6_announces_handled = stats.udp6_announces_handled as u32;
results.udp6_scrapes_handled = stats.udp6_scrapes_handled as u32;
}

Json(json!(results))
}
8 changes: 5 additions & 3 deletions src/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use axum::Router;
use futures::Future;
use warp::hyper;

use super::routes::root;
use super::routes::{get_stats, root};
use crate::tracker;

pub fn start(socket_addr: SocketAddr, _tracker: &Arc<tracker::Tracker>) -> impl Future<Output = hyper::Result<()>> {
let app = Router::new().route("/", get(root));
pub fn start(socket_addr: SocketAddr, tracker: &Arc<tracker::Tracker>) -> impl Future<Output = hyper::Result<()>> {
let app = Router::new()
.route("/", get(root))
.route("/stats", get(get_stats).with_state(tracker.clone()));

let server = axum::Server::bind(&socket_addr).serve(app.into_make_service());

Expand Down
57 changes: 54 additions & 3 deletions tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,18 +582,69 @@ mod tracker_apis {

mod for_entrypoint {
use crate::api::client::{Client, Query};
use crate::api::server::start_default_api_server;
use crate::api::server::start_default_api;
use crate::api::Version;

#[tokio::test]
async fn test_entrypoint() {
let api_server = start_default_api_server(&Version::Axum).await;
let api_server = start_default_api(&Version::Axum).await;

let response = Client::new(api_server.get_connection_info(), &Version::Axum)
.get("/", Query::default())
.get("", Query::default())
.await;

assert_eq!(response.status(), 200);
}
}

mod for_stats_resources {
use std::str::FromStr;

use torrust_tracker::api::resource::stats::Stats;
use torrust_tracker::protocol::info_hash::InfoHash;

use crate::api::client::Client;
use crate::api::fixtures::sample_peer;
use crate::api::server::start_default_api;
use crate::api::Version;

#[tokio::test]
async fn should_allow_getting_tracker_statistics() {
let api_server = start_default_api(&Version::Axum).await;

api_server
.add_torrent(
&InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(),
&sample_peer(),
)
.await;

let response = Client::new(api_server.get_connection_info(), &Version::Axum)
.get_tracker_statistics()
.await;

assert_eq!(response.status(), 200);
assert_eq!(
response.json::<Stats>().await.unwrap(),
Stats {
torrents: 1,
seeders: 1,
completed: 0,
leechers: 0,
tcp4_connections_handled: 0,
tcp4_announces_handled: 0,
tcp4_scrapes_handled: 0,
tcp6_connections_handled: 0,
tcp6_announces_handled: 0,
tcp6_scrapes_handled: 0,
udp4_connections_handled: 0,
udp4_announces_handled: 0,
udp4_scrapes_handled: 0,
udp6_connections_handled: 0,
udp6_announces_handled: 0,
udp6_scrapes_handled: 0,
}
);
}
}
}

0 comments on commit 6a9e2d5

Please sign in to comment.