Restructure solution into folders and fix persistence issues

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Opus 5.5 committed 2026-11-14 14:10:24 +01:00
1 parent 37987a394a
commit 7c39cb5562
62 files changed
+219 -151

No files matched your search

@@ -0,0 +1,43 @@
using Flypaper.Core.Models;
using Flypaper.Storage.Entities;
using Microsoft.EntityFrameworkCore;
namespace Flypaper.Storage.Dashboard;
public sealed class DashboardQueries(IDbContextFactory<FlypaperDbContext> dbContextFactory, TimeProvider timeProvider)
{
private const int RecentCount = 50;
private const int TopCount = 10;
public async Task<DashboardData> GetAsync(CancellationToken ct = default)
{
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
var events = db.TrapEvents.AsNoTracking();
var catches = events.Where(e => e.Outcome != TrapOutcome.Rejected);
var since = timeProvider.GetUtcNow().UtcDateTime.AddHours(-24);
return new DashboardData(
await catches.CountAsync(ct),
await catches.CountAsync(e => e.StartedAt >= since, ct),
await events.CountAsync(e => e.Outcome == TrapOutcome.Rejected, ct),
await events.Select(e => e.RemoteIp).Distinct().CountAsync(ct),
TimeSpan.FromMilliseconds(await catches.SumAsync(e => e.DurationMs, ct)),
await events.OrderByDescending(e => e.Id).Take(RecentCount).ToListAsync(ct),
await TopAsync(catches.GroupBy(e => e.RemoteIp), ct),
await TopAsync(catches.Where(e => e.Target != null).GroupBy(e => e.Target!), ct));
}
private static async Task<IReadOnlyList<TopEntry>> TopAsync(
IQueryable<IGrouping<string, TrapEventEntity>> groups,
CancellationToken ct)
{
var rows = await groups
.Select(g => new { g.Key, Count = g.Count(), DurationMs = g.Sum(e => e.DurationMs) })
.OrderByDescending(x => x.Count)
.ThenByDescending(x => x.DurationMs)
.Take(TopCount)
.ToListAsync(ct);
return rows.Select(x => new TopEntry(x.Key, x.Count, TimeSpan.FromMilliseconds(x.DurationMs))).ToList();
}
}