доработка графика
This commit is contained in:
@@ -15,6 +15,9 @@ import { drawChart } from "./chart.js";
|
|||||||
|
|
||||||
let lastChart = 0;
|
let lastChart = 0;
|
||||||
let connectionLost = false;
|
let connectionLost = false;
|
||||||
|
let consecutiveErrors = 0; // счётчик ошибок подряд
|
||||||
|
const MAX_ERRORS = 3; // после скольки ошибок замедляем опрос
|
||||||
|
let pollTimer = null; // храним ID таймера для возможности сброса
|
||||||
|
|
||||||
// ================= INIT =================
|
// ================= INIT =================
|
||||||
window.addEventListener("DOMContentLoaded", () => {
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
@@ -30,6 +33,12 @@ async function update() {
|
|||||||
fetchHistory()
|
fetchHistory()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
consecutiveErrors = 0;
|
||||||
|
|
||||||
|
if (connectionLost) {
|
||||||
|
console.log("[app] Соединение восстановлено");
|
||||||
|
}
|
||||||
|
|
||||||
// ===== STATUS =====
|
// ===== STATUS =====
|
||||||
const isOnline = h.pipe_alive && h.has_data;
|
const isOnline = h.pipe_alive && h.has_data;
|
||||||
|
|
||||||
@@ -37,7 +46,7 @@ async function update() {
|
|||||||
setStatus("На связи");
|
setStatus("На связи");
|
||||||
connectionLost = false;
|
connectionLost = false;
|
||||||
} else {
|
} else {
|
||||||
setStatus("Нет связи", true); // true = lost connection
|
setStatus("Нет связи", true);
|
||||||
connectionLost = true;
|
connectionLost = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,9 +66,8 @@ async function update() {
|
|||||||
setBars(smooth(percent));
|
setBars(smooth(percent));
|
||||||
setSignal(level);
|
setSignal(level);
|
||||||
} else {
|
} else {
|
||||||
// При потере связи - сбрасываем индикаторы
|
setBars(0, true);
|
||||||
setBars(0, true); // true = no connection
|
setSignal(0, true);
|
||||||
setSignal(0, true); // true = no connection
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DOM.lastByte) {
|
if (DOM.lastByte) {
|
||||||
@@ -95,7 +103,12 @@ async function update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("UPDATE ERROR:", e);
|
consecutiveErrors++;
|
||||||
|
|
||||||
|
if (consecutiveErrors === 1) {
|
||||||
|
console.warn("[app] Сервер недоступен, ждём...");
|
||||||
|
}
|
||||||
|
|
||||||
setStatus("СЕРВЕР НЕДОСТУПЕН", true);
|
setStatus("СЕРВЕР НЕДОСТУПЕН", true);
|
||||||
setBars(0, true);
|
setBars(0, true);
|
||||||
setSignal(0, true);
|
setSignal(0, true);
|
||||||
@@ -103,9 +116,37 @@ async function update() {
|
|||||||
if (DOM.lastByte) {
|
if (DOM.lastByte) {
|
||||||
DOM.lastByte.textContent = "--";
|
DOM.lastByte.textContent = "--";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (consecutiveErrors >= MAX_ERRORS) {
|
||||||
|
clearInterval(pollTimer);
|
||||||
|
pollTimer = setInterval(update, 2000);
|
||||||
|
console.log("[app] Замедлили опрос до 2с");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= LOOP =================
|
// ================= RECONNECT LOGIC =================
|
||||||
setInterval(update, 500);
|
function startPolling() {
|
||||||
|
if (pollTimer) clearInterval(pollTimer);
|
||||||
|
|
||||||
|
// Если были ошибки — сразу пробуем переподключиться
|
||||||
|
if (consecutiveErrors > 0) {
|
||||||
|
console.log("[app] Попытка переподключения...");
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
pollTimer = setInterval(update, 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Сброс при фокусе окна (пользователь вернулся — пробуем быстро)
|
||||||
|
window.addEventListener("focus", () => {
|
||||||
|
if (consecutiveErrors >= MAX_ERRORS) {
|
||||||
|
console.log("[app] Окно в фокусе, ускоряем опрос");
|
||||||
|
consecutiveErrors = 0;
|
||||||
|
startPolling();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ================= START =================
|
||||||
|
startPolling();
|
||||||
update();
|
update();
|
||||||
@@ -1,29 +1,30 @@
|
|||||||
// chart.js
|
// chart.js
|
||||||
let ctx = null;
|
|
||||||
|
|
||||||
export function drawChart(canvas, history) {
|
export function drawChart(canvas, history) {
|
||||||
if (!canvas || !history?.length) return;
|
if (!canvas || !history?.length) return;
|
||||||
|
|
||||||
const ctx = canvas.getContext("2d");
|
|
||||||
|
|
||||||
const rect = canvas.getBoundingClientRect();
|
const rect = canvas.getBoundingClientRect();
|
||||||
canvas.width = rect.width;
|
const displayHeight = rect.height || 120;
|
||||||
canvas.height = rect.height;
|
const displayWidth = rect.width || 800;
|
||||||
|
|
||||||
|
canvas.width = displayWidth;
|
||||||
|
canvas.height = displayHeight;
|
||||||
|
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
const w = canvas.width;
|
const w = canvas.width;
|
||||||
const h = canvas.height;
|
const h = canvas.height;
|
||||||
|
|
||||||
ctx.clearRect(0, 0, w, h);
|
ctx.clearRect(0, 0, w, h);
|
||||||
|
|
||||||
const stepX = w / 1000;
|
const stepX = w / history.length;
|
||||||
const scaleY = h / 63;
|
const scaleY = h / 63;
|
||||||
|
|
||||||
ctx.strokeStyle = "#00ff88";
|
ctx.strokeStyle = "#00ff88";
|
||||||
|
ctx.lineWidth = 2;
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
|
|
||||||
for (let i = 0; i < history.length; i++) {
|
for (let i = 0; i < history.length; i++) {
|
||||||
const x = i * stepX;
|
const x = i * stepX;
|
||||||
const y = h - history[i].value * scaleY;
|
const y = h - history[i] * scaleY;
|
||||||
|
|
||||||
if (i === 0) ctx.moveTo(x, y);
|
if (i === 0) ctx.moveTo(x, y);
|
||||||
else ctx.lineTo(x, y);
|
else ctx.lineTo(x, y);
|
||||||
|
|||||||
Reference in New Issue
Block a user