Broker usage: expose all_time_spend (member's lifetime total from SpendLogs) in both DB and HTTP paths
This commit is contained in:
1 parent
7af02f6466
commit
070c6525cd
1 file changed
+21
+21
@@ -2152,6 +2152,22 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
spend = float(team["spend"] or 0.0)
|
||||
reset_at = team["budget_reset_at"]
|
||||
usage = aggregate_model_usage(days=days, team_id=team_id)
|
||||
# All-time spend for this member: total of their spend-log rows
|
||||
# (the team `spend` counter resets with their allowance period).
|
||||
all_time_spend = 0.0
|
||||
try:
|
||||
conn = _pg_connect()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT COALESCE(SUM(spend), 0) FROM "LiteLLM_SpendLogs" WHERE team_id = %s',
|
||||
(team_id,),
|
||||
)
|
||||
all_time_spend = float(cur.fetchone()[0] or 0.0)
|
||||
finally:
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
logger.warning("broker_get_usage: all-time spend query failed: %s", e)
|
||||
model_list = [
|
||||
{"model": m["model"], "spend": m["spend"], "tokens": m["tokens"],
|
||||
"calls": m["requests"]}
|
||||
@@ -2163,6 +2179,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
"allowance": allowance,
|
||||
"credits": credits,
|
||||
"spend": spend,
|
||||
"all_time_spend": round(all_time_spend, 2),
|
||||
"remaining": max(balance - spend, 0.0),
|
||||
"total_tokens": usage["totals"]["total_tokens"],
|
||||
"reset_at": reset_at,
|
||||
@@ -2219,6 +2236,9 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
except Exception as e: # spend logs are best-effort; don't fail the whole view
|
||||
logger.warning("spend/logs fetch failed: %s", e)
|
||||
|
||||
# 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)
|
||||
|
||||
# Sort models by spend descending (which models are draining the most).
|
||||
model_list = [
|
||||
{"model": name, **stats}
|
||||
@@ -2231,6 +2251,7 @@ async def broker_get_usage(email: str, days: int = 0) -> dict:
|
||||
"allowance": allowance,
|
||||
"credits": credits,
|
||||
"spend": spend,
|
||||
"all_time_spend": all_time_spend,
|
||||
"remaining": max(balance - spend, 0.0),
|
||||
"total_tokens": total_tokens,
|
||||
"reset_at": reset_at,
|
||||
|
||||
Reference in new issue
Block a user