Repository: louislam/uptime-kuma
File: server/routers/api-router.js (JavaScript)

Code before:
            incident,
        });

    } catch (error) {
        send403(response, error.message);
    }
});

// Status Page - Monitor List
// Can fetch only if published
router.get("/api/status-page/monitor-list", cache("5 minutes"), async (_request, response) => {
    allowDevAllOrigin(response);

    try {
        await checkPublished();
        const publicGroupList = [];
        let list = await R.find("group", " public = 1 ORDER BY weight ");
        for (let groupBean of list) {
            let monitorGroup = await groupBean.toPublicJSON()
            if ((await getSettings("statusPage")).statusPageTags=="visible") {
                monitorGroup.monitorList = await Promise.all(monitorGroup.monitorList.map( async (monitor)=>{
                    // Includes tags as an array in response, allows for tags to be displayed on public status page
                    let tags = await R.getAll("SELECT mt.monitor_id,mt.value, tag.name, tag.color FROM monitor_tag mt JOIN tag ON mt.tag_id = tag.id WHERE mt.monitor_id = ?", [monitor.id]);
                    return {...monitor,tags: tags}
                }))
            }

            publicGroupList.push(monitorGroup);
        }

        response.json(publicGroupList);

    } catch (error) {
        send403(response, error.message);
    }
});

// Status Page Polling Data
// Can fetch only if published
router.get("/api/status-page/heartbeat", cache("5 minutes"), async (_request, response) => {
    allowDevAllOrigin(response);

    try {
        await checkPublished();

        let heartbeatList = {};
        let uptimeList = {};

        let monitorIDList = await R.getCol(`
            SELECT monitor_group.monitor_id FROM monitor_group, \`group\`
            WHERE monitor_group.group_id = \`group\`.id

Review comment (suggestion):
Logically it looks pretty sound, but is there any chance you can get this to format a little cleaner? It was a bit of a pain to read through. 

- the if clause on 150 I kept misinterpreting the parens
- the SQL could be formatted for clarity
- the callback chaining could likely do with some newlines and indenting

Also, wont this `getSettings` for every `groupBean`? I don't think that is necessary.



Code after:
            incident,
        });

    } catch (error) {
        send403(response, error.message);
    }
});

// Status Page - Monitor List
// Can fetch only if published
router.get("/api/status-page/monitor-list", cache("5 minutes"), async (_request, response) => {
    allowDevAllOrigin(response);

    try {
        await checkPublished();
        const publicGroupList = [];
        const tagsVisible = (await getSettings("statusPage")).statusPageTags;
        const list = await R.find("group", " public = 1 ORDER BY weight ");
        for (let groupBean of list) {
            let monitorGroup = await groupBean.toPublicJSON();
            if (tagsVisible) {
                monitorGroup.monitorList = await Promise.all(monitorGroup.monitorList.map(async (monitor) => {
                    // Includes tags as an array in response, allows for tags to be displayed on public status page
                    const tags = await R.getAll(
                            `SELECT monitor_tag.monitor_id, monitor_tag.value, tag.name, tag.color
                            FROM monitor_tag
                            JOIN tag
                            ON monitor_tag.tag_id = tag.id
                            WHERE monitor_tag.monitor_id = ?`, [monitor.id]
                        );
                    return {...monitor, tags: tags}
                }));
            }

            publicGroupList.push(monitorGroup);
        }

        response.json(publicGroupList);

    } catch (error) {
        send403(response, error.message);
    }
});

// Status Page Polling Data
// Can fetch only if published
router.get("/api/status-page/heartbeat", cache("5 minutes"), async (_request, response) => {
    allowDevAllOrigin(response);

    try {
        await checkPublished();