Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

+87 -13
View File
@@ -64,12 +64,20 @@
}
// Network bars are scaled relative to a rolling observed peak so the bar is
// meaningful even without a hard cap.
var netPeak = { rx: 1024 * 256, tx: 1024 * 256 };
// meaningful even without a hard cap. The peak decays toward the floor on
// every sample (~1min half-life at the 1s sample cadence) so a single burst
// doesn't flatten the ring for the rest of the session; sustained traffic
// keeps the peak pinned at its own level.
var NET_PEAK_FLOOR = 1024 * 256; // bytes/s lower bound for the scale
var NET_PEAK_DECAY = 0.99; // per-sample peak multiplier
var netPeak = { rx: NET_PEAK_FLOOR, tx: NET_PEAK_FLOOR };
function netPercent(dir, bps) {
bps = Number(bps) || 0;
if (bps > netPeak[dir]) netPeak[dir] = bps;
return (bps / netPeak[dir]) * 100;
var peak = netPeak[dir] * NET_PEAK_DECAY;
if (peak < NET_PEAK_FLOOR) peak = NET_PEAK_FLOOR;
if (bps > peak) peak = bps;
netPeak[dir] = peak;
return (bps / peak) * 100;
}
function applySample(s) {
@@ -112,9 +120,20 @@
}
setSvcText("svcNodes", sv.nodes ? (sv.nodes.online + "/" + sv.nodes.total + " online") : "n/a",
sv.nodes && sv.nodes.total > 0 && sv.nodes.online === sv.nodes.total);
setSvcText("svcContainers", sv.containers ? (sv.containers.running + " running, " + sv.containers.stopped + " stopped") : "n/a", true);
setSvcText("svcSsh", (sv.sshSessions || 0) + " active", true);
setSvcText("svcSockets", (sv.activeSockets || 0) + " connected", true);
// Real state, not a hardcoded "ok": stopped containers flag the row the
// same way offline nodes do, and a missing payload (probe failure or an
// older panel build) must not render green. "0 connected" is also flagged:
// anyone viewing this page live is a socket themselves, so a zero count
// means the realtime layer is down and the page is on REST fallback.
setSvcText("svcContainers",
sv.containers ? (sv.containers.running + " running, " + sv.containers.stopped + " stopped") : "n/a",
!!sv.containers && sv.containers.stopped === 0);
setSvcText("svcSsh",
sv.sshSessions != null ? sv.sshSessions + " active" : "n/a",
sv.sshSessions != null);
setSvcText("svcSockets",
sv.activeSockets != null ? sv.activeSockets + " connected" : "n/a",
Number(sv.activeSockets) > 0);
var ver = $("svcVersion");
if (ver && sv.panelVersion) ver.textContent = sv.panelVersion;
}
@@ -135,7 +154,13 @@
// ---- history graph -----------------------------------------------------
var graphMode = "today"; // or a YYYY-MM-DD string
// Last drawn day, kept so a resize can re-render at the new canvas size
// without refetching /api/status/metrics/day.
var lastGraphPoints = null;
var lastGraphLabel = "";
function drawGraph(points, label) {
lastGraphPoints = points || null;
lastGraphLabel = label || "";
var canvas = $("metricsGraph");
if (!canvas) return;
var ctx = canvas.getContext("2d");
@@ -260,6 +285,19 @@
}
}
function loadSnapshot() {
fetch("/api/status/snapshot", { credentials: "include" })
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (data) {
if (data && data.ok && data.snapshot) {
applySample(data.snapshot);
}
})
.catch(function (err) {
console.warn("[monitor] snapshot fetch error:", err);
});
}
function loadSummaryAndCalendar() {
fetch("/api/status/metrics/summary", { credentials: "include" })
.then(function (r) { return r.ok ? r.json() : null; })
@@ -271,23 +309,43 @@
if (todayCell) todayCell.classList.add("cal-cell--active");
loadDay(todayKey);
})
.catch(function () { buildCalendar([]); });
.catch(function () {
buildCalendar([]);
// The resize redraw reuses the last drawn day; populate it here too,
// otherwise a summary fetch failure leaves nothing for the resize
// handler to re-render.
loadDay(new Date().toISOString().slice(0, 10));
});
}
// ---- socket wiring -----------------------------------------------------
function connectSocket() {
if (typeof io !== "function") return;
SOCKET = window._monitorSocket || io({ withCredentials: true });
if (window._monitorSocket) {
SOCKET = window._monitorSocket;
if (SOCKET.connected) {
watching = false;
subscribe();
}
return;
}
SOCKET = io({ withCredentials: true, transports: ['websocket', 'polling'] });
window._monitorSocket = SOCKET;
SOCKET.on("connect", subscribe);
SOCKET.on("connect", function () {
watching = false;
subscribe();
});
SOCKET.on("monitor:snapshot", function (snap) { applySample(snap); });
SOCKET.on("monitor:sample", function (s) { applySample(s); });
SOCKET.on("monitor:error", function (e) {
console.warn("[monitor]", e && e.error);
});
if (SOCKET.connected) subscribe();
if (SOCKET.connected) {
watching = false;
subscribe();
}
}
function subscribe() {
@@ -309,11 +367,27 @@
window.addEventListener("beforeunload", unsubscribe);
function init() {
loadSnapshot();
connectSocket();
loadSummaryAndCalendar();
// Fallback REST polling every 3 seconds to guarantee metrics stay live
setInterval(function () {
if (!document.hidden) {
loadSnapshot();
}
}, 3000);
// Redraw the already-fetched graph at the new canvas size (debounced).
// The old handler fired a /api/status/metrics/day fetch per resize
// event, so a window drag turned into a request storm.
var resizeTimer = null;
window.addEventListener("resize", function () {
if (graphMode === "today") loadDay(new Date().toISOString().slice(0, 10));
else loadDay(graphMode);
if (resizeTimer) clearTimeout(resizeTimer);
resizeTimer = setTimeout(function () {
resizeTimer = null;
if (lastGraphPoints) drawGraph(lastGraphPoints, lastGraphLabel);
}, 150);
});
}