mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-23 14:54:05 +00:00
perf: less recursion for isUnderMaintenance using db query
This commit is contained in:
parent
16c04f6ac2
commit
c9e5dff162
1 changed files with 25 additions and 7 deletions
|
@ -1469,10 +1469,12 @@ class Monitor extends BeanModel {
|
|||
* @returns {Promise<boolean>} Is the monitor under maintenance
|
||||
*/
|
||||
static async isUnderMaintenance(monitorID) {
|
||||
const ancestorIDs = await Monitor.getAllAncestorIDs(monitorID);
|
||||
const allIDs = [ monitorID, ...ancestorIDs ];
|
||||
const maintenanceIDList = await R.getCol(`
|
||||
SELECT maintenance_id FROM monitor_maintenance
|
||||
WHERE monitor_id = ?
|
||||
`, [ monitorID ]);
|
||||
WHERE monitor_id IN (${allIDs.map((_) => "?").join(",")})
|
||||
`, allIDs);
|
||||
|
||||
for (const maintenanceID of maintenanceIDList) {
|
||||
const maintenance = await UptimeKumaServer.getInstance().getMaintenance(maintenanceID);
|
||||
|
@ -1481,11 +1483,6 @@ class Monitor extends BeanModel {
|
|||
}
|
||||
}
|
||||
|
||||
const parent = await Monitor.getParent(monitorID);
|
||||
if (parent != null) {
|
||||
return await Monitor.isUnderMaintenance(parent.id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1683,6 +1680,27 @@ class Monitor extends BeanModel {
|
|||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets recursive all ancestor ids
|
||||
* @param {number} monitorID ID of the monitor to get
|
||||
* @returns {Promise<number[]>} IDs of all ancestors
|
||||
*/
|
||||
static async getAllAncestorIDs(monitorID) {
|
||||
return await R.getCol(`
|
||||
WITH RECURSIVE Ancestors AS (
|
||||
SELECT parent FROM monitor
|
||||
WHERE id = ?
|
||||
UNION ALL
|
||||
SELECT m.parent FROM monitor m
|
||||
JOIN Ancestors a ON m.id = a.parent
|
||||
)
|
||||
SELECT parent AS ancestor_id FROM Ancestors
|
||||
WHERE parent IS NOT NULL;
|
||||
`, [
|
||||
monitorID,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets recursive all children ids
|
||||
* @param {number} monitorID ID of the monitor to get
|
||||
|
|
Loading…
Reference in a new issue