Files
go-service/cmd/server/web/js/app.js

102 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ===== BIT RENDER =====
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);
}
}
// ===== PROGRESS BAR =====
function setBar(id, value) {
value = Math.max(0, Math.min(100, value));
document.getElementById(id).style.width = value + "%";
}
// ===== SMOOTHING =====
let lastValue = 0;
function smoothBar(target) {
const alpha = 0.2;
lastValue = lastValue + alpha * (target - lastValue);
return lastValue;
}
// ===== MEDIAN FILTER =====
function median(values) {
if (values.length === 0) return 0;
const sorted = [...values].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 !== 0
? sorted[mid]
: (sorted[mid - 1] + sorted[mid]) / 2;
}
// ===== MAIN LOOP =====
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();
// ===== STATUS =====
document.getElementById("status").textContent = h.status || "неизвестно";
const filled = h.stats?.filled ?? 0;
const uptime = Math.round(h.uptime_sec || 0);
document.getElementById("meta").textContent =
"аптайм: " + uptime + "s | буфер: " + filled;
// ===== LATEST =====
const latest = Number(h.latest || 0);
document.getElementById("latest").textContent = latest;
renderBits(latest);
// ===== HISTORY =====
const arr = Array.isArray(hist.bytes) ? hist.bytes : [];
document.getElementById("history").innerHTML =
arr.map((b, i) =>
"[" + i + "] " +
(Number(b) & 0xFF).toString(2).padStart(8, "0") +
" = значение: " + (Number(b) & 0xFF)
).join("<br>");
// ===== FILTER (последние 9 значений) =====
const last = arr.slice(-9).map(v => Number(v) & 0xFF);
const filtered = median(last);
// ===== NORMALIZE =====
const percent = (filtered / 255) * 100;
// ===== SMOOTH =====
const smooth = smoothBar(percent);
// ===== ONLY CHANNEL 1 =====
setBar("bar1", smooth);
} catch (e) {
console.error("UPDATE ERROR:", e);
document.getElementById("status").textContent = "СЕРВЕР НЕДОСТУПЕН";
}
}
// быстрее и плавнее
setInterval(update, 500);
update();