From a581a856339b1b31f388217265aa042532007cad Mon Sep 17 00:00:00 2001 From: Nelson Chan <3271800+chakflying@users.noreply.github.com> Date: Mon, 20 May 2024 04:03:32 +0800 Subject: [PATCH] Feat: Use `UptimeCalculator` for PingChart (#4264) Co-authored-by: Frank Elsinga --- server/server.js | 2 + .../socket-handlers/chart-socket-handler.js | 38 ++ server/uptime-calculator.js | 6 +- src/components/PingChart.vue | 418 +++++++++++++++--- src/mixins/datetime.js | 27 ++ src/mixins/socket.js | 11 + 6 files changed, 426 insertions(+), 76 deletions(-) create mode 100644 server/socket-handlers/chart-socket-handler.js diff --git a/server/server.js b/server/server.js index 28fb44267..38158c546 100644 --- a/server/server.js +++ b/server/server.js @@ -149,6 +149,7 @@ const apicache = require("./modules/apicache"); const { resetChrome } = require("./monitor-types/real-browser-monitor-type"); const { EmbeddedMariaDB } = require("./embedded-mariadb"); const { SetupDatabase } = require("./setup-database"); +const { chartSocketHandler } = require("./socket-handlers/chart-socket-handler"); app.use(express.json()); @@ -1528,6 +1529,7 @@ let needSetup = false; apiKeySocketHandler(socket); remoteBrowserSocketHandler(socket); generalSocketHandler(socket, server); + chartSocketHandler(socket); log.debug("server", "added all socket handlers"); diff --git a/server/socket-handlers/chart-socket-handler.js b/server/socket-handlers/chart-socket-handler.js new file mode 100644 index 000000000..654db0e73 --- /dev/null +++ b/server/socket-handlers/chart-socket-handler.js @@ -0,0 +1,38 @@ +const { checkLogin } = require("../util-server"); +const { UptimeCalculator } = require("../uptime-calculator"); +const { log } = require("../../src/util"); + +module.exports.chartSocketHandler = (socket) => { + socket.on("getMonitorChartData", async (monitorID, period, callback) => { + try { + checkLogin(socket); + + log.debug("monitor", `Get Monitor Chart Data: ${monitorID} User ID: ${socket.userID}`); + + if (period == null) { + throw new Error("Invalid period."); + } + + let uptimeCalculator = await UptimeCalculator.getUptimeCalculator(monitorID); + + let data; + if (period <= 24) { + data = uptimeCalculator.getDataArray(period * 60, "minute"); + } else if (period <= 720) { + data = uptimeCalculator.getDataArray(period, "hour"); + } else { + data = uptimeCalculator.getDataArray(period / 24, "day"); + } + + callback({ + ok: true, + data, + }); + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); +}; diff --git a/server/uptime-calculator.js b/server/uptime-calculator.js index 3735a79a3..55059e960 100644 --- a/server/uptime-calculator.js +++ b/server/uptime-calculator.js @@ -290,7 +290,7 @@ class UptimeCalculator { dailyStatBean.pingMax = dailyData.maxPing; { // eslint-disable-next-line no-unused-vars - const { up, down, avgPing, minPing, maxPing, ...extras } = dailyData; + const { up, down, avgPing, minPing, maxPing, timestamp, ...extras } = dailyData; if (Object.keys(extras).length > 0) { dailyStatBean.extras = JSON.stringify(extras); } @@ -305,7 +305,7 @@ class UptimeCalculator { hourlyStatBean.pingMax = hourlyData.maxPing; { // eslint-disable-next-line no-unused-vars - const { up, down, avgPing, minPing, maxPing, ...extras } = hourlyData; + const { up, down, avgPing, minPing, maxPing, timestamp, ...extras } = hourlyData; if (Object.keys(extras).length > 0) { hourlyStatBean.extras = JSON.stringify(extras); } @@ -320,7 +320,7 @@ class UptimeCalculator { minutelyStatBean.pingMax = minutelyData.maxPing; { // eslint-disable-next-line no-unused-vars - const { up, down, avgPing, minPing, maxPing, ...extras } = minutelyData; + const { up, down, avgPing, minPing, maxPing, timestamp, ...extras } = minutelyData; if (Object.keys(extras).length > 0) { minutelyStatBean.extras = JSON.stringify(extras); } diff --git a/src/components/PingChart.vue b/src/components/PingChart.vue index c550b4b4f..60b0d3c9c 100644 --- a/src/components/PingChart.vue +++ b/src/components/PingChart.vue @@ -1,16 +1,24 @@