Skip to content

Commit 42850f3

Browse files
committed
refactor: [#1581] extract methods
1 parent 86e6406 commit 42850f3

File tree

2 files changed

+66
-43
lines changed

2 files changed

+66
-43
lines changed

packages/http-tracker-core/src/statistics/metrics.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use serde::Serialize;
22
use torrust_tracker_metrics::label::LabelSet;
33
use torrust_tracker_metrics::metric::MetricName;
4+
use torrust_tracker_metrics::metric_collection::aggregate::Sum;
45
use torrust_tracker_metrics::metric_collection::{Error, MetricCollection};
6+
use torrust_tracker_metrics::metric_name;
57
use torrust_tracker_primitives::DurationSinceUnixEpoch;
68

9+
use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
10+
711
/// Metrics collected by the tracker.
812
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
913
pub struct Metrics {
@@ -49,3 +53,61 @@ impl Metrics {
4953
self.metric_collection.set_gauge(metric_name, labels, value, now)
5054
}
5155
}
56+
57+
impl Metrics {
58+
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
59+
#[must_use]
60+
#[allow(clippy::cast_sign_loss)]
61+
#[allow(clippy::cast_possible_truncation)]
62+
pub fn tcp4_announces_handled(&self) -> u64 {
63+
self.metric_collection
64+
.sum(
65+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
66+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
67+
)
68+
.unwrap_or_default()
69+
.value() as u64
70+
}
71+
72+
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
73+
#[must_use]
74+
#[allow(clippy::cast_sign_loss)]
75+
#[allow(clippy::cast_possible_truncation)]
76+
pub fn tcp4_scrapes_handled(&self) -> u64 {
77+
self.metric_collection
78+
.sum(
79+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
80+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
81+
)
82+
.unwrap_or_default()
83+
.value() as u64
84+
}
85+
86+
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
87+
#[must_use]
88+
#[allow(clippy::cast_sign_loss)]
89+
#[allow(clippy::cast_possible_truncation)]
90+
pub fn tcp6_announces_handled(&self) -> u64 {
91+
self.metric_collection
92+
.sum(
93+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
94+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
95+
)
96+
.unwrap_or_default()
97+
.value() as u64
98+
}
99+
100+
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
101+
#[must_use]
102+
#[allow(clippy::cast_sign_loss)]
103+
#[allow(clippy::cast_possible_truncation)]
104+
pub fn tcp6_scrapes_handled(&self) -> u64 {
105+
self.metric_collection
106+
.sum(
107+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
108+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
109+
)
110+
.unwrap_or_default()
111+
.value() as u64
112+
}
113+
}

packages/rest-tracker-api-core/src/statistics/services.rs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::sync::Arc;
22

3-
use bittorrent_http_tracker_core::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
43
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
54
use bittorrent_udp_tracker_core::services::banning::BanService;
65
use bittorrent_udp_tracker_core::{self};
@@ -156,51 +155,13 @@ async fn get_protocol_metrics_from_labeled_metrics(
156155

157156
// TCPv4
158157

159-
#[allow(clippy::cast_sign_loss)]
160-
#[allow(clippy::cast_possible_truncation)]
161-
let tcp4_announces_handled = http_stats
162-
.metric_collection
163-
.sum(
164-
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
165-
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
166-
)
167-
.unwrap_or_default()
168-
.value() as u64;
169-
170-
#[allow(clippy::cast_sign_loss)]
171-
#[allow(clippy::cast_possible_truncation)]
172-
let tcp4_scrapes_handled = http_stats
173-
.metric_collection
174-
.sum(
175-
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
176-
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
177-
)
178-
.unwrap_or_default()
179-
.value() as u64;
158+
let tcp4_announces_handled = http_stats.tcp4_announces_handled();
159+
let tcp4_scrapes_handled = http_stats.tcp4_scrapes_handled();
180160

181161
// TCPv6
182162

183-
#[allow(clippy::cast_sign_loss)]
184-
#[allow(clippy::cast_possible_truncation)]
185-
let tcp6_announces_handled = http_stats
186-
.metric_collection
187-
.sum(
188-
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
189-
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
190-
)
191-
.unwrap_or_default()
192-
.value() as u64;
193-
194-
#[allow(clippy::cast_sign_loss)]
195-
#[allow(clippy::cast_possible_truncation)]
196-
let tcp6_scrapes_handled = http_stats
197-
.metric_collection
198-
.sum(
199-
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
200-
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
201-
)
202-
.unwrap_or_default()
203-
.value() as u64;
163+
let tcp6_announces_handled = http_stats.tcp6_announces_handled();
164+
let tcp6_scrapes_handled = http_stats.tcp6_scrapes_handled();
204165

205166
// UDP
206167

0 commit comments

Comments
 (0)