perf: less recursion for isUnderMaintenance using db query

This commit is contained in:
Peace 2024-10-17 00:07:07 +02:00
parent 16c04f6ac2
commit c9e5dff162
No known key found for this signature in database
GPG key ID: 0EF6B46E172B739F

View file

@ -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