Files
Checkmate/server/src/db/models/Monitor.ts
T

207 lines
4.2 KiB
TypeScript
Raw Normal View History

2026-01-12 22:58:46 +00:00
import { Schema, model, Types, type UpdateQuery } from "mongoose";
import type { Monitor, MonitorMatchMethod, MonitorThresholds } from "@/types/monitor.js";
import { MonitorTypes } from "@/types/monitor.js";
2025-06-10 09:49:53 +08:00
import Check from "./Check.js";
import MonitorStats from "./MonitorStats.js";
2025-06-13 14:54:59 +08:00
import StatusPage from "./StatusPage.js";
2025-04-20 11:29:53 -07:00
2026-01-12 22:58:46 +00:00
type MonitorDocumentBase = Omit<
Monitor,
"id" | "userId" | "teamId" | "notifications" | "selectedDisks" | "statusWindow" | "createdAt" | "updatedAt"
> & {
statusWindow: boolean[];
notifications: Types.ObjectId[];
selectedDisks: string[];
matchMethod?: MonitorMatchMethod;
thresholds?: MonitorThresholds;
};
interface MonitorDocument extends MonitorDocumentBase {
_id: Types.ObjectId;
userId: Types.ObjectId;
teamId: Types.ObjectId;
createdAt: Date;
updatedAt: Date;
}
const thresholdsSchema = new Schema<MonitorThresholds>(
{
usage_cpu: { type: Number },
usage_memory: { type: Number },
usage_disk: { type: Number },
usage_temperature: { type: Number },
},
{ _id: false }
);
const MonitorSchema = new Schema<MonitorDocument>(
2025-04-20 11:29:53 -07:00
{
userId: {
2026-01-12 22:58:46 +00:00
type: Schema.Types.ObjectId,
2025-04-20 11:29:53 -07:00
ref: "User",
immutable: true,
required: true,
},
teamId: {
2026-01-12 22:58:46 +00:00
type: Schema.Types.ObjectId,
2025-04-20 11:29:53 -07:00
ref: "Team",
immutable: true,
required: true,
},
name: {
type: String,
required: true,
},
description: {
type: String,
},
status: {
type: Boolean,
default: undefined,
},
2025-08-18 13:40:22 -07:00
statusWindow: {
type: [Boolean],
default: [],
},
statusWindowSize: {
type: Number,
default: 5,
},
statusWindowThreshold: {
type: Number,
2025-08-30 11:28:54 -07:00
default: 60,
2025-08-18 13:40:22 -07:00
},
2025-04-20 11:29:53 -07:00
type: {
type: String,
required: true,
2026-01-12 22:58:46 +00:00
enum: MonitorTypes,
2025-04-20 11:29:53 -07:00
},
2025-05-06 10:07:43 -07:00
ignoreTlsErrors: {
type: Boolean,
default: false,
},
2025-04-20 11:29:53 -07:00
jsonPath: {
type: String,
},
expectedValue: {
type: String,
},
matchMethod: {
type: String,
2025-06-09 14:22:11 +08:00
enum: ["equal", "include", "regex", ""],
2025-04-20 11:29:53 -07:00
},
url: {
type: String,
required: true,
},
port: {
type: Number,
},
isActive: {
type: Boolean,
default: true,
},
interval: {
type: Number,
default: 60000,
},
uptimePercentage: {
type: Number,
default: undefined,
},
notifications: [
{
2026-01-12 22:58:46 +00:00
type: Schema.Types.ObjectId,
ref: "Notification",
},
],
secret: {
type: String,
},
2025-04-20 11:29:53 -07:00
thresholds: {
2026-01-12 22:58:46 +00:00
type: thresholdsSchema,
2025-04-20 11:29:53 -07:00
},
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;
2025-04-20 11:29:53 -07:00
},
},
selectedDisks: {
type: [String],
default: [],
},
2025-08-02 02:04:06 +03:00
gameId: {
type: String,
},
group: {
type: String,
trim: true,
maxLength: 50,
default: null,
2026-01-12 22:58:46 +00:00
set(value: string | null) {
return value && value.trim() ? value.trim() : null;
2025-10-12 21:51:06 -04:00
},
},
2025-04-20 11:29:53 -07:00
},
{
timestamps: true,
}
);
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) {
2026-01-12 22:58:46 +00:00
const update = this.getUpdate() as UpdateQuery<MonitorDocument> | null;
if (update && !Array.isArray(update) && update.alertThreshold !== undefined) {
update.cpuAlertThreshold = update.alertThreshold;
update.memoryAlertThreshold = update.alertThreshold;
update.diskAlertThreshold = update.alertThreshold;
update.tempAlertThreshold = update.alertThreshold;
}
next();
});
2025-04-20 11:29:53 -07:00
MonitorSchema.index({ teamId: 1, type: 1 });
2026-01-12 22:58:46 +00:00
const MonitorModel = model<MonitorDocument>("Monitor", MonitorSchema);
export type { MonitorDocument };
export { MonitorModel };
export default MonitorModel;