From f89ed0a3a44b7d8dad2b98c9e7a0163bbd93d7ca Mon Sep 17 00:00:00 2001 From: Stefan Ottosson Date: Fri, 12 Aug 2022 18:43:24 +0200 Subject: [PATCH 001/188] feat: added HTTP method option for push monitor --- server/routers/api-router.js | 7 ++- src/pages/EditMonitor.vue | 82 ++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 201efc41f..bba789ac5 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -31,9 +31,8 @@ router.get("/api/entry-page", async (request, response) => { response.json(result); }); -router.get("/api/push/:pushToken", async (request, response) => { +router.all("/api/push/:pushToken", async (request, response) => { try { - let pushToken = request.params.pushToken; let msg = request.query.msg || "OK"; let ping = request.query.ping || null; @@ -48,6 +47,10 @@ router.get("/api/push/:pushToken", async (request, response) => { throw new Error("Monitor not found or not active."); } + if (monitor.method !== request.method) { + throw new Error("Monitor HTTP method (" + monitor.method + ") does not match request (" + request.method + ")."); + } + const previousHeartbeat = await Monitor.getPreviousHeartbeat(monitor.id); if (monitor.isUpsideDown()) { diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 82859a4ff..d82aa2800 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -398,7 +398,7 @@ - + + +
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 101/188] 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 | 422 ++++++++++++++---- src/mixins/datetime.js | 27 ++ src/mixins/socket.js | 11 + 6 files changed, 428 insertions(+), 78 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 @@