@@ -12,9 +12,61 @@ import type {
1212} from "@t3tools/contracts" ;
1313import { resolveProviderInstanceDisplayName } from "@t3tools/client-runtime/state/provider-instance-display" ;
1414
15+ /**
16+ * Where a window ends up if the account keeps its pace since the last reset:
17+ * empty before the reset, or at the reset with allowance to spare.
18+ */
19+ export type UsageLimitForecast =
20+ | { readonly type : "runs-out" ; readonly at : string ; readonly resetsAt : string }
21+ | { readonly type : "lasts" ; readonly unusedPercent : number ; readonly resetsAt : string } ;
22+
23+ /** A pace measured over less than this says nothing yet. */
24+ const FORECAST_MIN_ELAPSED_MS = 60 * 60_000 ;
25+
26+ /**
27+ * `points` are one window's readings, oldest first. The pace is taken from the
28+ * readings since the last drop, which is the current cycle. No forecast
29+ * without an announced reset still ahead, or before an hour of the cycle has
30+ * been recorded.
31+ */
32+ export function forecastUsageWindow (
33+ points : ReadonlyArray < UsageLimitHistoryPoint > ,
34+ nowMs : number ,
35+ ) : UsageLimitForecast | null {
36+ const last = points . at ( - 1 ) ;
37+ if ( last === undefined || last . resetsAt === undefined ) return null ;
38+ const resetsAtMs = Date . parse ( last . resetsAt ) ;
39+ if ( ! Number . isFinite ( resetsAtMs ) || resetsAtMs <= nowMs ) return null ;
40+ let start = last ;
41+ for ( let index = points . length - 2 ; index >= 0 ; index -= 1 ) {
42+ const point = points [ index ] ! ;
43+ if ( point . usedPercent > start . usedPercent ) break ;
44+ start = point ;
45+ }
46+ const elapsedMs = nowMs - Date . parse ( start . at ) ;
47+ if ( ! ( elapsedMs >= FORECAST_MIN_ELAPSED_MS ) ) return null ;
48+ const pacePerMs = ( last . usedPercent - start . usedPercent ) / elapsedMs ;
49+ const usedAtReset = last . usedPercent + pacePerMs * ( resetsAtMs - nowMs ) ;
50+ if ( usedAtReset < 100 ) {
51+ return {
52+ type : "lasts" ,
53+ unusedPercent : Math . round ( 100 - usedAtReset ) ,
54+ resetsAt : last . resetsAt ,
55+ } ;
56+ }
57+ const emptyAtMs = pacePerMs <= 0 ? nowMs : nowMs + ( 100 - last . usedPercent ) / pacePerMs ;
58+ return {
59+ type : "runs-out" ,
60+ at : new Date ( Math . min ( emptyAtMs , resetsAtMs ) ) . toISOString ( ) ,
61+ resetsAt : last . resetsAt ,
62+ } ;
63+ }
64+
1565export interface UsageLimitTrendLine {
1666 readonly windowId : string ;
1767 readonly label : string ;
68+ /** Absent when there is not enough to go on. */
69+ readonly forecast ?: UsageLimitForecast ;
1870 /** Remaining allowance now, by the last recorded reading. */
1971 readonly remainingPercent : number ;
2072 /**
@@ -95,7 +147,9 @@ export function buildUsageLimitTrend(input: {
95147 ( left , right ) => Date . parse ( left . at ) - Date . parse ( right . at ) ,
96148 ) ;
97149 const last = sorted . at ( - 1 ) ! ;
150+ const forecast = forecastUsageWindow ( sorted , input . nowMs ) ;
98151 return {
152+ ...( forecast === null ? { } : { forecast } ) ,
99153 windowId,
100154 label : last . label ,
101155 remainingPercent : Math . max ( 0 , 100 - last . usedPercent ) ,
0 commit comments