diff --git a/server/model/monitor.js b/server/model/monitor.js index cb044f0f1..5b098356b 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -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; } diff --git a/server/model/status_page.js b/server/model/status_page.js index 82d184bfd..5ac398c39 100644 --- a/server/model/status_page.js +++ b/server/model/status_page.js @@ -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); } diff --git a/server/routers/status-page-router.js b/server/routers/status-page-router.js index de075db8d..95bd9eace 100644 --- a/server/routers/status-page-router.js +++ b/server/routers/status-page-router.js @@ -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);