|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use axum::extract::State; |
1 | 4 | use axum::response::Json;
|
2 | 5 | use serde_json::{json, Value};
|
3 | 6 |
|
| 7 | +use crate::api::resource::stats::Stats; |
| 8 | +use crate::tracker::Tracker; |
| 9 | + |
4 | 10 | #[allow(clippy::unused_async)]
|
5 | 11 | pub async fn root() -> Json<Value> {
|
6 | 12 | Json(json!({ "data": 42 }))
|
7 | 13 | }
|
| 14 | + |
| 15 | +#[allow(clippy::unused_async)] |
| 16 | +pub async fn get_stats(State(tracker): State<Arc<Tracker>>) -> Json<Value> { |
| 17 | + let mut results = Stats { |
| 18 | + torrents: 0, |
| 19 | + seeders: 0, |
| 20 | + completed: 0, |
| 21 | + leechers: 0, |
| 22 | + tcp4_connections_handled: 0, |
| 23 | + tcp4_announces_handled: 0, |
| 24 | + tcp4_scrapes_handled: 0, |
| 25 | + tcp6_connections_handled: 0, |
| 26 | + tcp6_announces_handled: 0, |
| 27 | + tcp6_scrapes_handled: 0, |
| 28 | + udp4_connections_handled: 0, |
| 29 | + udp4_announces_handled: 0, |
| 30 | + udp4_scrapes_handled: 0, |
| 31 | + udp6_connections_handled: 0, |
| 32 | + udp6_announces_handled: 0, |
| 33 | + udp6_scrapes_handled: 0, |
| 34 | + }; |
| 35 | + |
| 36 | + let db = tracker.get_torrents().await; |
| 37 | + |
| 38 | + db.values().for_each(|torrent_entry| { |
| 39 | + let (seeders, completed, leechers) = torrent_entry.get_stats(); |
| 40 | + results.seeders += seeders; |
| 41 | + results.completed += completed; |
| 42 | + results.leechers += leechers; |
| 43 | + results.torrents += 1; |
| 44 | + }); |
| 45 | + |
| 46 | + let stats = tracker.get_stats().await; |
| 47 | + |
| 48 | + #[allow(clippy::cast_possible_truncation)] |
| 49 | + { |
| 50 | + results.tcp4_connections_handled = stats.tcp4_connections_handled as u32; |
| 51 | + results.tcp4_announces_handled = stats.tcp4_announces_handled as u32; |
| 52 | + results.tcp4_scrapes_handled = stats.tcp4_scrapes_handled as u32; |
| 53 | + results.tcp6_connections_handled = stats.tcp6_connections_handled as u32; |
| 54 | + results.tcp6_announces_handled = stats.tcp6_announces_handled as u32; |
| 55 | + results.tcp6_scrapes_handled = stats.tcp6_scrapes_handled as u32; |
| 56 | + results.udp4_connections_handled = stats.udp4_connections_handled as u32; |
| 57 | + results.udp4_announces_handled = stats.udp4_announces_handled as u32; |
| 58 | + results.udp4_scrapes_handled = stats.udp4_scrapes_handled as u32; |
| 59 | + results.udp6_connections_handled = stats.udp6_connections_handled as u32; |
| 60 | + results.udp6_announces_handled = stats.udp6_announces_handled as u32; |
| 61 | + results.udp6_scrapes_handled = stats.udp6_scrapes_handled as u32; |
| 62 | + } |
| 63 | + |
| 64 | + Json(json!(results)) |
| 65 | +} |
0 commit comments