From edd14f6e40a9843bdbd6ed86d7f3b557dab41080 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 21 Jul 2023 11:59:05 +0800 Subject: [PATCH] Perf: Fully batch importantHeartbeatList --- server/client.js | 19 +++++++++++++++---- server/server.js | 4 +--- src/mixins/socket.js | 10 ++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/server/client.js b/server/client.js index 2e3bd43b7b..543e31ebae 100644 --- a/server/client.js +++ b/server/client.js @@ -70,7 +70,7 @@ async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = /** * Important Heart beat list (aka event list) * @param {Socket} socket Socket.io instance - * @param {number} monitorID ID of monitor to send heartbeat history + * @param {number} monitorID ID of monitor to send heartbeat history, or null for all monitors * @param {boolean} [toUser=false] True = send to all browsers with the same user id, False = send to the current browser only * @param {boolean} [overwrite=false] Overwrite client-side's heartbeat list * @returns {Promise} @@ -78,14 +78,25 @@ async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = async function sendImportantHeartbeatList(socket, monitorID, toUser = false, overwrite = false) { const timeLogger = new TimeLogger(); - let list = await R.find("heartbeat", ` + let list = []; + + if (monitorID == null) { + // Send important beats for all monitors + list = await R.find("heartbeat", ` + important = 1 + ORDER BY time DESC + LIMIT 5000 + `); + } else { + list = await R.find("heartbeat", ` monitor_id = ? AND important = 1 ORDER BY time DESC LIMIT 500 `, [ - monitorID, - ]); + monitorID, + ]); + } timeLogger.print(`[Monitor: ${monitorID}] sendImportantHeartbeatList`); diff --git a/server/server.js b/server/server.js index 5f4ccc4682..f8dc9679c4 100644 --- a/server/server.js +++ b/server/server.js @@ -1681,9 +1681,7 @@ async function afterLogin(socket, user) { await sendHeartbeatList(socket, monitorID); } - for (let monitorID in monitorList) { - await sendImportantHeartbeatList(socket, monitorID); - } + await sendImportantHeartbeatList(socket, null); for (let monitorID in monitorList) { await Monitor.sendStats(io, monitorID, user.id); diff --git a/src/mixins/socket.js b/src/mixins/socket.js index 2d27d109a0..958adc60ca 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -222,6 +222,16 @@ export default { }); socket.on("importantHeartbeatList", (monitorID, data, overwrite) => { + if (monitorID == null) { + data.forEach(heartbeat => { + if (this.importantHeartbeatList[heartbeat.monitorID] === undefined) { + this.importantHeartbeatList[heartbeat.monitorID] = [ heartbeat ]; + } else { + this.importantHeartbeatList[heartbeat.monitorID].push(heartbeat); + } + }); + return; + } if (! (monitorID in this.importantHeartbeatList) || overwrite) { this.importantHeartbeatList[monitorID] = data; } else {