Files
go-service/cmd/server/web/js/app.js
2026-05-08 10:07:21 +03:00

90 lines
2.3 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.

// app.js (оркестратор)
import { fetchHealth, fetchHistory } from "./data.js";
import { state, smooth, addSignal } from "./state.js";
import {
initDOM,
setStatus,
setMeta,
setBars,
setSignal,
DOM
} from "./ui.js";
import { initAccordion } from "./accordion.js";
import { drawChart } from "./chart.js";
let lastChart = 0;
// ================= INIT =================
window.addEventListener("DOMContentLoaded", () => {
initDOM();
initAccordion();
});
// ================= UPDATE =================
async function update() {
try {
const [h, hist] = await Promise.all([
fetchHealth(),
fetchHistory()
]);
// ===== STATUS =====
setStatus(h.status || "неизвестно");
const arr = Array.isArray(hist.bytes) ? hist.bytes : [];
const raw = arr[arr.length - 1] ?? 0;
// ===== DECODE =====
const value = raw & 0x3F;
const level = (raw >> 6) & 0x3;
const percent = (value / 63) * 100;
// ===== STATE =====
addSignal(value);
// ===== UI =====
setBars(smooth(percent));
setSignal(level);
if (DOM.lastByte) {
DOM.lastByte.textContent = value;
}
// ===== META =====
const uptime = Math.round(h.uptime_sec || 0);
const filled = h.stats?.filled ?? 0;
const bps = Math.round(h.stats?.bits_per_sec ?? 0);
const Bps = Math.round(h.stats?.bytes_per_sec ?? 0);
setMeta(
`работа: ${uptime}s | буфер: ${filled} | ${bps} bit/s (${Bps} B/s)`
);
// ===== HISTORY =====
if (DOM.history) {
DOM.history.innerHTML = arr
.map((b, i) =>
`[${i}] ${(b & 0xFF).toString(2).padStart(8, "0")} = ${b & 0xFF}`
)
.join("<br>");
}
// ===== CHART =====
const now = performance.now();
if (now - lastChart > 100) {
drawChart(state.signalHistory);
lastChart = now;
}
} catch (e) {
console.error("UPDATE ERROR:", e);
setStatus("СЕРВЕР НЕДОСТУПЕН");
}
}
// ================= LOOP =================
setInterval(update, 500);
update();