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", "")
|
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:
|
def litellm_db_ready() -> bool:
|
||||||
"""True when LiteLLM DB read credentials are configured."""
|
"""True when LiteLLM DB read credentials are configured."""
|
||||||
return bool(LITELLM_DB_HOST and LITELLM_DB_USER and LITELLM_DB_NAME)
|
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),
|
"total_all_time_spend": round(total_all_time, 2),
|
||||||
"operator_spend": round(operator_spend, 2),
|
"operator_spend": round(operator_spend, 2),
|
||||||
"operator_all_time_spend": round(operator_all_time, 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))
|
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]:
|
def oc_balance_history() -> dict[str, float]:
|
||||||
"""metrics_daily.oc_balance_usd rows — the daily snapshots. Empty before
|
"""metrics_daily.oc_balance_usd rows — the daily snapshots. Empty before
|
||||||
the pipeline deployed (earlier balances were never recorded)."""
|
the pipeline deployed (earlier balances were never recorded)."""
|
||||||
@@ -5659,6 +5724,10 @@ async def _compute_money_payload() -> dict:
|
|||||||
"contributions": contrib_count,
|
"contributions": contrib_count,
|
||||||
"spend_all_time": spend_total,
|
"spend_all_time": spend_total,
|
||||||
"spend_rows": spend_rows,
|
"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": {
|
"sources": {
|
||||||
"revenue": "Open Collective contribution transactions (CREDIT, "
|
"revenue": "Open Collective contribution transactions (CREDIT, "
|
||||||
@@ -6648,7 +6717,16 @@ async def broker_metrics(request: Request, days: int = 90):
|
|||||||
"""
|
"""
|
||||||
_verify_broker_secret(request)
|
_verify_broker_secret(request)
|
||||||
series = metrics_series(days)
|
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")
|
@app.get("/broker/money-series")
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Regression test: ONE definition of the co-op's all-time spend.
|
||||||
|
|
||||||
|
WHY THIS EXISTS
|
||||||
|
On 2026-11-13 the panel's card said $941.37 and the member dashboard's said
|
||||||
|
$900.51 — the same claim, two numbers. The panel used the portal's 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 single source, exposed to the panel's boot payload, the money
|
||||||
|
payload and the broker's metrics.
|
||||||
|
|
||||||
|
The split has to be the same split on every surface, and a failure to compute
|
||||||
|
it must NEVER look like "$0 spent": the safe wrapper returns {} so callers
|
||||||
|
omit the key instead of printing a zero that reads as fact.
|
||||||
|
"""
|
||||||
|
import pathlib
|
||||||
|
import sys
|
||||||
|
|
||||||
|
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
||||||
|
sys.path.insert(0, str(ROOT))
|
||||||
|
|
||||||
|
import app.main as m # noqa: E402
|
||||||
|
|
||||||
|
FAILS = []
|
||||||
|
|
||||||
|
|
||||||
|
def check(cond, msg):
|
||||||
|
print((" ok " if cond else " FAIL ") + msg)
|
||||||
|
if not cond:
|
||||||
|
FAILS.append(msg)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeCursor:
|
||||||
|
def __init__(self, rows):
|
||||||
|
self._rows = rows
|
||||||
|
|
||||||
|
def execute(self, *a, **k):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def fetchall(self):
|
||||||
|
return self._rows
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *a):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class FakeConn:
|
||||||
|
def __init__(self, rows):
|
||||||
|
self._rows = rows
|
||||||
|
|
||||||
|
def cursor(self):
|
||||||
|
return FakeCursor(self._rows)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
ROWS = [
|
||||||
|
("member:alice@example.org", 10.005),
|
||||||
|
("member:bob@example.org", 5.005),
|
||||||
|
("agent:infrastructure-bot", 1.5),
|
||||||
|
("sandbox:throwaway", 0.02),
|
||||||
|
("", 1.07), # spend rows with no team at all
|
||||||
|
]
|
||||||
|
|
||||||
|
real_ready, real_connect = m.litellm_db_ready, m._pg_connect
|
||||||
|
try:
|
||||||
|
m.litellm_db_ready = lambda: True
|
||||||
|
m._pg_connect = lambda: FakeConn(ROWS)
|
||||||
|
t = m.coop_spend_totals()
|
||||||
|
|
||||||
|
print("1. the split adds up and covers every row")
|
||||||
|
check(t["member_all_time_spend"] == 15.01,
|
||||||
|
f"member side is the member: teams ($10.005 + $5.005 -> 15.01, got {t['member_all_time_spend']})")
|
||||||
|
check(t["own_all_time_spend"] == 2.59,
|
||||||
|
f"own side is everything else: agents, sandboxes, unattributed (got {t['own_all_time_spend']})")
|
||||||
|
check(t["spend_all_time"] == round(t["member_all_time_spend"] + t["own_all_time_spend"], 2),
|
||||||
|
"total == member + own, so no dollar is counted twice or dropped")
|
||||||
|
check(t["spend_all_time"] == round(sum(r[1] for r in ROWS), 2),
|
||||||
|
f"total is every SpendLogs row ({round(sum(r[1] for r in ROWS), 2)}), the same universe the money chart sums")
|
||||||
|
|
||||||
|
print("2. a failure to compute must not look like $0")
|
||||||
|
def boom():
|
||||||
|
raise RuntimeError("LiteLLM DB credentials not configured")
|
||||||
|
m._pg_connect = boom
|
||||||
|
check(m._coop_spend_totals_safe() == {},
|
||||||
|
"the safe wrapper returns {} so callers omit the key instead of showing a fake zero")
|
||||||
|
m.litellm_db_ready = lambda: False
|
||||||
|
check(m._coop_spend_totals_safe() == {},
|
||||||
|
"and it degrades the same way when the DB is simply not configured")
|
||||||
|
finally:
|
||||||
|
m.litellm_db_ready, m._pg_connect = real_ready, real_connect
|
||||||
|
|
||||||
|
print()
|
||||||
|
if FAILS:
|
||||||
|
print(f"coop spend totals: {len(FAILS)} FAILURE(S)")
|
||||||
|
sys.exit(1)
|
||||||
|
print("coop spend totals: OK — one definition, one number, no fake zeros")
|
||||||
Reference in new issue
Block a user