forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandlers.rs
67 lines (61 loc) · 2.02 KB
/
handlers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! API handlers for the [`stats`](crate::v1::context::stats)
//! API context.
use std::sync::Arc;
use axum::extract::State;
use axum::response::Response;
use axum_extra::extract::Query;
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
use bittorrent_udp_tracker_core::services::banning::BanService;
use serde::Deserialize;
use tokio::sync::RwLock;
use torrust_tracker_api_core::statistics::services::get_metrics;
use super::responses::{metrics_response, stats_response};
#[derive(Deserialize, Debug, Default)]
#[serde(rename_all = "lowercase")]
pub enum Format {
#[default]
Json,
Prometheus,
}
#[derive(Deserialize, Debug)]
pub struct QueryParams {
/// The [`Format`] of the stats.
#[serde(default)]
pub format: Option<Format>,
}
/// It handles the request to get the tracker statistics.
///
/// By default it returns a `200` response with the stats in JSON format.
///
/// You can add the GET parameter `format=prometheus` to get the stats in
/// Prometheus Text Exposition Format.
///
/// Refer to the [API endpoint documentation](crate::v1::context::stats#get-tracker-statistics)
/// for more information about this endpoint.
#[allow(clippy::type_complexity)]
pub async fn get_stats_handler(
State(state): State<(
Arc<InMemoryTorrentRepository>,
Arc<RwLock<BanService>>,
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
)>,
params: Query<QueryParams>,
) -> Response {
let metrics = get_metrics(
state.0.clone(),
state.1.clone(),
state.2.clone(),
state.3.clone(),
state.4.clone(),
)
.await;
match params.0.format {
Some(format) => match format {
Format::Json => stats_response(metrics),
Format::Prometheus => metrics_response(&metrics),
},
None => stats_response(metrics),
}
}