Move thresholds from Notification to Monitor

This commit is contained in:
Alex Holliday committed 2025-06-19 12:10:07 +08:00
1 parent f9e0930336
commit 7e5521eb13
2 files changed
+62 -35

No files matched your search

+62 -7
View File
@@ -69,6 +69,15 @@ const MonitorSchema = mongoose.Schema(
type: Number, type: Number,
default: undefined, default: undefined,
}, },
notifications: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Notification",
},
],
secret: {
type: String,
},
thresholds: { thresholds: {
type: { type: {
usage_cpu: { type: Number }, usage_cpu: { type: Number },
@@ -78,14 +87,33 @@ const MonitorSchema = mongoose.Schema(
}, },
_id: false, _id: false,
}, },
notifications: [ alertThreshold: {
{ type: Number,
type: mongoose.Schema.Types.ObjectId, default: 5,
ref: "Notification", },
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(); 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 }); MonitorSchema.index({ teamId: 1, type: 1 });
export default mongoose.model("Monitor", MonitorSchema); export default mongoose.model("Monitor", MonitorSchema);
-28
View File
@@ -28,34 +28,6 @@ const NotificationSchema = mongoose.Schema(
phone: { phone: {
type: String, 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, timestamps: true,