diff --git a/admin-panel-chart-harness.js b/admin-panel-chart-harness.js new file mode 100644 index 0000000..b699dda --- /dev/null +++ b/admin-panel-chart-harness.js @@ -0,0 +1,92 @@ +// Harness for the admin panel's page JS. Run through the skill's checker — +// ~/.hermes/skills/research/inference-cooperative/scripts/check-page-js.sh \ +// app/main.py admin-panel-chart-harness.js +// which evals stub + page JS + this file in ONE node scope, so the assertions +// below exercise the page's REAL functions (renderTrends, registerChartTips, +// showChartTip, moveChartTip) against a fixture shaped like the portal's +// /admin/metrics payload. Assert invariants, never the shape you happened to +// observe; when a check fails here, suspect this file first. +const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } }; + +// Fixture: oldest-first daily rows, with a NULL day (unmeasured) to exercise +// the gap rule (NULL renders as a gap, never a zero — and gets no tooltip). +trendsData = [ + { day: "2026-11-01", members_total: 71, spend_usd: 12.34, requests: 2900, energy_wh: 8400.5, co2e_g: 2100.25 }, + { day: "2026-11-02", members_total: 72, spend_usd: null, requests: 3100, energy_wh: null, co2e_g: null }, + { day: "2026-11-03", members_total: 74, spend_usd: 5.01, requests: 1204, energy_wh: 9100, co2e_g: 2300 }, + { day: "2026-11-04", members_total: 75, spend_usd: 7.89, requests: 980, energy_wh: 6400, co2e_g: 1600 }, +]; + +// ---- Bars (spend): every measured day is hoverable, gaps are not ---- +trendMetric = "spend"; +trendDays = 30; +renderTrends(); +{ + const host = document.getElementById("trend-body"); + const pts = chartTipData["trend-body"]; + assert(Array.isArray(pts) && pts.length === 4, "registry: one point per row (got " + (pts && pts.length) + ")"); + assert(pts[0].label === "2026-11-01", "registry label is the row's exact day"); + assert(pts[0].series[0][0] === "Spend per day", "registry series name is the chart title"); + // EXACT values: the tooltip string must be the data value through the + // panel's own formatter — never a pixel-derived estimate. + assert(pts[0].series[0][1] === "$12.34", "exact spend for 11-01 (got " + pts[0].series[0][1] + ")"); + assert(pts[2].series[0][1] === "$5.01", "exact spend for 11-03 (got " + pts[2].series[0][1] + ")"); + assert(pts[3].series[0][1] === "$7.89", "exact spend for 11-04 (got " + pts[3].series[0][1] + ")"); + assert(pts[1].series[0][1] === "not measured", "NULL day registers as not measured"); + + const marks = host.innerHTML.match(/data-tip-i="(\d+)"/g) || []; + const idxs = marks.map(s => parseInt(s.match(/\d+/)[0], 10)).sort((a, b) => a - b); + assert(JSON.stringify(idxs) === "[0,2,3]", "bars stamped only on measured days (got " + JSON.stringify(idxs) + ")"); + assert((host.innerHTML.match(/data-tip-host="trend-body"/g) || []).length === 3, "every bar carries its host id"); + assert(!//.test(host.innerHTML), "native <title> tooltips are gone (replaced by the shared helper)"); + + // Tooltip content for a hovered bar: exact date + exact value. + showChartTip("trend-body", 0, 500, 300); + const box = globalThis.__lastAppended; + assert(box && box.className === "chart-tip", "tooltip box created on first show"); + assert(box.innerHTML.indexOf("2026-11-01") !== -1, "tooltip shows the exact date"); + assert(box.innerHTML.indexOf("$12.34") !== -1, "tooltip shows the exact value"); + assert(box.style.display === "block", "tooltip visible while over a point"); + + // Viewport clamping: near the right/bottom edges the box flips and lands + // fully inside the viewport; near the origin it clamps to the margin. + moveChartTip(1430, 890); // stub box is 120x48, viewport 1440x900 + const x1 = parseInt(box.style.left, 10), y1 = parseInt(box.style.top, 10); + assert(x1 + 120 <= 1440 && y1 + 48 <= 900, "clamped inside the viewport (got " + x1 + "," + y1 + ")"); + moveChartTip(0, 0); + assert(parseInt(box.style.left, 10) >= 4 && parseInt(box.style.top, 10) >= 4, "clamped to the top-left margin"); + hideChartTip(); + assert(box.style.display === "none", "tooltip hides off-chart"); +} + +// ---- Line (members): circles stamped, same registry contract ---- +trendMetric = "members"; +renderTrends(); +{ + const host = document.getElementById("trend-body"); + const marks = (host.innerHTML.match(/data-tip-i="(\d+)"/g) || []).map(s => parseInt(s.match(/\d+/)[0], 10)).sort((a, b) => a - b); + assert(JSON.stringify(marks) === "[0,1,2,3]", "line points stamped on every measured day (got " + JSON.stringify(marks) + ")"); + const pts = chartTipData["trend-body"]; + assert(pts[1].series[0][1] === "72", "exact member count for 11-02 (got " + pts[1].series[0][1] + ")"); +} + +// ---- Multi-series contract (the shape the finance chart will register) ---- +{ + registerChartTips("finance-demo", [ + { label: "2026-11-04", series: [["Revenue", "$40.00"], ["Spend", "$12.34"]] }, + ]); + showChartTip("finance-demo", 0, 20, 20); + const box = globalThis.__lastAppended; + assert(box.innerHTML.indexOf("Revenue") !== -1 && box.innerHTML.indexOf("$40.00") !== -1, "multi-series: every series at the point is shown"); + assert(box.innerHTML.indexOf("$12.34") !== -1, "multi-series: second series value shown"); +} + +// ---- Wiring: the shared delegated listeners are registered once ---- +{ + const L = globalThis.__listeners || {}; + assert(!!L["pointerover"] && !!L["pointermove"] && !!L["pointerout"], "pointer listeners registered for all charts"); + assert(!!L["pointercancel"], "touch cancel hides the tooltip"); +} + +console.log("HARNESS OK: chart tooltips (bars, line, gaps, exact values, clamping, multi-series)"); +process.exit(0); diff --git a/app/main.py b/app/main.py index 3756476..4d30fff 100644 --- a/app/main.py +++ b/app/main.py @@ -283,6 +283,14 @@ PAGE_HTML = """<!DOCTYPE html> .trend-title { font-weight: 700; font-size: 14px; color: var(--ink); } .trend-body svg { display: block; width: 100%; height: auto; overflow: visible; } .trend-empty { color: var(--muted); font-size: 13px; margin: 28px 0; text-align: center; } + /* Chart hover tooltip: one shared box for every chart, positioned near the + pointer and clamped inside the viewport. pointer-events:none so it can + never intercept a click on the chart underneath. */ + .chart-tip { position: fixed; display: none; z-index: 60; pointer-events: none; background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 2px 10px rgba(45,51,39,0.16); padding: 7px 10px; font-size: 12px; max-width: 280px; } + .chart-tip .tip-day { color: var(--muted); font-size: 11px; margin-bottom: 3px; } + .chart-tip .tip-row { display: flex; justify-content: space-between; gap: 14px; } + .chart-tip .tip-row span { color: var(--muted); } + .chart-tip .tip-row b { color: var(--ink); font-variant-numeric: tabular-nums; } @media (max-width: 600px) { .trend-controls { gap: 10px; } .trend-card { padding: 12px 10px; } } .num { font-variant-numeric: tabular-nums; } .remaining-col { font-weight: 700; color: var(--green-deep); background: #eaf3ea; } @@ -574,14 +582,116 @@ function trendLabel(v) { return Number(v).toLocaleString(); } +// ---- Chart tooltips: exact values on hover ---- +// One shared helper serves EVERY chart on the panel; no library, no CDN. +// Contract for a chart (two steps, then tooltips just work): +// 1. At render time, stamp each hoverable data element with +// data-tip-host="<hostId>" and data-tip-i="<index into the data>" +// 2. registerChartTips("<hostId>", points) with one point per index: +// { label: "2026-11-07", series: [["Spend", "$12.34"], ["Revenue", "$40.00"]] } +// Values MUST come from the chart's own data arrays (exact), never +// pixel-estimated, and `series` carries EVERY series at that x. +// A single delegated listener set on `document` then serves all charts, so +// re-rendering a chart's innerHTML needs no re-binding, and a new chart (the +// finance chart, any future one) joins chart-by-chart as it renders. The box +// is a fixed-position div with pointer-events:none, clamped inside the +// viewport so it never flickers off-screen or swallows a click. +const chartTipData = {}; // hostId -> [{ label, series: [[name, value], ...] }] +let chartTipBox = null; // the singleton tooltip div, created on first use +let chartTipCur = null; // { host, idx } the point currently shown + +function registerChartTips(hostId, points) { chartTipData[hostId] = points; } + +function chartTipTarget(el) { + return el && el.closest ? el.closest("[data-tip-host]") : null; +} + +function hideChartTip() { + chartTipCur = null; + if (chartTipBox) chartTipBox.style.display = "none"; +} + +function showChartTip(hostId, idx, cx, cy) { + const p = (chartTipData[hostId] || [])[idx]; + if (!p) { hideChartTip(); return; } + if (!chartTipBox) { + chartTipBox = document.createElement("div"); + chartTipBox.className = "chart-tip"; + document.body.appendChild(chartTipBox); + } + const same = chartTipCur && chartTipCur.host === hostId && chartTipCur.idx === idx; + if (!same) { + let html = '<div class="tip-day">' + esc(p.label) + "</div>"; + for (let s = 0; s < p.series.length; s++) { + html += '<div class="tip-row"><span>' + esc(p.series[s][0]) + "</span><b>" + esc(p.series[s][1]) + "</b></div>"; + } + chartTipBox.innerHTML = html; + chartTipCur = { host: hostId, idx: idx }; + } + chartTipBox.style.display = "block"; + moveChartTip(cx, cy); +} + +// Position beside the pointer, flipped/clamped so the box stays fully inside +// the viewport at chart edges. +function moveChartTip(cx, cy) { + if (!chartTipBox || chartTipBox.style.display === "none") return; + const pad = 12, m = 4; + const w = chartTipBox.offsetWidth, h = chartTipBox.offsetHeight; + let x = cx + pad, y = cy + pad; + if (x + w > window.innerWidth - m) x = cx - pad - w; + if (x < m) x = m; + if (y + h > window.innerHeight - m) y = cy - pad - h; + if (y < m) y = m; + chartTipBox.style.left = x + "px"; + chartTipBox.style.top = y + "px"; +} + +document.addEventListener("pointerover", (e) => { + const t = chartTipTarget(e.target); + if (!t) return; + showChartTip(t.getAttribute("data-tip-host"), parseInt(t.getAttribute("data-tip-i"), 10), e.clientX, e.clientY); +}); +document.addEventListener("pointermove", (e) => { + const t = chartTipTarget(e.target); + if (!t) return; + const host = t.getAttribute("data-tip-host"); + const idx = parseInt(t.getAttribute("data-tip-i"), 10); + const cur = chartTipCur; + if (!cur || cur.host !== host || cur.idx !== idx) showChartTip(host, idx, e.clientX, e.clientY); + else moveChartTip(e.clientX, e.clientY); +}); +document.addEventListener("pointerout", (e) => { + const from = chartTipTarget(e.target); + if (!from) return; + const to = e.relatedTarget ? chartTipTarget(e.relatedTarget) : null; + if (!to || to.getAttribute("data-tip-host") !== from.getAttribute("data-tip-host")) hideChartTip(); +}); +// Basic touch: tap/drag shows and follows, release hides it after a beat. +document.addEventListener("pointerup", (e) => { + if (e.pointerType === "touch") setTimeout(hideChartTip, 1600); +}); +document.addEventListener("pointercancel", hideChartTip); +// Scrolling or resizing moves the chart under a fixed-position box - hide +// rather than show a tooltip for the wrong point. +window.addEventListener("scroll", hideChartTip, { passive: true }); +window.addEventListener("resize", hideChartTip); + function renderTrends() { const host = $("trend-body"); if (!host || !trendsData) return; + hideChartTip(); const conf = TREND_CONF[trendMetric]; const rows = trendDays === 0 ? trendsData : trendsData.slice(-trendDays); $("trend-title").textContent = conf.title; const vals = rows.map(trendVal); + // Register the EXACT values the tooltips will show, straight from the data + // rows - one point per index, in the same order as the stamped elements. + registerChartTips("trend-body", rows.map((row, i) => ({ + label: row.day, + series: [[conf.title, trendLabel(vals[i])]] + }))); const have = vals.filter(v => v !== null && v !== undefined); if (have.length < 2) { host.innerHTML = "<p class=trend-empty>Not enough history yet.</p>"; @@ -615,7 +725,7 @@ function renderTrends() { const v = vals[i]; if (v === null || v === undefined) continue; const yy = yAt(v); - parts.push('<rect x="' + (xAt(i) - bw / 2) + '" y="' + yy + '" width="' + bw + '" height="' + Math.max(0.6, baseY - yy) + '" fill="var(--green-mid)"><title>' + rows[i].day + ": " + trendLabel(v) + ""); + parts.push(''); } } else { // Break the line into contiguous measured runs: a gap stays a gap. @@ -634,7 +744,7 @@ function renderTrends() { } for (let i = 0; i < n; i++) { if (vals[i] === null || vals[i] === undefined) continue; - parts.push('' + rows[i].day + ": " + trendLabel(vals[i]) + ""); + parts.push(''); } } @@ -676,6 +786,7 @@ function clearArrows(table, sortState) { function switchView(view) { currentView = view; + hideChartTip(); $('table-members').style.display = view === 'members' ? '' : 'none'; $('table-models').style.display = view === 'models' ? '' : 'none'; // The Admin section is a members-view category; hide it in the Models view