From 400150486dd6ddf547f94ad03f491aa1556484eb Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 30 Oct 2024 10:20:42 +0800 Subject: [PATCH] implement base check --- Server/db/models/Check.js | 127 ++++++++++++++--------------- Server/db/models/HardwareCheck.js | 34 +------- Server/db/models/PageSpeedCheck.js | 18 ++-- 3 files changed, 71 insertions(+), 108 deletions(-) diff --git a/Server/db/models/Check.js b/Server/db/models/Check.js index 320ddefe9..ad50c208a 100644 --- a/Server/db/models/Check.js +++ b/Server/db/models/Check.js @@ -1,6 +1,63 @@ import mongoose from "mongoose"; -import EmailService from "../../service/emailService.js"; -import Notification from "./Notification.js"; + +const BaseCheckSchema = mongoose.Schema({ + /** + * Reference to the associated Monitor document. + * + * @type {mongoose.Schema.Types.ObjectId} + */ + monitorId: { + type: mongoose.Schema.Types.ObjectId, + ref: "Monitor", + immutable: true, + index: true, + }, + /** + * Status of the check (true for up, false for down). + * + * @type {Boolean} + */ + status: { + type: Boolean, + index: true, + }, + /** + * Response time of the check in milliseconds. + * + * @type {Number} + */ + responseTime: { + type: Number, + }, + /** + * HTTP status code received during the check. + * + * @type {Number} + */ + statusCode: { + type: Number, + index: true, + }, + /** + * Message or description of the check result. + * + * @type {String} + */ + message: { + type: String, + }, + /** + * Expiry date of the check, auto-calculated to expire after 30 days. + * + * @type {Date} + */ + + expiry: { + type: Date, + default: Date.now, + expires: 60 * 60 * 24 * 30, // 30 days + }, +}); /** * Check Schema for MongoDB collection. @@ -8,69 +65,7 @@ import Notification from "./Notification.js"; * Represents a check associated with a monitor, storing information * about the status and response of a particular check event. */ -const CheckSchema = mongoose.Schema( - { - /** - * Reference to the associated Monitor document. - * - * @type {mongoose.Schema.Types.ObjectId} - */ - monitorId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Monitor", - immutable: true, - index: true, - }, - /** - * Status of the check (true for up, false for down). - * - * @type {Boolean} - */ - status: { - type: Boolean, - index: true, - }, - /** - * Response time of the check in milliseconds. - * - * @type {Number} - */ - responseTime: { - type: Number, - }, - /** - * HTTP status code received during the check. - * - * @type {Number} - */ - statusCode: { - type: Number, - index: true, - }, - /** - * Message or description of the check result. - * - * @type {String} - */ - message: { - type: String, - }, - /** - * Expiry date of the check, auto-calculated to expire after 30 days. - * - * @type {Date} - */ - - expiry: { - type: Date, - default: Date.now, - expires: 60 * 60 * 24 * 30, // 30 days - }, - }, - { - timestamps: true, // Adds createdAt and updatedAt timestamps - } -); - +const CheckSchema = mongoose.Schema({ ...BaseCheckSchema.obj }, { timestamps: true }); CheckSchema.index({ createdAt: 1 }); export default mongoose.model("Check", CheckSchema); +export { BaseCheckSchema }; diff --git a/Server/db/models/HardwareCheck.js b/Server/db/models/HardwareCheck.js index c307b9804..75e4ac584 100644 --- a/Server/db/models/HardwareCheck.js +++ b/Server/db/models/HardwareCheck.js @@ -1,5 +1,5 @@ import mongoose from "mongoose"; - +import { BaseCheckSchema } from "./Check.js"; const cpuSchema = mongoose.Schema({ physical_core: { type: Number, default: 0 }, logical_core: { type: Number, default: 0 }, @@ -32,35 +32,7 @@ const hostSchema = mongoose.Schema({ const HardwareCheckSchema = mongoose.Schema( { - monitorId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Monitor", - immutable: true, - index: true, - }, - - status: { - type: Boolean, - index: true, - }, - - responseTime: { - type: Number, - }, - - statusCode: { - type: Number, - index: true, - }, - - message: { - type: String, - }, - expiry: { - type: Date, - default: Date.now, - expires: 60 * 60 * 24 * 30, // 30 days - }, + ...BaseCheckSchema.obj, cpu: { type: cpuSchema, default: () => ({}), @@ -81,4 +53,6 @@ const HardwareCheckSchema = mongoose.Schema( { timestamps: true } ); +HardwareCheckSchema.index({ createdAt: 1 }); + export default mongoose.model("HardwareCheck", HardwareCheckSchema); diff --git a/Server/db/models/PageSpeedCheck.js b/Server/db/models/PageSpeedCheck.js index 344cc685d..4ebc60280 100644 --- a/Server/db/models/PageSpeedCheck.js +++ b/Server/db/models/PageSpeedCheck.js @@ -1,5 +1,7 @@ import mongoose from "mongoose"; +import { BaseCheckSchema } from "./Check.js"; import logger from "../../utils/logger.js"; +import { time } from "console"; const AuditSchema = mongoose.Schema({ id: { type: String, required: true }, title: { type: String, required: true }, @@ -46,15 +48,7 @@ const AuditsSchema = mongoose.Schema({ const PageSpeedCheck = mongoose.Schema( { - monitorId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Monitor", - immutable: true, - }, - status: { - type: Boolean, - required: true, - }, + ...BaseCheckSchema.obj, accessibility: { type: Number, required: true, @@ -76,9 +70,7 @@ const PageSpeedCheck = mongoose.Schema( required: true, }, }, - { - timestamps: true, - } + { timestamps: true } ); /** @@ -112,4 +104,6 @@ PageSpeedCheck.pre("save", async function (next) { } }); +PageSpeedCheck.index({ createdAt: 1 }); + export default mongoose.model("PageSpeedCheck", PageSpeedCheck);