Merge pull request #565 from bluewave-labs/develop

MERGE Develop to Master
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-08-07 15:18:14 -07:00
commit 0631d3d2be
5 files changed
+108 -31

No files matched your search

@@ -47,7 +47,7 @@ const MonitorDetailsAreaChart = ({ checks, filter }) => {
}); });
}; };
const filterChecks = (checks, filter) => { const filterChecks = (checks, filter, numToDisplay) => {
const limits = { const limits = {
day: 24 * 60 * 60 * 1000, day: 24 * 60 * 60 * 1000,
week: 24 * 60 * 60 * 1000 * 7, week: 24 * 60 * 60 * 1000 * 7,
@@ -55,20 +55,29 @@ const MonitorDetailsAreaChart = ({ checks, filter }) => {
}; };
const now = new Date().getTime(); const now = new Date().getTime();
const result = []; let result = [];
for (let i = 0; i < checks.length; i++) { for (let i = 0; i < checks.length; i++) {
const checkTime = new Date(checks[i].createdAt).getTime(); const checkTime = new Date(checks[i].createdAt).getTime();
if (now - checkTime < limits[filter]) { if (now - checkTime < limits[filter]) {
result.push(checks[i]); result.push(checks[i]);
} }
} }
// If more than numToDisplay checks, pick every nth check
if (result.length > numToDisplay) {
const n = Math.ceil(result.length / numToDisplay);
result = result.filter((_, idx) => {
return idx % n === 0;
});
}
return result; return result;
}; };
let normalizedChecks = []; let normalizedChecks = [];
if (checks && checks.length > 0) { if (checks && checks.length > 0) {
const filteredChecks = filterChecks(checks, filter); const filteredChecks = filterChecks(checks, filter, 75);
normalizedChecks = NormalizeData(filteredChecks, 33, 100); normalizedChecks = NormalizeData(filteredChecks, 10, 100);
} }
return ( return (
+2 -2
View File
@@ -294,10 +294,10 @@ const Monitors = () => {
}} }}
> >
<Typography id="modal-delete-monitor" component="h2"> <Typography id="modal-delete-monitor" component="h2">
Really delete this monitor? Do you really want to delete this monitor?
</Typography> </Typography>
<Typography id="delete-monitor-confirmation"> <Typography id="delete-monitor-confirmation">
Once deleted, this monitor cannot be retrieved back. Once deleted, this monitor cannot be retrieved.
</Typography> </Typography>
<Stack <Stack
direction="row" direction="row"
+88 -19
View File
@@ -1,57 +1,125 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const EmailService = require("../service/emailService");
const Notification = require("../models/Notification");
/**
* Check Schema for MongoDB collection.
*
* Represents a check associated with a monitor, storing information
* about the status and response of a particular check event.
*/
const CheckSchema = mongoose.Schema( const CheckSchema = mongoose.Schema(
{ {
/**
* Reference to the associated Monitor document.
*
* @type {mongoose.Schema.Types.ObjectId}
*/
monitorId: { monitorId: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Monitor", ref: "Monitor",
immutable: true, immutable: true,
}, },
/**
* Status of the check (true for up, false for down).
*
* @type {Boolean}
*/
status: { status: {
type: Boolean, type: Boolean,
}, },
/**
* Response time of the check in milliseconds.
*
* @type {Number}
*/
responseTime: { responseTime: {
type: Number, // milliseconds type: Number,
}, },
/**
* HTTP status code received during the check.
*
* @type {Number}
*/
statusCode: { statusCode: {
type: Number, // 200, ... , 500 type: Number,
}, },
/**
* Message or description of the check result.
*
* @type {String}
*/
message: { message: {
type: String, type: String,
}, },
/**
* Expiry date of the check, auto-calculated to expire after 30 days.
*
* @type {Date}
*/
expiry: { expiry: {
type: Date, type: Date,
default: Date.now, default: Date.now,
expires: 60 * 60 * 24 * 30, // 30 days in seconds expires: 60 * 60 * 24 * 30, // 30 days
}, },
}, },
{ {
timestamps: true, timestamps: true, // Adds createdAt and updatedAt timestamps
} }
); );
/** /**
* When a check is created, we should update the stauts of the associated monitor * Pre-save middleware to handle status change notifications.
* if it has changed. Monitor starts with default status: true *
* @param {function} next - Callback to proceed to the next middleware. * This middleware checks if the status of the monitor associated
* with the check has changed and sends notifications if necessary.
*/ */
CheckSchema.pre("save", async function (next) { CheckSchema.pre("save", async function (next) {
try { try {
const monitor = await mongoose.model("Monitor").findById(this.monitorId); const monitor = await mongoose.model("Monitor").findById(this.monitorId);
if (monitor && monitor.status !== this.status) {
if (monitor.status === true && this.status === false) {
// TODO issue alert
console.log("Monitor went down");
}
if (monitor.status === false && this.status === true) { if (monitor) {
// TODO issue alert const notifications = await Notification.find({ monitorId: this.monitorId });
console.log("Monitor went up");
// Check if there are any notifications
if (notifications && notifications.length > 0) {
// Only send email if monitor status has changed
if (monitor.status !== this.status) {
const emailService = new EmailService();
if (monitor.status === true && this.status === false) {
// Notify users that the monitor is down
for (const notification of notifications) {
if (notification.type === "email") {
await emailService.buildAndSendEmail(
"serverIsDownTemplate",
{ monitorName: monitor.name, monitorUrl: monitor.url },
notification.address,
`Monitor ${monitor.name} is down`
);
}
}
}
if (monitor.status === false && this.status === true) {
// Notify users that the monitor is up
for (const notification of notifications) {
if (notification.type === "email") {
await emailService.buildAndSendEmail(
"serverIsUpTemplate",
{ monitorName: monitor.name, monitorUrl: monitor.url },
notification.address,
`Monitor ${monitor.name} is back up`
);
}
}
}
// Update monitor status
monitor.status = this.status;
await monitor.save();
}
} }
monitor.status = this.status;
await monitor.save();
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);
@@ -60,4 +128,5 @@ CheckSchema.pre("save", async function (next) {
} }
}); });
module.exports = mongoose.model("Check", CheckSchema); module.exports = mongoose.model("Check", CheckSchema);
+1 -1
View File
@@ -21,4 +21,4 @@ router.post(
checkController.deleteChecks checkController.deleteChecks
); );
module.exports = router; module.exports = router;
+4 -5
View File
@@ -1,7 +1,7 @@
const router = require("express").Router(); const router = require("express").Router();
const { verifyOwnership } = require("../middleware/verifyOwnership"); const { verifyOwnership } = require("../middleware/verifyOwnership");
const Monitor = require("../models/Monitor"); const Monitor = require("../models/Monitor");
const checkController = require("../controllers/pageSpeedCheckController"); const pageSpeedCheckController = require("../controllers/pageSpeedCheckController");
/** /**
* @route POST /:monitorId * @route POST /:monitorId
@@ -13,7 +13,7 @@ const checkController = require("../controllers/pageSpeedCheckController");
router.post( router.post(
"/:monitorId", "/:monitorId",
verifyOwnership(Monitor, "monitorId"), verifyOwnership(Monitor, "monitorId"),
checkController.createPageSpeedCheck pageSpeedCheckController.createPageSpeedCheck
); );
/** /**
@@ -26,7 +26,7 @@ router.post(
router.get( router.get(
"/:monitorId", "/:monitorId",
verifyOwnership(Monitor, "monitorId"), verifyOwnership(Monitor, "monitorId"),
checkController.getPageSpeedChecks pageSpeedCheckController.getPageSpeedChecks
); );
/** /**
@@ -39,8 +39,7 @@ router.get(
router.delete( router.delete(
"/:monitorId", "/:monitorId",
verifyOwnership(Monitor, "monitorId"), verifyOwnership(Monitor, "monitorId"),
checkController.deletePageSpeedCheck pageSpeedCheckController.deletePageSpeedCheck
); );
module.exports = router; module.exports = router;