mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-02 01:25:24 -05:00
performance improve while insert/update/delete
This commit is contained in:
parent
32f2fe140d
commit
ae7f01b3ea
@ -37,7 +37,7 @@ if (!semver.satisfies(nodeVersion, requiredNodeVersions)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const args = require("args-parser")(process.argv);
|
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");
|
const config = require("./config");
|
||||||
|
|
||||||
log.debug("server", "Arguments");
|
log.debug("server", "Arguments");
|
||||||
@ -695,7 +695,7 @@ let needSetup = false;
|
|||||||
|
|
||||||
await updateMonitorNotification(bean.id, notificationIDList);
|
await updateMonitorNotification(bean.id, notificationIDList);
|
||||||
|
|
||||||
await server.sendMonitorList(socket);
|
await server.sendMonitorList(socket, OPERATIONS.ADD, bean.id);
|
||||||
|
|
||||||
if (monitor.active !== false) {
|
if (monitor.active !== false) {
|
||||||
await startMonitor(socket.userID, bean.id);
|
await startMonitor(socket.userID, bean.id);
|
||||||
@ -850,7 +850,7 @@ let needSetup = false;
|
|||||||
await restartMonitor(socket.userID, bean.id);
|
await restartMonitor(socket.userID, bean.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
await server.sendMonitorList(socket);
|
await server.sendMonitorList(socket, OPERATIONS.UPDATE, bean.id);
|
||||||
|
|
||||||
callback({
|
callback({
|
||||||
ok: true,
|
ok: true,
|
||||||
@ -951,7 +951,7 @@ let needSetup = false;
|
|||||||
try {
|
try {
|
||||||
checkLogin(socket);
|
checkLogin(socket);
|
||||||
await startMonitor(socket.userID, monitorID);
|
await startMonitor(socket.userID, monitorID);
|
||||||
await server.sendMonitorList(socket);
|
await server.sendMonitorList(socket, OPERATIONS.UPDATE, monitorID);
|
||||||
|
|
||||||
callback({
|
callback({
|
||||||
ok: true,
|
ok: true,
|
||||||
@ -971,7 +971,7 @@ let needSetup = false;
|
|||||||
try {
|
try {
|
||||||
checkLogin(socket);
|
checkLogin(socket);
|
||||||
await pauseMonitor(socket.userID, monitorID);
|
await pauseMonitor(socket.userID, monitorID);
|
||||||
await server.sendMonitorList(socket);
|
await server.sendMonitorList(socket, OPERATIONS.UPDATE, monitorID);
|
||||||
|
|
||||||
callback({
|
callback({
|
||||||
ok: true,
|
ok: true,
|
||||||
@ -1017,8 +1017,7 @@ let needSetup = false;
|
|||||||
msg: "successDeleted",
|
msg: "successDeleted",
|
||||||
msgi18n: true,
|
msgi18n: true,
|
||||||
});
|
});
|
||||||
|
await server.sendMonitorList(socket, OPERATIONS.DELETE, monitorID);
|
||||||
await server.sendMonitorList(socket);
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
callback({
|
callback({
|
||||||
@ -1649,7 +1648,7 @@ async function afterLogin(socket, user) {
|
|||||||
|
|
||||||
// Create an array to store the combined promises for both sendHeartbeatList and sendStats
|
// Create an array to store the combined promises for both sendHeartbeatList and sendStats
|
||||||
const monitorPromises = [];
|
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
|
// Combine both sendHeartbeatList and sendStats for each monitor into a single Promise
|
||||||
monitorPromises.push(
|
monitorPromises.push(
|
||||||
Promise.all([
|
Promise.all([
|
||||||
|
@ -4,7 +4,7 @@ const fs = require("fs");
|
|||||||
const http = require("http");
|
const http = require("http");
|
||||||
const { Server } = require("socket.io");
|
const { Server } = require("socket.io");
|
||||||
const { R } = require("redbean-node");
|
const { R } = require("redbean-node");
|
||||||
const { log, isDev } = require("../src/util");
|
const { log, isDev, OPERATIONS } = require("../src/util");
|
||||||
const Database = require("./database");
|
const Database = require("./database");
|
||||||
const util = require("util");
|
const util = require("util");
|
||||||
const { Settings } = require("./settings");
|
const { Settings } = require("./settings");
|
||||||
@ -197,27 +197,47 @@ class UptimeKumaServer {
|
|||||||
/**
|
/**
|
||||||
* Send list of monitors to client
|
* Send list of monitors to client
|
||||||
* @param {Socket} socket Socket to send list on
|
* @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
|
* @returns {Promise<object>} List of monitors
|
||||||
*/
|
*/
|
||||||
async sendMonitorList(socket) {
|
async sendMonitorList(socket, op = OPERATIONS.LIST, monitorID = null) {
|
||||||
let list = await this.getMonitorJSONList(socket.userID);
|
let result = {};
|
||||||
this.io.to(socket.userID).emit("monitorList", list);
|
let list = {};
|
||||||
return 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.
|
* Get a list of monitors for the given user.
|
||||||
* @param {string} userID - The ID of the user to get monitors for.
|
* @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.
|
* @returns {Promise<object>} A promise that resolves to an object with monitor IDs as keys and monitor objects as values.
|
||||||
*
|
*
|
||||||
* Generated by Trelent
|
* Generated by Trelent
|
||||||
*/
|
*/
|
||||||
async getMonitorJSONList(userID) {
|
async getMonitorJSONList(userID, monitorID = null) {
|
||||||
let result = {};
|
let result = {};
|
||||||
|
|
||||||
let monitorList = await R.find("monitor", " user_id = ? ORDER BY weight DESC, name", [
|
// Initialize query and parameters
|
||||||
userID,
|
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
|
// Collect monitor IDs
|
||||||
// Create monitorData with id, active
|
// Create monitorData with id, active
|
||||||
|
@ -5,7 +5,7 @@ import Favico from "favico.js";
|
|||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import mitt from "mitt";
|
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";
|
import { getDevContainerServerHostname, isDevContainer, getToastSuccessTimeout, getToastErrorTimeout } from "../util-frontend.js";
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
|
|
||||||
@ -141,7 +141,7 @@ export default {
|
|||||||
|
|
||||||
socket.on("monitorList", (data) => {
|
socket.on("monitorList", (data) => {
|
||||||
// Add Helper function
|
// Add Helper function
|
||||||
Object.entries(data).forEach(([ monitorID, monitor ]) => {
|
Object.entries(data.list).forEach(([ monitorID, monitor ]) => {
|
||||||
monitor.getUrl = () => {
|
monitor.getUrl = () => {
|
||||||
try {
|
try {
|
||||||
return new URL(monitor.url);
|
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) => {
|
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
|
* The storage currently in use
|
||||||
* @returns {Storage} Current storage
|
* @returns {Storage} Current storage
|
||||||
|
@ -1636,7 +1636,6 @@ message HealthCheckResponse {
|
|||||||
await this.startParentGroupMonitor();
|
await this.startParentGroupMonitor();
|
||||||
}
|
}
|
||||||
this.processing = false;
|
this.processing = false;
|
||||||
this.$root.getMonitorList();
|
|
||||||
this.$router.push("/dashboard/" + res.monitorID);
|
this.$router.push("/dashboard/" + res.monitorID);
|
||||||
} else {
|
} else {
|
||||||
this.processing = false;
|
this.processing = false;
|
||||||
|
@ -66,6 +66,12 @@ exports.CONSOLE_STYLE_BgMagenta = "\x1b[45m";
|
|||||||
exports.CONSOLE_STYLE_BgCyan = "\x1b[46m";
|
exports.CONSOLE_STYLE_BgCyan = "\x1b[46m";
|
||||||
exports.CONSOLE_STYLE_BgWhite = "\x1b[47m";
|
exports.CONSOLE_STYLE_BgWhite = "\x1b[47m";
|
||||||
exports.CONSOLE_STYLE_BgGray = "\x1b[100m";
|
exports.CONSOLE_STYLE_BgGray = "\x1b[100m";
|
||||||
|
exports.OPERATIONS = {
|
||||||
|
LIST: "list",
|
||||||
|
ADD: "add",
|
||||||
|
UPDATE: "update",
|
||||||
|
DELETE: "delete",
|
||||||
|
};
|
||||||
const consoleModuleColors = [
|
const consoleModuleColors = [
|
||||||
exports.CONSOLE_STYLE_FgCyan,
|
exports.CONSOLE_STYLE_FgCyan,
|
||||||
exports.CONSOLE_STYLE_FgGreen,
|
exports.CONSOLE_STYLE_FgGreen,
|
||||||
|
@ -75,6 +75,13 @@ export const CONSOLE_STYLE_BgCyan = "\x1b[46m";
|
|||||||
export const CONSOLE_STYLE_BgWhite = "\x1b[47m";
|
export const CONSOLE_STYLE_BgWhite = "\x1b[47m";
|
||||||
export const CONSOLE_STYLE_BgGray = "\x1b[100m";
|
export const CONSOLE_STYLE_BgGray = "\x1b[100m";
|
||||||
|
|
||||||
|
export const OPERATIONS = {
|
||||||
|
LIST: "list",
|
||||||
|
ADD: "add",
|
||||||
|
UPDATE: "update",
|
||||||
|
DELETE: "delete",
|
||||||
|
};
|
||||||
|
|
||||||
const consoleModuleColors = [
|
const consoleModuleColors = [
|
||||||
CONSOLE_STYLE_FgCyan,
|
CONSOLE_STYLE_FgCyan,
|
||||||
CONSOLE_STYLE_FgGreen,
|
CONSOLE_STYLE_FgGreen,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user