app: redesign the panel and add limit forecasts and daily week use (#6)

Redesign the stats panel after the developer's feedback on v1 and add
limit forecasts, daily week use and token-based estimates (D7, D8).

- Panel: a fixed header with range and provider controls, limit tiles
  with pace chips, meters and forecasts, week use per day, token KPIs
  and charts, one burn-up chart per limit window with past instances,
  and a sortable breakdown whose "Other" row expands. One tooltip
  component for every chart, tile and table.
- Core (D7): average and recent-rate forecasts, the reach date whenever
  use is over pace, days of a long window with even pace and daily
  budget, window instances derived from resets with a 15-minute
  tolerance, and estimates from price-weighted tokens where samples are
  missing. Forecasts start at 10 % of a window, or as soon as it is
  ahead, so a Codex week far ahead right after its reset warns too.
- Settings, popups and the version (1.1.0) follow the new design.

Closes #5
Closes #7
This commit was merged in pull request #6.
This commit is contained in:
2026-10-15 19:06:04 +02:00
parent f8afd11679
commit adb8f5b88d
31 changed files with 2461 additions and 246 deletions
+49
View File
@@ -0,0 +1,49 @@
namespace FinalToken.Core;
public sealed record RateForecast(double PercentPerDay, double UseAtReset, DateTimeOffset? ReachesLimit,
double Increase = 0, TimeSpan Span = default);
public sealed record LimitDay(DateTimeOffset Start, DateTimeOffset End, double? Used, double? Cumulative,
bool IsToday, bool IsFuture, UseOrigin Origin = UseOrigin.Measured);
public sealed record WindowDays(IReadOnlyList<LimitDay> Current, IReadOnlyList<LimitDay> Previous,
double? Today, double EvenPace, double? DailyBudget, UseOrigin TodayOrigin = UseOrigin.Measured);
/// <summary>Pure D7 calculations. Missing baselines use marked token estimates when calibration is available.</summary>
public static partial class LimitAnalysis
{
/// <summary>D7: minute-rounded probe resets may drift within 15 minutes in either direction.</summary>
public static bool SameInstance(LimitWindow first, LimitWindow second) =>
first.Provider == second.Provider && first.Id == second.Id &&
first.ResetsAt is { } firstReset && second.ResetsAt is { } secondReset &&
(firstReset - secondReset).Duration() <= TimeSpan.FromMinutes(15);
public static double? AverageUseAtReset(LimitWindow window, DateTimeOffset now, PaceResult? pace = null)
{
pace ??= Pace.Calculate(window, now);
return Pace.CanForecast(window, now, pace) && pace.ElapsedFraction is > 0 and var elapsed ? window.UsedPercent / elapsed : null;
}
public static RateForecast? Recent(LimitWindow window, IEnumerable<LimitWindow> samples, DateTimeOffset now, PaceResult? pace = null)
{
pace ??= Pace.Calculate(window, now);
if (!Pace.CanForecast(window, now, pace) || window.ResetsAt is not { } reset || window.Length is not { Ticks: > 0 } length || reset <= now) return null;
var points = Instance(window, samples).Append(window).Where(s => s.SampleTime >= now.AddHours(-24) &&
s.SampleTime >= reset - length && s.SampleTime <= now).OrderBy(s => s.SampleTime).ToArray();
if (points.Length < 2) return null;
var first = points[0]; var last = points[^1];
var span = last.SampleTime - first.SampleTime;
if (span < TimeSpan.FromHours(3) || points.Zip(points.Skip(1)).Any(p => p.Second.UsedPercent < p.First.UsedPercent)) return null;
var rate = (last.UsedPercent - first.UsedPercent) / span.TotalDays;
var projected = window.UsedPercent + rate * (reset - now).TotalDays;
DateTimeOffset? reaches = null;
if (rate > 0 && projected > 100)
reaches = now.AddDays((100 - window.UsedPercent) / rate);
return new(rate, projected, reaches, last.UsedPercent - first.UsedPercent, span);
}
public static WindowDays? Days(LimitWindow window, IEnumerable<LimitWindow> samples, DateTimeOffset now, TimeZoneInfo? zone = null)
=> Analyze(window, samples, [], now, zone)?.Days;
private static IEnumerable<LimitWindow> Instance(LimitWindow window, IEnumerable<LimitWindow> samples) =>
samples.Where(s => SameInstance(window, s) &&
double.IsFinite(s.UsedPercent) && s.UsedPercent is >= 0 and <= 100);
}