Add data management, randomized HTML drip and public README
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3671019e60
commit
f5d8ca7484
46 files changed
+1601
-172
No files matched your search
@@ -0,0 +1,62 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Storage.Maintenance;
|
||||
|
||||
internal sealed partial class RetentionCleanup(
|
||||
TrapDataMaintenance maintenance,
|
||||
IOptionsMonitor<RetentionOptions> options,
|
||||
TimeProvider timeProvider,
|
||||
ILogger<RetentionCleanup> logger) : BackgroundService
|
||||
{
|
||||
private const int VacuumThreshold = 5_000;
|
||||
private static readonly TimeSpan Interval = TimeSpan.FromDays(1);
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
await CleanupAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
LogCleanupFailed(ex);
|
||||
}
|
||||
|
||||
await Task.Delay(Interval, timeProvider, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task<int> CleanupAsync(CancellationToken ct)
|
||||
{
|
||||
var days = options.CurrentValue.Days;
|
||||
if (days <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var cutoff = timeProvider.GetUtcNow().UtcDateTime.AddDays(-days);
|
||||
var deleted = await maintenance.DeleteOlderThanAsync(cutoff, ct);
|
||||
if (deleted == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
LogDeleted(deleted, days);
|
||||
if (deleted >= VacuumThreshold)
|
||||
{
|
||||
await maintenance.VacuumAsync(ct);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Information, Message = "Deleted {Count} trap events older than {Days} days")]
|
||||
private partial void LogDeleted(int count, int days);
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Warning, Message = "Retention cleanup failed")]
|
||||
private partial void LogCleanupFailed(Exception exception);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Flypaper.Storage.Maintenance;
|
||||
|
||||
public sealed class RetentionOptions
|
||||
{
|
||||
public const string SectionName = "Flypaper:Retention";
|
||||
|
||||
public int Days { get; set; } = 90;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Networking;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Flypaper.Storage.Maintenance;
|
||||
|
||||
public sealed class TrapDataMaintenance(IDbContextFactory<FlypaperDbContext> dbContextFactory)
|
||||
{
|
||||
private const int DeleteChunkSize = 500;
|
||||
|
||||
public async Task<TrapDataStats> GetStatsAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
var count = await db.TrapEvents.CountAsync(ct);
|
||||
var oldest = count == 0 ? (DateTime?)null : await db.TrapEvents.MinAsync(e => e.StartedAt, ct);
|
||||
return new TrapDataStats(count, oldest, DatabaseSize(db));
|
||||
}
|
||||
|
||||
public async Task<int> DeleteOlderThanAsync(DateTime cutoffUtc, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
return await db.TrapEvents.Where(e => e.StartedAt < cutoffUtc).ExecuteDeleteAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<int> DeletePrivateAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
var privateIps = (await db.TrapEvents.Select(e => e.RemoteIp).Distinct().ToListAsync(ct))
|
||||
.Where(ip => IPAddress.TryParse(ip, out var address) && PrivateNetworks.Contains(address))
|
||||
.ToList();
|
||||
|
||||
var deleted = 0;
|
||||
foreach (var chunk in privateIps.Chunk(DeleteChunkSize))
|
||||
{
|
||||
deleted += await db.TrapEvents.Where(e => chunk.Contains(e.RemoteIp)).ExecuteDeleteAsync(ct);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public async Task<int> DeleteAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
return await db.TrapEvents.ExecuteDeleteAsync(ct);
|
||||
}
|
||||
|
||||
public async Task VacuumAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
await db.Database.ExecuteSqlRawAsync("VACUUM", ct);
|
||||
}
|
||||
|
||||
private static long? DatabaseSize(FlypaperDbContext db)
|
||||
{
|
||||
var path = db.Database.GetDbConnection().DataSource;
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var wal = new FileInfo(path + "-wal");
|
||||
return new FileInfo(path).Length + (wal.Exists ? wal.Length : 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Flypaper.Storage.Maintenance;
|
||||
|
||||
public sealed record TrapDataStats(int Count, DateTime? Oldest, long? DatabaseBytes);
|
||||
Reference in new issue
Block a user