dashboard: show the co-op's own share in Co-op usage

The Co-op usage view charted the co-op-wide spend (which spans the co-op's own
operator accounts) with nothing saying so. Adds a stats pair — co-op spend all
time, and of which the co-op's own accounts — summed from the series' new
operator_spend_usd (NULL days skipped, not counted as zero), and labels the
spend chart and the model board as including the co-op's own accounts.
This commit is contained in:
inference-bot committed 2026-11-02 15:44:34 +00:00
1 parent 094a3cb588
commit 55e81bfff9
1 file changed
+25 -3
+25 -3
View File
@@ -415,6 +415,9 @@ PAGE_HTML = """<!DOCTYPE html>
.coop-trend-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; } .coop-trend-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.coop-trend-title { font-weight: 700; font-size: 15px; color: var(--ink, #2d3327); } .coop-trend-title { font-weight: 700; font-size: 15px; color: var(--ink, #2d3327); }
.coop-trend-controls { display: flex; gap: 18px; flex-wrap: wrap; margin-bottom: 10px; } .coop-trend-controls { display: flex; gap: 18px; flex-wrap: wrap; margin-bottom: 10px; }
.coop-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 16px; margin: 2px 0 12px; }
.coop-stats .num { font-size: 20px; font-weight: 700; color: var(--ink, #2d3327); }
.coop-stats .lbl { font-size: 12px; color: var(--muted); }
.trend-group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; } .trend-group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.coop-trend-body svg { display: block; width: 100%; height: auto; overflow: visible; } .coop-trend-body svg { display: block; width: 100%; height: auto; overflow: visible; }
.board-row { .board-row {
@@ -600,6 +603,10 @@ PAGE_HTML = """<!DOCTYPE html>
<div class="coop-trend-head"> <div class="coop-trend-head">
<span class="coop-trend-title" id="trend-title">The co-op over time</span> <span class="coop-trend-title" id="trend-title">The co-op over time</span>
</div> </div>
<div class="coop-stats">
<div class="stat"><div class="num" id="coop-total">—</div><div class="lbl">co-op spend, all time</div></div>
<div class="stat"><div class="num" id="coop-own">—</div><div class="lbl">of which the co-op's own accounts</div></div>
</div>
<div class="coop-trend-controls"> <div class="coop-trend-controls">
<span class="trend-group"> <span class="trend-group">
<span class="board-note" style="margin:0;">Show</span> <span class="board-note" style="margin:0;">Show</span>
@@ -618,7 +625,7 @@ PAGE_HTML = """<!DOCTYPE html>
</div> </div>
<div id="trend-body" class="coop-trend-body"><div class="empty">Loading</div></div> <div id="trend-body" class="coop-trend-body"><div class="empty">Loading</div></div>
</div> </div>
<div class="board-note">Usage across the co-op, last 30 days</div> <div class="board-note">Usage across the co-op, including the co-op's own accounts, last 30 days</div>
<div style="display:flex; align-items:center; gap:10px; margin-bottom:10px;"> <div style="display:flex; align-items:center; gap:10px; margin-bottom:10px;">
<span class="board-note" style="margin:0;">Sort by</span> <span class="board-note" style="margin:0;">Sort by</span>
<button class="mode-btn active" id="sort-cost" onclick="setBoardSort('cost')">Cost</button> <button class="mode-btn active" id="sort-cost" onclick="setBoardSort('cost')">Cost</button>
@@ -854,13 +861,13 @@ let trendDays = 90; // 0 = all time
const TREND_CONF = { const TREND_CONF = {
members: { kind: "line", title: "The co-op over time" }, members: { kind: "line", title: "The co-op over time" },
requests: { kind: "bar", title: "Requests the co-op served" }, requests: { kind: "bar", title: "Requests the co-op served" },
spend: { kind: "bar", title: "What the co-op spent" }, spend: { kind: "bar", title: "What the co-op spent (members + the co-op's own accounts)" },
energy: { kind: "bar", title: "Energy the co-op used" }, energy: { kind: "bar", title: "Energy the co-op used" },
co2e: { kind: "bar", title: "Carbon the co-op emitted" } co2e: { kind: "bar", title: "Carbon the co-op emitted" }
}; };
async function loadMetrics() { async function loadMetrics() {
if (trendsData) { renderTrends(); return; } if (trendsData) { renderCoopTotals(); renderTrends(); return; }
const host = $("trend-body"); const host = $("trend-body");
try { try {
const r = await fetch("/api/metrics"); const r = await fetch("/api/metrics");
@@ -868,12 +875,27 @@ async function loadMetrics() {
const d = await r.json(); const d = await r.json();
if (d.error) { host.innerHTML = "<div class=empty>" + d.error + "</div>"; return; } if (d.error) { host.innerHTML = "<div class=empty>" + d.error + "</div>"; return; }
trendsData = d.series || []; trendsData = d.series || [];
renderCoopTotals();
renderTrends(); renderTrends();
} catch (e) { } catch (e) {
host.innerHTML = "<div class=empty>Could not load the co-op chart</div>"; host.innerHTML = "<div class=empty>Could not load the co-op chart</div>";
} }
} }
// The co-op total spans the members AND the co-op's own (operator) accounts —
// spend_usd is the whole co-op. operator_spend_usd is the co-op's own share of
// 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);
}
function setTrendMetric(m) { function setTrendMetric(m) {
trendMetric = m; trendMetric = m;
$("tm-members").className = "mode-btn" + (m === "members" ? " active" : ""); $("tm-members").className = "mode-btn" + (m === "members" ? " active" : "");