From 71c7ee69c70b4aec4e72fde84b102fda321f0ee4 Mon Sep 17 00:00:00 2001 From: Peace Date: Thu, 17 Oct 2024 00:04:45 +0200 Subject: [PATCH] feat: db statement to get all active monitors --- server/model/monitor.js | 18 ++++++++++++++++++ server/server.js | 3 +-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 741284f1e..7f9185be2 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -1608,6 +1608,24 @@ class Monitor extends BeanModel { }; } + /** + * Gets all active monitors + * @returns {Promise} Active Monitors + */ + static async getAllActiveMonitors() { + return R.convertToBeans("monitor", await R.getAll(` + WITH RECURSIVE MonitorHierarchy AS ( + SELECT * FROM monitor + WHERE parent IS NULL AND active = 1 + UNION ALL + SELECT m.* FROM monitor m + INNER JOIN MonitorHierarchy mh ON m.parent = mh.id + WHERE m.active = 1 + ) + SELECT * FROM MonitorHierarchy; + `)); + } + /** * Gets Parent of the monitor * @param {number} monitorID ID of monitor to get diff --git a/server/server.js b/server/server.js index 782c47f39..8dba73a1e 100644 --- a/server/server.js +++ b/server/server.js @@ -1810,8 +1810,7 @@ async function pauseMonitor(userID, monitorID) { * @returns {Promise} */ async function startMonitors() { - let list = await R.find("monitor", " active = 1 "); - list = list.filter((monitor) => Monitor.isActive(monitor.id, monitor.active)); + let list = await Monitor.getAllActiveMonitors(); for (let monitor of list) { server.monitorList[monitor.id] = monitor;