Merge pull request #251 from bluewave-labs/feat/monitor-status-change

Monitor now has a status, status will be updated on new check, #235
This commit is contained in:
Alexander Holliday
2024-07-03 13:09:31 -07:00
committed by GitHub
2 changed files with 32 additions and 0 deletions

View File

@@ -31,4 +31,32 @@ const CheckSchema = mongoose.Schema(
}
);
/**
* When a check is created, we should update the stauts of the associated monitor
* if it has changed. Monitor starts with default status: true
* @param {function} next - Callback to proceed to the next middleware.
*/
CheckSchema.pre("save", async function (next) {
try {
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) {
// TODO issue alert
console.log("Monitor went up");
}
await monitor.save();
}
} catch (error) {
console.log(error);
} finally {
next();
}
});
module.exports = mongoose.model("Check", CheckSchema);

View File

@@ -14,6 +14,10 @@ const MonitorSchema = mongoose.Schema(
description: {
type: String,
},
status: {
type: Boolean,
default: true,
},
type: {
type: String,
required: true,