refactor: consolidate stat-card markup in static analytics template (#4923) - #4931
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request refactors the shared static analytics template to consolidate duplicated “stat card” HTML into a single statCard() helper with an options object, while also tightening up change-percentage calculation and the access-requests stats/table rendering logic.
Changes:
- Expanded
statCard(value, label, { change, gridClass, tooltip })to support optional change rows, optional grid classes, tooltips, and multi-line labels. - Updated the top stats grid (
renderStats) and key metrics (renderEventCounts) to reusestatCard()andpctChange(). - Reworked access-requests rendering to accept pre-fetched JSON, compute per-service counts generically, and render per-service cards dynamically.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Follow-up review pass ( The defect: Also in this commit:
One item deliberately not applied, flagged for a call: Verified: display parity against real AnVIL fixture data (−19.9% / −23.7% / −31.7% / +7.1% identical before and after), all three card families, table change cells still coloured, tooltips working, malformed-payload path intact, zero JS errors. 🤖 Generated with Claude Code |
…4923) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
9e3df87 to
960ccc0
Compare
NoopDog
left a comment
There was a problem hiding this comment.
Approving — the consolidation is behaviour-preserving and both intended fixes work (-0.04% no longer renders as "+-0.0%" in green, and a non-finite ratio renders N/A instead of NaN%).
Verified: !(prior > 0) exactly negates the old guard; changeClass's == null matches the old != null checks at all three table call sites; Number(change).toFixed(1) reproduces the old pre-rounded string; the pairLabelLines hoist reads the same card.label properties as before; escapeHtml leaves newlines and quotes untouched, and the " substitution correctly happens after & escaping. All six statCard/pctChange/changeClass call sites are updated, and generator.py does a plain shutil.copy2 (no Jinja), so the new destructuring braces are safe.
Two notes on the PR description rather than the code:
- "byte-identical" top stats grid — the change text is identical, but
changeHtmlis now built in its own template literal and interpolated, so the<div class="stat-change">block carries different leading/trailing whitespace and indentation. HTML-insignificant, no visual effect — just not byte-identical. - Stale commit SHA — the description says only
63859e11belongs to this PR, but the branch carries two #4923 commits on top of #4930:98f35b3c(refactor) and960ccc00(sign-before-rounding + sharedchangeClass). Worth correcting so reviewers of the stack look at the right range.
Closes #4923
What changed
The stat-card markup in the shared static analytics template was hand-written in three places and had diverged (tooltip/change-row/grid-class combinations). This PR consolidates all three onto the existing
statCard()helper, extended with an options object:statCard(value, label, { change, gridClass, tooltip })— omittingchangeomits the change row;nullrenders "N/A vs prior month"; multi-line labels (\n) render as line breaks;gridClassis omitted when the container sizes cards itself (access-requests grid).renderStats): output equivalent — the rendered change text is identical across all change states (positive, negative, null, zero). Not byte-identical:changeHtmlis now built in its own template literal and interpolated, so the<div class="stat-change">block carries different leading/trailing whitespace and indentation. HTML-insignificant, no visual effect.renderEventCounts): inline copy deleted; now also reusespctChange()instead of hand-rolling the same formula.pctChange()hardened with aNumber.isFiniteguard (a non-finite result now renders "N/A vs prior month" instead of "NaN%" — pre-existing, practically unreachable edge).-0.04%no longer renders as"+-0.0%"in green;changeClass()is shared across all call sites.Two deliberate deltas, flagged for anyone pixel- or diff-comparing:
stat-label-wrapwrapper as the top stats, which adds a few pixels of label height on those cards — that's the unification working as intended.prioris 0 or missing — e.g. a metric introduced this month) previously rendered+0% vs prior month, because the old code interpolated the number0raw. It now routes throughNumber(change).toFixed(1)and renders+0.0%. Purely cosmetic, and consistent with the other cards now that they all round to one decimal.How verified
statCard()vs the legacy markup for all three shapes and every change-value path;pctChange()unit cases including the NaN guard.fui-grid-item-6), access-request cards (no change rows), plus the full fix: generalize access-request service classification and stat cards in static analytics template #4922 regression suite (multi-service, single-service, malformed payloads) — zero JS errors./code-reviewat high effort: no correctness bugs; the two cleanup findings it raised are applied in this PR, the label-wrap geometry note is the deliberate delta above.🤖 Generated with Claude Code