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

Code before:
        const requestedDuration = Math.min(request.params.duration ? parseInt(request.params.duration, 10) : 24, 720);
        const overrideValue = value && parseFloat(value);

        const publicAvgPing = parseInt(await R.getCell(`
                SELECT AVG(ping) FROM monitor_group, \`group\`, heartbeat
                WHERE monitor_group.group_id = \`group\`.id
                AND heartbeat.time > DATETIME('now', ? || ' hours')
                AND heartbeat.ping IS NOT NULL
                AND public = 1
                AND heartbeat.monitor_id = ?
            `,
        [-requestedDuration, requestedMonitorId]
        ));

        const badgeValues = { style };

        if (!publicAvgPing) {
            // return a "N/A" badge in naColor (grey), if monitor is not public / not available / non exsitant

            badgeValues.message = "N/A";
            badgeValues.color = badgeConstants.naColor;
        } else {
            const avgPing = parseInt(overrideValue ?? publicAvgPing);

            badgeValues.color = color;
            // use a given, custom labelColor or use the default badge label color ( defined by badge-maker)
            badgeValues.labelColor = labelColor ?? "";
            // build a lable string. If a custom label is given, override the default one ( requestedDuration )
            badgeValues.label = filterAndJoin([labelPrefix, label ?? requestedDuration, labelSuffix]);
            badgeValues.message = filterAndJoin([prefix, avgPing, suffix]);
        }

        // build the SVG based on given values
        const svg = makeBadge(badgeValues);

        response.type("image/svg+xml");
        response.send(svg);
    } catch (error) {
        send403(response, error.message);
    }
});

async function checkPublished() {
    if (! await isPublished()) {
        throw new Error("The status page is not published");
    }
}

/**
 * Default is published
 * @returns {Promise<boolean>}

Review comment (suggestion):
```suggestion
            // use a given, custom labelColor or use the default badge label color (defined by badge-maker)
```

Code after:
            const avgPing = parseInt(overrideValue ?? publicAvgPing);

            badgeValues.color = color;
            // use a given, custom labelColor or use the default badge label color (defined by badge-maker)
            badgeValues.labelColor = labelColor ?? "";
            // build a lable string. If a custom label is given, override the default one (requestedDuration)
            badgeValues.label = filterAndJoin([ labelPrefix, label ?? requestedDuration, labelSuffix ]);
            badgeValues.message = filterAndJoin([ prefix, avgPing, suffix ]);
        }

        // build the SVG based on given values
        const svg = makeBadge(badgeValues);

        response.type("image/svg+xml");
        response.send(svg);
    } catch (error) {
        send403(response, error.message);
    }
});

/**
 * Send a 403 response
 * @param {Object} res Express response object
 * @param {string} [msg=""] Message to send
 */
function send403(res, msg = "") {
    res.status(403).json({
        "status": "fail",
        "msg": msg,
    });
}

module.exports = router;