Add status page summary API

pull/1927/head
Gero Gerke 2 years ago
parent 9a8b484ee8
commit 3ac752246d

@ -31,7 +31,7 @@ class Monitor extends BeanModel {
* Only show necessary data to public * Only show necessary data to public
* @returns {Object} * @returns {Object}
*/ */
async toPublicJSON(showTags = false) { async toPublicJSON(showTags = false, includeStatus = false) {
let obj = { let obj = {
id: this.id, id: this.id,
name: this.name, name: this.name,
@ -45,6 +45,12 @@ class Monitor extends BeanModel {
if (showTags) { if (showTags) {
obj.tags = await this.getTags(); obj.tags = await this.getTags();
} }
if (includeStatus) {
const heartbeat = await Monitor.getPreviousHeartbeat(this.id);
obj.status = heartbeat.status === 1 ? "up" : "down";
}
return obj; return obj;
} }

@ -72,8 +72,9 @@ class StatusPage extends BeanModel {
/** /**
* Get all status page data in one call * Get all status page data in one call
* @param {StatusPage} statusPage * @param {StatusPage} statusPage
* @param {boolean} includeStatus whether each monitor should include the status of the monitor ("up" or "down")
*/ */
static async getStatusPageData(statusPage) { static async getStatusPageData(statusPage, includeStatus = false) {
// Incident // Incident
let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [ let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [
statusPage.id, statusPage.id,
@ -92,7 +93,7 @@ class StatusPage extends BeanModel {
]); ]);
for (let groupBean of list) { for (let groupBean of list) {
let monitorGroup = await groupBean.toPublicJSON(showTags); let monitorGroup = await groupBean.toPublicJSON(showTags, includeStatus);
publicGroupList.push(monitorGroup); publicGroupList.push(monitorGroup);
} }

@ -2,7 +2,7 @@ let express = require("express");
const apicache = require("../modules/apicache"); const apicache = require("../modules/apicache");
const { UptimeKumaServer } = require("../uptime-kuma-server"); const { UptimeKumaServer } = require("../uptime-kuma-server");
const StatusPage = require("../model/status_page"); const StatusPage = require("../model/status_page");
const { allowDevAllOrigin, send403 } = require("../util-server"); const { allowAllOrigin, allowDevAllOrigin, send403 } = require("../util-server");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const Monitor = require("../model/monitor"); const Monitor = require("../model/monitor");
@ -26,6 +26,39 @@ router.get("/status-page", cache("5 minutes"), async (request, response) => {
await StatusPage.handleStatusPageResponse(response, server.indexHTML, slug); await StatusPage.handleStatusPageResponse(response, server.indexHTML, slug);
}); });
// Status page config, incident, monitor list with status ("up" or "down")
router.get("/api/status-page/:slug/summary", cache("5 minutes"), async (request, response) => {
allowAllOrigin(response);
let slug = request.params.slug;
try {
// Get Status Page
let statusPage = await R.findOne("status_page", " slug = ? ", [
slug
]);
if (!statusPage) {
return null;
}
let statusPageData = await StatusPage.getStatusPageData(statusPage, true);
if (!statusPageData) {
response.statusCode = 404;
response.json({
msg: "Not Found"
});
return;
}
// Response
response.json(statusPageData);
} catch (error) {
send403(response, error.message);
}
});
// Status page config, incident, monitor list // Status page config, incident, monitor list
router.get("/api/status-page/:slug", cache("5 minutes"), async (request, response) => { router.get("/api/status-page/:slug", cache("5 minutes"), async (request, response) => {
allowDevAllOrigin(response); allowDevAllOrigin(response);

Loading…
Cancel
Save