From ae7f01b3eac568952619f32668126e77123302d2 Mon Sep 17 00:00:00 2001 From: vishalsabhaya Date: Mon, 19 Aug 2024 11:36:23 +0900 Subject: [PATCH] performance improve while insert/update/delete --- server/server.js | 15 +++++++------- server/uptime-kuma-server.js | 38 ++++++++++++++++++++++++++--------- src/mixins/socket.js | 39 +++++++++++++++++++++++++++++++++--- src/pages/EditMonitor.vue | 1 - src/util.js | 6 ++++++ src/util.ts | 7 +++++++ 6 files changed, 85 insertions(+), 21 deletions(-) diff --git a/server/server.js b/server/server.js index b181092e2..7440f9e6b 100644 --- a/server/server.js +++ b/server/server.js @@ -37,7 +37,7 @@ if (!semver.satisfies(nodeVersion, requiredNodeVersions)) { } const args = require("args-parser")(process.argv); -const { sleep, log, getRandomInt, genSecret, isDev } = require("../src/util"); +const { sleep, log, getRandomInt, genSecret, isDev, OPERATIONS } = require("../src/util"); const config = require("./config"); log.debug("server", "Arguments"); @@ -695,7 +695,7 @@ let needSetup = false; await updateMonitorNotification(bean.id, notificationIDList); - await server.sendMonitorList(socket); + await server.sendMonitorList(socket, OPERATIONS.ADD, bean.id); if (monitor.active !== false) { await startMonitor(socket.userID, bean.id); @@ -850,7 +850,7 @@ let needSetup = false; await restartMonitor(socket.userID, bean.id); } - await server.sendMonitorList(socket); + await server.sendMonitorList(socket, OPERATIONS.UPDATE, bean.id); callback({ ok: true, @@ -951,7 +951,7 @@ let needSetup = false; try { checkLogin(socket); await startMonitor(socket.userID, monitorID); - await server.sendMonitorList(socket); + await server.sendMonitorList(socket, OPERATIONS.UPDATE, monitorID); callback({ ok: true, @@ -971,7 +971,7 @@ let needSetup = false; try { checkLogin(socket); await pauseMonitor(socket.userID, monitorID); - await server.sendMonitorList(socket); + await server.sendMonitorList(socket, OPERATIONS.UPDATE, monitorID); callback({ ok: true, @@ -1017,8 +1017,7 @@ let needSetup = false; msg: "successDeleted", msgi18n: true, }); - - await server.sendMonitorList(socket); + await server.sendMonitorList(socket, OPERATIONS.DELETE, monitorID); } catch (e) { callback({ @@ -1649,7 +1648,7 @@ async function afterLogin(socket, user) { // Create an array to store the combined promises for both sendHeartbeatList and sendStats const monitorPromises = []; - for (let monitorID in monitorList) { + for (let monitorID in monitorList.list) { // Combine both sendHeartbeatList and sendStats for each monitor into a single Promise monitorPromises.push( Promise.all([ diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index d0fc54e90..33ff3eae9 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -4,7 +4,7 @@ const fs = require("fs"); const http = require("http"); const { Server } = require("socket.io"); const { R } = require("redbean-node"); -const { log, isDev } = require("../src/util"); +const { log, isDev, OPERATIONS } = require("../src/util"); const Database = require("./database"); const util = require("util"); const { Settings } = require("./settings"); @@ -197,27 +197,47 @@ class UptimeKumaServer { /** * Send list of monitors to client * @param {Socket} socket Socket to send list on + * @param {string} op list, add, update, delete + * @param {number} monitorID update or deleted monitor id * @returns {Promise} List of monitors */ - async sendMonitorList(socket) { - let list = await this.getMonitorJSONList(socket.userID); - this.io.to(socket.userID).emit("monitorList", list); - return list; + async sendMonitorList(socket, op = OPERATIONS.LIST, monitorID = null) { + let result = {}; + let list = {}; + + if (op !== OPERATIONS.DELETE) { + list = await this.getMonitorJSONList(socket.userID, monitorID); + } + + result["op"] = op; + result["monitorID"] = monitorID; + result["list"] = list; + this.io.to(socket.userID).emit("monitorList", result); + return result; } /** * Get a list of monitors for the given user. * @param {string} userID - The ID of the user to get monitors for. + * @param {number} monitorID - The ID of monitor for. * @returns {Promise} A promise that resolves to an object with monitor IDs as keys and monitor objects as values. * * Generated by Trelent */ - async getMonitorJSONList(userID) { + async getMonitorJSONList(userID, monitorID = null) { let result = {}; - let monitorList = await R.find("monitor", " user_id = ? ORDER BY weight DESC, name", [ - userID, - ]); + // Initialize query and parameters + let query = " user_id = ? "; + let queryParams = [ userID ]; + + // Add condition for monitorID if provided + if (monitorID) { + query += "AND id = ? "; + queryParams.push(monitorID); + } + + let monitorList = await R.find("monitor", query + "ORDER BY weight DESC, name", queryParams); // Collect monitor IDs // Create monitorData with id, active diff --git a/src/mixins/socket.js b/src/mixins/socket.js index 150e39a25..82fa14fdd 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -5,7 +5,7 @@ import Favico from "favico.js"; import dayjs from "dayjs"; import mitt from "mitt"; -import { DOWN, MAINTENANCE, PENDING, UP } from "../util.ts"; +import { DOWN, MAINTENANCE, PENDING, UP, OPERATIONS } from "../util.ts"; import { getDevContainerServerHostname, isDevContainer, getToastSuccessTimeout, getToastErrorTimeout } from "../util-frontend.js"; const toast = useToast(); @@ -141,7 +141,7 @@ export default { socket.on("monitorList", (data) => { // Add Helper function - Object.entries(data).forEach(([ monitorID, monitor ]) => { + Object.entries(data.list).forEach(([ monitorID, monitor ]) => { monitor.getUrl = () => { try { return new URL(monitor.url); @@ -150,7 +150,16 @@ export default { } }; }); - this.monitorList = data; + + if (data.op === OPERATIONS.ADD) { + this.monitorList = this.updateMonitorList(data.list); + } else if (data.op === OPERATIONS.UPDATE) { + this.monitorList = this.updateMonitorList(data.list); + } else if (data.op === OPERATIONS.DELETE) { + this.monitorList = this.deleteMonitorList(data.monitorID); + } else { + this.monitorList = data.list; + } }); socket.on("maintenanceList", (data) => { @@ -285,6 +294,30 @@ export default { }); }, + /** + * update into existing list + * @param {object} list add, updated, pause & resume list + * @returns {object} list + */ + updateMonitorList(list) { + Object.entries(list).forEach(([ monitorID, updatedMonitor ]) => { + this.monitorList[monitorID] = updatedMonitor; + }); + return this.monitorList; + }, + + /** + * delete from existing list + * @param {number} monitorID deleted monitorID + * @returns {object} list + */ + deleteMonitorList(monitorID) { + if (this.monitorList[monitorID]) { + delete this.monitorList[monitorID]; + } + return this.monitorList; + }, + /** * The storage currently in use * @returns {Storage} Current storage diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index fd2f3c72f..51c156dfe 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -1636,7 +1636,6 @@ message HealthCheckResponse { await this.startParentGroupMonitor(); } this.processing = false; - this.$root.getMonitorList(); this.$router.push("/dashboard/" + res.monitorID); } else { this.processing = false; diff --git a/src/util.js b/src/util.js index c88d04968..e67d86a59 100644 --- a/src/util.js +++ b/src/util.js @@ -66,6 +66,12 @@ exports.CONSOLE_STYLE_BgMagenta = "\x1b[45m"; exports.CONSOLE_STYLE_BgCyan = "\x1b[46m"; exports.CONSOLE_STYLE_BgWhite = "\x1b[47m"; exports.CONSOLE_STYLE_BgGray = "\x1b[100m"; +exports.OPERATIONS = { + LIST: "list", + ADD: "add", + UPDATE: "update", + DELETE: "delete", +}; const consoleModuleColors = [ exports.CONSOLE_STYLE_FgCyan, exports.CONSOLE_STYLE_FgGreen, diff --git a/src/util.ts b/src/util.ts index efc8f3a89..560be921f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -75,6 +75,13 @@ export const CONSOLE_STYLE_BgCyan = "\x1b[46m"; export const CONSOLE_STYLE_BgWhite = "\x1b[47m"; export const CONSOLE_STYLE_BgGray = "\x1b[100m"; +export const OPERATIONS = { + LIST: "list", + ADD: "add", + UPDATE: "update", + DELETE: "delete", +}; + const consoleModuleColors = [ CONSOLE_STYLE_FgCyan, CONSOLE_STYLE_FgGreen,