Repository: louislam/uptime-kuma
File: server/model/status_page.js (JavaScript)

Code before:
        return $.root().html();
    }

    /**
     * Get all data required for RSS
     * @param {StatusPage} statusPage Status page to get data for
     * @returns {object} Status page data
     */
    static async getRSSPageData(statusPage) {
        // get all heartbeats that correspond to this statusPage
        const config = await statusPage.toPublicJSON();

        // Public Group List
        const showTags = !!statusPage.show_tags;

        const list = await R.find("group", " public = 1 AND status_page_id = ? ORDER BY weight ", [
            statusPage.id
        ]);

        let heartbeats = [];

        for (let groupBean of list) {
            let monitorGroup = await groupBean.toPublicJSON(showTags, config?.showCertificateExpiry);
            for (const a of monitorGroup.monitorList) {
                const id = a.id;
                const heartbeat = await R.findOne("heartbeat", "monitor_id = ? ORDER BY time DESC", [ id ]);
                if (heartbeat && !heartbeat.status) {
                    heartbeats.push({ ...a, status: heartbeat.status, time: heartbeat.time });
                }
            }
        }

        return {
            heartbeats
        };
    }

    /**
     * Get all status page data in one call
     * @param {StatusPage} statusPage Status page to get data for
     * @returns {object} Status page data
     */
    static async getStatusPageData(statusPage) {
        const config = await statusPage.toPublicJSON();

        // Incident
        let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [
            statusPage.id,
        ]);

        if (incident) {

Review comment (performance):
FYI: This is fine here, because we cache the response for 5m, but usually this would be a N+1 query.
See https://github.com/louislam/uptime-kuma/pull/5025 for a realted PR.

I have not measured the performance impact of this PR on said endpoint as I currently am quite swamped with other work. If you can refactor this, that would be nice.
If you can't, so be it ^^

Code after:
        `);

        head.append(script);

        // manifest.json
        $("link[rel=manifest]").attr("href", `/api/status-page/${statusPage.slug}/manifest.json`);

        return $.root().html();
    }

    /**
     * @param {heartbeats} heartbeats from getRSSPageData
     * @returns {number} status_page constant from util.ts
     */
    static overallStatus(heartbeats) {
        if (heartbeats.length === 0) {
            return -1;
        }

        let status = STATUS_PAGE_ALL_UP;
        let hasUp = false;

        for (let beat of heartbeats) {
            if (beat.status === MAINTENANCE) {
                return STATUS_PAGE_MAINTENANCE;
            } else if (beat.status === UP) {
                hasUp = true;
            } else {
                status = STATUS_PAGE_PARTIAL_DOWN;
            }
        }

        if (! hasUp) {
            status = STATUS_PAGE_ALL_DOWN;
        }

        return status;
    }

    /**
     * @param {number} status from overallStatus
     * @returns {string} description
     */
    static getStatusDescription(status) {
        if (status === -1) {
            return "No Services";
        }

        if (status === STATUS_PAGE_ALL_UP) {
            return "All Systems Operational";
        }