From b83c59e308c1f97e5c3d305d5f7e96d0f7b6f2c4 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Mon, 18 Oct 2021 19:00:39 +0800 Subject: [PATCH] WIP: Add options for chart period Fix: Fix callback, add toast on error Fix: Improve styling Fix: Restore default chart behavior Fix: Replace 1h with 3h draft only --- server/client.js | 37 ++++++++++++++------ server/server.js | 19 ++++++++++ src/components/PingChart.vue | 67 ++++++++++++++++++++++++++++++++---- src/mixins/socket.js | 4 +++ 4 files changed, 111 insertions(+), 16 deletions(-) diff --git a/server/client.js b/server/client.js index c7b3bc162..951ffd8aa 100644 --- a/server/client.js +++ b/server/client.js @@ -31,19 +31,36 @@ async function sendNotificationList(socket) { * @param toUser True = send to all browsers with the same user id, False = send to the current browser only * @param overwrite Overwrite client-side's heartbeat list */ -async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) { +async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false, period = null) { const timeLogger = new TimeLogger(); - let list = await R.getAll(` - SELECT * FROM heartbeat - WHERE monitor_id = ? - ORDER BY time DESC - LIMIT 100 - `, [ - monitorID, - ]); + let result; - let result = list.reverse(); + if (period) { + let list = await R.getAll(` + SELECT * FROM heartbeat + WHERE monitor_id = ? AND + time > DATETIME('now', '-' || ? || ' hours') + ORDER BY time ASC + `, [ + monitorID, + period, + ]); + + result = list; + + } else { + let list = await R.getAll(` + SELECT * FROM heartbeat + WHERE monitor_id = ? + ORDER BY time DESC + LIMIT 100 + `, [ + monitorID, + ]); + + result = list.reverse(); + } if (toUser) { io.to(socket.userID).emit("heartbeatList", monitorID, result, overwrite); diff --git a/server/server.js b/server/server.js index 3084cad21..4209b45c3 100644 --- a/server/server.js +++ b/server/server.js @@ -644,6 +644,25 @@ exports.entryPage = "dashboard"; } }); + socket.on("getMonitorBeats", async (monitorID, period, callback) => { + try { + checkLogin(socket); + + console.log(`Get Monitor Beats: ${monitorID} User ID: ${socket.userID}`); + + await sendHeartbeatList(socket, monitorID, true, true, period); + + callback({ + ok: true + }); + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + // Start or Resume the monitor socket.on("resumeMonitor", async (monitorID, callback) => { try { diff --git a/src/components/PingChart.vue b/src/components/PingChart.vue index 0baa9881d..4818bba2f 100644 --- a/src/components/PingChart.vue +++ b/src/components/PingChart.vue @@ -1,5 +1,18 @@ + + diff --git a/src/mixins/socket.js b/src/mixins/socket.js index d7ac8bcb9..679794496 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -328,6 +328,10 @@ export default { clearStatistics(callback) { socket.emit("clearStatistics", callback); }, + + getMonitorBeats(monitorID, period, callback) { + socket.emit("getMonitorBeats", monitorID, period, callback); + } }, computed: {