Skip to content

Commit

Permalink
Adds page limit to stats server.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanedwardes committed Aug 6, 2024
1 parent fbc7e3d commit 7d02af3
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions misc/Ae.Dns.Console/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -66,6 +67,12 @@ async Task WriteTable(DataTable table)
await context.Response.WriteAsync("</table>");
}
var pageLimit = 20;
if (context.Request.Query.ContainsKey("limit"))
{
_ = int.TryParse(context.Request.Query["limit"], out pageLimit);
}
async Task GroupToTable(IEnumerable<IGrouping<string?, DnsQuery>> groups, params string[] headings)
{
var table = new DataTable();
Expand All @@ -77,17 +84,17 @@ async Task GroupToTable(IEnumerable<IGrouping<string?, DnsQuery>> groups, params
table.Columns.Add("Percentage");
var itemCounts = groups.Select(x => KeyValuePair.Create<string?, int>(x.Key, x.Count())).OrderByDescending(x => x.Value).ToList();
var itemCounts = groups.Select(x => KeyValuePair.Create(x.Key, x.Count())).OrderByDescending(x => x.Value).ToList();
var totalCount = itemCounts.Sum(x => x.Value);
int CalculatePercentage(int count) => (int)(count / (double)totalCount * (double)100d);
foreach (var group in itemCounts.Take(20))
foreach (var group in itemCounts.Take(pageLimit))
{
table.Rows.Add(group.Key, group.Value, CalculatePercentage(group.Value) + "%");
}
var remaining = itemCounts.Skip(20).Sum(x => x.Value);
var remaining = itemCounts.Skip(pageLimit).Sum(x => x.Value);
if (remaining > 0)
{
table.Rows.Add("Other", remaining, CalculatePercentage(remaining) + "%");
Expand Down

0 comments on commit 7d02af3

Please sign in to comment.