mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-27 16:54:04 +00:00
perf: less sql statements and concurrent start/stop
This commit is contained in:
parent
6179f31348
commit
f622898ff1
2 changed files with 33 additions and 90 deletions
|
@ -1661,25 +1661,24 @@ class Monitor extends BeanModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets recursive all child ids
|
* Gets recursive all children ids
|
||||||
* @param {number} monitorID ID of the monitor to get
|
* @param {number} monitorID ID of the monitor to get
|
||||||
* @returns {Promise<Array>} IDs of all children
|
* @returns {Promise<number[]>} IDs of all children
|
||||||
*/
|
*/
|
||||||
static async getAllChildrenIDs(monitorID) {
|
static async getAllChildrenIDs(monitorID) {
|
||||||
const childs = await Monitor.getChildren(monitorID);
|
const children = await R.getAll(`
|
||||||
|
WITH RECURSIVE MonitorHierarchy(id) AS (
|
||||||
|
SELECT id FROM monitor WHERE id = ?
|
||||||
|
UNION ALL
|
||||||
|
SELECT m.id FROM monitor m INNER JOIN MonitorHierarchy mh ON m.parent = mh.id
|
||||||
|
)
|
||||||
|
SELECT id FROM MonitorHierarchy WHERE id != ?;
|
||||||
|
`, [
|
||||||
|
monitorID,
|
||||||
|
monitorID
|
||||||
|
]);
|
||||||
|
|
||||||
if (childs === null) {
|
return children.map((child) => child.id);
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
let childrenIDs = [];
|
|
||||||
|
|
||||||
for (const child of childs) {
|
|
||||||
childrenIDs.push(child.id);
|
|
||||||
childrenIDs = childrenIDs.concat(await Monitor.getAllChildrenIDs(child.id));
|
|
||||||
}
|
|
||||||
|
|
||||||
return childrenIDs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1752,51 +1752,20 @@ async function startMonitor(userID, monitorID) {
|
||||||
userID,
|
userID,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let monitor = await R.findOne("monitor", " id = ? ", [
|
const childrenIDs = await Monitor.getAllChildrenIDs(monitorID);
|
||||||
monitorID,
|
console.log(childrenIDs);
|
||||||
]);
|
const monitorIDs = [ monitorID, ...childrenIDs ];
|
||||||
|
|
||||||
|
let monitors = await R.find("monitor", ` id IN (${monitorIDs.map((_) => "?").join(",")})`, monitorIDs);
|
||||||
|
|
||||||
|
await Promise.all(monitors.map(async (monitor) => {
|
||||||
if (monitor.id in server.monitorList) {
|
if (monitor.id in server.monitorList) {
|
||||||
await server.monitorList[monitor.id].stop();
|
await server.monitorList[monitor.id].stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
server.monitorList[monitor.id] = monitor;
|
server.monitorList[monitor.id] = monitor;
|
||||||
await monitor.start(io);
|
await monitor.start(io);
|
||||||
|
}));
|
||||||
const children = await Monitor.getChildren(monitorID);
|
|
||||||
for (let child of children) {
|
|
||||||
await startMonitorAsChild(userID, child.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start the specified monitor as child
|
|
||||||
* @param {number} userID ID of user who owns monitor
|
|
||||||
* @param {number} monitorID ID of monitor to start
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async function startMonitorAsChild(userID, monitorID) {
|
|
||||||
|
|
||||||
let monitor = await R.findOne("monitor", " id = ? ", [
|
|
||||||
monitorID,
|
|
||||||
]);
|
|
||||||
if (monitor.active !== 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("manage", `Resume Monitor: ${monitorID} User ID: ${userID}`);
|
|
||||||
|
|
||||||
if (monitor.id in server.monitorList) {
|
|
||||||
await server.monitorList[monitor.id].stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
server.monitorList[monitor.id] = monitor;
|
|
||||||
await monitor.start(io);
|
|
||||||
|
|
||||||
const children = await Monitor.getChildren(monitorID);
|
|
||||||
for (let child of children) {
|
|
||||||
await startMonitorAsChild(userID, child.id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1825,41 +1794,16 @@ async function pauseMonitor(userID, monitorID) {
|
||||||
userID,
|
userID,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (monitorID in server.monitorList) {
|
const childrenIDs = await Monitor.getAllChildrenIDs(monitorID);
|
||||||
await server.monitorList[monitorID].stop();
|
const monitorIDs = [ monitorID, ...childrenIDs ];
|
||||||
|
|
||||||
|
await Promise.all(monitorIDs.map(async (currentMonitorID) => {
|
||||||
|
if (currentMonitorID in server.monitorList) {
|
||||||
|
await server.monitorList[currentMonitorID].stop();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
server.monitorList[monitorID].active = 0;
|
server.monitorList[monitorID].active = 0;
|
||||||
}
|
|
||||||
|
|
||||||
const children = await Monitor.getChildren(monitorID);
|
|
||||||
for (let child of children) {
|
|
||||||
await pauseMonitorAsChild(userID, child.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pause a given monitor as child
|
|
||||||
* @param {number} userID ID of user who owns monitor
|
|
||||||
* @param {number} monitorID ID of monitor to start
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async function pauseMonitorAsChild(userID, monitorID) {
|
|
||||||
let monitor = await R.findOne("monitor", " id = ? ", [
|
|
||||||
monitorID,
|
|
||||||
]);
|
|
||||||
if (monitor.active !== 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("manage", `Pause Monitor: ${monitorID} User ID: ${userID}`);
|
|
||||||
|
|
||||||
if (monitorID in server.monitorList) {
|
|
||||||
await server.monitorList[monitorID].stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
const children = await Monitor.getChildren(monitorID);
|
|
||||||
for (let child of children) {
|
|
||||||
await pauseMonitorAsChild(userID, child.id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue