optimisation for creation, update, list and delete

This commit is contained in:
vishalsabhaya 2024-09-05 17:16:26 +09:00
parent 043f1a2ed9
commit cb1b414365
5 changed files with 59 additions and 55 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, OPERATIONS } = require("../src/util");
const { sleep, log, getRandomInt, genSecret, isDev } = 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, OPERATIONS.ADD, bean.id);
await server.sendUpdateMonitorIntoList(socket, 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, OPERATIONS.UPDATE, bean.id);
await server.sendUpdateMonitorIntoList(socket, bean.id);
callback({
ok: true,
@ -951,7 +951,7 @@ let needSetup = false;
try {
checkLogin(socket);
await startMonitor(socket.userID, monitorID);
await server.sendMonitorList(socket, OPERATIONS.UPDATE, monitorID);
await server.sendUpdateMonitorIntoList(socket, monitorID);
callback({
ok: true,
@ -971,7 +971,7 @@ let needSetup = false;
try {
checkLogin(socket);
await pauseMonitor(socket.userID, monitorID);
await server.sendMonitorList(socket, OPERATIONS.UPDATE, monitorID);
await server.sendUpdateMonitorIntoList(socket, monitorID);
callback({
ok: true,
@ -1017,7 +1017,7 @@ let needSetup = false;
msg: "successDeleted",
msgi18n: true,
});
await server.sendMonitorList(socket, OPERATIONS.DELETE, monitorID);
await server.sendDeleteMonitorFromList(socket, monitorID);
} catch (e) {
callback({
@ -1647,7 +1647,7 @@ async function afterLogin(socket, user) {
await StatusPage.sendStatusPageList(io, socket);
const monitorPromises = [];
for (let monitorID in monitorList.list) {
for (let monitorID in monitorList) {
monitorPromises.push(sendHeartbeatList(socket, monitorID));
monitorPromises.push(Monitor.sendStats(io, monitorID, user.id));
}

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, OPERATIONS } = require("../src/util");
const { log, isDev } = require("../src/util");
const Database = require("./database");
const util = require("util");
const { Settings } = require("./settings");
@ -197,23 +197,33 @@ class UptimeKumaServer {
/**
* Send list of monitors to client
* @param {Socket} socket Socket to send list on
* @param {string} op list, add, update, delete
* @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;
}
/**
* Update Monitor into list
* @param {Socket} socket Socket to send list on
* @param {number} monitorID update or deleted monitor id
* @returns {Promise<object>} List of monitors
*/
async sendMonitorList(socket, op = OPERATIONS.LIST, monitorID = null) {
let result = {};
let list = {};
async sendUpdateMonitorIntoList(socket, monitorID) {
let list = await this.getMonitorJSONList(socket.userID, monitorID);
this.io.to(socket.userID).emit("updateMonitorIntoList", 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;
/**
* Delete Monitor from list
* @param {Socket} socket Socket to send list on
* @param {number} monitorID update or deleted monitor id
*/
async sendDeleteMonitorFromList(socket, monitorID) {
this.io.to(socket.userID).emit("deleteMonitorFromList", monitorID);
}
/**

View File

@ -5,7 +5,7 @@ import Favico from "favico.js";
import dayjs from "dayjs";
import mitt from "mitt";
import { DOWN, MAINTENANCE, PENDING, UP, OPERATIONS } from "../util.ts";
import { DOWN, MAINTENANCE, PENDING, UP } from "../util.ts";
import { getDevContainerServerHostname, isDevContainer, getToastSuccessTimeout, getToastErrorTimeout } from "../util-frontend.js";
const toast = useToast();
@ -140,26 +140,18 @@ export default {
});
socket.on("monitorList", (data) => {
// Add Helper function
Object.entries(data.list).forEach(([ monitorID, monitor ]) => {
monitor.getUrl = () => {
try {
return new URL(monitor.url);
} catch (_) {
return null;
}
};
});
this.assignMonitorUrlParser(data);
console.log(data);
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("updateMonitorIntoList", (data) => {
this.assignMonitorUrlParser(data);
this.monitorList = this.updateMonitorList(data);
});
socket.on("deleteMonitorFromList", (monitorID) => {
this.monitorList = this.deleteMonitorList(monitorID);
});
socket.on("maintenanceList", (data) => {
@ -293,7 +285,21 @@ export default {
location.reload();
});
},
/**
* parse all urls from list.
* @param {object} data Monitor data to modify
*/
assignMonitorUrlParser(data) {
Object.entries(data).forEach(([monitorID, monitor]) => {
monitor.getUrl = () => {
try {
return new URL(monitor.url);
} catch (_) {
return null;
}
};
});
},
/**
* update into existing list
* @param {object} list add, updated, pause & resume list

View File

@ -66,12 +66,7 @@ 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,13 +75,6 @@ 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,