diff --git a/app/main.py b/app/main.py index 85f9369..bddc1d3 100644 --- a/app/main.py +++ b/app/main.py @@ -579,12 +579,14 @@ PAGE_HTML = """ .usage-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; } .usage-header h2 { margin-bottom: 0; } - .mode-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; } + /* Same button shape as the admin panel's range buttons — border, 8px radius, + green-mid when active — so the two apps' charts read as one design. */ + .mode-switch { display: inline-flex; gap: 6px; } .mode-btn { - font-family: inherit; cursor: pointer; border: none; background: #fff; color: var(--muted); - font-size: 12px; font-weight: 600; padding: 6px 12px; border-radius: 0; + font-family: inherit; cursor: pointer; border: 1px solid var(--border); background: #fff; + color: var(--muted); font-size: 12px; font-weight: 600; padding: 5px 12px; border-radius: 8px; } - .mode-btn.active { background: var(--green-deep); color: #fff; } + .mode-btn.active { background: var(--green-mid); color: #fff; border-color: var(--green-mid); } .docs-link { display: inline-block; margin-top: 14px; color: var(--green-deep); font-size: 13px; font-weight: 600; text-decoration: none; } .docs-link:hover { text-decoration: underline; } @@ -622,6 +624,18 @@ PAGE_HTML = """ .board-fill { height: 100%; background: var(--green-mid); border-radius: 4px; } .board-sub { color: var(--muted); font-size: 11px; margin-top: 4px; } .keys { margin-top: 8px; } + .key-controls { display: flex; align-items: center; gap: 6px; margin: 10px 0 6px; } + /* One row per key: label, bar, value. The chat key is a row like any other, + so the built-in key's usage is comparable instead of a footnote. */ + .key-bar-row { + display: grid; grid-template-columns: minmax(110px, 1.4fr) 2.6fr auto auto; + gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border); + } + .key-bar-row:last-child { border-bottom: none; } + .key-bar-label { min-width: 0; } + .key-bar-track { background: #eef0ea; border-radius: 6px; height: 14px; overflow: hidden; } + .key-bar-fill { background: var(--green-mid); height: 100%; border-radius: 6px; } + .key-bar-val { font-weight: 600; font-size: 12px; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; } .key-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--border); @@ -841,7 +855,6 @@ PAGE_HTML = """
—
co-op spend, all time
-
—
of which the co-op's own accounts
@@ -875,8 +888,12 @@ PAGE_HTML = """

API keys

Keys let you use the co-op's AI from your own tools. They draw from the same balance as chat.

+
+ Show + + +
No API keys yet.
-
@@ -1343,6 +1360,7 @@ function renderModels(models) { // through the broker as aggregate, non-personal numbers. Fetched once at the // widest range; the toggles re-render client-side without refetching. let trendsData = null; +let metricsTotals = null; // the co-op's all-time spend, from /api/metrics let trendMetric = "members"; // members | requests | spend let trendDays = 90; // 0 = all time @@ -1427,6 +1445,9 @@ async function loadMetrics() { const d = await r.json(); if (d.error) { host.innerHTML = "
" + d.error + "
"; return; } trendsData = d.series || []; + // The co-op's all-time spend, straight from the portal (see the panel: it + // and this page now read the same helper, so they cannot disagree). + metricsTotals = d.totals || null; renderCoopTotals(); renderTrends(); } catch (e) { @@ -1439,13 +1460,18 @@ async function loadMetrics() { // it (NULL where a day was not measured, so it is skipped rather than counted // as zero). Both summed over the widest range the series carries (all time). function renderCoopTotals() { - let total = 0, own = 0; - (trendsData || []).forEach(row => { - if (typeof row.spend_usd === "number") total += row.spend_usd; - if (typeof row.operator_spend_usd === "number") own += row.operator_spend_usd; - }); - $("coop-total").textContent = fmtMoney(total); - $("coop-own").textContent = fmtMoney(own); + // The server's figure, not a sum of whatever rows this page happens to hold: + // the same number the admin panel shows. Summing the series in the browser + // gave $900.51 where the portal's own all-time spend was $941.37 (2026-11-13). + const el = $("coop-total"); + const t = (metricsTotals && typeof metricsTotals.spend_all_time === "number") + ? metricsTotals.spend_all_time : null; + if (t === null) { + // Never invent a number here. If the server did not send one, say so. + el.textContent = "—"; + return; + } + el.textContent = fmtMoney(t); } function setTrendMetric(m) { @@ -1638,47 +1664,61 @@ function renderBoard(models) { }).join(''); } -function renderKeys(keys, keySpend) { - const el = $('keys'); - // Spend rows come from the portal, keyed by the member's key name; the chat - // key is carried separately (is_chat) and rendered on its own line. - const spendFor = {}; - (keySpend || []).forEach(s => { if (!s.is_chat) spendFor[s.name] = s; }); - if (!keys.length) { - el.innerHTML = '
No API keys yet. Create one below.
'; - renderChatSpend(keySpend); - return; - } - el.innerHTML = keys.map(k => { - const fp = k.fingerprint || ''; - const created = k.created_at ? ' · created ' + k.created_at.slice(0,10) : ''; - const s = spendFor[k.name]; - // Secondary detail: this key's all-time spend (server-side, from the spend - // logs — LiteLLM's own per-key counter is period-only). - const spent = ' · ' + fmtMoney(s ? s.spend : 0) + ' spent'; - return '
' + - '
' + escapeHtml(k.name) + '
' + - '
' + escapeHtml(created) + - (fp ? ' · ends …' + escapeHtml(fp) : '') + escapeHtml(spent) + '
' + - '
' + - '' + - '
'; - }).join(''); - renderChatSpend(keySpend); +// A key's usage is easier to compare as bars than as a sentence: same numbers, +// relative lengths. Spend or requests, one row per key, the chat key included. +let keyRowsData = { keys: [], spend: [] }; +let keyMetric = "spend"; + +function setKeyMetric(m) { + keyMetric = m; + const sp = document.getElementById("km-spend"), rq = document.getElementById("km-requests"); + if (sp) sp.className = "mode-btn" + (m === "spend" ? " active" : ""); + if (rq) rq.className = "mode-btn" + (m === "requests" ? " active" : ""); + renderKeys(keyRowsData.keys, keyRowsData.spend); } -function renderChatSpend(keySpend) { - const el = $('key-chat'); - if (!el) return; - const chat = (keySpend || []).find(s => s.is_chat); - if (!chat) { el.innerHTML = ''; return; } - // The built-in chat key is not one of the member's API keys, so its spend is - // shown on its own line rather than mixed into the list above. - el.innerHTML = 'Chat (built-in) · ' + - '' + fmtMoney(chat.spend) + ' spent' + - (chat.requests ? ' · ' + chat.requests.toLocaleString() + ' requests' : ''); +function renderKeys(keys, keySpend) { + const el = $('keys'); + keys = keys || []; keySpend = keySpend || []; + keyRowsData = { keys: keys, spend: keySpend }; + const rows = keys.map(k => { + const s = keySpend.find(x => !x.is_chat && x.name === k.name) || {}; + return { + name: k.name, chat: false, + meta: (k.created_at ? 'created ' + k.created_at.slice(0, 10) : '') + + (k.fingerprint ? ' · ends …' + k.fingerprint : ''), + spend: Number(s.spend) || 0, requests: Number(s.requests) || 0 + }; + }); + const chat = keySpend.find(x => x.is_chat); + if (chat) { + rows.push({ name: 'Chat (built-in)', chat: true, meta: 'the key the chat app uses', + spend: Number(chat.spend) || 0, requests: Number(chat.requests) || 0 }); + } + if (!rows.length) { + el.innerHTML = '
No API keys yet. Create one below.
'; + return; + } + rows.sort((a, b) => (b[keyMetric] || 0) - (a[keyMetric] || 0)); + const max = Math.max.apply(null, rows.map(r => r[keyMetric] || 0)); + el.innerHTML = rows.map(r => { + const v = r[keyMetric] || 0; + const pct = max > 0 ? Math.round((v / max) * 100) : 0; + const shown = keyMetric === "spend" ? fmtMoney(v) : v.toLocaleString() + ' requests'; + return '
' + + '
' + escapeHtml(r.name) + '
' + + '
' + escapeHtml(r.meta) + '
' + + '' + + '
' + escapeHtml(shown) + '
' + + (r.chat ? '' + : '') + + '
'; + }).join(''); } + + function escapeHtml(s) { return String(s).replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); }