File size: 9,768 Bytes
161cb23
 
 
 
460e6e1
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460e6e1
 
 
 
 
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460e6e1
 
 
 
 
 
 
 
 
 
 
161cb23
 
 
 
419a527
 
161cb23
 
 
460e6e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161cb23
460e6e1
 
 
 
161cb23
460e6e1
 
 
 
 
 
 
 
 
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
a1415e8
 
161cb23
a1415e8
 
161cb23
a1415e8
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1415e8
 
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460e6e1
 
 
161cb23
 
 
 
 
 
 
 
460e6e1
 
 
 
 
 
 
 
161cb23
460e6e1
161cb23
 
 
 
 
460e6e1
161cb23
 
 
 
 
 
 
 
 
 
460e6e1
 
 
 
 
 
 
 
 
 
 
 
 
161cb23
 
 
 
 
 
419a527
161cb23
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// 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();