performance improve while insert/update/delete

This commit is contained in:
vishalsabhaya 2024-08-19 11:36:23 +09:00
parent 32f2fe140d
commit ae7f01b3ea
6 changed files with 85 additions and 21 deletions

View File

@ -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([

View File

@ -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<object>} 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<object>} 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

View File

@ -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

View File

@ -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;

View File

@ -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,

View File

@ -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,