money series: /admin/money-series (revenue/spend/OC balance) + daily OC balance snapshot

- /admin/money-series (X-Admin-Token, 30/min): three series for the admin
  panel's Money chart, hourly in-memory cache with stale-on-error fallback.
  revenue = OC contribution transactions (CREDIT/CONTRIBUTION/non-refund,
  monthly + one-time, org-backed included), cumulative from 2026-09-06;
  spend = one GROUP BY day pass over the full LiteLLM SpendLogs (no team
  filter, same definition as metrics_snapshot.spend_by_day); balance = OC
  current balance + daily snapshots.
- OC exposes only the CURRENT balance — no historical series exists — so
  metrics_snapshot.py now writes oc_balance_usd into metrics_daily daily
  (from 2026-11-08 onward; balance history cannot be backfilled). The
  endpoint seeds an existing row's balance with the live value without
  touching the snapshot's other columns.
- Aggregate totals only: no member emails or names selected or returned.
This commit is contained in:
inference-bot committed 2026-11-08 01:50:17 +00:00
1 parent 3cc2fad1dd
commit dcce9fcb35
3 files changed
+417 -6

No files matched your search

+62 -3
View File
@@ -20,7 +20,7 @@ Rules this file follows deliberately:
- **Runs in Cloudron's scheduler container**, which gets the platform env
(declared app env + addon vars) but NOT variables the app's own start.sh
sourced. Everything needed here is a declared app env var: LITELLM_DB_*,
LISTMONK_*, MATRIX_BOT_TOKEN, LISTMONK_LIST_ID.
LISTMONK_*, MATRIX_BOT_TOKEN, LISTMONK_LIST_ID, OC_PERSONAL_TOKEN.
- `members_new` uses `welcome_sent_at`, the provisioning timestamp — the closest
thing the portal stores to a join date. Manual members have no welcome email,
so their joins are absent from the growth curve (noted, not guessed).
@@ -84,6 +84,11 @@ def connect() -> sqlite3.Connection:
("tokens_measured", "INTEGER"),
("tokens_estimated", "INTEGER"),
("factor_revision", "TEXT"),
# The collective's Open Collective balance, read once a day. The OC API
# exposes only the CURRENT balance — there is no historical series — so
# a real balance history can only accrue from these daily snapshots
# (from 2026-11-08 onward; earlier days stay NULL by design).
("oc_balance_usd", "REAL"),
):
try:
conn.execute(f"ALTER TABLE metrics_daily ADD COLUMN {col} {decl}")
@@ -513,6 +518,55 @@ def impact_for(models: dict[str, int]) -> dict:
}
# ---------------------------------------------------------------- OC balance
OC_GRAPHQL_URL = "https://opencollective.com/api/graphql/v2"
def oc_collective_balance() -> float | None:
"""The collective's CURRENT Open Collective balance, in USD.
OC exposes only the current balance — there is NO historical series — so a
real balance history can only accrue from these daily snapshots (from
2026-11-08 onward; earlier days stay NULL, which charts must render as a
gap, never a zero). Fail-soft like every other source here: a failed read
prints and returns None.
"""
token = os.environ.get("OC_PERSONAL_TOKEN", "")
slug = os.environ.get("OC_COLLECTIVE_SLUG", "inference-cooperative")
if not token:
print("metrics: OC_PERSONAL_TOKEN not set — oc_balance_usd left NULL")
return None
query = ("query Bal($slug: String) { collective(slug: $slug) "
"{ stats { balance { valueInCents currency } } } }")
req = urllib.request.Request(
OC_GRAPHQL_URL,
data=json.dumps({"query": query, "variables": {"slug": slug}}).encode("utf-8"),
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
"Api-Version": "2022-01-01",
# The default Python UA is blocked by Cloudflare (error 1010), which
# reads like a scope failure — see references/opencollective-api.md.
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
},
)
try:
with urllib.request.urlopen(req, timeout=30) as resp:
data = json.loads(resp.read() or b"{}")
bal = (
((data.get("data") or {}).get("collective") or {}).get("stats") or {}
).get("balance") or {}
cents = bal.get("valueInCents")
if cents is None:
print("metrics: OC balance field missing from response — oc_balance_usd left NULL")
return None
return round(int(cents) / 100.0, 2)
except Exception as exc: # noqa: BLE001 - report, never guess
print(f"metrics: OC balance read failed ({exc}) — oc_balance_usd left NULL")
return None
# ---------------------------------------------------------------- writes
def upsert(day: str, row: dict) -> None:
@@ -523,8 +577,9 @@ def upsert(day: str, row: dict) -> None:
(day, members_active, members_total, members_new, newsletter_subs,
matrix_space_members, spend_usd, operator_spend_usd, requests,
out_tokens, energy_wh,
co2e_g, tokens_measured, tokens_estimated, factor_revision, recorded_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
co2e_g, tokens_measured, tokens_estimated, factor_revision,
oc_balance_usd, recorded_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(day) DO UPDATE SET
members_active=excluded.members_active,
members_total=excluded.members_total,
@@ -540,6 +595,7 @@ def upsert(day: str, row: dict) -> None:
tokens_measured=excluded.tokens_measured,
tokens_estimated=excluded.tokens_estimated,
factor_revision=excluded.factor_revision,
oc_balance_usd=excluded.oc_balance_usd,
recorded_at=excluded.recorded_at""",
(
day,
@@ -557,6 +613,7 @@ def upsert(day: str, row: dict) -> None:
row.get("tokens_measured"),
row.get("tokens_estimated"),
row.get("factor_revision"),
row.get("oc_balance_usd"),
dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"),
),
)
@@ -587,6 +644,7 @@ def snapshot(day: str | None = None) -> dict:
tot = sum(day_models.values())
out_tokens = tot or None
impact = impact_for(day_models)
balance = oc_collective_balance()
row = {
"members_active": m["active"],
"members_total": m["total"],
@@ -602,6 +660,7 @@ def snapshot(day: str | None = None) -> dict:
"tokens_measured": impact["tokens_measured"],
"tokens_estimated": impact["tokens_estimated"],
"factor_revision": impact["factor_revision"],
"oc_balance_usd": balance,
}
upsert(day, row)
print(f"metrics: recorded {day} -> {json.dumps(row)}")