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

172 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-01-12 22:58:46 +00:00
import { Schema, model, Types, type UpdateQuery } from "mongoose";
2026-02-14 21:21:18 +00:00
import type { Monitor, MonitorMatchMethod, CheckSnapshot } from "@/types/monitor.js";
2026-02-11 18:58:12 +00:00
import { MonitorTypes, MonitorStatuses } from "@/types/monitor.js";
2025-04-20 11:29:53 -07:00
2026-01-27 18:26:31 +00:00
type CheckSnapshotDocument = Omit<CheckSnapshot, "createdAt"> & { createdAt: Date };
2026-01-12 22:58:46 +00:00
type MonitorDocumentBase = Omit<
Monitor,
2026-01-27 18:26:31 +00:00
"id" | "userId" | "teamId" | "notifications" | "selectedDisks" | "statusWindow" | "recentChecks" | "createdAt" | "updatedAt"
2026-01-12 22:58:46 +00:00
> & {
statusWindow: boolean[];
2026-01-27 18:26:31 +00:00
recentChecks: CheckSnapshotDocument[];
2026-01-12 22:58:46 +00:00
notifications: Types.ObjectId[];
selectedDisks: string[];
matchMethod?: MonitorMatchMethod;
};
interface MonitorDocument extends MonitorDocumentBase {
_id: Types.ObjectId;
userId: Types.ObjectId;
teamId: Types.ObjectId;
createdAt: Date;
updatedAt: Date;
}
2026-01-27 18:26:31 +00:00
const checkSnapshotSchema = new Schema<CheckSnapshotDocument>(
{
id: { type: String, required: true },
status: { type: Boolean, required: true },
createdAt: { type: Date, required: true },
},
{ _id: false, strict: false }
);
2026-01-12 22:58:46 +00:00
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: {
2026-02-11 18:58:12 +00:00
type: String,
enum: MonitorStatuses,
default: "initializing",
2025-04-20 11:29:53 -07:00
},
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,
},
2026-01-30 20:21:31 +00:00
useAdvancedMatching: {
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,
},
cpuAlertThreshold: {
type: Number,
2026-02-14 21:29:30 +00:00
default: 100,
},
memoryAlertThreshold: {
type: Number,
2026-02-14 21:29:30 +00:00
default: 100,
},
diskAlertThreshold: {
type: Number,
2026-02-14 21:29:30 +00:00
default: 100,
},
tempAlertThreshold: {
type: Number,
2026-02-14 21:29:30 +00:00
default: 100,
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
},
},
2026-01-27 18:26:31 +00:00
recentChecks: {
type: [checkSnapshotSchema],
default: [],
},
2025-04-20 11:29:53 -07:00
},
{
timestamps: true,
}
);
MonitorSchema.index({ teamId: 1, type: 1 });
2026-01-12 22:58:46 +00:00
const MonitorModel = model<MonitorDocument>("Monitor", MonitorSchema);
2026-01-27 18:58:08 +00:00
export type { MonitorDocument, CheckSnapshotDocument };
2026-01-12 22:58:46 +00:00
export { MonitorModel };
export default MonitorModel;