diff options
Diffstat (limited to 'ot_stats.c')
-rw-r--r-- | ot_stats.c | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -320,7 +320,7 @@ typedef struct { | |||
320 | /* Fetches stats from tracker */ | 320 | /* Fetches stats from tracker */ |
321 | size_t stats_top_txt(char *reply, int amount) { | 321 | size_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 | } |