diff --git a/server/db/models/Monitor.js b/server/db/models/Monitor.js index 7bb38f520..38aeff0c3 100755 --- a/server/db/models/Monitor.js +++ b/server/db/models/Monitor.js @@ -69,6 +69,15 @@ const MonitorSchema = mongoose.Schema( type: Number, default: undefined, }, + notifications: [ + { + type: mongoose.Schema.Types.ObjectId, + ref: "Notification", + }, + ], + secret: { + type: String, + }, thresholds: { type: { usage_cpu: { type: Number }, @@ -78,14 +87,33 @@ const MonitorSchema = mongoose.Schema( }, _id: false, }, - notifications: [ - { - type: mongoose.Schema.Types.ObjectId, - ref: "Notification", + alertThreshold: { + type: Number, + default: 5, + }, + cpuAlertThreshold: { + type: Number, + default: function () { + return this.alertThreshold; + }, + }, + memoryAlertThreshold: { + type: Number, + default: function () { + return this.alertThreshold; + }, + }, + diskAlertThreshold: { + type: Number, + default: function () { + return this.alertThreshold; + }, + }, + tempAlertThreshold: { + type: Number, + default: function () { + return this.alertThreshold; }, - ], - secret: { - type: String, }, }, { @@ -132,6 +160,33 @@ MonitorSchema.pre("deleteMany", async function (next) { next(); }); +MonitorSchema.pre("save", function (next) { + if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) { + this.cpuAlertThreshold = this.alertThreshold; + } + if (!this.memoryAlertThreshold || this.isModified("alertThreshold")) { + this.memoryAlertThreshold = this.alertThreshold; + } + if (!this.diskAlertThreshold || this.isModified("alertThreshold")) { + this.diskAlertThreshold = this.alertThreshold; + } + if (!this.tempAlertThreshold || this.isModified("alertThreshold")) { + this.tempAlertThreshold = this.alertThreshold; + } + next(); +}); + +MonitorSchema.pre("findOneAndUpdate", function (next) { + const update = this.getUpdate(); + if (update.alertThreshold) { + update.cpuAlertThreshold = update.alertThreshold; + update.memoryAlertThreshold = update.alertThreshold; + update.diskAlertThreshold = update.alertThreshold; + update.tempAlertThreshold = update.alertThreshold; + } + next(); +}); + MonitorSchema.index({ teamId: 1, type: 1 }); export default mongoose.model("Monitor", MonitorSchema); diff --git a/server/db/models/Notification.js b/server/db/models/Notification.js index fe314fc74..a6a3c7c09 100755 --- a/server/db/models/Notification.js +++ b/server/db/models/Notification.js @@ -28,34 +28,6 @@ const NotificationSchema = mongoose.Schema( phone: { type: String, }, - alertThreshold: { - type: Number, - default: 5, - }, - cpuAlertThreshold: { - type: Number, - default: function () { - return this.alertThreshold; - }, - }, - memoryAlertThreshold: { - type: Number, - default: function () { - return this.alertThreshold; - }, - }, - diskAlertThreshold: { - type: Number, - default: function () { - return this.alertThreshold; - }, - }, - tempAlertThreshold: { - type: Number, - default: function () { - return this.alertThreshold; - }, - }, }, { timestamps: true,