Add trap logging, dashboard, EF migrations and Dockerfile
This commit is contained in:
1 parent
f9161f48e7
commit
b561924efa
26 files changed
+784
-62
No files matched your search
@@ -0,0 +1,13 @@
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
public sealed record DashboardData(
|
||||
int TotalCatches,
|
||||
int CatchesLast24Hours,
|
||||
int Rejected,
|
||||
int UniqueIps,
|
||||
TimeSpan TotalHoldTime,
|
||||
IReadOnlyList<TrapEventEntity> Recent,
|
||||
IReadOnlyList<TopEntry> TopIps,
|
||||
IReadOnlyList<TopEntry> TopTargets);
|
||||
|
||||
public sealed record TopEntry(string Key, int Count, TimeSpan HoldTime);
|
||||
@@ -0,0 +1,49 @@
|
||||
using Flypaper.Core;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
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);
|
||||
|
||||
var recent = await events.OrderByDescending(e => e.Id).Take(RecentCount).ToListAsync(ct);
|
||||
foreach (var trapEvent in recent)
|
||||
{
|
||||
trapEvent.StartedAt = DateTime.SpecifyKind(trapEvent.StartedAt, DateTimeKind.Utc);
|
||||
trapEvent.EndedAt = DateTime.SpecifyKind(trapEvent.EndedAt, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
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)),
|
||||
recent,
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
internal sealed class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<FlypaperDbContext>
|
||||
{
|
||||
public FlypaperDbContext CreateDbContext(string[] args) =>
|
||||
new(new DbContextOptionsBuilder<FlypaperDbContext>().UseSqlite("Data Source=flypaper.db").Options);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -12,6 +12,14 @@ public static class FlypaperStorageExtensions
|
||||
services.AddSingleton<ChannelTrapEventSink>();
|
||||
services.AddSingleton<ITrapEventSink>(sp => sp.GetRequiredService<ChannelTrapEventSink>());
|
||||
services.AddHostedService<TrapEventWriter>();
|
||||
services.AddSingleton<DashboardQueries>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static async Task MigrateFlypaperDatabaseAsync(this IServiceProvider services, CancellationToken ct = default)
|
||||
{
|
||||
var factory = services.GetRequiredService<IDbContextFactory<FlypaperDbContext>>();
|
||||
await using var db = await factory.CreateDbContextAsync(ct);
|
||||
await db.Database.MigrateAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Flypaper.Storage;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Flypaper.Storage.Migrations
|
||||
{
|
||||
[DbContext(typeof(FlypaperDbContext))]
|
||||
[Migration("20261114122204_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.15");
|
||||
|
||||
modelBuilder.Entity("Flypaper.Storage.TrapEventEntity", b =>
|
||||
{
|
||||
b.Property<long>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("BytesSent")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ClientInfo")
|
||||
.HasMaxLength(2048)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("DurationMs")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime>("EndedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Outcome")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Protocol")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("RemoteIp")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("SessionId")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("StartedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Strategy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Target")
|
||||
.HasMaxLength(2048)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RemoteIp");
|
||||
|
||||
b.HasIndex("StartedAt");
|
||||
|
||||
b.ToTable("TrapEvents");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Flypaper.Storage.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "TrapEvents",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<long>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
SessionId = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
Protocol = table.Column<string>(type: "TEXT", maxLength: 16, nullable: false),
|
||||
RemoteIp = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
|
||||
Strategy = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
|
||||
StartedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
|
||||
EndedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
|
||||
DurationMs = table.Column<long>(type: "INTEGER", nullable: false),
|
||||
BytesSent = table.Column<long>(type: "INTEGER", nullable: false),
|
||||
Outcome = table.Column<string>(type: "TEXT", maxLength: 16, nullable: false),
|
||||
Target = table.Column<string>(type: "TEXT", maxLength: 2048, nullable: true),
|
||||
ClientInfo = table.Column<string>(type: "TEXT", maxLength: 2048, nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_TrapEvents", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_TrapEvents_RemoteIp",
|
||||
table: "TrapEvents",
|
||||
column: "RemoteIp");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_TrapEvents_StartedAt",
|
||||
table: "TrapEvents",
|
||||
column: "StartedAt");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "TrapEvents");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Flypaper.Storage;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Flypaper.Storage.Migrations
|
||||
{
|
||||
[DbContext(typeof(FlypaperDbContext))]
|
||||
partial class FlypaperDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.15");
|
||||
|
||||
modelBuilder.Entity("Flypaper.Storage.TrapEventEntity", b =>
|
||||
{
|
||||
b.Property<long>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("BytesSent")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ClientInfo")
|
||||
.HasMaxLength(2048)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("DurationMs")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime>("EndedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Outcome")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Protocol")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("RemoteIp")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("SessionId")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("StartedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Strategy")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Target")
|
||||
.HasMaxLength(2048)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RemoteIp");
|
||||
|
||||
b.HasIndex("StartedAt");
|
||||
|
||||
b.ToTable("TrapEvents");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,6 @@ internal sealed class TrapEventWriter(
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await using (var db = await dbContextFactory.CreateDbContextAsync(stoppingToken))
|
||||
{
|
||||
await db.Database.EnsureCreatedAsync(stoppingToken);
|
||||
}
|
||||
|
||||
var batch = new List<TrapEvent>(MaxBatchSize);
|
||||
while (await sink.Reader.WaitToReadAsync(stoppingToken))
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user