Repository: louislam/uptime-kuma
File: server/monitor-types/group.js (JavaScript)

Code before:
const { MonitorType } = require("./monitor-type");
const Monitor = require("../model/monitor");

class GroupMonitorType extends MonitorType {
    name = "group";

    /**
     * @inheritdoc
     */
    async check(monitor, heartbeat, _server) {
        const children = await Monitor.getChildren(monitor.id);

        if (children.length === 0) {
            // Set status pending if group is empty
            heartbeat.status = PENDING;
            heartbeat.msg = "Group empty";
            return;
        }

        let worstStatus = UP;
        const downChildren = [];
        const pendingChildren = [];

        for (const child of children) {
            if (!child.active) {
                // Ignore inactive children
                continue;
            }

            const label = child.name || `#${child.id}`;
            const lastBeat = await Monitor.getPreviousHeartbeat(child.id);

            if (!lastBeat) {
                if (worstStatus === UP) {
                    worstStatus = PENDING;
                }
                pendingChildren.push(label);
                continue;
            }

            if (lastBeat.status === DOWN) {
                worstStatus = DOWN;
                downChildren.push(label);
            } else if (lastBeat.status === PENDING) {
                if (worstStatus !== DOWN) {
                    worstStatus = PENDING;
                }
                pendingChildren.push(label);
            }
        }


Review comment (suggestion):
Lets clarify this for future readers 😉 

```suggestion
                // Ignore inactive (=paused) children
```

Code after:
const { MonitorType } = require("./monitor-type");
const Monitor = require("../model/monitor");

class GroupMonitorType extends MonitorType {
    name = "group";

    /**
     * @inheritdoc
     */
    async check(monitor, heartbeat, _server) {
        const children = await Monitor.getChildren(monitor.id);

        if (children.length === 0) {
            // Set status pending if group is empty
            heartbeat.status = PENDING;
            heartbeat.msg = "Group empty";
            return;
        }

        let worstStatus = UP;
        const downChildren = [];
        const pendingChildren = [];

        for (const child of children) {
            if (!child.active) {
                // Ignore inactive (=paused) children
                continue;
            }

            const label = child.name || `#${child.id}`;
            const lastBeat = await Monitor.getPreviousHeartbeat(child.id);

            if (!lastBeat) {
                if (worstStatus === UP) {
                    worstStatus = PENDING;
                }
                pendingChildren.push(label);
                continue;
            }

            if (lastBeat.status === DOWN) {
                worstStatus = DOWN;
                downChildren.push(label);
            } else if (lastBeat.status === PENDING) {
                if (worstStatus !== DOWN) {
                    worstStatus = PENDING;
                }
                pendingChildren.push(label);
            }
        }