Added pre save function to PageSpeedCheck to update Monitor status (#485)
This commit is contained in:
1 parent
979c198702
commit
27b9a6055f
1 file changed
+23
-2
@@ -48,6 +48,27 @@ const PageSpeedCheck = mongoose.Schema(
|
|||||||
* @typedef {mongoose.Model<PageSpeedCheck>} LighthouseMetricsModel
|
* @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);
|
module.exports = mongoose.model("PageSpeedCheck", PageSpeedCheck);
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user