diff --git a/server/model/monitor.js b/server/model/monitor.js index 593f7436b..f38259fb2 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -1469,10 +1469,12 @@ class Monitor extends BeanModel { * @returns {Promise} 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} 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