mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-20 02:25:56 +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
|
* @returns {Promise<boolean>} Is the monitor under maintenance
|
||||||
*/
|
*/
|
||||||
static async isUnderMaintenance(monitorID) {
|
static async isUnderMaintenance(monitorID) {
|
||||||
|
const ancestorIDs = await Monitor.getAllAncestorIDs(monitorID);
|
||||||
|
const allIDs = [ monitorID, ...ancestorIDs ];
|
||||||
const maintenanceIDList = await R.getCol(`
|
const maintenanceIDList = await R.getCol(`
|
||||||
SELECT maintenance_id FROM monitor_maintenance
|
SELECT maintenance_id FROM monitor_maintenance
|
||||||
WHERE monitor_id = ?
|
WHERE monitor_id IN (${allIDs.map((_) => "?").join(",")})
|
||||||
`, [ monitorID ]);
|
`, allIDs);
|
||||||
|
|
||||||
for (const maintenanceID of maintenanceIDList) {
|
for (const maintenanceID of maintenanceIDList) {
|
||||||
const maintenance = await UptimeKumaServer.getInstance().getMaintenance(maintenanceID);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1683,6 +1680,27 @@ class Monitor extends BeanModel {
|
||||||
return path;
|
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
|
* Gets recursive all children ids
|
||||||
* @param {number} monitorID ID of the monitor to get
|
* @param {number} monitorID ID of the monitor to get
|
||||||
|
|
Loading…
Add table
Reference in a new issue