feat: easier debugging when getting peers from the db

This commit is contained in:
FrenchGithubUser
2025-11-02 20:23:07 +01:00
parent 5460f87a1a
commit 417df9cbc3
5 changed files with 39 additions and 27 deletions
Generated
-1
View File
@@ -569,7 +569,6 @@ dependencies = [
"anyhow",
"bincode",
"chrono 0.4.41",
"futures-util",
"indexmap",
"log",
"parking_lot",
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT\n peers.ip as \"ip_address: IpAddr\",\n peers.user_id as \"user_id\",\n peers.torrent_id as \"torrent_id\",\n peers.port as \"port\",\n peers.seeder as \"is_seeder: bool\",\n peers.active as \"is_active: bool\",\n peers.updated_at as \"updated_at: DateTime<Utc>\",\n peers.uploaded as \"uploaded\",\n peers.downloaded as \"downloaded\",\n peers.peer_id as \"peer_id: PeerId\"\n FROM\n peers\n ",
"query": "\n SELECT\n peers.ip AS \"ip_address: IpAddr\",\n peers.user_id AS \"user_id\",\n peers.torrent_id AS \"torrent_id\",\n peers.port AS \"port\",\n peers.seeder AS \"is_seeder: bool\",\n peers.active AS \"is_active: bool\",\n peers.updated_at AS \"updated_at: DateTime<Utc>\",\n peers.uploaded AS \"uploaded\",\n peers.downloaded AS \"downloaded\",\n peers.peer_id AS \"peer_id: PeerId\"\n FROM peers\n ",
"describe": {
"columns": [
{
@@ -21,7 +21,7 @@
{
"ordinal": 3,
"name": "port",
"type_info": "Int2"
"type_info": "Int4"
},
{
"ordinal": 4,
@@ -70,5 +70,5 @@
false
]
},
"hash": "5f5cc37d639a2f0650ac2afcdc610538f49816480be0f51c40a67bb74d97e874"
"hash": "f6d849721ff84614c129c14455d9a6adbe0ad29b7876963d5bd9015c0f73ba9d"
}
-1
View File
@@ -16,4 +16,3 @@ ringmap = { version = "0.2.0", features = ["serde"] }
actix-web = "4"
log = "0.4"
parking_lot = "0.12.4"
futures-util = "0.3.31"
+31 -22
View File
@@ -1,5 +1,4 @@
use chrono::{DateTime, Utc};
use futures_util::TryStreamExt;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use sqlx::{Database, Decode, PgPool};
@@ -75,41 +74,51 @@ impl Map {
}
// Load peers into each torrent
let mut peers = sqlx::query!(
let peers = sqlx::query!(
r#"
SELECT
peers.ip as "ip_address: IpAddr",
peers.user_id as "user_id",
peers.torrent_id as "torrent_id",
peers.port as "port",
peers.seeder as "is_seeder: bool",
peers.active as "is_active: bool",
peers.updated_at as "updated_at: DateTime<Utc>",
peers.uploaded as "uploaded",
peers.downloaded as "downloaded",
peers.peer_id as "peer_id: PeerId"
FROM
peers
"#
SELECT
peers.ip AS "ip_address: IpAddr",
peers.user_id AS "user_id",
peers.torrent_id AS "torrent_id",
peers.port AS "port",
peers.seeder AS "is_seeder: bool",
peers.active AS "is_active: bool",
peers.updated_at AS "updated_at: DateTime<Utc>",
peers.uploaded AS "uploaded",
peers.downloaded AS "downloaded",
peers.peer_id AS "peer_id: PeerId"
FROM peers
"#
)
.fetch(db);
.fetch_all(db)
.await
.expect("Failed loading peers from database");
while let Some(peer) = peers.try_next().await.expect("Failed loading peers.") {
map.entry(peer.torrent_id as u32).and_modify(|torrent| {
for peer in peers {
let torrent_id =
u32::try_from(peer.torrent_id).expect("torrent_id out of range for u32");
let user_id = u32::try_from(peer.user_id).expect("user_id out of range for u32");
#[allow(clippy::expect_fun_call)]
let port = u16::try_from(peer.port).expect(&format!(
"Invalid port number in database. Peer: {:?}",
peer
));
map.entry(torrent_id).and_modify(|torrent| {
torrent.peers.insert(
peer::Index {
user_id: peer.user_id as u32,
user_id,
peer_id: peer.peer_id,
},
Peer {
ip_address: peer.ip_address,
port: peer.port as u16,
port,
is_seeder: peer.is_seeder,
is_active: peer.is_active,
has_sent_completed: false,
updated_at: peer
.updated_at
.expect("Peer with a null updated_at found in database."),
.expect("Peer with null updated_at found in database."),
uploaded: peer.uploaded as u64,
downloaded: peer.downloaded as u64,
},
@@ -25,6 +25,7 @@ use arcadia_shared::tracker::models::{
user_update::{self, UserUpdate},
};
use chrono::{Duration, Utc};
use log::debug;
use rand::{rng, seq::IteratorRandom, Rng};
#[derive(Debug)]
@@ -568,6 +569,10 @@ pub async fn exec(
},
);
}
debug!(
"Announce response: {:?}",
String::from_utf8_lossy(&response)
);
Ok(HttpResponse::Ok().body(response))
}