|
|
@ -6,6 +6,7 @@ const apicache = require("../modules/apicache");
|
|
|
|
const Monitor = require("../model/monitor");
|
|
|
|
const Monitor = require("../model/monitor");
|
|
|
|
const dayjs = require("dayjs");
|
|
|
|
const dayjs = require("dayjs");
|
|
|
|
const { UP, flipStatus, debug } = require("../../src/util");
|
|
|
|
const { UP, flipStatus, debug } = require("../../src/util");
|
|
|
|
|
|
|
|
const StatusPage = require("../model/status_page");
|
|
|
|
let router = express.Router();
|
|
|
|
let router = express.Router();
|
|
|
|
|
|
|
|
|
|
|
|
let cache = apicache.middleware;
|
|
|
|
let cache = apicache.middleware;
|
|
|
@ -82,11 +83,12 @@ router.get("/api/push/:pushToken", async (request, response) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Status Page Config
|
|
|
|
// Status page config, incident, monitor list
|
|
|
|
router.get("/api/status-page/config/:slug", async (request, response) => {
|
|
|
|
router.get("/api/status-page/:slug", cache("5 minutes"), async (request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
let slug = request.params.slug;
|
|
|
|
let slug = request.params.slug;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get Status Page
|
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
slug
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
]);
|
|
|
@ -99,50 +101,30 @@ router.get("/api/status-page/config/:slug", async (request, response) => {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response.json(await statusPage.toPublicJSON());
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Status Page - Get the current Incident
|
|
|
|
|
|
|
|
// Can fetch only if published
|
|
|
|
|
|
|
|
router.get("/api/status-page/incident/:slug", async (_, response) => {
|
|
|
|
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await checkPublished();
|
|
|
|
// 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");
|
|
|
|
statusPage.id,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
if (incident) {
|
|
|
|
if (incident) {
|
|
|
|
incident = incident.toPublicJSON();
|
|
|
|
incident = incident.toPublicJSON();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response.json({
|
|
|
|
// Public Group List
|
|
|
|
ok: true,
|
|
|
|
|
|
|
|
incident,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
send403(response, error.message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Status Page - Monitor List
|
|
|
|
|
|
|
|
// Can fetch only if published
|
|
|
|
|
|
|
|
router.get("/api/status-page/monitor-list/:slug", cache("5 minutes"), async (_request, response) => {
|
|
|
|
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await checkPublished();
|
|
|
|
|
|
|
|
const publicGroupList = [];
|
|
|
|
const publicGroupList = [];
|
|
|
|
const tagsVisible = (await getSettings("statusPage")).statusPageTags;
|
|
|
|
const tagsVisible = !!statusPage.show_tags;
|
|
|
|
const list = await R.find("group", " public = 1 ORDER BY weight ");
|
|
|
|
const list = await R.find("group", " public = 1 AND status_page_id = ? ORDER BY weight ", [
|
|
|
|
|
|
|
|
statusPage.id
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
for (let groupBean of list) {
|
|
|
|
for (let groupBean of list) {
|
|
|
|
let monitorGroup = await groupBean.toPublicJSON();
|
|
|
|
let monitorGroup = await groupBean.toPublicJSON();
|
|
|
|
if (tagsVisible) {
|
|
|
|
if (tagsVisible) {
|
|
|
|
monitorGroup.monitorList = await Promise.all(monitorGroup.monitorList.map(async (monitor) => {
|
|
|
|
monitorGroup.monitorList = await Promise.all(monitorGroup.monitorList.map(async (monitor) => {
|
|
|
|
// Includes tags as an array in response, allows for tags to be displayed on public status page
|
|
|
|
// Includes tags as an array in response, allows for tags to be displayed on public status page
|
|
|
|
const tags = await R.getAll(
|
|
|
|
const tags = await R.getAll(
|
|
|
|
`SELECT monitor_tag.monitor_id, monitor_tag.value, tag.name, tag.color
|
|
|
|
`SELECT monitor_tag.monitor_id, monitor_tag.value, tag.name, tag.color
|
|
|
|
FROM monitor_tag
|
|
|
|
FROM monitor_tag
|
|
|
|
JOIN tag
|
|
|
|
JOIN tag
|
|
|
|
ON monitor_tag.tag_id = tag.id
|
|
|
|
ON monitor_tag.tag_id = tag.id
|
|
|
@ -158,29 +140,39 @@ router.get("/api/status-page/monitor-list/:slug", cache("5 minutes"), async (_re
|
|
|
|
publicGroupList.push(monitorGroup);
|
|
|
|
publicGroupList.push(monitorGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response.json(publicGroupList);
|
|
|
|
// Response
|
|
|
|
|
|
|
|
response.json({
|
|
|
|
|
|
|
|
config: await statusPage.toPublicJSON(),
|
|
|
|
|
|
|
|
incident,
|
|
|
|
|
|
|
|
publicGroupList
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
send403(response, error.message);
|
|
|
|
send403(response, error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Status Page Polling Data
|
|
|
|
// Status Page Polling Data
|
|
|
|
// Can fetch only if published
|
|
|
|
// Can fetch only if published
|
|
|
|
router.get("/api/status-page/heartbeat/:slug", cache("5 minutes"), async (_request, response) => {
|
|
|
|
router.get("/api/status-page/heartbeat/:slug", cache("1 minutes"), async (request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await checkPublished();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let heartbeatList = {};
|
|
|
|
let heartbeatList = {};
|
|
|
|
let uptimeList = {};
|
|
|
|
let uptimeList = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let slug = request.params.slug;
|
|
|
|
|
|
|
|
let statusPageID = await StatusPage.slugToID(slug);
|
|
|
|
|
|
|
|
|
|
|
|
let monitorIDList = await R.getCol(`
|
|
|
|
let monitorIDList = await R.getCol(`
|
|
|
|
SELECT monitor_group.monitor_id FROM monitor_group, \`group\`
|
|
|
|
SELECT monitor_group.monitor_id FROM monitor_group, \`group\`
|
|
|
|
WHERE monitor_group.group_id = \`group\`.id
|
|
|
|
WHERE monitor_group.group_id = \`group\`.id
|
|
|
|
AND public = 1
|
|
|
|
AND public = 1
|
|
|
|
`);
|
|
|
|
AND \`group\`.status_page_id = ?
|
|
|
|
|
|
|
|
`, [
|
|
|
|
|
|
|
|
statusPageID
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
for (let monitorID of monitorIDList) {
|
|
|
|
for (let monitorID of monitorIDList) {
|
|
|
|
let list = await R.getAll(`
|
|
|
|
let list = await R.getAll(`
|
|
|
@ -209,12 +201,6 @@ router.get("/api/status-page/heartbeat/:slug", cache("5 minutes"), async (_reque
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function checkPublished() {
|
|
|
|
|
|
|
|
if (! await isPublished()) {
|
|
|
|
|
|
|
|
throw new Error("The status page is not published");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Default is published
|
|
|
|
* Default is published
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|