File size: 7,683 Bytes
8a93f94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const ACCENT = "#5eead4";
const BASE = "#6b7280";
const css = (v) => getComputedStyle(document.documentElement).getPropertyValue(v).trim();

let DATA = null;
let scoreChart = null;
let lenChart = null;

const $ = (id) => document.getElementById(id);

function shortKey(k) {
  if (!k || k.length <= 14) return { head: k || "—", tail: "" };
  return { head: k.slice(0, 8), tail: "…" + k.slice(-6) };
}

async function load() {
  try {
    const res = await fetch("./data.json", { cache: "no-store" });
    if (!res.ok) throw new Error("HTTP " + res.status);
    DATA = await res.json();
  } catch (err) {
    console.error("Failed to load data.json", err);
    $("board-empty").hidden = false;
    $("board-empty").textContent = "Could not load data.json — has the refresh job run yet?";
    return;
  }
  hydrateUpdated();
  buildViews();
  renderActiveView();
}

function hydrateUpdated() {
  const ts = DATA.generated_at;
  const label = ts ? new Date(ts).toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" }) : "unknown";
  $("updated").textContent = "updated " + label;
  $("footer-meta").textContent = DATA.sample ? "sample data" : "live data";
}

function buildViews() {
  const select = $("validator-select");
  select.innerHTML = "";
  const views = [];

  if (DATA.aggregate && Array.isArray(DATA.aggregate.rankings) && DATA.aggregate.rankings.length) {
    views.push({ id: "__consensus__", label: "Consensus · all validators" });
  }
  for (const v of DATA.validators || []) {
    const sk = shortKey(v.hotkey);
    views.push({ id: v.hotkey, label: "Validator " + sk.head + sk.tail });
  }
  for (const view of views) {
    const opt = document.createElement("option");
    opt.value = view.id;
    opt.textContent = view.label;
    select.appendChild(opt);
  }
  select.onchange = renderActiveView;

  $("stat-validators").textContent = (DATA.validators || []).length || "0";
  const everyMiner = new Set();
  for (const v of DATA.validators || []) for (const r of v.rankings || []) everyMiner.add(r.hotkey);
  $("stat-miners").textContent = everyMiner.size || (DATA.aggregate?.rankings?.length ?? 0);
}

function activeView() {
  const id = $("validator-select").value;
  if (id === "__consensus__" || !id) {
    return {
      rankings: DATA.aggregate?.rankings || [],
      history: DATA.aggregate?.history || null,
    };
  }
  const v = (DATA.validators || []).find((x) => x.hotkey === id);
  return { rankings: v?.rankings || [], history: v?.history || null };
}

function renderActiveView() {
  const view = activeView();
  renderBoard(view.rankings);
  renderStats(view);
  renderTrends(view);
}

function renderStats(view) {
  const top = view.rankings[0];
  $("stat-top").textContent = top ? top.score.toFixed(3) : "—";
  const ep = view.history?.epochs;
  $("stat-epoch").textContent = ep && ep.length ? ep[ep.length - 1] : "—";
}

function renderBoard(rankings) {
  const body = $("board-body");
  body.innerHTML = "";
  $("board-empty").hidden = rankings.length > 0;
  const max = rankings.reduce((m, r) => Math.max(m, r.score), 0) || 1;

  rankings.forEach((r, i) => {
    const tr = document.createElement("tr");
    tr.className = "rank-" + (i + 1);
    const sk = shortKey(r.hotkey);
    const pct = Math.max(2, Math.round((r.score / max) * 100));

    const rankCell = document.createElement("td");
    rankCell.className = "c-rank";
    const rank = document.createElement("span");
    rank.className = "rank-num";
    rank.textContent = String(i + 1).padStart(2, "0");
    rankCell.appendChild(rank);

    const hotkeyCell = document.createElement("td");
    const hotkey = document.createElement("span");
    hotkey.className = "hotkey";
    const head = document.createElement("span");
    head.className = "head";
    head.textContent = sk.head;
    const tail = document.createElement("span");
    tail.className = "tail";
    tail.textContent = sk.tail;
    hotkey.append(head, tail);
    hotkeyCell.appendChild(hotkey);

    const scoreCell = document.createElement("td");
    scoreCell.className = "score-val";
    scoreCell.textContent = r.score.toFixed(4);

    const barCell = document.createElement("td");
    barCell.className = "c-bar";
    const bar = document.createElement("div");
    bar.className = "bar";
    const barFill = document.createElement("i");
    barFill.style.width = `${pct}%`;
    bar.appendChild(barFill);
    barCell.appendChild(bar);

    tr.append(rankCell, hotkeyCell, scoreCell, barCell);
    body.appendChild(tr);
  });
}

function seriesFor(history, hotkey) {
  if (!history) return null;
  const epochs = history.epochs || [];
  const miner = history.miners ? history.miners[hotkey] : null;
  const original = history.original || null;
  return { epochs, miner, original };
}

function hasPoints(arr) {
  return Array.isArray(arr) && arr.some((x) => x !== null && x !== undefined && !Number.isNaN(x));
}

function renderTrends(view) {
  const top = view.rankings[0];
  const s = top ? seriesFor(view.history, top.hotkey) : null;
  const sk = top ? shortKey(top.hotkey) : { head: "—", tail: "" };
  $("trend-miner").textContent = sk.head + sk.tail;

  drawTrend("score", s, "score", top);
  drawTrend("len", s, "completion_len", top);
}

function drawTrend(which, s, field, top) {
  const canvasId = which === "score" ? "chart-score" : "chart-len";
  const awaitId = which === "score" ? "await-score" : "await-len";
  const minerData = s?.miner?.[field];
  const origData = s?.original?.[field];
  const ok = top && s && s.epochs.length && (hasPoints(minerData) || hasPoints(origData));

  $(awaitId).hidden = !!ok;
  if (!ok) {
    destroyChart(which);
    return;
  }

  const ctx = $(canvasId).getContext("2d");
  const datasets = [];
  if (hasPoints(minerData)) datasets.push(lineDS("Top miner", minerData, ACCENT, css("--accent-ghost")));
  if (hasPoints(origData)) datasets.push(lineDS("Original model", origData, BASE, "transparent", true));

  const cfg = {
    type: "line",
    data: { labels: s.epochs, datasets },
    options: chartOptions(which === "len" ? "tokens" : "score"),
  };
  destroyChart(which);
  const chart = new Chart(ctx, cfg);
  if (which === "score") scoreChart = chart; else lenChart = chart;
}

function lineDS(label, data, color, fill, dashed = false) {
  return {
    label, data,
    borderColor: color,
    backgroundColor: fill,
    borderWidth: 2,
    borderDash: dashed ? [5, 4] : [],
    pointRadius: 0,
    pointHoverRadius: 4,
    tension: 0.25,
    fill: !dashed,
    spanGaps: true,
  };
}

function chartOptions(yLabel) {
  const grid = css("--line-soft");
  const tick = css("--faint");
  return {
    responsive: true,
    maintainAspectRatio: false,
    interaction: { mode: "index", intersect: false },
    plugins: {
      legend: { labels: { color: css("--muted"), boxWidth: 10, boxHeight: 10, usePointStyle: true, font: { size: 11 } } },
      tooltip: {
        backgroundColor: "#0b0e12", borderColor: css("--line"), borderWidth: 1,
        titleColor: css("--muted"), bodyColor: css("--text"), padding: 10, displayColors: true,
      },
    },
    scales: {
      x: { title: { display: true, text: "epoch", color: tick, font: { size: 10 } }, grid: { color: grid }, ticks: { color: tick, font: { size: 10 } } },
      y: { title: { display: true, text: yLabel, color: tick, font: { size: 10 } }, grid: { color: grid }, ticks: { color: tick, font: { size: 10 } } },
    },
  };
}

function destroyChart(which) {
  if (which === "score" && scoreChart) { scoreChart.destroy(); scoreChart = null; }
  if (which === "len" && lenChart) { lenChart.destroy(); lenChart = null; }
}

window.addEventListener("DOMContentLoaded", load);