From 9ccf42fe556fefd494ab65d1c55fab185aefee13 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 3 Jul 2024 11:41:12 -0700 Subject: [PATCH 1/4] Monitor now has a status, status will be updated on new check --- Server/models/Check.js | 17 +++++++++++++++++ Server/models/Monitor.js | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/Server/models/Check.js b/Server/models/Check.js index d57030202..7e46f418d 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -31,4 +31,21 @@ const CheckSchema = mongoose.Schema( } ); +CheckSchema.pre("save", async function (next) { + const monitor = await mongoose.model("Monitor").findById(this.monitorId); + + if (monitor.status === true && this.status === false) { + console.log("Monitor went down"); + } + + if (monitor.status === false && this.status === true) { + console.log("Monitor went up"); + } + + monitor.status = this.status; + await monitor.save(); + + next; +}); + module.exports = mongoose.model("Check", CheckSchema); diff --git a/Server/models/Monitor.js b/Server/models/Monitor.js index 8455aa312..4b3bd7c63 100644 --- a/Server/models/Monitor.js +++ b/Server/models/Monitor.js @@ -14,6 +14,10 @@ const MonitorSchema = mongoose.Schema( description: { type: String, }, + status: { + type: Boolean, + default: true, + }, type: { type: String, required: true, From 031ae06d417bc26adcbc0bee00290968ec80e3ca Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 3 Jul 2024 11:46:30 -0700 Subject: [PATCH 2/4] Add conditional to avoid extra db operatons --- Server/models/Check.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Server/models/Check.js b/Server/models/Check.js index 7e46f418d..b1cda5616 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -31,20 +31,26 @@ 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 + */ + CheckSchema.pre("save", async function (next) { const monitor = await mongoose.model("Monitor").findById(this.monitorId); + if (monitor.status !== this.status) { + if (monitor.status === true && this.status === false) { + // TODO issue alert + console.log("Monitor went down"); + } - if (monitor.status === true && this.status === false) { - console.log("Monitor went down"); + if (monitor.status === false && this.status === true) { + // TODO issue alert + console.log("Monitor went up"); + } + await monitor.save(); } - if (monitor.status === false && this.status === true) { - console.log("Monitor went up"); - } - - monitor.status = this.status; - await monitor.save(); - next; }); From 505d9f28907bd7af7b03c5f7f59640f0ddce0136 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 3 Jul 2024 12:32:42 -0700 Subject: [PATCH 3/4] Added error checking and fixed typo --- Server/models/Check.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Server/models/Check.js b/Server/models/Check.js index b1cda5616..2bcb912f4 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -37,21 +37,25 @@ const CheckSchema = mongoose.Schema( */ CheckSchema.pre("save", async function (next) { - const monitor = await mongoose.model("Monitor").findById(this.monitorId); - if (monitor.status !== this.status) { - if (monitor.status === true && this.status === false) { - // TODO issue alert - console.log("Monitor went down"); - } + 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"); + if (monitor.status === false && this.status === true) { + // TODO issue alert + console.log("Monitor went up"); + } + await monitor.save(); } - await monitor.save(); + } catch (error) { + console.log(error); + } finally { + next(); } - - next; }); module.exports = mongoose.model("Check", CheckSchema); From ceecf3427cda070ad1b2249bd1a79c17f995c404 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 3 Jul 2024 12:34:57 -0700 Subject: [PATCH 4/4] Added JS doc --- Server/models/Check.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Server/models/Check.js b/Server/models/Check.js index 2bcb912f4..339c21482 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -34,6 +34,7 @@ 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) {