core: add the reset history, API list prices and window series
The developer wants figures for every reset: how long an instance lasted, its tokens, its final use, its API value, what it left unused and what an early reset handed out (D12, D13). Instances come from one window series, so a Codex limit that moves between the primary and secondary slot stays one history (D7). Building the spans once also fixes two D7 cases: the hour in which one instance ends and the next starts goes to the new instance instead of being lost, and a late sample of an old instance no longer places it after a newer one. Refs #14 Refs #9
This commit is contained in:
@@ -24,26 +24,12 @@ public static partial class LimitAnalysis
|
||||
{
|
||||
if (window.ResetsAt is not { } reset || window.Length is not { Ticks: > 0 } length || Pace.Calculate(window, now).State == PaceState.Unknown) return null;
|
||||
zone ??= TimeZoneInfo.Local;
|
||||
var observations = samples.Append(window).Where(s => s.Provider == window.Provider && s.Id == window.Id &&
|
||||
s.ResetsAt is not null && s.Length is { Ticks: > 0 } && s.SampleTime <= now && double.IsFinite(s.UsedPercent) && s.UsedPercent is >= 0 and <= 100).OrderBy(s => s.SampleTime).ToArray();
|
||||
var weighted = tokens.Where(p => TokenWeights.Matches(window, p.Model)).GroupBy(p => p.Time)
|
||||
.Select(g => (Time: g.Key, Weight: g.Sum(p => TokenWeights.Weighted(window.Provider, p.Counts)))).OrderBy(p => p.Time).ToArray();
|
||||
// D7: samples identify real instances; resets and lengths define their boundaries.
|
||||
var groups = new List<List<LimitWindow>> { new() { window } };
|
||||
foreach (var observation in observations)
|
||||
{
|
||||
var group = groups.FirstOrDefault(g => SameInstance(g[0], observation));
|
||||
if (group is null) groups.Add([observation]); else group.Add(observation);
|
||||
}
|
||||
var spans = groups.Select(g =>
|
||||
{
|
||||
var anchor = SameInstance(g[0], window) ? window : g[^1];
|
||||
return (Anchor: anchor, Start: anchor.ResetsAt!.Value - anchor.Length!.Value, End: anchor.ResetsAt.Value);
|
||||
}).OrderBy(g => g.Start).ToArray();
|
||||
for (var i = 0; i + 1 < spans.Length; i++)
|
||||
if (spans[i + 1].Start < spans[i].End) spans[i].End = spans[i + 1].Start;
|
||||
var spans = RealSpans(window, samples, now);
|
||||
var observations = spans.SelectMany(s => s.Samples).ToArray();
|
||||
var weighted = WeightedTokens(window, tokens);
|
||||
var currentSpan = spans.Single(g => SameInstance(g.Anchor, window));
|
||||
var current = Build(window, currentSpan.Start, currentSpan.End, now < currentSpan.End ? now : currentSpan.End, null);
|
||||
var current = BuildInstance(window, observations, weighted, currentSpan.Start, currentSpan.End,
|
||||
now < currentSpan.End ? now : currentSpan.End, null, TokenEnd(currentSpan.End, spans), now, zone);
|
||||
var realPast = spans.Where(g => g.Start < currentSpan.Start).OrderByDescending(g => g.Start).ToArray();
|
||||
var past = new List<LimitInstance>();
|
||||
var realIndex = 0;
|
||||
@@ -57,8 +43,9 @@ public static partial class LimitAnalysis
|
||||
// A shorter gap stays unassigned. Sessions never produce inferred candidates.
|
||||
var span = realIndex < realPast.Length && (!canInfer || realPast[realIndex].End > inferredStart)
|
||||
? realPast[realIndex++]
|
||||
: (Anchor: window with { ResetsAt = cursor }, Start: inferredStart, End: cursor);
|
||||
var instance = Build(span.Anchor, span.Start, span.End, span.End < now ? span.End : now, current.Scale);
|
||||
: new InstanceSpan(window with { ResetsAt = cursor }, [], inferredStart, cursor);
|
||||
var instance = BuildInstance(span.Anchor, observations, weighted, span.Start, span.End,
|
||||
span.End < now ? span.End : now, current.Scale, TokenEnd(span.End, spans), now, zone);
|
||||
if (instance.HasData) past.Add(instance);
|
||||
cursor = span.Start;
|
||||
}
|
||||
@@ -70,75 +57,76 @@ public static partial class LimitAnalysis
|
||||
reset > now ? (100 - window.UsedPercent) / (reset - now).TotalDays : null, today?.Origin ?? UseOrigin.Measured);
|
||||
}
|
||||
return new(current, past, days);
|
||||
}
|
||||
|
||||
LimitInstance Build(LimitWindow anchor, DateTimeOffset start, DateTimeOffset end, DateTimeOffset until,
|
||||
double? fallback)
|
||||
private static LimitInstance BuildInstance(LimitWindow anchor, IReadOnlyList<LimitWindow> observations,
|
||||
(DateTimeOffset Time, double Weight)[] weighted, DateTimeOffset start, DateTimeOffset end, DateTimeOffset until,
|
||||
double? fallback, DateTimeOffset tokenEnd, DateTimeOffset now, TimeZoneInfo zone)
|
||||
{
|
||||
double Weight(DateTimeOffset from, DateTimeOffset to)
|
||||
{
|
||||
double Weight(DateTimeOffset from, DateTimeOffset to)
|
||||
{
|
||||
var hour = Hour(from < start ? start : from);
|
||||
var exclusiveEnd = Hour(end);
|
||||
return weighted.Where(p => p.Time >= hour && p.Time < to && p.Time < exclusiveEnd).Sum(p => p.Weight);
|
||||
}
|
||||
var measured = Instance(anchor, observations).Where(s => s.SampleTime >= start - TimeSpan.FromMinutes(15) && s.SampleTime <= until)
|
||||
.GroupBy(s => s.SampleTime).Select(g => g.Last()).OrderBy(s => s.SampleTime).ToArray();
|
||||
var last = measured.LastOrDefault();
|
||||
var denominator = last is null ? 0 : Weight(start, last.SampleTime);
|
||||
var total = Weight(start, until);
|
||||
double? scale = total <= 0 ? null : last is not null ? denominator > 0 ? last.UsedPercent / denominator : null : fallback;
|
||||
var points = new SortedDictionary<DateTimeOffset, LimitPoint> { [start] = new(start, 0, UseOrigin.Measured) };
|
||||
if (scale is { } k)
|
||||
{
|
||||
foreach (var time in weighted.Where(p => p.Time >= Hour(start) && p.Time < until && p.Time < Hour(end)).SelectMany(p => new[] { p.Time, p.Time.AddHours(1) }).Append(until).Distinct())
|
||||
{
|
||||
var cutoff = time < until ? time : until;
|
||||
if (cutoff <= start) continue;
|
||||
// Samples own the measured span; tokens fill before the first sample and after the last.
|
||||
if (measured.Length == 0 || cutoff < measured[0].SampleTime)
|
||||
points[cutoff] = new(cutoff, k * Weight(start, cutoff), UseOrigin.Estimated);
|
||||
else if (last is not null && cutoff > last.SampleTime)
|
||||
points[cutoff] = new(cutoff, last.UsedPercent + k * (Weight(start, cutoff) - Weight(start, last.SampleTime)), UseOrigin.Estimated);
|
||||
}
|
||||
}
|
||||
foreach (var sample in measured) points[sample.SampleTime] = new(sample.SampleTime, sample.UsedPercent, UseOrigin.Measured);
|
||||
DateTimeOffset? reached = null; UseOrigin? reachedOrigin = null;
|
||||
var raw = points.Values.OrderBy(p => p.Time).ToArray();
|
||||
for (var i = 1; i < raw.Length; i++)
|
||||
{
|
||||
var point = raw[i]; var prior = raw[i - 1];
|
||||
if (reached is null && point.Used >= 100)
|
||||
{
|
||||
reached = point.Origin == UseOrigin.Measured || point.Used <= prior.Used ? point.Time
|
||||
: prior.Time.AddTicks((long)((point.Time - prior.Time).Ticks * Math.Clamp((100 - prior.Used) / (point.Used - prior.Used), 0, 1)));
|
||||
reachedOrigin = point.Origin;
|
||||
if (point.Origin == UseOrigin.Estimated) points[reached.Value] = new(reached.Value, 100, UseOrigin.Estimated);
|
||||
}
|
||||
if (point.Origin == UseOrigin.Estimated)
|
||||
points[point.Time] = point with { Used = reached is not null ? 100 : Math.Min(100, point.Used) };
|
||||
}
|
||||
double EstimatedAt(DateTimeOffset time, double factor) => reachedOrigin == UseOrigin.Estimated && time >= reached
|
||||
? 100 : Math.Min(100, factor * Weight(start, time));
|
||||
var daily = new List<LimitDay>();
|
||||
for (var day = CalendarDays.Start(start, zone); day < end; day = CalendarDays.NextStart(day, zone))
|
||||
{
|
||||
var next = CalendarDays.NextStart(day, zone); var dayEnd = next < end ? next : end;
|
||||
var cutoff = dayEnd < until ? dayEnd : until; var future = day > until;
|
||||
var baseline = day <= start ? 0 : Last(day);
|
||||
var cumulative = cutoff >= start ? Last(cutoff) : null;
|
||||
if (last is not null && scale is not null && cutoff > last.SampleTime && Weight(start, cutoff) > Weight(start, last.SampleTime)) cumulative = null;
|
||||
double? use = !future && baseline is { } b && cumulative is { } c ? Math.Max(0, c - b) : null;
|
||||
var origin = UseOrigin.Measured;
|
||||
if (!future && use is null && scale is { } factor)
|
||||
{
|
||||
use = Math.Max(0, EstimatedAt(cutoff, factor) - (day > start ? EstimatedAt(day, factor) : 0)); origin = UseOrigin.Estimated;
|
||||
cumulative ??= EstimatedAt(cutoff, factor);
|
||||
}
|
||||
daily.Add(new(day, dayEnd, use, future ? null : cumulative, day == CalendarDays.Start(now, zone), future, origin));
|
||||
}
|
||||
var series = points.Values.OrderBy(p => p.Time).ToArray();
|
||||
double? peak = series.Length > 1 ? series.Max(p => p.Used) : null;
|
||||
return new(start, anchor.ResetsAt!.Value, series, daily, scale, peak, reached, end, reachedOrigin);
|
||||
double? Last(DateTimeOffset time) => measured.LastOrDefault(s => s.SampleTime <= time)?.UsedPercent;
|
||||
var hour = Hour(from < start ? start : from);
|
||||
var exclusiveEnd = tokenEnd;
|
||||
return weighted.Where(p => p.Time >= hour && p.Time < to && p.Time < exclusiveEnd).Sum(p => p.Weight);
|
||||
}
|
||||
var measured = Instance(anchor, observations).Where(s => s.SampleTime >= start - TimeSpan.FromMinutes(15) && s.SampleTime <= until)
|
||||
.GroupBy(s => s.SampleTime).Select(g => g.Last()).OrderBy(s => s.SampleTime).ToArray();
|
||||
var last = measured.LastOrDefault();
|
||||
var denominator = last is null ? 0 : Weight(start, last.SampleTime);
|
||||
var total = Weight(start, until);
|
||||
double? scale = total <= 0 ? null : last is not null ? denominator > 0 ? last.UsedPercent / denominator : null : fallback;
|
||||
var points = new SortedDictionary<DateTimeOffset, LimitPoint> { [start] = new(start, 0, UseOrigin.Measured) };
|
||||
if (scale is { } k)
|
||||
{
|
||||
foreach (var time in weighted.Where(p => p.Time >= Hour(start) && p.Time < until && p.Time < tokenEnd).SelectMany(p => new[] { p.Time, p.Time.AddHours(1) }).Append(until).Distinct())
|
||||
{
|
||||
var cutoff = time < until ? time : until;
|
||||
if (cutoff <= start) continue;
|
||||
// Samples own the measured span; tokens fill before the first sample and after the last.
|
||||
if (measured.Length == 0 || cutoff < measured[0].SampleTime)
|
||||
points[cutoff] = new(cutoff, k * Weight(start, cutoff), UseOrigin.Estimated);
|
||||
else if (last is not null && cutoff > last.SampleTime)
|
||||
points[cutoff] = new(cutoff, last.UsedPercent + k * (Weight(start, cutoff) - Weight(start, last.SampleTime)), UseOrigin.Estimated);
|
||||
}
|
||||
}
|
||||
foreach (var sample in measured) points[sample.SampleTime] = new(sample.SampleTime, sample.UsedPercent, UseOrigin.Measured);
|
||||
DateTimeOffset? reached = null; UseOrigin? reachedOrigin = null;
|
||||
var raw = points.Values.OrderBy(p => p.Time).ToArray();
|
||||
for (var i = 1; i < raw.Length; i++)
|
||||
{
|
||||
var point = raw[i]; var prior = raw[i - 1];
|
||||
if (reached is null && point.Used >= 100)
|
||||
{
|
||||
reached = point.Origin == UseOrigin.Measured || point.Used <= prior.Used ? point.Time
|
||||
: prior.Time.AddTicks((long)((point.Time - prior.Time).Ticks * Math.Clamp((100 - prior.Used) / (point.Used - prior.Used), 0, 1)));
|
||||
reachedOrigin = point.Origin;
|
||||
if (point.Origin == UseOrigin.Estimated) points[reached.Value] = new(reached.Value, 100, UseOrigin.Estimated);
|
||||
}
|
||||
if (point.Origin == UseOrigin.Estimated)
|
||||
points[point.Time] = point with { Used = reached is not null ? 100 : Math.Min(100, point.Used) };
|
||||
}
|
||||
double EstimatedAt(DateTimeOffset time, double factor) => reachedOrigin == UseOrigin.Estimated && time >= reached
|
||||
? 100 : Math.Min(100, factor * Weight(start, time));
|
||||
var daily = new List<LimitDay>();
|
||||
for (var day = CalendarDays.Start(start, zone); day < end; day = CalendarDays.NextStart(day, zone))
|
||||
{
|
||||
var next = CalendarDays.NextStart(day, zone); var dayEnd = next < end ? next : end;
|
||||
var cutoff = dayEnd < until ? dayEnd : until; var future = day > until;
|
||||
var baseline = day <= start ? 0 : Last(day);
|
||||
var cumulative = cutoff >= start ? Last(cutoff) : null;
|
||||
if (last is not null && scale is not null && cutoff > last.SampleTime && Weight(start, cutoff) > Weight(start, last.SampleTime)) cumulative = null;
|
||||
double? use = !future && baseline is { } b && cumulative is { } c ? Math.Max(0, c - b) : null;
|
||||
var origin = UseOrigin.Measured;
|
||||
if (!future && use is null && scale is { } factor)
|
||||
{
|
||||
use = Math.Max(0, EstimatedAt(cutoff, factor) - (day > start ? EstimatedAt(day, factor) : 0)); origin = UseOrigin.Estimated;
|
||||
cumulative ??= EstimatedAt(cutoff, factor);
|
||||
}
|
||||
daily.Add(new(day, dayEnd, use, future ? null : cumulative, day == CalendarDays.Start(now, zone), future, origin));
|
||||
}
|
||||
var series = points.Values.OrderBy(p => p.Time).ToArray();
|
||||
double? peak = series.Length > 1 ? series.Max(p => p.Used) : null;
|
||||
return new(start, anchor.ResetsAt!.Value, series, daily, scale, peak, reached, end, reachedOrigin);
|
||||
double? Last(DateTimeOffset time) => measured.LastOrDefault(s => s.SampleTime <= time)?.UsedPercent;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user