How fast the week is being spent right now — list $ per hour over the last 60 min from the hourly plan-meter bins — against the pace that reaches the week target at the reset. GET /api/usage/burn (usage/burn.py) for every account with a weekTarget; BurnGauge tile beside its week gauge: needle vs the needed pace at the top of the dial, amber + snail behind, green + flame on pace, "need $X/h" and "→ N% at reset". Polled every minute. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
4.3 KiB
Python
111 lines
4.3 KiB
Python
"""The burn rate: how fast an account with a week target is spending its
|
|
week right now, against the pace that would reach the target at the reset.
|
|
|
|
"Right now" is the last 60 minutes, read off the plan meter's hourly bins
|
|
(``calibrate.meters``: list $ and plan units per epoch hour, every run of the
|
|
account plus the hub's side calls): the current hour's bin whole, plus the
|
|
share of the previous hour's that falls inside the window (spread evenly over
|
|
that hour). Units become % of the week through the rate in force
|
|
(``calibrate.current``).
|
|
|
|
The pace needed is a straight line from where the week is now to its goal at
|
|
the reset (``weekTarget.goal``, the personal Max: 100): ``(goal - pct) /
|
|
hours left``, in % per hour and, through the same rate, $ per hour. The home
|
|
page's burn-rate tile draws the first against the second as a speedometer.
|
|
|
|
What claude.ai or an unpaired machine spends never reaches the meter, so the
|
|
rate here only counts what the hub archives — the same blind spot as the
|
|
Plan usage dashboard's "untracked" share.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from . import calibrate, target
|
|
|
|
HOUR = 3600
|
|
WINDOW_S = HOUR # "right now" = the last 60 minutes
|
|
RATIO_DAYS = 7 # $ per unit when the fit has no usdPerPct
|
|
|
|
|
|
def rolling(bins: dict[int, list[float]], now: float, span: float = WINDOW_S
|
|
) -> tuple[float, float]:
|
|
"""(list $, plan units) over the ``span`` seconds before ``now`` from
|
|
hourly bins — whole hours inside the span, the partly covered oldest one
|
|
prorated."""
|
|
start = now - span
|
|
usd = units = 0.0
|
|
for h in range(int(start // HOUR), int(now // HOUR) + 1):
|
|
v = bins.get(h)
|
|
if not v:
|
|
continue
|
|
lo, hi = max(start, h * HOUR), min(now, (h + 1) * HOUR)
|
|
if hi <= lo:
|
|
continue
|
|
# The current hour is still filling: its bin is all inside the span.
|
|
share = 1.0 if (h + 1) * HOUR > now else (hi - lo) / HOUR
|
|
usd += v[0] * share
|
|
units += v[1] * share
|
|
return usd, units
|
|
|
|
|
|
def _usd_per_unit(bins: dict[int, list[float]], now: float) -> float | None:
|
|
h1 = int(now // HOUR)
|
|
usd = units = 0.0
|
|
for h in range(h1 - RATIO_DAYS * 24, h1 + 1):
|
|
v = bins.get(h)
|
|
if v:
|
|
usd += v[0]
|
|
units += v[1]
|
|
return usd / units if units > 0 else None
|
|
|
|
|
|
def burn(deps, account: str, now: float | None = None) -> dict | None:
|
|
"""The account's burn rate, or None when it has no week target."""
|
|
wt = target.of(account)
|
|
if wt is None:
|
|
return None
|
|
now = time.time() if now is None else now
|
|
goal = float(wt["goal"])
|
|
bins = calibrate.meters(deps, account)
|
|
usd_h, units_h = rolling(bins, now)
|
|
|
|
wk = (calibrate.current(deps, account) or {}).get("week") or {}
|
|
upp = wk.get("unitsPerPct") or None
|
|
usd_pp = wk.get("usdPerPct")
|
|
if not usd_pp and upp:
|
|
per = _usd_per_unit(bins, now)
|
|
usd_pp = per * upp if per else None
|
|
|
|
lim = deps.usage_poller.limits().get(account) or {}
|
|
win = (lim.get("windows") or {}).get("seven_day") or {}
|
|
pct = win.get("pct")
|
|
resets_at = win.get("resetsAt")
|
|
if resets_at and resets_at <= now: # it reset: a fresh week, unread
|
|
pct, resets_at = 0.0, None
|
|
hours_left = (resets_at - now) / HOUR if resets_at else None
|
|
|
|
pct_h = units_h / upp if upp else None
|
|
need_pct_h = (max(0.0, goal - float(pct)) / hours_left
|
|
if pct is not None and hours_left and hours_left > 0 else None)
|
|
need_usd_h = need_pct_h * usd_pp if need_pct_h is not None and usd_pp else None
|
|
proj = (min(100.0, float(pct) + pct_h * hours_left)
|
|
if pct is not None and pct_h is not None and hours_left else None)
|
|
return {
|
|
"account": account,
|
|
"windowMinutes": WINDOW_S // 60,
|
|
"usdPerHour": round(usd_h, 4),
|
|
"unitsPerHour": round(units_h, 4),
|
|
"pctPerHour": round(pct_h, 4) if pct_h is not None else None,
|
|
"goal": goal,
|
|
"pct": pct,
|
|
"resetsAt": resets_at,
|
|
"hoursLeft": round(hours_left, 3) if hours_left is not None else None,
|
|
"neededPctPerHour": round(need_pct_h, 4) if need_pct_h is not None else None,
|
|
"neededUsdPerHour": round(need_usd_h, 4) if need_usd_h is not None else None,
|
|
"projected": round(proj, 2) if proj is not None else None,
|
|
"stale": bool(lim.get("stale", True)),
|
|
"at": now,
|
|
}
|