broker: per-member daily usage series in /broker/usage (daily: day/spend/requests)
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
The member dashboard's usage chart reads per-day spend + requests for the signed-in member, bounded by the existing days param (0 = all time, the unchanged default). Same source and day convention as the co-op daily series (to_char(startTime)); interior zero days are explicit so chart spacing is true; requests excludes failed attempts; spend sums every row. Additive and backward compatible: every existing field untouched. Tested in tests/test_broker_usage_daily.py (route table, SQL shape, fallback twin, fail-soft degradation).
This commit is contained in:
1 parent
afffd52dec
commit
3c64b150db
2 files changed
+301
No files matched your search
+105
@@ -587,6 +587,96 @@ def aggregate_model_usage(days: int = 0, team_id: str | None = None) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _fill_daily_days(rows: list[dict]) -> list[dict]:
|
||||
"""Fill calendar days WITHOUT usage rows with spend 0 / requests 0 between
|
||||
the first and last measured day, so the chart's x spacing is true: a day
|
||||
with no requests IS a zero-usage day (measured), not an unmeasured gap —
|
||||
the fail-closed NULL rule is for days the SOURCE could not read, which
|
||||
these are not (a GROUP BY simply omits them)."""
|
||||
if not rows:
|
||||
return rows
|
||||
from datetime import date, timedelta
|
||||
start = date.fromisoformat(rows[0]["day"])
|
||||
end = date.fromisoformat(rows[-1]["day"])
|
||||
by_day = {r["day"]: r for r in rows}
|
||||
out = []
|
||||
d = start
|
||||
while d <= end:
|
||||
key = d.isoformat()
|
||||
out.append(by_day.get(key) or {"day": key, "spend": 0.0, "requests": 0})
|
||||
d += timedelta(days=1)
|
||||
return out
|
||||
|
||||
|
||||
def daily_series_from_rows(rows) -> list[dict]:
|
||||
"""Per-day usage series from spend-log rows ALREADY filtered to one team —
|
||||
the HTTP fallback's client-side twin of member_daily_usage. spend sums every
|
||||
row; requests counts only non-failure rows (same rules as the DB path)."""
|
||||
daily_map: dict[str, dict] = {}
|
||||
for row in rows if isinstance(rows, list) else []:
|
||||
day = str(row.get("startTime") or "")[:10]
|
||||
if not day:
|
||||
continue
|
||||
d = daily_map.setdefault(day, {"spend": 0.0, "requests": 0})
|
||||
d["spend"] += float(row.get("spend") or 0.0)
|
||||
if str(row.get("status") or "").lower() != "failure":
|
||||
d["requests"] += 1
|
||||
return _fill_daily_days([
|
||||
{"day": k, "spend": round(v["spend"], 6), "requests": v["requests"]}
|
||||
for k, v in sorted(daily_map.items())
|
||||
])
|
||||
|
||||
|
||||
def member_daily_usage(days: int = 0, team_id: str | None = None) -> list[dict]:
|
||||
"""The member's own per-day usage (spend + requests), for the dashboard's
|
||||
usage chart. Same source and day convention as the co-op daily series
|
||||
(metrics_snapshot.spend_by_day): to_char("startTime", 'YYYY-MM-DD'), so the
|
||||
two series agree on which day a request belongs to.
|
||||
|
||||
`days` bounds the window (0 = all time). spend sums EVERY log row (a call
|
||||
that failed after tokens were consumed still cost money); `requests` counts
|
||||
only metered calls that succeeded, like aggregate_model_usage.
|
||||
"""
|
||||
if not litellm_db_ready():
|
||||
raise RuntimeError("LiteLLM DB credentials not configured")
|
||||
|
||||
where = []
|
||||
params: list = []
|
||||
if days and days > 0:
|
||||
from datetime import datetime, timedelta, timezone
|
||||
cutoff = (datetime.now(timezone.utc) - timedelta(days=days)).strftime(
|
||||
"%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
where.append("\"startTime\" >= %s")
|
||||
params.append(cutoff)
|
||||
if team_id:
|
||||
where.append("team_id = %s")
|
||||
params.append(team_id)
|
||||
where_sql = ("WHERE " + " AND ".join(where)) if where else ""
|
||||
|
||||
sql = f"""
|
||||
SELECT to_char("startTime", 'YYYY-MM-DD') AS day,
|
||||
COALESCE(SUM(spend), 0) AS spend,
|
||||
COUNT(*) FILTER (WHERE status IS DISTINCT FROM 'failure') AS requests
|
||||
FROM "LiteLLM_SpendLogs"
|
||||
{where_sql}
|
||||
GROUP BY 1
|
||||
ORDER BY 1
|
||||
"""
|
||||
|
||||
conn = _pg_connect()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql, params)
|
||||
rows = cur.fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return _fill_daily_days([
|
||||
{"day": r[0], "spend": round(float(r[1] or 0.0), 6), "requests": int(r[2] or 0)}
|
||||
for r in rows
|
||||
])
|
||||
|
||||
|
||||
def get_team_budget_from_db(team_id: str) -> dict | None:
|
||||
"""Fetch a team's spend/budget/reset directly from LiteLLM's Postgres.
|
||||
|
||||
@@ -5882,6 +5972,14 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
except Exception as e:
|
||||
logger.warning("broker_get_usage: per-key spend failed: %s", e)
|
||||
key_spend = []
|
||||
# Per-day series for the member's usage chart (30/90/all-time
|
||||
# toggles re-render client-side from this). Best-effort like
|
||||
# key_spend: a failure degrades to an empty series, never an error.
|
||||
try:
|
||||
daily = member_daily_usage(days=days, team_id=team_id)
|
||||
except Exception as e:
|
||||
logger.warning("broker_get_usage: daily usage query failed: %s", e)
|
||||
daily = []
|
||||
return {
|
||||
"email": email,
|
||||
"guest": is_guest_account(email),
|
||||
@@ -5896,6 +5994,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
"reset_at": reset_at,
|
||||
"models": model_list,
|
||||
"key_spend": key_spend,
|
||||
"daily": daily,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning("broker_get_usage: DB path failed (%s); falling back to HTTP", e)
|
||||
@@ -5923,6 +6022,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
# filter client-side on the returned rows.
|
||||
models = {}
|
||||
total_tokens = 0
|
||||
team_rows: list = []
|
||||
try:
|
||||
r2 = await client.get(
|
||||
f"{LITELLM_BASE}/spend/logs",
|
||||
@@ -5935,6 +6035,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
for row in rows if isinstance(rows, list) else []:
|
||||
if row.get("team_id") != team_id:
|
||||
continue
|
||||
team_rows.append(row)
|
||||
# Prefer model_group: it carries our provider/model alias (e.g.
|
||||
# greenpt/glm-5.3-flash) when present; the raw `model` field holds
|
||||
# messy upstream/internal names. Older rows predating the alias
|
||||
@@ -5949,6 +6050,9 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
total_tokens += int(row.get("total_tokens") or 0)
|
||||
except Exception as e: # spend logs are best-effort; don't fail the whole view
|
||||
logger.warning("spend/logs fetch failed: %s", e)
|
||||
# Per-day series for the usage chart (same day convention as the DB
|
||||
# path: the UTC day of startTime).
|
||||
daily = daily_series_from_rows(team_rows)
|
||||
|
||||
# All-time spend from the same log rows (team counter is period-only).
|
||||
all_time_spend = round(sum(m["spend"] for m in models.values()), 2)
|
||||
@@ -5976,6 +6080,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
# per-key counter is cycle-only); this HTTP fallback runs only when the
|
||||
# DB is unconfigured, so there is nothing accurate to report here.
|
||||
"key_spend": [],
|
||||
"daily": daily,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user