mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-02 01:25:24 -05:00
optimisation for creation, update, list and delete
This commit is contained in:
parent
043f1a2ed9
commit
cb1b414365
@ -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, OPERATIONS } = require("../src/util");
|
const { sleep, log, getRandomInt, genSecret, isDev } = 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, OPERATIONS.ADD, bean.id);
|
await server.sendUpdateMonitorIntoList(socket, 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, OPERATIONS.UPDATE, bean.id);
|
await server.sendUpdateMonitorIntoList(socket, 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, OPERATIONS.UPDATE, monitorID);
|
await server.sendUpdateMonitorIntoList(socket, 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, OPERATIONS.UPDATE, monitorID);
|
await server.sendUpdateMonitorIntoList(socket, monitorID);
|
||||||
|
|
||||||
callback({
|
callback({
|
||||||
ok: true,
|
ok: true,
|
||||||
@ -1017,7 +1017,7 @@ let needSetup = false;
|
|||||||
msg: "successDeleted",
|
msg: "successDeleted",
|
||||||
msgi18n: true,
|
msgi18n: true,
|
||||||
});
|
});
|
||||||
await server.sendMonitorList(socket, OPERATIONS.DELETE, monitorID);
|
await server.sendDeleteMonitorFromList(socket, monitorID);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
callback({
|
callback({
|
||||||
@ -1647,7 +1647,7 @@ async function afterLogin(socket, user) {
|
|||||||
await StatusPage.sendStatusPageList(io, socket);
|
await StatusPage.sendStatusPageList(io, socket);
|
||||||
|
|
||||||
const monitorPromises = [];
|
const monitorPromises = [];
|
||||||
for (let monitorID in monitorList.list) {
|
for (let monitorID in monitorList) {
|
||||||
monitorPromises.push(sendHeartbeatList(socket, monitorID));
|
monitorPromises.push(sendHeartbeatList(socket, monitorID));
|
||||||
monitorPromises.push(Monitor.sendStats(io, monitorID, user.id));
|
monitorPromises.push(Monitor.sendStats(io, monitorID, user.id));
|
||||||
}
|
}
|
||||||
|
@ -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, OPERATIONS } = require("../src/util");
|
const { log, isDev } = 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,23 +197,33 @@ 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
|
* @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
|
* @param {number} monitorID update or deleted monitor id
|
||||||
* @returns {Promise<object>} List of monitors
|
* @returns {Promise<object>} List of monitors
|
||||||
*/
|
*/
|
||||||
async sendMonitorList(socket, op = OPERATIONS.LIST, monitorID = null) {
|
async sendUpdateMonitorIntoList(socket, monitorID) {
|
||||||
let result = {};
|
let list = await this.getMonitorJSONList(socket.userID, monitorID);
|
||||||
let list = {};
|
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;
|
* Delete Monitor from list
|
||||||
result["list"] = list;
|
* @param {Socket} socket Socket to send list on
|
||||||
this.io.to(socket.userID).emit("monitorList", result);
|
* @param {number} monitorID update or deleted monitor id
|
||||||
return result;
|
*/
|
||||||
|
async sendDeleteMonitorFromList(socket, monitorID) {
|
||||||
|
this.io.to(socket.userID).emit("deleteMonitorFromList", monitorID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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, OPERATIONS } from "../util.ts";
|
import { DOWN, MAINTENANCE, PENDING, UP } 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();
|
||||||
|
|
||||||
@ -140,26 +140,18 @@ export default {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on("monitorList", (data) => {
|
socket.on("monitorList", (data) => {
|
||||||
// Add Helper function
|
this.assignMonitorUrlParser(data);
|
||||||
Object.entries(data.list).forEach(([ monitorID, monitor ]) => {
|
console.log(data);
|
||||||
monitor.getUrl = () => {
|
this.monitorList = data;
|
||||||
try {
|
|
||||||
return new URL(monitor.url);
|
|
||||||
} catch (_) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (data.op === OPERATIONS.ADD) {
|
socket.on("updateMonitorIntoList", (data) => {
|
||||||
this.monitorList = this.updateMonitorList(data.list);
|
this.assignMonitorUrlParser(data);
|
||||||
} else if (data.op === OPERATIONS.UPDATE) {
|
this.monitorList = this.updateMonitorList(data);
|
||||||
this.monitorList = this.updateMonitorList(data.list);
|
});
|
||||||
} else if (data.op === OPERATIONS.DELETE) {
|
|
||||||
this.monitorList = this.deleteMonitorList(data.monitorID);
|
socket.on("deleteMonitorFromList", (monitorID) => {
|
||||||
} else {
|
this.monitorList = this.deleteMonitorList(monitorID);
|
||||||
this.monitorList = data.list;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("maintenanceList", (data) => {
|
socket.on("maintenanceList", (data) => {
|
||||||
@ -293,7 +285,21 @@ export default {
|
|||||||
location.reload();
|
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
|
* update into existing list
|
||||||
* @param {object} list add, updated, pause & resume list
|
* @param {object} list add, updated, pause & resume list
|
||||||
|
@ -66,12 +66,7 @@ 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,13 +75,6 @@ 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