chart module: re-pin to the canonical revision that adds the definition layer
Same byte-identical module as the panel; the dashboard's own port to the shared definitions is the next commit. check-page-js.sh's DOM stub now supports setAttribute/insertAdjacentHTML, which the shared metric-button builder needs.
This commit is contained in:
1 parent
8c469c1242
commit
c5d4677ec3
3 files changed
+176
-8
No files matched your search
+161
-1
@@ -44,6 +44,21 @@
|
|||||||
* drawBars(fr, vals, opts) bars; NULLs skipped (no zero bars).
|
* drawBars(fr, vals, opts) bars; NULLs skipped (no zero bars).
|
||||||
* drawLineRuns(fr, vals, opts) lines broken into measured runs.
|
* drawLineRuns(fr, vals, opts) lines broken into measured runs.
|
||||||
* hoverTargets(fr, vals, host) transparent hover circles on measured points.
|
* hoverTargets(fr, vals, host) transparent hover circles on measured points.
|
||||||
|
*
|
||||||
|
* Definition layer — the ONE place a chart is described (both UIs read it):
|
||||||
|
* CHART_METRICS the metric list, in order: id, title, kind (line|bar|multi),
|
||||||
|
* cont (continuous axis), fmt (money|count|wh|g), val(row);
|
||||||
|
* "multi" carries `series` (the money chart). Anything a
|
||||||
|
* member may see belongs in this list.
|
||||||
|
* metricDef(id) the definition for a metric id.
|
||||||
|
* fmtMoney/fmtMetric/fmtMoneyTick shared value formatters, so both apps'
|
||||||
|
* labels match byte for byte.
|
||||||
|
* renderChart({host, def, rows, aria}) -> { svg, legend, empty? }
|
||||||
|
* one renderer for both UIs: registers the EXACT tooltip
|
||||||
|
* values, breaks lines at NULLs (a gap is never a zero), and
|
||||||
|
* returns markup plus legend items. The APP writes the DOM —
|
||||||
|
* element ids, toggle class names and empty-state markup are
|
||||||
|
* the only parts that legitimately differ per app.
|
||||||
*/
|
*/
|
||||||
(function (global) {
|
(function (global) {
|
||||||
"use strict";
|
"use strict";
|
||||||
@@ -241,6 +256,144 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==== Chart definitions — the ONE place a chart is described ==============
|
||||||
|
// A chart is DATA here, not markup: the same metrics, in the same order, with
|
||||||
|
// the same titles and the same shape in every UI. Each app supplies its own
|
||||||
|
// data rows and its own DOM (its toggle classes, its empty-state markup, its
|
||||||
|
// legend element), which is the only thing that legitimately differs.
|
||||||
|
//
|
||||||
|
// WHY: the two UIs had drifted while sharing only the drawing primitives —
|
||||||
|
// the same five metrics shipped with different titles and different chart
|
||||||
|
// kinds (the panel's "Spend per day" was the dashboard's "What the co-op
|
||||||
|
// spent (members + the co-op's own accounts)"). Anything a member may see
|
||||||
|
// belongs in THIS list, so it exists once and cannot drift again.
|
||||||
|
//
|
||||||
|
// fmt selects the value formatter: "money" | "count" | "wh" | "g".
|
||||||
|
// kind is "line" | "bar" | "multi" (multi = N series on one axis, the money
|
||||||
|
// chart: cumulative revenue, cumulative token spend, and the OC balance,
|
||||||
|
// which only exists from the day the snapshots began, so it draws as a gap
|
||||||
|
// before that — never as a zero).
|
||||||
|
const CHART_METRICS = [
|
||||||
|
{ id: "members", title: "Members", kind: "line", cont: false, fmt: "count",
|
||||||
|
val: r => r.members_total },
|
||||||
|
{ id: "requests", title: "Requests per day", kind: "bar", cont: false, fmt: "count",
|
||||||
|
val: r => r.requests },
|
||||||
|
{ id: "spend", title: "Token spend per day", kind: "bar", cont: true, fmt: "money",
|
||||||
|
val: r => r.spend_usd },
|
||||||
|
{ id: "money", title: "Money", kind: "multi", cont: true, fmt: "money",
|
||||||
|
aria: "Cumulative revenue, token spend and OC balance over time",
|
||||||
|
series: [
|
||||||
|
{ name: "Revenue", key: "revenue_cum", color: "var(--green-deep)" },
|
||||||
|
{ name: "Token spend", key: "spend_cum", color: "#4a6a8a" },
|
||||||
|
{ name: "OC balance", key: "oc_balance", color: "#a97a2a", dotSingle: true }
|
||||||
|
] },
|
||||||
|
{ id: "energy", title: "Energy per day", kind: "bar", cont: true, fmt: "wh",
|
||||||
|
val: r => r.energy_wh },
|
||||||
|
{ id: "co2e", title: "Carbon per day", kind: "bar", cont: true, fmt: "g",
|
||||||
|
val: r => r.co2e_g }
|
||||||
|
];
|
||||||
|
|
||||||
|
function metricDef(id) {
|
||||||
|
for (let i = 0; i < CHART_METRICS.length; i++) if (CHART_METRICS[i].id === id) return CHART_METRICS[i];
|
||||||
|
return CHART_METRICS[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Value formatting (shared so the two apps' labels match byte for byte;
|
||||||
|
// both already agreed, this only stops them from diverging) ----
|
||||||
|
function fmtMoney(v) {
|
||||||
|
const n = Math.abs(Number(v)) < 0.005 ? 0 : (Number(v) || 0);
|
||||||
|
return "$" + n.toFixed(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fmtMetric(def, v) {
|
||||||
|
if (v === null || v === undefined) return "not measured"; // the gap rule
|
||||||
|
switch (def && def.fmt) {
|
||||||
|
case "money": return fmtMoney(v);
|
||||||
|
case "wh": return Number(v).toLocaleString() + " Wh";
|
||||||
|
case "g": return Number(v).toLocaleString() + " g";
|
||||||
|
default: return Number(v).toLocaleString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Money-axis ticks: the 1/2/5 x 10^k rule (niceStep continuous) with integer
|
||||||
|
// dollars at or above $1 and cents below, so a $0.50 tick does not read "$1".
|
||||||
|
function fmtMoneyTick(v) {
|
||||||
|
return v >= 1 ? "$" + Math.round(v).toLocaleString() : "$" + v.toFixed(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- The chart renderer: one implementation for both UIs ----
|
||||||
|
// renderChart({ host, def, rows, aria }) -> { svg, legend }
|
||||||
|
// * registers the EXACT tooltip values (the module's contract) under `host`
|
||||||
|
// * returns the SVG string and the legend items; the APP writes the DOM,
|
||||||
|
// because that is the part that differs (element ids, class names)
|
||||||
|
// * a NULL value stays a GAP: no zero, no bar, and no tooltip stamp
|
||||||
|
// `rows` for a "multi" chart carry the series keys; otherwise `def.val(row)`.
|
||||||
|
function renderChart(cfg) {
|
||||||
|
const def = cfg.def, rows = cfg.rows || [], host = cfg.host;
|
||||||
|
const legend = [];
|
||||||
|
if (def.kind === "multi") {
|
||||||
|
const series = def.series || [];
|
||||||
|
const vals = series.map(s => rows.map(r => r[s.key]));
|
||||||
|
const tops = [];
|
||||||
|
for (let si = 0; si < vals.length; si++) {
|
||||||
|
for (let i = 0; i < vals[si].length; i++) {
|
||||||
|
const v = vals[si][i];
|
||||||
|
if (v !== null && v !== undefined) tops.push(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const maxV = tops.length ? Math.max.apply(null, tops) : 0;
|
||||||
|
const step = niceStep((maxV <= 0 ? 1 : maxV * 1.08) / 4, true);
|
||||||
|
const fr = chartFrame({
|
||||||
|
n: rows.length, top: step * 4, step,
|
||||||
|
tickLabel: def.fmt === "money" ? fmtMoneyTick : (v => fmtMetric(def, v)),
|
||||||
|
aria: cfg.aria || def.aria || def.title,
|
||||||
|
gridStroke: cfg.gridStroke || "var(--border)"
|
||||||
|
});
|
||||||
|
registerChartTips(host, rows.map((r, i) => ({
|
||||||
|
label: r.day,
|
||||||
|
series: series.map((s, si) => [s.name, fmtMetric(def, vals[si][i])])
|
||||||
|
})));
|
||||||
|
for (let si = 0; si < series.length; si++) {
|
||||||
|
const s = series[si];
|
||||||
|
drawLineRuns(fr, vals[si], {
|
||||||
|
color: s.color, dotSingle: s.dotSingle !== false, dotR: 3,
|
||||||
|
area: !!s.area, areaOpacity: s.areaOpacity
|
||||||
|
});
|
||||||
|
hoverTargets(fr, vals[si], host);
|
||||||
|
legend.push({ color: s.color, name: s.name });
|
||||||
|
}
|
||||||
|
dateMarks(fr, rows.map(r => r.day));
|
||||||
|
fr.parts.push("</svg>");
|
||||||
|
return { svg: fr.parts.join(""), legend };
|
||||||
|
}
|
||||||
|
|
||||||
|
const vals = rows.map(def.val);
|
||||||
|
registerChartTips(host, rows.map((r, i) => ({
|
||||||
|
label: r.day,
|
||||||
|
series: [[def.title, fmtMetric(def, vals[i])]]
|
||||||
|
})));
|
||||||
|
const have = vals.filter(v => v !== null && v !== undefined);
|
||||||
|
if (have.length < 2) return { svg: "", legend, empty: true };
|
||||||
|
const maxV = Math.max.apply(null, have);
|
||||||
|
const step = niceStep((maxV <= 0 ? 1 : maxV * 1.08) / 4, def.cont);
|
||||||
|
const tickLabel = v => fmtMetric(def, v);
|
||||||
|
const fr = chartFrame({
|
||||||
|
n: vals.length, top: step * 4, step, tickLabel,
|
||||||
|
aria: cfg.aria || (def.title + " over time"),
|
||||||
|
gridStroke: cfg.gridStroke || "var(--border)"
|
||||||
|
});
|
||||||
|
if (def.kind === "bar") {
|
||||||
|
drawBars(fr, vals, { color: "var(--green-mid)", host });
|
||||||
|
} else {
|
||||||
|
drawLineRuns(fr, vals, { color: "var(--green-deep)", area: true });
|
||||||
|
hoverTargets(fr, vals, host);
|
||||||
|
}
|
||||||
|
dateMarks(fr, rows.map(r => r.day));
|
||||||
|
fr.parts.push("</svg>");
|
||||||
|
return { svg: fr.parts.join(""), legend };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// chartTipData is exported for inspection (harnesses assert on the exact
|
// chartTipData is exported for inspection (harnesses assert on the exact
|
||||||
// registered points); treat it as read-only in page code.
|
// registered points); treat it as read-only in page code.
|
||||||
global.CoOpChart = {
|
global.CoOpChart = {
|
||||||
@@ -256,6 +409,13 @@
|
|||||||
dateMarks,
|
dateMarks,
|
||||||
drawBars,
|
drawBars,
|
||||||
drawLineRuns,
|
drawLineRuns,
|
||||||
hoverTargets
|
hoverTargets,
|
||||||
|
// the definition layer both UIs read
|
||||||
|
CHART_METRICS,
|
||||||
|
metricDef,
|
||||||
|
fmtMoney,
|
||||||
|
fmtMetric,
|
||||||
|
fmtMoneyTick,
|
||||||
|
renderChart
|
||||||
};
|
};
|
||||||
})(typeof globalThis !== "undefined" ? globalThis : window);
|
})(typeof globalThis !== "undefined" ? globalThis : window);
|
||||||
+14
-6
@@ -62,12 +62,20 @@ if [ -n "$HARNESS" ]; then
|
|||||||
// Minimal browser stubs, loaded BEFORE the page JS: these pages call
|
// Minimal browser stubs, loaded BEFORE the page JS: these pages call
|
||||||
// load()/loadModels() at the end of the script, which need document + fetch.
|
// load()/loadModels() at the end of the script, which need document + fetch.
|
||||||
const els = {};
|
const els = {};
|
||||||
const mkEl = (id) => ({
|
const mkEl = (id) => {
|
||||||
id, style: {}, textContent: "", innerHTML: "", className: "",
|
const el = {
|
||||||
offsetWidth: 120, offsetHeight: 48,
|
id, style: {}, textContent: "", innerHTML: "", className: "",
|
||||||
getAttribute: () => null,
|
offsetWidth: 120, offsetHeight: 48,
|
||||||
appendChild: () => {},
|
getAttribute: () => null,
|
||||||
});
|
setAttribute: () => {},
|
||||||
|
appendChild: () => {},
|
||||||
|
};
|
||||||
|
// The pages build some markup by inserting HTML into a container
|
||||||
|
// (buildMetricButtons inserts the SHARED metric list), so the stub has to
|
||||||
|
// support it or the harness cannot exercise that path at all.
|
||||||
|
el.insertAdjacentHTML = (pos, html) => { el.innerHTML += html; };
|
||||||
|
return el;
|
||||||
|
};
|
||||||
globalThis.document = {
|
globalThis.document = {
|
||||||
getElementById: (id) => (els[id] = els[id] || mkEl(id)),
|
getElementById: (id) => (els[id] = els[id] || mkEl(id)),
|
||||||
querySelectorAll: () => [],
|
querySelectorAll: () => [],
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import sys
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parent.parent
|
ROOT = Path(__file__).resolve().parent.parent
|
||||||
CANONICAL_SHA256 = "a029fd40189acc38b8b218921c56e24e0208c8c8e0ab550a320bbd57449f5fed"
|
CANONICAL_SHA256 = "233c39faa7b40a9827fefd21aeb5505fdfa770833de2decebc8fe8bbbaee1903"
|
||||||
SPLICE = 'PAGE_HTML = PAGE_HTML.replace("/*@@CHART_MODULE@@*/", CHART_MODULE_JS)'
|
SPLICE = 'PAGE_HTML = PAGE_HTML.replace("/*@@CHART_MODULE@@*/", CHART_MODULE_JS)'
|
||||||
|
|
||||||
failures = []
|
failures = []
|
||||||
|
|||||||
Reference in new issue
Block a user