summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.gzip2
-rw-r--r--ot_stats.c26
2 files changed, 21 insertions, 7 deletions
diff --git a/Makefile.gzip b/Makefile.gzip
index 6e0c066..70d6d62 100644
--- a/Makefile.gzip
+++ b/Makefile.gzip
@@ -1,4 +1,4 @@
1FEATURES+=-DWANT_COMPRESSION_GZIP 1FEATURES+=-DWANT_COMPRESSION_GZIP
2FEATURES+=-DWANT_COMPRESSION_GZIP_ALWAYS 2#FEATURES+=-DWANT_COMPRESSION_GZIP_ALWAYS
3 3
4LDFLAGS+=-lz 4LDFLAGS+=-lz
diff --git a/ot_stats.c b/ot_stats.c
index 158884f..b2eaec9 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
@@ -329,14 +329,16 @@ size_t stats_top_txt(char *reply, int amount) {
329 329
330 byte_zero(top100s, sizeof(top100s)); 330 byte_zero(top100s, sizeof(top100s));
331 byte_zero(top100c, sizeof(top100c)); 331 byte_zero(top100c, sizeof(top100c));
332 byte_zero(top100l, sizeof(top100l));
332 333
333 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { 334 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
334 ot_vector *torrents_list = mutex_bucket_lock(bucket); 335 ot_vector *torrents_list = mutex_bucket_lock(bucket);
335 for (j = 0; j < torrents_list->size; ++j) { 336 for (j = 0; j < torrents_list->size; ++j) {
336 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j; 337 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j;
337 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; 338 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; 339 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
339 idx = amount - 1; 340 size_t leech_count = peer_count - seed_count;
341 idx = amount - 1;
340 while ((idx >= 0) && (peer_count > top100c[idx].val)) 342 while ((idx >= 0) && (peer_count > top100c[idx].val))
341 --idx; 343 --idx;
342 if (idx++ != amount - 1) { 344 if (idx++ != amount - 1) {
@@ -352,6 +354,14 @@ size_t stats_top_txt(char *reply, int amount) {
352 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash)); 354 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash));
353 top100s[idx].val = seed_count; 355 top100s[idx].val = seed_count;
354 } 356 }
357 idx = amount - 1;
358 while ((idx >= 0) && (leech_count > top100l[idx].val))
359 --idx;
360 if (idx++ != amount - 1) {
361 memmove(top100l + idx + 1, top100l + idx, (amount - 1 - idx) * sizeof(ot_record));
362 memcpy(&top100l[idx].hash, &torrent->hash, sizeof(ot_hash));
363 top100l[idx].val = leech_count;
364 }
355 } 365 }
356 mutex_bucket_unlock(bucket, 0); 366 mutex_bucket_unlock(bucket, 0);
357 if (!g_opentracker_running) 367 if (!g_opentracker_running)
@@ -366,6 +376,10 @@ size_t stats_top_txt(char *reply, int amount) {
366 for (idx = 0; idx < amount; ++idx) 376 for (idx = 0; idx < amount; ++idx)
367 if (top100s[idx].val) 377 if (top100s[idx].val)
368 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash)); 378 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash));
379 r += sprintf(r, "Top %d torrents by leechers:\n", amount);
380 for (idx = 0; idx < amount; ++idx)
381 if (top100l[idx].val)
382 r += sprintf(r, "\t%zd\t%s\n", top100l[idx].val, to_hex(hex_out, top100l[idx].hash));
369 383
370 return r - reply; 384 return r - reply;
371} 385}