From a387a2fa730aec32e363b328b4dda58df09a7a71 Mon Sep 17 00:00:00 2001 From: kert Date: Fri, 13 Nov 2026 04:00:41 -0500 Subject: [PATCH] feat(lex): Timeline and Valuation views (S1) --- dev/scripts/dossier_page/template.html | 6 + .../public/assets/dossier/view-timeline.js | 108 +++++++++++- .../public/assets/dossier/view-valuation.js | 160 +++++++++++++++++- tests/lex/views-history.test.mjs | 117 +++++++++++++ 4 files changed, 381 insertions(+), 10 deletions(-) create mode 100644 tests/lex/views-history.test.mjs diff --git a/dev/scripts/dossier_page/template.html b/dev/scripts/dossier_page/template.html index 1c3d5e1..46f6f79 100644 --- a/dev/scripts/dossier_page/template.html +++ b/dev/scripts/dossier_page/template.html @@ -287,6 +287,12 @@ button.dis { font-family: var(--font-body); font-size: 14px; padding: 3px 10px; .tl-item .top { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; } .tl-item .ttl { font-weight: 600; } .tl-item .sum { font-size: 14px; color: var(--secondary-foreground); } +.tl-item .tl-blk { border-top: 1px dashed var(--border); padding-top: 6px; display: grid; gap: 4px; } +.tl-item details > summary { cursor: pointer; color: var(--muted-foreground); } +.tl-item[hidden], .tl-year[hidden] { display: none; } +.tl-item .cite { white-space: normal; overflow-wrap: anywhere; } +.tl-item .facts { font-size: 14px; } +.tbl td.why { min-width: 14rem; max-width: 26rem; } .type { font-family: var(--font-mono); font-size: 10px; letter-spacing: .08em; text-transform: uppercase; padding: 1px 5px; border: 1px solid var(--border); color: var(--secondary-foreground); background: var(--secondary); white-space: nowrap; } .type.origin { border-color: var(--s-alt); } .type.valuation { border-color: var(--s-final); } diff --git a/infra/gitea/custom/public/assets/dossier/view-timeline.js b/infra/gitea/custom/public/assets/dossier/view-timeline.js index 1803869..122733d 100644 --- a/infra/gitea/custom/public/assets/dossier/view-timeline.js +++ b/infra/gitea/custom/public/assets/dossier/view-timeline.js @@ -1,10 +1,108 @@ -// lex · Timeline view. A stub until S1 Task 7 fills it in. +// lex · Timeline view: the family's dated changes, by year, each with its +// facts, our classification and CMS's own summary kept apart. // -// A view module exports needs (the API slices it reads), render(data, state) -// returning markup, and optionally mount(root, data, state, nav). +// Telehealth events show only their cited text (the stored neutral wording). + +import { esc } from "./fmt.js"; +import { factHtml, tagged } from "./cite.js"; export const needs = ["header", "timeline"]; -export function render() { - return `

This view is being built.

`; +const KIND = { + deleted: "Deleted", + cpt_panel: "CPT panel", + telehealth_list: "Telehealth list", + appeared: "Appeared", + disappeared: "Disappeared", + descriptor_change: "Descriptor change", + revalued: "Revalued", + valuation: "Valuation", + comments: "Comments", + formation: "Formation", + origin: "Origin", + cpt_edition: "CPT edition", +}; + +// the .type chip's colour class (template.html): valuation, origin, cms, link +const TYPE_CLASS = { valuation: "valuation", revalued: "valuation", origin: "origin", formation: "origin", comments: "cms" }; + +const kindLabel = (k) => (k.startsWith("link:") ? "Linked code: " + k.slice(5) : KIND[k] || k.replace(/_/g, " ")); +const kindClass = (k) => (k.startsWith("link:") ? "link" : TYPE_CLASS[k] || ""); +const filterKey = (k) => (k.startsWith("link:") ? "link" : k); +const filterLabel = (k) => (k === "link" ? "Linked codes" : kindLabel(k)); + +const list = (ids, facts) => (ids || []).map((id) => facts[id]).filter(Boolean).map((f) => factHtml(f)).join(""); + +function eventHtml(e, facts) { + const telehealth = e.kind === "telehealth_list"; + let h = `
`; + h += `
${esc(kindLabel(e.kind))}${tagged(e.what_changed, facts)}
`; + if ((e.codes || []).length) h += `
${e.codes.map((c) => `${esc(c)}`).join("")}
`; + const sources = list(e.fact_ids, facts); + if (sources) h += `
Sources and facts (${(e.fact_ids || []).filter((id) => facts[id]).length})
    ${sources}
`; + if (!telehealth) { + const ours = list(e.stance_fact_ids, facts); + if (ours) h += `
Our classification
    ${ours}
`; + const cms = list(e.cms_summary_fact_ids, facts); + if (cms) h += `
CMS's summary cms
    ${cms}
`; + } + return h + "
"; +} + +export function render(data, state) { + const T = data.timeline; + const facts = T.facts || {}; + const code = (state && state.code) || ""; + const all = T.events || []; + const events = all.filter((e) => !code || (e.codes || []).includes(code)); + const unplaced = (T.unplaced || []).map((id) => facts[id]).filter((f) => f && (!code || (f.codes || []).includes(code))); + + const counts = new Map(); + for (const e of events) counts.set(filterKey(e.kind), (counts.get(filterKey(e.kind)) || 0) + 1); + const codes = Array.from(new Set(all.flatMap((e) => e.codes || []))).sort(); + if (code && !codes.includes(code)) codes.push(code); + + let h = `

Timeline

+

Dated changes to the family: what appeared, was deleted or revalued, and CMS's own summaries of comments. Open an entry's sources to read the fact behind it.

`; + h += `
+ + ${Array.from(counts, ([k, n]) => ``).join("")} + +
`; + + const years = Array.from(new Set(events.map((e) => e.year))).sort((a, b) => a - b); + h += `
`; + if (!years.length) h += `

No timeline entries${code ? " for " + esc(code) : ""}.

`; + for (const y of years) { + const its = events.filter((e) => e.year === y); + h += `

CY${esc(y)}${its.length} ${its.length === 1 ? "entry" : "entries"}

${its.map((e) => eventHtml(e, facts)).join("")}
`; + } + h += `
`; + if (unplaced.length) { + h += `
Not placed on the timeline
+

Facts the timeline did not attach to a dated entry. They are listed here so none is lost.

+ `; + } + return h; +} + +export function mount(root, data, state, nav) { + const bar = root.querySelector("#tl-f"); + const sel = root.querySelector("#tl-code"); + if (bar) { + bar.addEventListener("click", (e) => { + const b = e.target.closest("[data-k]"); + if (!b) return; + for (const x of bar.querySelectorAll("[data-k]")) x.setAttribute("aria-pressed", String(x === b)); + // the kind filter is UI only: it hides rows, it does not touch the URL + for (const it of root.querySelectorAll(".tl-item")) it.hidden = b.dataset.k !== "all" && it.dataset.kind !== b.dataset.k; + for (const y of root.querySelectorAll(".tl-year")) y.hidden = !y.querySelector(".tl-item:not([hidden])"); + }); + } + if (sel) sel.addEventListener("change", () => nav.go({ code: sel.value }, { push: false })); + root.addEventListener("click", (e) => { + if (e.target.closest("a, button, summary, details, [data-fact], [data-ref], [data-refs]")) return; + const it = e.target.closest(".tl-item"); + if (it && it.dataset.open) nav.openFact(it.dataset.open); + }); } diff --git a/infra/gitea/custom/public/assets/dossier/view-valuation.js b/infra/gitea/custom/public/assets/dossier/view-valuation.js index 7be5c18..fa6e5e2 100644 --- a/infra/gitea/custom/public/assets/dossier/view-valuation.js +++ b/infra/gitea/custom/public/assets/dossier/view-valuation.js @@ -1,10 +1,160 @@ -// lex · Valuation view. A stub until S1 Task 7 fills it in. +// lex · Valuation view: RUC recommendation against CMS's proposed and final +// work RVUs, one table per rule, then the dot plot across the rounds. // -// A view module exports needs (the API slices it reads), render(data, state) -// returning markup, and optionally mount(root, data, state, nav). +// Proposed values sit in their own hatched columns with a "proposed" pill and +// are never blended with final ones. Columns that are empty across every row +// are left out (the slice's `columns`). + +import { esc, int, n2 } from "./fmt.js"; +import { citeHtml, factHtml } from "./cite.js"; export const needs = ["header", "valuation"]; -export function render() { - return `

This view is being built.

`; +const OPTIONAL = { + ruc_percentile: ["Survey pct.", "text"], + survey_intra_min: ["Survey intra (min)", "int"], + survey_total_min: ["Survey total (min)", "int"], + comparators: ["Comparators", "text"], + surveying_societies: ["Surveying societies", "text"], + requested_work_rvu: ["Requested", "rvu"], + rationale_quote: ["CMS rationale", "why"], + pe_note: ["PE note", "why"], +}; + +const asArr = (c) => (Array.isArray(c) ? c : c ? [c] : []); +const filled = (v) => v !== null && v !== undefined && v !== "" && !(Array.isArray(v) && v.length === 0); +const byId = (a, b) => parseInt(String(a.id).slice(1), 10) - parseInt(String(b.id).slice(1), 10); + +// The row's source: its rationale, else the cite behind its final value. +function sourceCite(r) { + if (r.rationale) return r.rationale; + const c = r.cites || {}; + for (const k of ["final_work_rvu", "decision", "proposed_work_rvu", "ruc_work_rvu", ...Object.keys(c)]) { + const [first] = asArr(c[k]); + if (first) return first; + } + return null; +} + +function cell(r, key, kind) { + const v = r[key]; + if (kind === "int") return `${v == null ? "—" : esc(int(v))}`; + if (kind === "rvu") return `${esc(n2(v))}`; + return `${filled(v) ? esc(v) : "—"}`; +} + +function groupTable(rows, columns, deleted) { + let h = `
`; + h += columns.map((c) => `${esc(OPTIONAL[c][0])}`).join(""); + h += ``; + for (const { r, i } of rows) { + const del = deleted.has(r.code); + h += ``; + h += columns.map((c) => cell(r, c, OPTIONAL[c][1])).join(""); + h += ``; + } + return h + `
CodeRUC rec.CMS proposed proposedCMS finalDecisionSource
${esc(r.code)}${esc(n2(r.ruc_work_rvu))}${esc(n2(r.proposed_work_rvu))}${esc(n2(r.final_work_rvu))}${esc(r.decision || "—")}${citeHtml(sourceCite(r))}
`; +} + +/** One row's evidence for the drawer: every non-empty field, each with its cite. */ +export function roundEvidence(r) { + const skip = new Set(["family", "cites", "rationale"]); + const dl = Object.entries(r) + .filter(([k, v]) => !skip.has(k) && filled(v)) + .map(([k, v]) => `
${esc(k.replace(/_/g, " "))}
${esc(v)}${asArr((r.cites || {})[k]).map((c) => " " + citeHtml(c)).join("")}
`) + .join(""); + const rat = r.rationale ? `
${citeHtml(r.rationale)}
Rationale
` : ""; + return `
Evidence · valuation round
+

${esc(r.code)} · CY${esc(r.rule_year)} ${esc(r.rule_kind)} rule

+
proposed final
+
${dl}
${rat}
`; +} + +/** Work RVUs by code: an open ring for each RUC recommendation, a circle for + * each CMS final value, one position per round year (older rounds paler). */ +export function dotPlot(rounds, deleted = new Set()) { + const codes = Array.from(new Set(rounds.map((r) => r.code))).sort(); + const years = Array.from(new Set(rounds.map((r) => r.rule_year))).sort((a, b) => a - b); + const vals = rounds.flatMap((r) => [r.ruc_work_rvu, r.final_work_rvu]).filter((v) => v != null).map(Number); + const xmax = Math.max(1, Math.ceil(Math.max(0, ...vals))); + const W = 660, rowH = 24, top = 46, left = 70, right = 24, H = top + codes.length * rowH + 30; + const X = (v) => left + (v / xmax) * (W - left - right); + const step = xmax > 8 ? 2 : 1; + const fade = (y) => (years.length > 1 ? (0.45 + (0.55 * years.indexOf(y)) / (years.length - 1)).toFixed(2) : "1"); + let s = ``; + for (let v = 0; v <= xmax; v += step) s += `${v.toFixed(1)}`; + s += `work RVU`; + s += `RUC recommendation + CMS final${years.length > 1 ? " (older rounds paler)" : ""}`; + codes.forEach((c, i) => { + const y = top + i * rowH + rowH / 2; + s += `${esc(c)}`; + s += ``; + for (const yr of years) { + const rs = rounds.filter((r) => r.code === c && r.rule_year === yr); + const ruc = rs.map((r) => r.ruc_work_rvu).find((v) => v != null); + const fins = Array.from(new Set(rs.filter((r) => r.rule_kind === "final" && r.final_work_rvu != null).map((r) => Number(r.final_work_rvu)))); + if (ruc != null) { + for (const f of fins) if (f !== Number(ruc)) s += ``; + s += ``; + } + for (const f of fins) s += ``; + } + }); + return s + ``; +} + +export function render(data) { + const V = data.valuation; + const rounds = V.rounds || []; + const facts = Object.values(V.facts || {}).sort(byId); + const deleted = new Set(((data.header && data.header.code_groups) || {}).deleted || []); + const columns = (V.columns || []).filter((c) => OPTIONAL[c]); + let h = `

Valuation

+

The RUC's recommendation against CMS's proposed and final work RVUs, per code per rule. Proposed values are hatched and kept in their own column; they are never blended with final ones.

+
proposed from the proposed rulefinal from the final rule
`; + if (!rounds.length) h += `

No valuation rounds captured for this family.

`; + + const keyOf = (r) => [r.rule_year, r.rule_kind, r.item_key]; + const groups = []; + rounds.forEach((r, i) => { + let g = groups.find((x) => x.key.join("|") === keyOf(r).join("|")); + if (!g) groups.push((g = { key: keyOf(r), rows: [] })); + g.rows.push({ r, i }); + }); + const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0); + groups.sort((a, b) => cmp(a.key[0], b.key[0]) || cmp(String(a.key[1]), String(b.key[1])) || cmp(String(a.key[2]), String(b.key[2]))); + const years = Array.from(new Set([...groups.map((g) => g.key[0]), ...facts.map((f) => f.year)])).sort((a, b) => (a ?? 1e9) - (b ?? 1e9)); + for (const y of years) { + for (const g of groups.filter((x) => x.key[0] === y)) { + g.rows.sort((a, b) => String(a.r.code).localeCompare(String(b.r.code))); + h += `

CY${esc(y)} · ${esc(g.key[1])} rule ${esc(g.key[2])}

${groupTable(g.rows, columns, deleted)}`; + } + const yf = facts.filter((f) => f.year === y); + if (yf.length) h += `
Round and practice-expense facts${y == null ? "" : ", CY" + esc(y)}
`; + } + if (rounds.length) h += `

Work RVUs across the rounds

${dotPlot(rounds, deleted)}
`; + return h; +} + +export function mount(root, data, state, nav) { + const rounds = data.valuation.rounds || []; + const open = (tr) => { + for (const x of root.querySelectorAll("tr.sel")) x.classList.remove("sel"); + tr.classList.add("sel"); + nav.openEvidence(roundEvidence(rounds[+tr.dataset.round])); + }; + root.addEventListener("click", (e) => { + if (e.target.closest("a, button")) return; + const tr = e.target.closest("tr[data-round]"); + if (tr) open(tr); + }); + root.addEventListener("keydown", (e) => { + if (e.key !== "Enter" && e.key !== " ") return; + const tr = e.target.closest && e.target.closest("tr[data-round]"); + if (tr && e.target === tr) { + e.preventDefault(); + open(tr); + } + }); } diff --git a/tests/lex/views-history.test.mjs b/tests/lex/views-history.test.mjs new file mode 100644 index 0000000..0870589 --- /dev/null +++ b/tests/lex/views-history.test.mjs @@ -0,0 +1,117 @@ +// lex · Timeline and Valuation views. +import { test } from "node:test"; +import { equal as eq, ok } from "node:assert/strict"; +import { readFileSync } from "node:fs"; + +import { parseLocation } from "../../infra/gitea/custom/public/assets/dossier/state.js"; +import * as timeline from "../../infra/gitea/custom/public/assets/dossier/view-timeline.js"; +import * as valuation from "../../infra/gitea/custom/public/assets/dossier/view-valuation.js"; + +const fx = (name) => JSON.parse(readFileSync(new URL(`./fixtures/${name}.json`, import.meta.url), "utf8")); +const H = fx("header"); +const T = fx("timeline"); +const V = fx("valuation"); +const st = (url) => parseLocation(new URL(url, "https://lex.test")); +const count = (s, sub) => s.split(sub).length - 1; + +test("timeline: every fact, years ascending, blocks apart, unplaced list", () => { + const t = timeline.render({ header: H, timeline: T }, st("/timeline?family=HOME-VISITS")); + for (const id of Object.keys(T.facts)) ok(t.includes(`data-fact="${id}"`), id); + ok(t.indexOf("Our classification") < t.indexOf("CMS's summary") && t.includes("Not placed on the timeline")); + ok(t.includes("CY2023") && t.includes(">Deleted<") && t.includes("Linked code: G0318")); + ok(t.includes("]*>cms { + const t = timeline.render({ header: H, timeline: T }, st("/timeline?family=HOME-VISITS#code=99341")); + ok(!t.includes("99343 was deleted")); + ok(t.includes("Invented valuation line")); + ok(/