diff --git a/server/server.js b/server/server.js index 753f88b87d..66b4855267 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()); @@ -1522,6 +1523,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 0000000000..654db0e730 --- /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 7b53267481..fa252791b0 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 c550b4b4fa..60b0d3c9c1 100644 --- a/src/components/PingChart.vue +++ b/src/components/PingChart.vue @@ -1,16 +1,24 @@