Restructure solution into folders and fix persistence issues
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37987a394a
commit
7c39cb5562
62 files changed
+219
-151
No files matched your search
+3
-1
@@ -1,4 +1,6 @@
|
||||
namespace Flypaper.Storage;
|
||||
using Flypaper.Storage.Entities;
|
||||
|
||||
namespace Flypaper.Storage.Dashboard;
|
||||
|
||||
public sealed record DashboardData(
|
||||
int TotalCatches,
|
||||
+4
-10
@@ -1,7 +1,8 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Dashboard;
|
||||
|
||||
public sealed class DashboardQueries(IDbContextFactory<FlypaperDbContext> dbContextFactory, TimeProvider timeProvider)
|
||||
{
|
||||
@@ -15,20 +16,13 @@ public sealed class DashboardQueries(IDbContextFactory<FlypaperDbContext> dbCont
|
||||
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 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));
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Entities;
|
||||
|
||||
public sealed class SettingEntity
|
||||
{
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Models;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Entities;
|
||||
|
||||
public sealed class TrapEventEntity
|
||||
{
|
||||
@@ -44,5 +44,5 @@ public sealed class TrapEventEntity
|
||||
};
|
||||
|
||||
private static string? Truncate(string? value, int maxLength) =>
|
||||
value is { Length: > 0 } && value.Length > maxLength ? value[..maxLength] : value;
|
||||
value?.Length > maxLength ? value[..maxLength] : value;
|
||||
}
|
||||
+3
-2
@@ -1,7 +1,8 @@
|
||||
using System.Threading.Channels;
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Events;
|
||||
|
||||
public sealed class ChannelTrapEventSink : ITrapEventSink
|
||||
{
|
||||
+3
-2
@@ -1,9 +1,10 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Events;
|
||||
|
||||
internal sealed class TrapEventWriter(
|
||||
ChannelTrapEventSink sink,
|
||||
@@ -1,3 +1,4 @@
|
||||
using Flypaper.Storage.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
@@ -10,6 +11,9 @@ public sealed class FlypaperDbContext(DbContextOptions<FlypaperDbContext> option
|
||||
|
||||
public DbSet<SettingEntity> Settings => Set<SettingEntity>();
|
||||
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) =>
|
||||
configurationBuilder.Properties<DateTime>().HaveConversion<UtcDateTimeConverter>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
var trapEvent = modelBuilder.Entity<TrapEventEntity>();
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Storage.Dashboard;
|
||||
using Flypaper.Storage.Events;
|
||||
using Flypaper.Storage.Settings;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -39,6 +42,6 @@ public static class FlypaperStorageExtensions
|
||||
await db.Database.MigrateAsync(ct);
|
||||
}
|
||||
|
||||
services.GetService<DbConfigurationSource>()?.Reload();
|
||||
services.GetService<DbConfigurationSource>()?.TryReload();
|
||||
}
|
||||
}
|
||||
+17
-10
@@ -1,22 +1,27 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Settings;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Settings;
|
||||
|
||||
internal sealed class DbConfigurationProvider(string connectionString) : ConfigurationProvider
|
||||
{
|
||||
public override void Load() => Data = Read();
|
||||
public override void Load() => Data = TryRead() ?? new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public void Reload()
|
||||
public bool TryReload()
|
||||
{
|
||||
Load();
|
||||
if (TryRead() is not { } data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Data = data;
|
||||
OnReload();
|
||||
return true;
|
||||
}
|
||||
|
||||
private Dictionary<string, string?> Read()
|
||||
private Dictionary<string, string?>? TryRead()
|
||||
{
|
||||
var data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
var builder = new SqliteConnectionStringBuilder(connectionString) { Mode = SqliteOpenMode.ReadOnly };
|
||||
|
||||
try
|
||||
@@ -26,6 +31,8 @@ internal sealed class DbConfigurationProvider(string connectionString) : Configu
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT \"Key\", \"Value\" FROM \"Settings\"";
|
||||
using var reader = command.ExecuteReader();
|
||||
|
||||
var data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
while (reader.Read())
|
||||
{
|
||||
var key = reader.GetString(0);
|
||||
@@ -34,12 +41,12 @@ internal sealed class DbConfigurationProvider(string connectionString) : Configu
|
||||
data[key] = reader.GetString(1);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
catch (SqliteException)
|
||||
{
|
||||
data.Clear();
|
||||
return null;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Settings;
|
||||
|
||||
public sealed class DbConfigurationSource(string connectionString) : IConfigurationSource
|
||||
{
|
||||
@@ -9,5 +9,5 @@ public sealed class DbConfigurationSource(string connectionString) : IConfigurat
|
||||
public IConfigurationProvider Build(IConfigurationBuilder builder) =>
|
||||
_provider = new DbConfigurationProvider(connectionString);
|
||||
|
||||
public void Reload() => _provider?.Reload();
|
||||
public bool TryReload() => _provider?.TryReload() ?? false;
|
||||
}
|
||||
+21
-5
@@ -1,9 +1,14 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Settings;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
namespace Flypaper.Storage.Settings;
|
||||
|
||||
public sealed class RuntimeSettingsStore(IDbContextFactory<FlypaperDbContext> dbContextFactory, DbConfigurationSource source)
|
||||
public sealed partial class RuntimeSettingsStore(
|
||||
IDbContextFactory<FlypaperDbContext> dbContextFactory,
|
||||
DbConfigurationSource source,
|
||||
ILogger<RuntimeSettingsStore> logger)
|
||||
{
|
||||
public async Task SaveAsync(RuntimeSettings settings, CancellationToken ct = default)
|
||||
{
|
||||
@@ -13,14 +18,14 @@ public sealed class RuntimeSettingsStore(IDbContextFactory<FlypaperDbContext> db
|
||||
db.Settings.AddRange(settings.ToConfiguration().Select(pair => new SettingEntity { Key = pair.Key, Value = pair.Value }));
|
||||
await db.SaveChangesAsync(ct);
|
||||
await transaction.CommitAsync(ct);
|
||||
source.Reload();
|
||||
Reload();
|
||||
}
|
||||
|
||||
public async Task ResetAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
await db.Settings.ExecuteDeleteAsync(ct);
|
||||
source.Reload();
|
||||
Reload();
|
||||
}
|
||||
|
||||
public async Task<bool> HasOverridesAsync(CancellationToken ct = default)
|
||||
@@ -28,4 +33,15 @@ public sealed class RuntimeSettingsStore(IDbContextFactory<FlypaperDbContext> db
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
return await db.Settings.AnyAsync(ct);
|
||||
}
|
||||
|
||||
private void Reload()
|
||||
{
|
||||
if (!source.TryReload())
|
||||
{
|
||||
LogReloadFailed();
|
||||
}
|
||||
}
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Warning, Message = "Settings were saved but could not be reloaded from the database; previous values stay active")]
|
||||
private partial void LogReloadFailed();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
internal sealed class UtcDateTimeConverter() : ValueConverter<DateTime, DateTime>(
|
||||
value => value.Kind == DateTimeKind.Utc ? value : value.ToUniversalTime(),
|
||||
value => DateTime.SpecifyKind(value, DateTimeKind.Utc));
|
||||
Reference in new issue
Block a user