// Leaderboard and stats page. Opens in its own tab from the main page. import "./board.css"; import { byId, el } from "./dom.js"; import { LANGUAGES, NOTES, sentenceCase } from "./labels.js"; import MODEL_SCORES from "./model-scores.json"; import { fetchLeaderboard, fetchStats } from "./refiner.js"; import REWARD_FACTS from "./reward-facts.json"; import { formatCount, formatMs } from "./telemetry.js"; const SVG_NS = "http://www.w3.org/2000/svg"; const MIN_LANGUAGE_FILES = 20; const MAX_KINDS = 5; const MAX_LANGUAGES = 6; const MINUTE = 60_000; const HOUR = 60 * MINUTE; const DAY = 24 * HOUR; // reward-facts.json holds published benchmark figures per scorer version, copied from the README // benchmark table at each release. The strip stays hidden when the running scorer has no entry. const README_AT = "https://github.com/brianbell-x/codehygiene/blob"; const SORTED_MODELS = [...MODEL_SCORES.models].sort( (left, right) => right.score - left.score || left.name.localeCompare(right.name), ); const state = { kind: "repos", board: null, stats: null, yours: null }; function icon(name) { const svg = document.createElementNS(SVG_NS, "svg"); svg.setAttribute("class", "icon"); svg.setAttribute("aria-hidden", "true"); const use = document.createElementNS(SVG_NS, "use"); use.setAttribute("href", `#${name}`); svg.append(use); return svg; } function points(score) { return String(Math.round(score)); } function track(percent) { const width = Math.max(0, Math.min(100, percent)); return el("span", { class: "track", "aria-hidden": "true" }, [el("i", { style: `width: ${width}%` })]); } function since(isoTime) { const elapsed = Date.now() - Date.parse(isoTime); if (elapsed < MINUTE) { return "Updated just now"; } if (elapsed < HOUR) { return `Updated ${Math.floor(elapsed / MINUTE)} min ago`; } if (elapsed < DAY) { return `Updated ${Math.floor(elapsed / HOUR)} h ago`; } return `Updated ${Math.floor(elapsed / DAY)} d ago`; } function repoCell(repo) { const [owner, name] = repo.split("/"); return el("a", { class: "board-repo", href: `https://github.com/${repo}`, target: "_blank", rel: "noopener", }, [ icon("i-github"), el("span", { class: "board-repo-name" }, [ el("span", { class: "board-owner", text: `${owner}/` }), el("span", { text: name }), ]), ]); } function modelCell(row) { return el("span", { class: "board-model", title: row.id }, [ icon("i-cpu"), el("span", { class: "board-repo-name" }, [ el("span", { class: "board-owner", text: `${row.provider} / ` }), el("span", { text: row.name }), ]), ]); } function repoRow(row, index) { const yours = state.yours && row.repo.toLowerCase() === state.yours; return el("li", { class: yours ? "board-row is-yours" : "board-row" }, [ el("span", { class: "board-rank", text: String(index + 1) }), repoCell(row.repo), track(row.score), el("span", { class: "board-score", text: points(row.score) }), ]); } function modelRow(row, index) { return el("li", { class: "board-row" }, [ el("span", { class: "board-rank", text: String(index + 1) }), modelCell(row), track(row.score), el("span", { class: "board-score", text: points(row.score) }), ]); } function renderNote() { const note = byId("board-note"); const showModels = state.kind === "models" && Boolean(MODEL_SCORES.note); note.hidden = !showModels; note.textContent = showModels ? MODEL_SCORES.note : ""; note.classList.toggle("is-preview", showModels && Boolean(MODEL_SCORES.preview)); } function renderBoard() { const isModels = state.kind === "models"; const rows = isModels ? SORTED_MODELS : (state.board?.cleanest ?? []); const rowView = isModels ? modelRow : repoRow; byId("board-list").replaceChildren(...rows.map(rowView)); byId("board-list").hidden = rows.length === 0; byId("board-empty").hidden = rows.length > 0 || (!isModels && !state.board); for (const button of document.querySelectorAll(".board-mode")) { button.setAttribute("aria-pressed", String(button.dataset.kind === state.kind)); } byId("tab-board").setAttribute("href", isModels ? "#models" : "#board"); renderNote(); if (!isModels) { byId("board-list").querySelector(".is-yours")?.scrollIntoView({ block: "center" }); } } function tile(label, value, note) { return el("div", { class: "tile" }, [ el("p", { class: "tile-label", text: label }), el("p", { class: "tile-value", text: value }), el("p", { class: "tile-note", text: note }), ]); } function statTiles(stats) { const timed = stats.score_ms_median === null ? "—" : formatMs(stats.score_ms_median); const typical = stats.median_score === null ? "—" : String(stats.median_score); const avgNote = stats.avg_score_scored === null ? "No files yet" : `${Math.round(stats.avg_score_scored)} avg score`; return [ tile("Files scored", formatCount(stats.files_scored), avgNote), tile("Issues flagged", formatCount(stats.issues_found ?? 0), "across scored files"), tile("Scoring time", timed, "per file, median"), tile("Typical file", typical, `of 100, ${formatCount(stats.leaderboard_entries)} repos ranked`), ]; } function barRow(label, percent, value) { return el("div", { class: "bar-row" }, [ el("span", { class: "bar-label", text: label }), track(percent), el("span", { class: "bar-value", text: value }), ]); } function emptyChart() { return el("p", { class: "panel-empty", text: "No data yet" }); } function kindBars(stats) { const source = stats.squiggles_by_kind ?? {}; const kinds = Object.entries(source) .filter(([kind, count]) => count > 0 && kind in NOTES) .sort((left, right) => right[1] - left[1]) .slice(0, MAX_KINDS); const top = kinds[0]?.[1] ?? 1; const rows = kinds.map(([kind, count]) => { const label = sentenceCase(NOTES[kind]); return barRow(label, (count / top) * 100, formatCount(count)); }); return rows.length ? rows : [emptyChart()]; } function languageAverages(byExt) { const totals = new Map(); for (const [ext, row] of Object.entries(byExt)) { const language = LANGUAGES[ext]; if (!language || row.avg_score === null) { continue; } const total = totals.get(language) ?? { files: 0, points: 0 }; total.files += row.scored; total.points += row.avg_score * row.scored; totals.set(language, total); } return [...totals] .filter(([, total]) => total.files >= MIN_LANGUAGE_FILES) .map(([language, total]) => [language, total.points / total.files]) .sort((left, right) => right[1] - left[1]) .slice(0, MAX_LANGUAGES); } function languageBars(stats) { const averages = languageAverages(stats.by_ext); const rows = averages.map(([language, average]) => barRow(language, average, points(average))); return rows.length ? rows : [emptyChart()]; } function rewardFact(fact) { return el("div", { class: "reward-fact", title: fact.title }, [ el("dt", { text: fact.label }), el("dd", { text: fact.value }), ]); } function renderReward(version) { const reward = REWARD_FACTS[version]; byId("reward").hidden = !reward; if (!reward) { return; } byId("reward-version").textContent = `Benchmarks, v${version}`; byId("reward-source").href = `${README_AT}/${reward.ref}/README.md#benchmarks`; byId("reward-facts").replaceChildren(...reward.facts.map(rewardFact)); } function renderStats() { const stats = state.stats; if (!stats) { return; } byId("tiles").replaceChildren(...statTiles(stats)); byId("chart-kinds").replaceChildren(...kindBars(stats)); byId("chart-languages").replaceChildren(...languageBars(stats)); renderReward(stats.scorer_version); } function currentView() { return window.location.hash === "#stats" ? "stats" : "board"; } function updatedText() { if (currentView() === "stats") { return "Live"; } if (state.kind === "models") { return MODEL_SCORES.preview ? "Preview" : "Avg score"; } const updated = state.board?.updated_at; return updated ? since(updated) : ""; } function renderUpdated() { byId("board-updated").textContent = updatedText(); } function syncFromHash() { if (window.location.hash === "#models") { state.kind = "models"; } else if (window.location.hash === "#board" || window.location.hash.startsWith("#repo=")) { state.kind = "repos"; } } function showView() { syncFromHash(); const view = currentView(); byId("view-board").hidden = view !== "board"; byId("view-stats").hidden = view !== "stats"; byId("tab-board").setAttribute("aria-current", view === "board" ? "page" : "false"); byId("tab-stats").setAttribute("aria-current", view === "stats" ? "page" : "false"); renderBoard(); renderUpdated(); } function readYours() { const match = window.location.hash.match(/^#repo=(.+)$/); return match ? decodeURIComponent(match[1]).toLowerCase() : null; } function boot() { state.yours = readYours(); for (const button of document.querySelectorAll(".board-mode")) { button.addEventListener("click", () => { state.kind = button.dataset.kind; const hash = state.kind === "models" ? "#models" : "#board"; window.history.replaceState(null, "", hash); renderBoard(); renderUpdated(); }); } window.addEventListener("hashchange", () => { state.yours = readYours() ?? state.yours; showView(); }); showView(); fetchLeaderboard().then((board) => { state.board = board; renderBoard(); renderUpdated(); }, () => { state.board = { cleanest: [] }; renderBoard(); }); fetchStats().then((stats) => { state.stats = stats; renderStats(); }, () => byId("tiles").replaceChildren(emptyChart())); } boot();