Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
72 lines
2.6 KiB
C#
72 lines
2.6 KiB
C#
using Febe.Core.Abstractions;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Engine;
|
|
using Febe.Core.Storage;
|
|
|
|
namespace Febe.Tests.Engine.Support;
|
|
|
|
/// <summary>
|
|
/// Independent check of the budgets from <c>action_log</c>: at every action, the rolling hour and the local day so far
|
|
/// must stay within the profile's full limits (reductions only make them lower).
|
|
/// </summary>
|
|
internal static class BudgetAudit
|
|
{
|
|
private static readonly TimeSpan Hour = TimeSpan.FromMinutes(60);
|
|
|
|
public static async Task<IReadOnlyList<string>> OverrunsAsync(IFebeStorage storage, CautionProfile profile, TimeZoneInfo zone, DateTimeOffset from, DateTimeOffset to)
|
|
{
|
|
var actions = await storage.ActionLog.ListAsync(from, to);
|
|
var problems = new List<string>();
|
|
foreach (var kind in Enum.GetValues<ActionKind>())
|
|
{
|
|
var list = actions.Where(a => a.Category == kind).OrderBy(a => a.At).ThenBy(a => a.Id).ToList();
|
|
var first = 0;
|
|
long hour = 0;
|
|
long day = 0;
|
|
DateOnly? current = null;
|
|
foreach (var action in list)
|
|
{
|
|
hour += action.Amount;
|
|
while (list[first].At <= action.At - Hour)
|
|
{
|
|
hour -= list[first].Amount;
|
|
first++;
|
|
}
|
|
|
|
var today = LocalCalendar.Today(action.At, zone);
|
|
if (today != current)
|
|
{
|
|
current = today;
|
|
day = 0;
|
|
}
|
|
|
|
day += action.Amount;
|
|
var over = kind == ActionKind.DataDownload
|
|
? day > profile.DataPerDayBytes
|
|
: hour > profile.Budgets[kind].PerHour || day > profile.Budgets[kind].PerDay;
|
|
if (over)
|
|
{
|
|
problems.Add($"{kind} at {action.At:O}: hour {hour}, day {day}");
|
|
}
|
|
}
|
|
}
|
|
|
|
return problems;
|
|
}
|
|
|
|
/// <summary>Actions (data excluded) that fall outside the active window of their day.</summary>
|
|
public static async Task<IReadOnlyList<ActionLogRecord>> OutsideWindowAsync(IFebeStorage storage, DayPlanner planner, DateTimeOffset from, DateTimeOffset to)
|
|
{
|
|
var outside = new List<ActionLogRecord>();
|
|
foreach (var action in (await storage.ActionLog.ListAsync(from, to)).Where(a => a.Category != ActionKind.DataDownload))
|
|
{
|
|
if (await planner.GetWindowAtAsync(action.At) is null)
|
|
{
|
|
outside.Add(action);
|
|
}
|
|
}
|
|
|
|
return outside;
|
|
}
|
|
}
|