refactor public status page

This commit is contained in:
Alex Holliday committed 2025-02-04 13:06:42 -08:00
1 parent cc8d4bc98c
commit dc3fc7628a
15 files changed
+605 -53

No files matched your search

@@ -32,6 +32,7 @@ class StatusPageController {
next(handleError(error, SERVICE_NAME, "createStatusPage"));
}
};
getStatusPage = async (req, res, next) => {
try {
const statusPage = await this.db.getStatusPage();
@@ -43,6 +44,17 @@ class StatusPageController {
next(handleError(error, SERVICE_NAME, "getStatusPage"));
}
};
deleteStatusPage = async (req, res, next) => {
try {
await this.db.deleteStatusPage();
return res.success({
msg: successMessages.STATUS_PAGE_DELETE,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteStatusPage"));
}
};
}
export default StatusPageController;
+77 -5
View File
@@ -1,5 +1,6 @@
import StatusPage from "../../models/StatusPage.js";
import { errorMessages } from "../../../utils/messages.js";
import { NormalizeData } from "../../../utils/dataUtils.js";
const SERVICE_NAME = "statusPageModule";
@@ -28,14 +29,75 @@ const createStatusPage = async (statusPageData, image) => {
const getStatusPage = async () => {
try {
const statusPage = await StatusPage.findOne();
if (statusPage === null || statusPage === undefined) {
// const statusPage = await StatusPage.findOne();
// if (statusPage === null || statusPage === undefined) {
// const error = new Error(errorMessages.STATUS_PAGE_NOT_FOUND);
// error.status = 404;
// throw error;
// }
// const ids = statusPage.monitors;
// const monitors = await Monitor.find({ _id: { $in: ids } }).lean();
// return { statusPage, monitors };
const statusPageQuery = await StatusPage.aggregate([
{ $limit: 1 },
{
$lookup: {
from: "monitors",
localField: "monitors",
foreignField: "_id",
as: "monitors",
},
},
{
$unwind: "$monitors",
},
{
$lookup: {
from: "checks",
let: { monitorId: "$monitors._id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
},
},
{ $sort: { createdAt: -1 } },
{ $limit: 25 },
],
as: "monitors.checks",
},
},
{
$group: {
_id: "$_id",
statusPage: { $first: "$$ROOT" },
monitors: { $push: "$monitors" },
},
},
{
$project: {
statusPage: 1,
monitors: 1,
},
},
]);
if (!statusPageQuery.length) {
const error = new Error(errorMessages.STATUS_PAGE_NOT_FOUND);
error.status = 404;
throw error;
}
return statusPage;
const { statusPage, monitors } = statusPageQuery[0];
const normalizedMonitors = monitors.map((monitor) => {
return {
...monitor,
checks: NormalizeData(monitor.checks, 10, 100),
};
});
return { statusPage, monitors: normalizedMonitors };
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getStatusPageByUrl";
@@ -43,4 +105,14 @@ const getStatusPage = async () => {
}
};
export { createStatusPage, getStatusPage };
const deleteStatusPage = async () => {
try {
await StatusPage.deleteOne({});
} catch (error) {
error.service = SERVICE_NAME;
error.method = "createStatusPage";
throw error;
}
};
export { createStatusPage, getStatusPage, deleteStatusPage };
+1
View File
@@ -18,6 +18,7 @@ class StatusPageRoutes {
verifyJWT,
this.statusPageController.createStatusPage
);
this.router.delete("/", verifyJWT, this.statusPageController.deleteStatusPage);
}
getRouter() {
+1 -1
View File
@@ -136,7 +136,7 @@ const successMessages = {
// Status Page
STATUS_PAGE_BY_URL: "Got status page by url successfully",
STATUS_PAGE_CREATE: "Status page created successfully",
STATUS_PAGE_DELETE: "Status page deleted successfully",
// Docker
DOCKER_SUCCESS: "Docker container status fetched successfully",