Added pre save function to PageSpeedCheck to update Monitor status (#485)

This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-07-30 11:24:25 -07:00
1 parent 979c198702
commit 27b9a6055f
1 file changed
+23 -2
+23 -2
View File
@@ -48,6 +48,27 @@ const PageSpeedCheck = mongoose.Schema(
* @typedef {mongoose.Model<PageSpeedCheck>} LighthouseMetricsModel
*/
PageSpeedCheck.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");
}
monitor.status = this.status;
await monitor.save();
}
} catch (error) {
console.log(error);
} finally {
next();
}
});
module.exports = mongoose.model("PageSpeedCheck", PageSpeedCheck);