usage charts on the shared chart module: my-usage chart + co-op trend tooltips
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
Both usage views get charts. The co-op trend refactor onto the shared coop_chart.js module replaces its native <title> tooltips with the panel's exact-value hover tooltips (registerChartTips contract). NEW: the my-usage view gets a per-day chart (Spend/Requests metric toggle, 30/90/all-time period toggle) fed by /broker/usage's new daily series, same shared module, same tooltip contract. No chart library, no CDN. CI: .woodpecker.yml (oidc + chart sync pins, both harnesses via the vendored check-page-js.sh, which splices the module as the app does). Verified: rendering captured across all chart states matches the panel's shared-module output; harnesses exercise exact values, zero days, gaps, clamping, no-refetch toggles.
This commit is contained in:
1 parent
622982a371
commit
8c469c1242
6 files changed
+750
-67
No files matched your search
Executable
+91
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env bash
|
||||
# Pre-deploy check for the panel/dashboard PAGE_HTML frontends.
|
||||
#
|
||||
# scripts/check-page-js.sh <path/to/app/main.py> [harness.js]
|
||||
#
|
||||
# 1. Slices PAGE_HTML out of the SOURCE TEXT. Do NOT import the module instead:
|
||||
# app/oidc.py needs authlib, which is not installed on the steward host.
|
||||
# 2. Extracts the single <script> block and runs `node --check` on it.
|
||||
# 3. Given a harness file, runs stub + page JS + harness in ONE node eval scope,
|
||||
# so the harness can call the page's real functions against real data.
|
||||
#
|
||||
# Never skip step 2. It is load-bearing, not ritual: it has caught a broken
|
||||
# string escape and a duplicate `const` that py_compile passed, either of which
|
||||
# blanks every data table on the live page.
|
||||
#
|
||||
# Harness rules (learned the hard way — see references/panel-and-dashboard-patterns.md):
|
||||
# * Assert invariants, not the shape you happened to observe. Parse the
|
||||
# rendered markup into value/position pairs and check the RELATIONSHIP.
|
||||
# * A regex ending in `<` swallows the closing bracket and garbles every
|
||||
# captured label. Match `[^<]*` and stop.
|
||||
# * parseFloat("10-28") is 10. Date labels impersonate numeric ticks; filter
|
||||
# them out by their dash before trusting a numeric parse.
|
||||
# * When a check fails, suspect the ASSERTION first and fix it — never loosen
|
||||
# it to get a green run.
|
||||
set -euo pipefail
|
||||
|
||||
MAIN="${1:?usage: check-page-js.sh <path/to/app/main.py> [harness.js]}"
|
||||
HARNESS="${2:-}"
|
||||
TMP="$(mktemp -d)"
|
||||
trap 'rm -rf "$TMP"' EXIT
|
||||
|
||||
python3 - "$MAIN" "$TMP/page.js" <<'PY'
|
||||
import re, sys
|
||||
src = open(sys.argv[1]).read()
|
||||
marker = 'PAGE_HTML = """'
|
||||
start = src.index(marker) + len(marker)
|
||||
html = src[start:src.index('"""', start)]
|
||||
blocks = re.findall(r"<script>(.*?)</script>", html, re.S)
|
||||
if len(blocks) != 1:
|
||||
sys.exit(f"expected exactly 1 <script> block in PAGE_HTML, found {len(blocks)}")
|
||||
js = blocks[0]
|
||||
# Splice the shared chart module exactly as the app does at import time, so
|
||||
# the checked/eval'd JS is what actually serves. The placeholder is a comment,
|
||||
# and a missing module file leaves the placeholder in place (the harness then
|
||||
# fails loudly on the missing CoOpChart instead of silently checking stale JS).
|
||||
from pathlib import Path
|
||||
placeholder = "/*@@CHART_MODULE@@*/"
|
||||
if placeholder in js:
|
||||
module = Path(sys.argv[1]).resolve().parent / "coop_chart.js"
|
||||
if module.exists():
|
||||
js = js.replace(placeholder, module.read_text())
|
||||
print(f"spliced shared chart module ({module.name}) into the extracted page JS")
|
||||
open(sys.argv[2], "w").write(js)
|
||||
print(f"extracted {len(js)} chars of page JS from PAGE_HTML")
|
||||
PY
|
||||
|
||||
node --check "$TMP/page.js"
|
||||
echo "node --check: OK"
|
||||
|
||||
if [ -n "$HARNESS" ]; then
|
||||
cat > "$TMP/stub.js" <<'JS'
|
||||
// Minimal browser stubs, loaded BEFORE the page JS: these pages call
|
||||
// load()/loadModels() at the end of the script, which need document + fetch.
|
||||
const els = {};
|
||||
const mkEl = (id) => ({
|
||||
id, style: {}, textContent: "", innerHTML: "", className: "",
|
||||
offsetWidth: 120, offsetHeight: 48,
|
||||
getAttribute: () => null,
|
||||
appendChild: () => {},
|
||||
});
|
||||
globalThis.document = {
|
||||
getElementById: (id) => (els[id] = els[id] || mkEl(id)),
|
||||
querySelectorAll: () => [],
|
||||
body: { innerHTML: "", appendChild: (el) => { globalThis.__lastAppended = el; } },
|
||||
createElement: (tag) => mkEl(undefined),
|
||||
addEventListener: (type, fn) => { (globalThis.__listeners = globalThis.__listeners || {})[type] = fn; },
|
||||
};
|
||||
globalThis.fetch = async () => ({
|
||||
status: 200,
|
||||
json: async () => ({ series: [], totals: {}, members: [], admins: [], models: [] }),
|
||||
});
|
||||
globalThis.window = {
|
||||
innerWidth: 1440,
|
||||
innerHeight: 900,
|
||||
addEventListener: (type, fn) => {},
|
||||
};
|
||||
JS
|
||||
node -e 'const fs=require("fs"), r=p=>fs.readFileSync(p,"utf8");
|
||||
eval(r(process.argv[1]) + r(process.argv[2]) + r(process.argv[3]));' \
|
||||
"$TMP/stub.js" "$TMP/page.js" "$HARNESS"
|
||||
fi
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
"""coop_chart.js sync pin — the repo copy is byte-identical to the canonical
|
||||
module, and main.py splices it into the served page.
|
||||
|
||||
WHY THIS EXISTS
|
||||
app/coop_chart.js is a byte-identical copy of the canonical module
|
||||
(co-op/ops-docs repo: scripts/shared/coop_chart.js) — the single source for
|
||||
the hand-rolled chart rendering + hover tooltips both UIs ship. This test
|
||||
pins the copy to the canonical revision recorded below: ANY hand edit to
|
||||
app/coop_chart.js breaks the pin and goes red. To change the module, edit
|
||||
the canonical file and run ops-docs/scripts/sync-chart-module.sh, which
|
||||
re-copies and re-pins both repos in one step (check-chart-sync.py is the
|
||||
pre-deploy gate).
|
||||
|
||||
Generated by sync-chart-module.sh — do not edit by hand.
|
||||
|
||||
Run: python3 tests/test_chart_sync.py
|
||||
"""
|
||||
import hashlib
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
CANONICAL_SHA256 = "a029fd40189acc38b8b218921c56e24e0208c8c8e0ab550a320bbd57449f5fed"
|
||||
SPLICE = 'PAGE_HTML = PAGE_HTML.replace("/*@@CHART_MODULE@@*/", CHART_MODULE_JS)'
|
||||
|
||||
failures = []
|
||||
|
||||
copy = ROOT / "app" / "coop_chart.js"
|
||||
if not copy.exists():
|
||||
failures.append("app/coop_chart.js missing")
|
||||
else:
|
||||
got = hashlib.sha256(copy.read_bytes()).hexdigest()
|
||||
if got == CANONICAL_SHA256:
|
||||
print(f"ok - app/coop_chart.js matches the pinned canonical revision ({got[:16]}…)")
|
||||
else:
|
||||
failures.append(
|
||||
"app/coop_chart.js DRIFTED from the pinned canonical revision\n"
|
||||
f" pinned {CANONICAL_SHA256}\n"
|
||||
f" actual {got}\n"
|
||||
" fix: edit the canonical module (ops-docs: scripts/shared/coop_chart.js) "
|
||||
"and run ops-docs/scripts/sync-chart-module.sh — never hand-edit the copy.")
|
||||
|
||||
main_src = (ROOT / "app" / "main.py")
|
||||
if not main_src.exists():
|
||||
failures.append("app/main.py missing")
|
||||
else:
|
||||
msrc = main_src.read_text()
|
||||
if SPLICE not in msrc:
|
||||
failures.append(
|
||||
"app/main.py does not splice the shared chart module into PAGE_HTML:\n"
|
||||
f" expected literal: {SPLICE}")
|
||||
if 'parent / "coop_chart.js"' not in msrc:
|
||||
failures.append("app/main.py does not load app/coop_chart.js")
|
||||
|
||||
if failures:
|
||||
print()
|
||||
print("FAILED: " + ", ".join(failures))
|
||||
sys.exit(1)
|
||||
print("chart sync pin: OK")
|
||||
Reference in new issue
Block a user