Files
go-service/web/js/app.js
2026-05-01 10:18:39 +03:00

68 lines
1.8 KiB
JavaScript

function renderBits(byte) {
const bits = document.getElementById("bits");
bits.innerHTML = "";
byte = Number(byte || 0);
for (let i = 7; i >= 0; i--) {
const v = (byte >> i) & 1;
const d = document.createElement("div");
d.className = "bit" + (v ? " on" : "");
d.textContent = v;
bits.appendChild(d);
}
}
function setBar(id, value) {
value = Math.max(0, Math.min(100, value));
document.getElementById(id).style.width = value + "%";
}
async function update() {
try {
const r1 = await fetch("/api/health");
const r2 = await fetch("/api/history");
const h = await r1.json();
const hist = await r2.json();
console.log("health:", h);
console.log("history:", hist);
document.getElementById("status").textContent = h.status || "unknown";
const filled = h.stats?.filled ?? 0;
const uptime = Math.round(h.uptime_sec || 0);
document.getElementById("meta").textContent =
"uptime: " + uptime + "s | filled: " + filled;
const latest = Number(h.latest || 0);
document.getElementById("latest").textContent = latest;
renderBits(latest);
setBar("bar1", (latest & 15) * 6.6);
setBar("bar2", ((latest * 3) % 16) * 6.6);
setBar("bar3", ((latest * 5) % 16) * 6.6);
const arr = Array.isArray(hist.bytes) ? hist.bytes : [];
document.getElementById("history").innerHTML =
arr.map((b, i) =>
"[" + i + "] " +
(Number(b) & 15).toString(2).padStart(4, "0") +
" = " + (Number(b) & 15)
).join("<br>");
} catch (e) {
console.error("UPDATE ERROR:", e);
document.getElementById("status").textContent = "SERVER DOWN";
}
}
setInterval(update, 500);
update();