Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status page summary API #1927

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions server/model/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class Group extends BeanModel {
* @param {boolean} [showTags=false] Should the JSON include monitor tags
* @returns {Object}
Empty2k12 marked this conversation as resolved.
Show resolved Hide resolved
*/
async toPublicJSON(showTags = false) {
async toPublicJSON(showTags = false, showStatus = false) {
let monitorBeanList = await this.getMonitorList();
let monitorList = [];

for (let bean of monitorBeanList) {
monitorList.push(await bean.toPublicJSON(showTags));
monitorList.push(await bean.toPublicJSON(showTags, showStatus));
}

return {
Expand Down
8 changes: 7 additions & 1 deletion server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Monitor extends BeanModel {
* Only show necessary data to public
Empty2k12 marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Object}
*/
async toPublicJSON(showTags = false) {
async toPublicJSON(showTags = false, includeStatus = false) {
let obj = {
id: this.id,
name: this.name,
Expand All @@ -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";
Empty2k12 marked this conversation as resolved.
Show resolved Hide resolved
}

return obj;
}

Expand Down
15 changes: 12 additions & 3 deletions server/model/status_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ 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")
* @param {boolean} includeConfig whether the config for the status paghe should be included in the returned JSON
Empty2k12 marked this conversation as resolved.
Show resolved Hide resolved
*/
static async getStatusPageData(statusPage) {
static async getStatusPageData(statusPage, includeStatus = false, includeConfig = true) {
// Incident
let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [
statusPage.id,
Expand All @@ -92,13 +94,20 @@ 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);
}

let config = {};
if (includeConfig) {
config = {
config: await statusPage.toPublicJSON()
};
}

// Response
return {
config: await statusPage.toPublicJSON(),
...config,
incident,
publicGroupList
};
Expand Down
35 changes: 34 additions & 1 deletion server/routers/status-page-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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, false);

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);
Expand Down