Add status page summary API

This commit is contained in:
Gero Gerke 2022-07-20 00:11:15 +02:00
parent 9a8b484ee8
commit 3ac752246d
3 changed files with 44 additions and 4 deletions

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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);