perf: option to only get ids of active children

This commit is contained in:
Peace 2024-10-17 17:16:08 +02:00
parent 0a479ecb50
commit ec2cebc5df
No known key found for this signature in database
GPG key ID: 0EF6B46E172B739F
2 changed files with 22 additions and 2 deletions

View file

@ -1701,9 +1701,29 @@ class Monitor extends BeanModel {
/**
* Gets recursive all children ids
* @param {number} monitorID ID of the monitor to get
* @param {boolean} onlyActive Return only monitors which are active (including all ancestors)
* @returns {Promise<number[]>} IDs of all children
*/
static async getAllChildrenIDs(monitorID) {
static async getAllChildrenIDs(monitorID, onlyActive = false) {
if (onlyActive) {
// Gets all children monitor ids recursive but only if they and all their ancestors are active
return await R.getCol(`
WITH RECURSIVE MonitorHierarchy(id, active_chain) AS (
SELECT id, active FROM monitor
WHERE id = ?
UNION ALL
SELECT m.id, m.active * mh.active_chain FROM monitor m
INNER JOIN MonitorHierarchy mh ON m.parent = mh.id
WHERE mh.active_chain = 1
)
SELECT id FROM MonitorHierarchy
WHERE id != ? AND active_chain = 1;
`, [
monitorID,
monitorID
]);
}
// Gets all children monitor ids recursive
return await R.getCol(`
WITH RECURSIVE MonitorHierarchy(id) AS (
SELECT id FROM monitor WHERE id = ?

View file

@ -1752,7 +1752,7 @@ async function startMonitor(userID, monitorID) {
userID,
]);
const childrenIDs = await Monitor.getAllChildrenIDs(monitorID);
const childrenIDs = await Monitor.getAllChildrenIDs(monitorID, true);
const monitorIDs = [ monitorID, ...childrenIDs ];
let monitors = await R.find("monitor", ` id IN (${monitorIDs.map((_) => "?").join(",")})`, monitorIDs);