forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.rs
More file actions
77 lines (70 loc) · 3.1 KB
/
handler.rs
File metadata and controls
77 lines (70 loc) · 3.1 KB
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
68
69
70
71
72
73
74
75
76
77
use std::sync::Arc;
use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric_name;
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use torrust_tracker_swarm_coordination_registry::event::Event;
use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository;
use crate::statistics::repository::Repository;
use crate::statistics::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL;
pub async fn handle_event(
event: Event,
stats_repository: &Arc<Repository>,
db_downloads_metric_repository: &Arc<DatabaseDownloadsMetricRepository>,
persistent_torrent_completed_stat: bool,
now: DurationSinceUnixEpoch,
) {
match event {
// Torrent events
Event::TorrentAdded { info_hash, .. } => {
tracing::debug!(info_hash = ?info_hash, "Torrent added",);
}
Event::TorrentRemoved { info_hash } => {
tracing::debug!(info_hash = ?info_hash, "Torrent removed",);
}
// Peer events
Event::PeerAdded { info_hash, peer } => {
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer added", );
}
Event::PeerRemoved { info_hash, peer } => {
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer removed", );
}
Event::PeerUpdated {
info_hash,
old_peer,
new_peer,
} => {
tracing::debug!(info_hash = ?info_hash, old_peer = ?old_peer, new_peer = ?new_peer, "Peer updated");
}
Event::PeerDownloadCompleted { info_hash, peer } => {
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer download completed", );
// Increment the number of downloads for all the torrents in memory
let _unused = stats_repository
.increment_counter(
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
&LabelSet::default(),
now,
)
.await;
if persistent_torrent_completed_stat {
// Increment the number of downloads for the torrent in the database
match db_downloads_metric_repository.increase_downloads_for_torrent(&info_hash) {
Ok(()) => {
tracing::debug!(info_hash = ?info_hash, "Number of torrent downloads increased");
}
Err(err) => {
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads for the torrent");
}
}
// Increment the global number of downloads (for all torrents) in the database
match db_downloads_metric_repository.increase_global_downloads() {
Ok(()) => {
tracing::debug!("Global number of downloads increased");
}
Err(err) => {
tracing::error!(error = ?err, "Failed to increase global number of downloads");
}
}
}
}
}
}