From 3a89624d32f49c84c3f6b506f40deebf4a679f27 Mon Sep 17 00:00:00 2001
From: Frank Elsinga <frank@elsinga.de>
Date: Fri, 19 Jan 2024 00:01:15 +0100
Subject: [PATCH 1/2] extracted the group monitor ot a different monitoring
 type

---
 server/model/monitor.js       | 35 +----------------------
 server/monitor-types/group.js | 54 +++++++++++++++++++++++++++++++++++
 server/uptime-kuma-server.js  |  2 ++
 3 files changed, 57 insertions(+), 34 deletions(-)
 create mode 100644 server/monitor-types/group.js

diff --git a/server/model/monitor.js b/server/model/monitor.js
index b2fed86f5..7853ad376 100644
--- a/server/model/monitor.js
+++ b/server/model/monitor.js
@@ -382,39 +382,6 @@ class Monitor extends BeanModel {
                 if (await Monitor.isUnderMaintenance(this.id)) {
                     bean.msg = "Monitor under maintenance";
                     bean.status = MAINTENANCE;
-                } else if (this.type === "group") {
-                    const children = await Monitor.getChildren(this.id);
-
-                    if (children.length > 0) {
-                        bean.status = UP;
-                        bean.msg = "All children up and running";
-                        for (const child of children) {
-                            if (!child.active) {
-                                // Ignore inactive childs
-                                continue;
-                            }
-                            const lastBeat = await Monitor.getPreviousHeartbeat(child.id);
-
-                            // Only change state if the monitor is in worse conditions then the ones before
-                            // lastBeat.status could be null
-                            if (!lastBeat) {
-                                bean.status = PENDING;
-                            } else if (bean.status === UP && (lastBeat.status === PENDING || lastBeat.status === DOWN)) {
-                                bean.status = lastBeat.status;
-                            } else if (bean.status === PENDING && lastBeat.status === DOWN) {
-                                bean.status = lastBeat.status;
-                            }
-                        }
-
-                        if (bean.status !== UP) {
-                            bean.msg = "Child inaccessible";
-                        }
-                    } else {
-                        // Set status pending if group is empty
-                        bean.status = PENDING;
-                        bean.msg = "Group empty";
-                    }
-
                 } else if (this.type === "http" || this.type === "keyword" || this.type === "json-query") {
                     // Do not do any queries/high loading things before the "bean.ping"
                     let startTime = dayjs().valueOf();
@@ -1515,7 +1482,7 @@ class Monitor extends BeanModel {
     /**
      * Gets all Children of the monitor
      * @param {number} monitorID ID of monitor to get
-     * @returns {Promise<LooseObject<any>>} Children
+     * @returns {Promise<LooseObject<any>[]>} Children
      */
     static async getChildren(monitorID) {
         return await R.getAll(`
diff --git a/server/monitor-types/group.js b/server/monitor-types/group.js
new file mode 100644
index 000000000..2fabcb274
--- /dev/null
+++ b/server/monitor-types/group.js
@@ -0,0 +1,54 @@
+const {
+    UP,
+    PENDING,
+    DOWN,
+} = require("../../src/util");
+const { MonitorType } = require("./monitor-type");
+const Monitor = require("../model/monitor");
+
+class GroupMonitorType extends MonitorType {
+
+    name = "dns";
+
+    /**
+     * @inheritdoc
+     */
+    async check(monitor, heartbeat, _server) {
+        const children = await Monitor.getChildren(monitor.id);
+
+        if (children.length > 0) {
+            heartbeat.status = UP;
+            heartbeat.msg = "All children up and running";
+            for (const child of children) {
+                if (!child.active) {
+                    // Ignore inactive childs
+                    continue;
+                }
+                const lastBeat = await Monitor.getPreviousHeartbeat(child.id);
+
+                // Only change state if the monitor is in worse conditions then the ones before
+                // lastBeat.status could be null
+                if (!lastBeat) {
+                    heartbeat.status = PENDING;
+                } else if (heartbeat.status === UP && (lastBeat.status === PENDING || lastBeat.status === DOWN)) {
+                    heartbeat.status = lastBeat.status;
+                } else if (heartbeat.status === PENDING && lastBeat.status === DOWN) {
+                    heartbeat.status = lastBeat.status;
+                }
+            }
+
+            if (heartbeat.status !== UP) {
+                heartbeat.msg = "Child inaccessible";
+            }
+        } else {
+            // Set status pending if group is empty
+            heartbeat.status = PENDING;
+            heartbeat.msg = "Group empty";
+        }
+    }
+}
+
+module.exports = {
+    GroupMonitorType,
+};
+
diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js
index 2eec9c7c1..80091d55e 100644
--- a/server/uptime-kuma-server.js
+++ b/server/uptime-kuma-server.js
@@ -113,6 +113,7 @@ class UptimeKumaServer {
         UptimeKumaServer.monitorTypeList["tailscale-ping"] = new TailscalePing();
         UptimeKumaServer.monitorTypeList["dns"] = new DnsMonitorType();
         UptimeKumaServer.monitorTypeList["mqtt"] = new MqttMonitorType();
+        UptimeKumaServer.monitorTypeList["group"] = new GroupMonitorType();
 
         // Allow all CORS origins (polling) in development
         let cors = undefined;
@@ -516,3 +517,4 @@ const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor
 const { TailscalePing } = require("./monitor-types/tailscale-ping");
 const { DnsMonitorType } = require("./monitor-types/dns");
 const { MqttMonitorType } = require("./monitor-types/mqtt");
+const { GroupMonitorType } = require("./monitor-types/group");

From 48abd7900583ed2ccd0dc37af75d3f0ce62daa79 Mon Sep 17 00:00:00 2001
From: Frank Elsinga <frank@elsinga.de>
Date: Fri, 19 Jan 2024 00:16:21 +0100
Subject: [PATCH 2/2] formatting fixes

---
 server/monitor-types/group.js | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/server/monitor-types/group.js b/server/monitor-types/group.js
index 2fabcb274..28d0443d4 100644
--- a/server/monitor-types/group.js
+++ b/server/monitor-types/group.js
@@ -1,14 +1,9 @@
-const {
-    UP,
-    PENDING,
-    DOWN,
-} = require("../../src/util");
+const { UP, PENDING, DOWN } = require("../../src/util");
 const { MonitorType } = require("./monitor-type");
 const Monitor = require("../model/monitor");
 
 class GroupMonitorType extends MonitorType {
-
-    name = "dns";
+    name = "group";
 
     /**
      * @inheritdoc