test+chore: real-SQLite query coverage, batch detect writes, finish date centralization
Review follow-ups: - (HIGH) Add real-SQLite round-trip tests for the new query methods so the load-bearing lexical O-format date ordering is verified, not just mocked: Anomaly ListByDateRange/CountSince, Snapshot CountSince/ListByEvents grouping, Event Query/GetMany. - (MED) DetectAnomaliesUseCase: one SaveChanges per event instead of per anomaly. - (LOW) Route PlacedBetRepository + ExcelExporter date bounds through SqliteDateText. - (LOW) Backtest: reject a one-sided date range (was silently ignored). - (LOW) Refresh stale comments after the detector fan-out.
This commit is contained in:
@@ -14,7 +14,7 @@ namespace Marathon.Application.UseCases;
|
||||
/// <item>Loads all tracked events.</item>
|
||||
/// <item>For each event, fetches its last-24-hour live snapshots.</item>
|
||||
/// <item>Runs <see cref="AnomalyDetector"/> over the snapshot timeline.</item>
|
||||
/// <item>Persists any new anomalies that have not already been stored (dedup by EventId + DetectedAt minute-window).</item>
|
||||
/// <item>Persists any new anomalies that have not already been stored (dedup by EventId + Kind + DetectedAt minute-window).</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
@@ -138,8 +138,8 @@ public sealed class DetectAnomaliesUseCase
|
||||
List<Anomaly> existingForEvent,
|
||||
CancellationToken ct)
|
||||
{
|
||||
// Fan out over every detector kind; dedup below keys on EventId + Kind so the
|
||||
// flip and steam signals for one event persist independently.
|
||||
// Fan out over every detector; dedup below keys on EventId + Kind so the flip,
|
||||
// steam, and freeze signals for one event persist independently.
|
||||
var detected = detectors
|
||||
.SelectMany(d => d.Detect(ev.Id, snapshots))
|
||||
.ToList();
|
||||
@@ -154,11 +154,15 @@ public sealed class DetectAnomaliesUseCase
|
||||
continue;
|
||||
|
||||
await _anomalyRepo.AddAsync(anomaly, ct);
|
||||
await _anomalyRepo.SaveChangesAsync(ct);
|
||||
existingForEvent.Add(anomaly); // Keep local list in sync so the same cycle doesn't re-add.
|
||||
persisted++;
|
||||
}
|
||||
|
||||
// One write per event rather than per anomaly — with three detectors an event
|
||||
// can yield several new anomalies in a single cycle.
|
||||
if (persisted > 0)
|
||||
await _anomalyRepo.SaveChangesAsync(ct);
|
||||
|
||||
return persisted;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,9 +25,10 @@ internal sealed class ExcelExporter : IExcelExporter
|
||||
string outputPath,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
// Load all snapshots in the date range with their bets eagerly
|
||||
var fromStr = range.From.ToString("O");
|
||||
var toStr = range.To.ToString("O");
|
||||
// Load all snapshots in the date range with their bets eagerly. Bounds use the
|
||||
// shared SqliteDateText encoding so they match the persisted CapturedAt keys.
|
||||
var fromStr = SqliteDateText.Key(range.From);
|
||||
var toStr = SqliteDateText.Key(range.To);
|
||||
|
||||
var snapshotEntities = await _db.Snapshots.AsNoTracking()
|
||||
.Include(s => s.Bets)
|
||||
|
||||
@@ -40,10 +40,10 @@ internal sealed class PlacedBetRepository : IPlacedBetRepository
|
||||
|
||||
public async Task<IReadOnlyList<PlacedBet>> ListByDateRangeAsync(DateRange range, CancellationToken ct = default)
|
||||
{
|
||||
// PlacedAt is stored as ISO 8601 TEXT — same lexical-equals-chronological ordering
|
||||
// trick used in EventRepository.ListByDateRangeAsync.
|
||||
var fromStr = range.From.ToString("O");
|
||||
var toStr = range.To.ToString("O");
|
||||
// PlacedAt is stored via SqliteDateText (O-format TEXT) — same lexical-equals-
|
||||
// chronological ordering used across the repositories.
|
||||
var fromStr = SqliteDateText.Key(range.From);
|
||||
var toStr = SqliteDateText.Key(range.To);
|
||||
|
||||
var entities = await _db.PlacedBets.AsNoTracking()
|
||||
.Where(b => b.PlacedAt.CompareTo(fromStr) >= 0
|
||||
|
||||
@@ -33,6 +33,10 @@ public sealed class BacktestForm
|
||||
{
|
||||
if (StartingBankroll <= 0m) { error = "Bankroll must be positive."; return false; }
|
||||
if (MinScore is < 0m or > 1m) { error = "Min score must be in [0, 1]."; return false; }
|
||||
// A one-sided range would be silently ignored (ToDateRange needs both bounds), so
|
||||
// require both-or-neither and give the user explicit feedback.
|
||||
if (From.HasValue != To.HasValue)
|
||||
{ error = "Set both From and To dates, or leave both empty."; return false; }
|
||||
if (From is { } f && To is { } t && f.Date > t.Date)
|
||||
{ error = "From date must be on or before To date."; return false; }
|
||||
switch (StakeRule)
|
||||
|
||||
Reference in New Issue
Block a user