From 2f7b60f5e50b6c2c370739e376580d74b172f39e Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 22 Oct 2021 18:38:41 +0800 Subject: [PATCH] Feat: Use separate storage for custom chart period Fix: Fix import error --- server/client.js | 37 ++++++--------------- server/server.js | 17 ++++++++-- src/components/PingChart.vue | 64 +++++++++++++++++++++++------------- 3 files changed, 67 insertions(+), 51 deletions(-) diff --git a/server/client.js b/server/client.js index 951ffd8a..c7b3bc16 100644 --- a/server/client.js +++ b/server/client.js @@ -31,36 +31,19 @@ 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, period = null) { +async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) { const timeLogger = new TimeLogger(); - let result; + let list = await R.getAll(` + SELECT * FROM heartbeat + WHERE monitor_id = ? + ORDER BY time DESC + LIMIT 100 + `, [ + monitorID, + ]); - 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(); - } + let 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 4209b45c..49e867da 100644 --- a/server/server.js +++ b/server/server.js @@ -650,10 +650,23 @@ exports.entryPage = "dashboard"; console.log(`Get Monitor Beats: ${monitorID} User ID: ${socket.userID}`); - await sendHeartbeatList(socket, monitorID, true, true, period); + if (period == null) { + throw new Error("Invalid period."); + } + + let list = await R.getAll(` + SELECT * FROM heartbeat + WHERE monitor_id = ? AND + time > DATETIME('now', '-' || ? || ' hours') + ORDER BY time ASC + `, [ + monitorID, + period, + ]); callback({ - ok: true + ok: true, + data: list, }); } catch (e) { callback({ diff --git a/src/components/PingChart.vue b/src/components/PingChart.vue index 4818bba2..0c76721c 100644 --- a/src/components/PingChart.vue +++ b/src/components/PingChart.vue @@ -15,7 +15,7 @@ - @@ -217,7 +237,7 @@ export default { } .period-options { - padding: 0.3em 1.5em; + padding: 0.3em 2.2em; margin-bottom: -1.5em; float: right; position: relative;