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
* @returns {Object}
*/
async toPublicJSON(showTags = false) {
async toPublicJSON(showTags = false, includeStatus = false) {
let obj = {
id: this.id,
name: this.name,
@ -45,6 +45,12 @@ class Monitor extends BeanModel {
if (showTags) {
obj.tags = await this.getTags();
}
if (includeStatus) {
const heartbeat = await Monitor.getPreviousHeartbeat(this.id);
obj.status = heartbeat.status === 1 ? "up" : "down";
}
return obj;
}

@ -72,8 +72,9 @@ class StatusPage extends BeanModel {
/**
* Get all status page data in one call
* @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
let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [
statusPage.id,
@ -92,7 +93,7 @@ class StatusPage extends BeanModel {
]);
for (let groupBean of list) {
let monitorGroup = await groupBean.toPublicJSON(showTags);
let monitorGroup = await groupBean.toPublicJSON(showTags, includeStatus);
publicGroupList.push(monitorGroup);
}

@ -2,7 +2,7 @@ let express = require("express");
const apicache = require("../modules/apicache");
const { UptimeKumaServer } = require("../uptime-kuma-server");
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 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);
});
// 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
router.get("/api/status-page/:slug", cache("5 minutes"), async (request, response) => {
allowDevAllOrigin(response);

Loading…
Cancel
Save