spend: ONE all-time figure, computed once, for every surface that shows it
ci/woodpecker/push/woodpecker Pipeline was canceled
ci/woodpecker/push/woodpecker Pipeline was canceled
The panel's card said $941.37 and the member dashboard's said $900.51 for the
same claim. The panel used its own per-team query; the dashboard summed the
chart series IN THE BROWSER, which disagrees with the server whenever a row is
null, capped or stale.
coop_spend_totals() is now the definition: one GROUP BY over SpendLogs joined to
the team table, so
spend_all_time = every row (members, operators, agent teams, sandboxes) —
the same universe litellm_spend_by_day() sums, so the
cards and the money chart agree by construction
own_all_time_spend = the part that is the co-op's own (non-member teams, plus
rows with no team)
and member_all_time_spend = the rest. Exposed through /admin/overview (the
panel's boot payload), the money payload, and /broker/metrics (the dashboard's).
_coop_spend_totals_safe() returns {} rather than a zero: a failed computation
must never read as "the co-op has spent nothing".
tests/test_coop_spend_totals.py pins the arithmetic (total == member + own ==
every row) and the no-fake-zero rule.
This commit is contained in:
1 parent
e15a01c338
commit
2a77b0c988
2 files changed
+180
-1
No files matched your search
+79
-1
@@ -500,6 +500,18 @@ LITELLM_DB_PASSWORD = os.environ.get("LITELLM_DB_PASSWORD", "")
|
||||
LITELLM_DB_NAME = os.environ.get("LITELLM_DB_NAME", "")
|
||||
|
||||
|
||||
def _coop_spend_totals_safe() -> dict:
|
||||
"""coop_spend_totals() that degrades to {} — a spend figure is a display
|
||||
detail; a failure to compute it must not blank the panel or the dashboard.
|
||||
Callers OMIT the key rather than showing a zero, because a zero reads as
|
||||
"the co-op has spent nothing", which is a lie."""
|
||||
try:
|
||||
return coop_spend_totals()
|
||||
except Exception as e:
|
||||
logger.warning("coop spend totals unavailable: %s", e)
|
||||
return {}
|
||||
|
||||
|
||||
def litellm_db_ready() -> bool:
|
||||
"""True when LiteLLM DB read credentials are configured."""
|
||||
return bool(LITELLM_DB_HOST and LITELLM_DB_USER and LITELLM_DB_NAME)
|
||||
@@ -5291,6 +5303,14 @@ async def admin_overview(request: Request):
|
||||
"total_all_time_spend": round(total_all_time, 2),
|
||||
"operator_spend": round(operator_spend, 2),
|
||||
"operator_all_time_spend": round(operator_all_time, 2),
|
||||
# The co-op's spend, the ONE definition both apps show (see
|
||||
# coop_spend_totals). spend_all_time spans members AND the co-op's
|
||||
# own accounts; own_all_time_spend is the breakout.
|
||||
**{
|
||||
k: v
|
||||
for k, v in _coop_spend_totals_safe().items()
|
||||
if k in ("spend_all_time", "own_all_time_spend")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -5533,6 +5553,51 @@ def litellm_spend_by_day() -> tuple[dict[str, float], float, int]:
|
||||
return by_day, round(sum(by_day.values()), 6), int(sum(int(r[2]) for r in rows))
|
||||
|
||||
|
||||
def coop_spend_totals() -> dict:
|
||||
"""The co-op's all-time spend, ONE definition, for every surface that shows it.
|
||||
|
||||
The admin panel's cards, the money payload and the member dashboard all read
|
||||
THIS: the panel used its own per-team query and the dashboard summed the
|
||||
chart series in the browser, so the same claim came out as $941.37 in one app
|
||||
and $900.51 in the other (2026-11-13).
|
||||
|
||||
total = every SpendLogs row, whatever team it belongs to — the same universe
|
||||
as litellm_spend_by_day(), so the cards and the money chart agree.
|
||||
own = the co-op's own use: every team that is not a member team, plus rows
|
||||
with no team at all (nothing here is a member's usage).
|
||||
"""
|
||||
if not litellm_db_ready():
|
||||
raise RuntimeError("LiteLLM DB credentials not configured")
|
||||
conn = _pg_connect()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""SELECT COALESCE(t.team_alias, '') AS alias,
|
||||
COALESCE(SUM(s."spend"), 0) AS spend
|
||||
FROM "LiteLLM_SpendLogs" s
|
||||
LEFT JOIN "LiteLLM_TeamTable" t ON t.team_id = s."team_id"
|
||||
GROUP BY 1"""
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
member = own = 0.0
|
||||
aliases = 0
|
||||
for alias, spend in rows:
|
||||
val = float(spend or 0.0)
|
||||
aliases += 1
|
||||
if str(alias or "").startswith("member:"):
|
||||
member += val
|
||||
else:
|
||||
own += val
|
||||
return {
|
||||
"spend_all_time": round(member + own, 2),
|
||||
"member_all_time_spend": round(member, 2),
|
||||
"own_all_time_spend": round(own, 2),
|
||||
"aliases": aliases,
|
||||
}
|
||||
|
||||
|
||||
def oc_balance_history() -> dict[str, float]:
|
||||
"""metrics_daily.oc_balance_usd rows — the daily snapshots. Empty before
|
||||
the pipeline deployed (earlier balances were never recorded)."""
|
||||
@@ -5659,6 +5724,10 @@ async def _compute_money_payload() -> dict:
|
||||
"contributions": contrib_count,
|
||||
"spend_all_time": spend_total,
|
||||
"spend_rows": spend_rows,
|
||||
# The member-owned vs co-op's-own split of that same all-time spend,
|
||||
# from the one shared definition.
|
||||
"own_all_time_spend": (_coop_split := _coop_spend_totals_safe()).get("own_all_time_spend"),
|
||||
"member_all_time_spend": _coop_split.get("member_all_time_spend"),
|
||||
},
|
||||
"sources": {
|
||||
"revenue": "Open Collective contribution transactions (CREDIT, "
|
||||
@@ -6648,7 +6717,16 @@ async def broker_metrics(request: Request, days: int = 90):
|
||||
"""
|
||||
_verify_broker_secret(request)
|
||||
series = metrics_series(days)
|
||||
return {"series": series, "count": len(series), "days": max(1, min(int(days or 90), 730))}
|
||||
return {
|
||||
"series": series,
|
||||
"count": len(series),
|
||||
"days": max(1, min(int(days or 90), 730)),
|
||||
# The co-op's all-time spend, from the same helper the admin panel reads,
|
||||
# so the two apps cannot report different totals for the same claim. The
|
||||
# dashboard used to sum the series in the browser and got $900.51 where
|
||||
# the panel said $941.37.
|
||||
"totals": _coop_spend_totals_safe(),
|
||||
}
|
||||
|
||||
|
||||
@app.get("/broker/money-series")
|
||||
|
||||
Reference in new issue
Block a user