summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2024-05-10 15:41:52 +0200
committerDirk Engling <erdgeist@erdgeist.org>2024-05-10 15:41:52 +0200
commit37f5b2403bd76a785c05afc843572f464410d457 (patch)
tree3c2f1a2fbbd2ad183e1c89a4d9ca1f4d6a4cef04 /ot_stats.c
parent2c88c7b65ad0b70f35fc79aa81879d839db937a0 (diff)
Add top100 torrents by leechers to the list
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/ot_stats.c b/ot_stats.c
index 158884f..245961d 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -320,7 +320,7 @@ typedef struct {
320/* Fetches stats from tracker */ 320/* Fetches stats from tracker */
321size_t stats_top_txt(char *reply, int amount) { 321size_t stats_top_txt(char *reply, int amount) {
322 size_t j; 322 size_t j;
323 ot_record top100s[100], top100c[100]; 323 ot_record top100s[100], top100c[100], top100l[100];
324 char *r = reply, hex_out[42]; 324 char *r = reply, hex_out[42];
325 int idx, bucket; 325 int idx, bucket;
326 326
@@ -331,12 +331,13 @@ size_t stats_top_txt(char *reply, int amount) {
331 byte_zero(top100c, sizeof(top100c)); 331 byte_zero(top100c, sizeof(top100c));
332 332
333 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { 333 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
334 ot_vector *torrents_list = mutex_bucket_lock(bucket); 334 ot_vector *torrents_list = mutex_bucket_lock(bucket);
335 for (j = 0; j < torrents_list->size; ++j) { 335 for (j = 0; j < torrents_list->size; ++j) {
336 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j; 336 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j;
337 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; 337 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
338 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; 338 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
339 idx = amount - 1; 339 size_t leech_count = peer_count - seed_count;
340 idx = amount - 1;
340 while ((idx >= 0) && (peer_count > top100c[idx].val)) 341 while ((idx >= 0) && (peer_count > top100c[idx].val))
341 --idx; 342 --idx;
342 if (idx++ != amount - 1) { 343 if (idx++ != amount - 1) {
@@ -352,6 +353,14 @@ size_t stats_top_txt(char *reply, int amount) {
352 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash)); 353 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash));
353 top100s[idx].val = seed_count; 354 top100s[idx].val = seed_count;
354 } 355 }
356 idx = amount - 1;
357 while ((idx >= 0) && (leech_count > top100l[idx].val))
358 --idx;
359 if (idx++ != amount - 1) {
360 memmove(top100l + idx + 1, top100l + idx, (amount - 1 - idx) * sizeof(ot_record));
361 memcpy(&top100l[idx].hash, &torrent->hash, sizeof(ot_hash));
362 top100l[idx].val = leech_count;
363 }
355 } 364 }
356 mutex_bucket_unlock(bucket, 0); 365 mutex_bucket_unlock(bucket, 0);
357 if (!g_opentracker_running) 366 if (!g_opentracker_running)
@@ -366,6 +375,10 @@ size_t stats_top_txt(char *reply, int amount) {
366 for (idx = 0; idx < amount; ++idx) 375 for (idx = 0; idx < amount; ++idx)
367 if (top100s[idx].val) 376 if (top100s[idx].val)
368 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash)); 377 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash));
378 r += sprintf(r, "Top %d torrents by leechers:\n", amount);
379 for (idx = 0; idx < amount; ++idx)
380 if (top100l[idx].val)
381 r += sprintf(r, "\t%zd\t%s\n", top100l[idx].val, to_hex(hex_out, top100l[idx].hash));
369 382
370 return r - reply; 383 return r - reply;
371} 384}