-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): [#143] remove duplicate code
- Extract domain logic: `Tracker::get_torrents_metrics`. - Move domain logic from web framework controllers to domain services: `get_metrics`. - Remove duplicate code in current Warp API and new Axum API.
- Loading branch information
1 parent
7331c82
commit 0615c9f
Showing
6 changed files
with
107 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,46 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::tracker::services::statistics::TrackerMetrics; | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub struct Stats { | ||
pub torrents: u32, | ||
pub seeders: u32, | ||
pub completed: u32, | ||
pub leechers: u32, | ||
pub tcp4_connections_handled: u32, | ||
pub tcp4_announces_handled: u32, | ||
pub tcp4_scrapes_handled: u32, | ||
pub tcp6_connections_handled: u32, | ||
pub tcp6_announces_handled: u32, | ||
pub tcp6_scrapes_handled: u32, | ||
pub udp4_connections_handled: u32, | ||
pub udp4_announces_handled: u32, | ||
pub udp4_scrapes_handled: u32, | ||
pub udp6_connections_handled: u32, | ||
pub udp6_announces_handled: u32, | ||
pub udp6_scrapes_handled: u32, | ||
pub torrents: u64, | ||
pub seeders: u64, | ||
pub completed: u64, | ||
pub leechers: u64, | ||
pub tcp4_connections_handled: u64, | ||
pub tcp4_announces_handled: u64, | ||
pub tcp4_scrapes_handled: u64, | ||
pub tcp6_connections_handled: u64, | ||
pub tcp6_announces_handled: u64, | ||
pub tcp6_scrapes_handled: u64, | ||
pub udp4_connections_handled: u64, | ||
pub udp4_announces_handled: u64, | ||
pub udp4_scrapes_handled: u64, | ||
pub udp6_connections_handled: u64, | ||
pub udp6_announces_handled: u64, | ||
pub udp6_scrapes_handled: u64, | ||
} | ||
|
||
impl From<TrackerMetrics> for Stats { | ||
fn from(metrics: TrackerMetrics) -> Self { | ||
Self { | ||
torrents: metrics.torrents_metrics.torrents, | ||
seeders: metrics.torrents_metrics.seeders, | ||
completed: metrics.torrents_metrics.completed, | ||
leechers: metrics.torrents_metrics.leechers, | ||
tcp4_connections_handled: metrics.protocol_metrics.tcp4_connections_handled, | ||
tcp4_announces_handled: metrics.protocol_metrics.tcp4_announces_handled, | ||
tcp4_scrapes_handled: metrics.protocol_metrics.tcp4_scrapes_handled, | ||
tcp6_connections_handled: metrics.protocol_metrics.tcp6_connections_handled, | ||
tcp6_announces_handled: metrics.protocol_metrics.tcp6_announces_handled, | ||
tcp6_scrapes_handled: metrics.protocol_metrics.tcp6_scrapes_handled, | ||
udp4_connections_handled: metrics.protocol_metrics.udp4_connections_handled, | ||
udp4_announces_handled: metrics.protocol_metrics.udp4_announces_handled, | ||
udp4_scrapes_handled: metrics.protocol_metrics.udp4_scrapes_handled, | ||
udp6_connections_handled: metrics.protocol_metrics.udp6_connections_handled, | ||
udp6_announces_handled: metrics.protocol_metrics.udp6_announces_handled, | ||
udp6_scrapes_handled: metrics.protocol_metrics.udp6_scrapes_handled, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod statistics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::tracker::statistics::Metrics; | ||
use crate::tracker::{TorrentsMetrics, Tracker}; | ||
|
||
pub struct TrackerMetrics { | ||
pub torrents_metrics: TorrentsMetrics, | ||
pub protocol_metrics: Metrics, | ||
} | ||
|
||
pub async fn get_metrics(tracker: Arc<Tracker>) -> TrackerMetrics { | ||
let torrents_metrics = tracker.get_torrents_metrics().await; | ||
let stats = tracker.get_stats().await; | ||
|
||
TrackerMetrics { | ||
torrents_metrics, | ||
protocol_metrics: Metrics { | ||
tcp4_connections_handled: stats.tcp4_connections_handled, | ||
tcp4_announces_handled: stats.tcp4_announces_handled, | ||
tcp4_scrapes_handled: stats.tcp4_scrapes_handled, | ||
tcp6_connections_handled: stats.tcp6_connections_handled, | ||
tcp6_announces_handled: stats.tcp6_announces_handled, | ||
tcp6_scrapes_handled: stats.tcp6_scrapes_handled, | ||
udp4_connections_handled: stats.udp4_connections_handled, | ||
udp4_announces_handled: stats.udp4_announces_handled, | ||
udp4_scrapes_handled: stats.udp4_scrapes_handled, | ||
udp6_connections_handled: stats.udp6_connections_handled, | ||
udp6_announces_handled: stats.udp6_announces_handled, | ||
udp6_scrapes_handled: stats.udp6_scrapes_handled, | ||
}, | ||
} | ||
} |