refactoring

This commit is contained in:
Alex Holliday committed 2026-01-12 22:58:46 +00:00
1 parent e090d3cfd3
commit 7a6b0d5da4
19 files changed
+699 -116

No files matched your search

Executable → Regular
+108 -32
View File
@@ -1,7 +1,78 @@
import mongoose from "mongoose";
import { MonitorTypes } from "@/types/monitor.js";
import { Schema, model, Types } from "mongoose";
import {
MonitorTypes,
type MonitorType,
} from "@/types/monitor.js";
import type {
Check,
CheckAudits,
CheckCaptureInfo,
CheckCpuInfo,
CheckDiskInfo,
CheckErrorInfo,
CheckHostInfo,
CheckMemoryInfo,
CheckMetadata,
CheckNetworkInterfaceInfo,
CheckTimings,
CheckTimingPhases,
} from "@/types/check.js";
const cpuSchema = new mongoose.Schema(
type CheckMetadataDocument = Omit<CheckMetadata, "monitorId" | "teamId"> & {
monitorId: Types.ObjectId;
teamId: Types.ObjectId;
type: MonitorType;
};
type CheckDocumentBase = Omit<
Check,
"id" | "metadata" | "expiry" | "ackAt" | "createdAt" | "updatedAt"
> & {
metadata: CheckMetadataDocument;
expiry: Date;
ackAt?: Date | null;
createdAt: Date;
updatedAt: Date;
__v: number;
};
interface CheckDocument extends CheckDocumentBase {
_id: Types.ObjectId;
}
const timingPhasesSchema = new Schema<CheckTimingPhases>(
{
wait: { type: Number, default: 0 },
dns: { type: Number, default: 0 },
tcp: { type: Number, default: 0 },
tls: { type: Number, default: 0 },
request: { type: Number, default: 0 },
firstByte: { type: Number, default: 0 },
download: { type: Number, default: 0 },
total: { type: Number, default: 0 },
},
{ _id: false }
);
const timingsSchema = new Schema<CheckTimings>(
{
start: { type: Number, default: 0 },
socket: { type: Number, default: 0 },
lookup: { type: Number, default: 0 },
connect: { type: Number, default: 0 },
secureConnect: { type: Number, default: 0 },
upload: { type: Number, default: 0 },
response: { type: Number, default: 0 },
end: { type: Number, default: 0 },
phases: {
type: timingPhasesSchema,
default: () => ({}),
},
},
{ _id: false }
);
const cpuSchema = new Schema<CheckCpuInfo>(
{
physical_core: { type: Number, default: 0 },
logical_core: { type: Number, default: 0 },
@@ -13,7 +84,7 @@ const cpuSchema = new mongoose.Schema(
{ _id: false }
);
const memorySchema = new mongoose.Schema(
const memorySchema = new Schema<CheckMemoryInfo>(
{
total_bytes: { type: Number, default: 0 },
available_bytes: { type: Number, default: 0 },
@@ -23,7 +94,7 @@ const memorySchema = new mongoose.Schema(
{ _id: false }
);
const diskSchema = new mongoose.Schema(
const diskSchema = new Schema<CheckDiskInfo>(
{
device: { type: String, default: "" },
mountpoint: { type: String, default: "" },
@@ -36,7 +107,7 @@ const diskSchema = new mongoose.Schema(
{ _id: false }
);
const hostSchema = new mongoose.Schema(
const hostSchema = new Schema<CheckHostInfo>(
{
os: { type: String, default: "" },
platform: { type: String, default: "" },
@@ -45,7 +116,7 @@ const hostSchema = new mongoose.Schema(
{ _id: false }
);
const errorSchema = new mongoose.Schema(
const errorSchema = new Schema<CheckErrorInfo>(
{
metric: { type: [String], default: [] },
err: { type: String, default: "" },
@@ -53,7 +124,7 @@ const errorSchema = new mongoose.Schema(
{ _id: false }
);
const captureSchema = new mongoose.Schema(
const captureSchema = new Schema<CheckCaptureInfo>(
{
version: { type: String, default: "" },
mode: { type: String, default: "" },
@@ -61,9 +132,9 @@ const captureSchema = new mongoose.Schema(
{ _id: false }
);
const networkInterfaceSchema = new mongoose.Schema(
const networkInterfaceSchema = new Schema<CheckNetworkInterfaceInfo>(
{
name: { type: String },
name: { type: String, default: "" },
bytes_sent: { type: Number, default: 0 },
bytes_recv: { type: Number, default: 0 },
packets_sent: { type: Number, default: 0 },
@@ -78,17 +149,30 @@ const networkInterfaceSchema = new mongoose.Schema(
{ _id: false }
);
const metadataSchema = new mongoose.Schema(
const auditsSchema = new Schema<CheckAudits>(
{
cls: { type: Number, default: 0 },
si: { type: Number, default: 0 },
fcp: { type: Number, default: 0 },
lcp: { type: Number, default: 0 },
tbt: { type: Number, default: 0 },
},
{ _id: false }
);
const metadataSchema = new Schema<CheckMetadataDocument>(
{
monitorId: {
type: mongoose.Schema.Types.ObjectId,
type: Schema.Types.ObjectId,
ref: "Monitor",
required: true,
immutable: true,
index: true,
},
teamId: {
type: mongoose.Schema.Types.ObjectId,
type: Schema.Types.ObjectId,
ref: "Team",
required: true,
immutable: true,
index: true,
},
@@ -102,7 +186,7 @@ const metadataSchema = new mongoose.Schema(
{ _id: false }
);
const CheckSchema = new mongoose.Schema(
const CheckSchema = new Schema<CheckDocument>(
{
metadata: {
type: metadataSchema,
@@ -112,40 +196,32 @@ const CheckSchema = new mongoose.Schema(
type: Boolean,
index: true,
},
responseTime: {
type: Number,
},
timings: {
type: Object,
default: {},
type: timingsSchema,
default: undefined,
},
statusCode: {
type: Number,
index: true,
},
message: {
type: String,
},
expiry: {
type: Date,
default: Date.now,
},
ack: {
type: Boolean,
default: false,
},
ackAt: {
type: Date,
default: undefined,
},
// Hardware fields
cpu: {
type: cpuSchema,
default: () => ({}),
@@ -162,23 +238,18 @@ const CheckSchema = new mongoose.Schema(
type: hostSchema,
default: () => ({}),
},
errors: {
type: [errorSchema],
default: () => [],
},
capture: {
type: captureSchema,
default: () => ({}),
},
net: {
type: [networkInterfaceSchema],
default: () => [],
},
// PageSpeed fields
accessibility: {
type: Number,
},
@@ -192,7 +263,8 @@ const CheckSchema = new mongoose.Schema(
type: Number,
},
audits: {
type: Object,
type: auditsSchema,
default: undefined,
},
},
{
@@ -211,4 +283,8 @@ CheckSchema.index({ "metadata.monitorId": 1, updatedAt: 1 });
CheckSchema.index({ "metadata.monitorId": 1, updatedAt: -1 });
CheckSchema.index({ "metadata.teamId": 1, updatedAt: -1 });
export default mongoose.model("Check", CheckSchema);
const CheckModel = model<CheckDocument>("Check", CheckSchema);
export type { CheckDocument, CheckMetadataDocument };
export { CheckModel };
export default CheckModel;
+48 -24
View File
@@ -1,18 +1,49 @@
import mongoose from "mongoose";
import { Schema, model, Types, type UpdateQuery } from "mongoose";
import type { Monitor, MonitorMatchMethod, MonitorThresholds } from "@/types/monitor.js";
import { MonitorTypes } from "@/types/monitor.js";
import Check from "./Check.js";
import MonitorStats from "./MonitorStats.js";
import StatusPage from "./StatusPage.js";
const MonitorSchema = mongoose.Schema(
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>(
{
userId: {
type: mongoose.Schema.Types.ObjectId,
type: Schema.Types.ObjectId,
ref: "User",
immutable: true,
required: true,
},
teamId: {
type: mongoose.Schema.Types.ObjectId,
type: Schema.Types.ObjectId,
ref: "Team",
immutable: true,
required: true,
@@ -43,7 +74,7 @@ const MonitorSchema = mongoose.Schema(
type: {
type: String,
required: true,
enum: ["http", "ping", "pagespeed", "hardware", "docker", "port", "game"],
enum: MonitorTypes,
},
ignoreTlsErrors: {
type: Boolean,
@@ -71,7 +102,6 @@ const MonitorSchema = mongoose.Schema(
default: true,
},
interval: {
// in milliseconds
type: Number,
default: 60000,
},
@@ -81,7 +111,7 @@ const MonitorSchema = mongoose.Schema(
},
notifications: [
{
type: mongoose.Schema.Types.ObjectId,
type: Schema.Types.ObjectId,
ref: "Notification",
},
],
@@ -89,13 +119,7 @@ const MonitorSchema = mongoose.Schema(
type: String,
},
thresholds: {
type: {
usage_cpu: { type: Number },
usage_memory: { type: Number },
usage_disk: { type: Number },
usage_temperature: { type: Number },
},
_id: false,
type: thresholdsSchema,
},
alertThreshold: {
type: Number,
@@ -137,7 +161,7 @@ const MonitorSchema = mongoose.Schema(
trim: true,
maxLength: 50,
default: null,
set: function (value) {
set(value: string | null) {
return value && value.trim() ? value.trim() : null;
},
},
@@ -148,7 +172,6 @@ const MonitorSchema = mongoose.Schema(
);
MonitorSchema.pre("findOneAndDelete", async function (next) {
// Delete checks and stats
try {
const doc = await this.model.findOne(this.getFilter());
@@ -157,20 +180,17 @@ MonitorSchema.pre("findOneAndDelete", async function (next) {
}
await Check.deleteMany({ monitorId: doc._id });
// Deal with status pages
await StatusPage.updateMany({ monitors: doc?._id }, { $pull: { monitors: doc?._id } });
await MonitorStats.deleteMany({ monitorId: doc?._id.toString() });
next();
} catch (error) {
next(error);
next(error as Error);
}
});
MonitorSchema.pre("deleteMany", async function (next) {
const filter = this.getFilter();
const monitors = await this.model.find(filter).select(["_id", "type"]).lean();
const monitors = (await this.model.find(filter).select(["_id", "type"]).lean()) as { _id: Types.ObjectId }[];
for (const monitor of monitors) {
await Check.deleteMany({ monitorId: monitor._id });
@@ -197,8 +217,8 @@ MonitorSchema.pre("save", function (next) {
});
MonitorSchema.pre("findOneAndUpdate", function (next) {
const update = this.getUpdate();
if (update.alertThreshold) {
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;
@@ -209,4 +229,8 @@ MonitorSchema.pre("findOneAndUpdate", function (next) {
MonitorSchema.index({ teamId: 1, type: 1 });
export default mongoose.model("Monitor", MonitorSchema);
const MonitorModel = model<MonitorDocument>("Monitor", MonitorSchema);
export type { MonitorDocument };
export { MonitorModel };
export default MonitorModel;
+5
View File
@@ -0,0 +1,5 @@
export * from "@/db/models/Monitor.js";
export { default as MonitorModel } from "@/db/models/Monitor.js";
export * from "@/db/models/Check.js";
export { default as CheckModel } from "@/db/models/Check.js";
+25 -18
View File
@@ -1,4 +1,6 @@
import { ObjectId } from "mongodb";
import mongoose from "mongoose";
import { CheckModel } from "@/db/models/index.js";
import { buildChecksSummaryByTeamIdPipeline } from "./checkModuleQueries.js";
const SERVICE_NAME = "checkModule";
@@ -12,18 +14,15 @@ const dateRangeLookup = {
};
class CheckModule {
constructor({ logger, Check, HardwareCheck, PageSpeedCheck, Monitor, User }) {
constructor({ logger, Monitor, User }) {
this.logger = logger;
this.Check = Check;
this.HardwareCheck = HardwareCheck;
this.PageSpeedCheck = PageSpeedCheck;
this.Monitor = Monitor;
this.User = User;
}
createChecks = async (checks) => {
try {
await this.Check.insertMany(checks, { ordered: false });
await CheckModel.insertMany(checks, { ordered: false });
} catch (error) {
error.service = SERVICE_NAME;
error.method = "createCheck";
@@ -41,7 +40,7 @@ class CheckModule {
// Match
const matchStage = {
monitorId: new ObjectId(monitorId),
"metadata.monitorId": new ObjectId(monitorId),
...(typeof status !== "undefined" && { status }),
...(typeof ack !== "undefined" && ackStage),
...(dateRangeLookup[dateRange] && {
@@ -79,7 +78,7 @@ class CheckModule {
skip = page * rowsPerPage;
}
const checks = await this.Check.aggregate([
const checks = await CheckModel.aggregate([
{ $match: matchStage },
{ $sort: { createdAt: sortOrder } },
{
@@ -115,7 +114,7 @@ class CheckModule {
const ackStage = ack === "true" ? { ack: true } : { $or: [{ ack: false }, { ack: { $exists: false } }] };
const matchStage = {
teamId: new ObjectId(teamId),
"metadata.teamId": new ObjectId(teamId),
status: false,
...(typeof ack !== "undefined" && ackStage),
...(dateRangeLookup[dateRange] && {
@@ -170,7 +169,7 @@ class CheckModule {
},
];
const checks = await this.Check.aggregate(aggregatePipeline);
const checks = await CheckModel.aggregate(aggregatePipeline);
return checks[0];
} catch (error) {
error.service = SERVICE_NAME;
@@ -181,7 +180,11 @@ class CheckModule {
ackCheck = async (checkId, teamId, ack) => {
try {
const updatedCheck = await this.Check.findOneAndUpdate({ _id: checkId, teamId: teamId }, { $set: { ack, ackAt: new Date() } }, { new: true });
const updatedCheck = await CheckModel.findOneAndUpdate(
{ _id: checkId, "metadata.teamId": teamId },
{ $set: { ack, ackAt: new Date() } },
{ new: true }
);
if (!updatedCheck) {
throw new Error("Check not found");
@@ -197,7 +200,11 @@ class CheckModule {
ackAllChecks = async (monitorId, teamId, ack, path) => {
try {
const updatedChecks = await this.Check.updateMany(path === "monitor" ? { monitorId } : { teamId }, { $set: { ack, ackAt: new Date() } });
const filter =
path === "monitor"
? { "metadata.monitorId": new mongoose.Types.ObjectId(monitorId) }
: { "metadata.teamId": new mongoose.Types.ObjectId(teamId) };
const updatedChecks = await CheckModel.updateMany(filter, { $set: { ack, ackAt: new Date() } });
return updatedChecks.modifiedCount;
} catch (error) {
error.service = SERVICE_NAME;
@@ -209,9 +216,9 @@ class CheckModule {
getChecksSummaryByTeamId = async ({ teamId }) => {
try {
const matchStage = {
teamId: new ObjectId(teamId),
"metadata.teamId": new ObjectId(teamId),
};
const checks = await this.Check.aggregate(buildChecksSummaryByTeamIdPipeline({ matchStage }));
const checks = await CheckModel.aggregate(buildChecksSummaryByTeamIdPipeline({ matchStage }));
return checks[0].summary;
} catch (error) {
error.service = SERVICE_NAME;
@@ -221,7 +228,7 @@ class CheckModule {
};
deleteChecks = async (monitorId) => {
try {
const result = await this.Check.deleteMany({ monitorId });
const result = await CheckModel.deleteMany({ "metadata.monitorId": monitorId });
return result.deletedCount;
} catch (error) {
error.service = SERVICE_NAME;
@@ -237,7 +244,7 @@ class CheckModule {
const monitorIds = teamMonitors.map((monitor) => monitor._id);
// Delete all checks for these monitors in one operation
const deleteResult = await this.Check.deleteMany({ monitorId: { $in: monitorIds } });
const deleteResult = await CheckModel.deleteMany({ "metadata.monitorId": { $in: monitorIds } });
return deleteResult.deletedCount;
} catch (error) {
@@ -249,7 +256,7 @@ class CheckModule {
updateChecksTTL = async (teamId, ttl) => {
try {
await this.Check.collection.dropIndex("expiry_1");
await CheckModel.collection.dropIndex("expiry_1");
} catch (error) {
this.logger.error({
message: error.message,
@@ -260,9 +267,9 @@ class CheckModule {
}
try {
await this.Check.collection.createIndex(
await CheckModel.collection.createIndex(
{ expiry: 1 },
{ expireAfterSeconds: ttl } // TTL in seconds, adjust as necessary
{ expireAfterSeconds: ttl, partialFilterExpression: { "metadata.mode": { $exists: true } } }
);
} catch (error) {
error.service = SERVICE_NAME;
+14 -9
View File
@@ -8,13 +8,14 @@ import {
buildFilteredMonitorsByTeamIdPipeline,
} from "./monitorModuleQueries.js";
import { CheckModel } from "@/db/models/index.js";
const SERVICE_NAME = "monitorModule";
class MonitorModule {
constructor({ Monitor, MonitorStats, Check, stringService, fs, path, fileURLToPath, ObjectId, NormalizeData, NormalizeDataUptimeDetails }) {
constructor({ Monitor, MonitorStats, stringService, fs, path, fileURLToPath, ObjectId, NormalizeData, NormalizeDataUptimeDetails }) {
this.Monitor = Monitor;
this.MonitorStats = MonitorStats;
this.Check = Check;
this.stringService = stringService;
this.fs = fs;
this.path = path;
@@ -123,15 +124,18 @@ class MonitorModule {
//Helper
getMonitorChecks = async (monitorId, dateRange, sortOrder) => {
const objectId = new this.ObjectId(monitorId);
const indexSpec = {
monitorId: 1,
updatedAt: sortOrder, // This will be 1 or -1
"metadata.monitorId": 1,
updatedAt: sortOrder,
};
const matchBase = { "metadata.monitorId": objectId };
const [checksAll, checksForDateRange] = await Promise.all([
this.Check.find({ monitorId }).sort({ createdAt: sortOrder }).hint(indexSpec).lean(),
this.Check.find({
monitorId,
CheckModel.find(matchBase).sort({ createdAt: sortOrder }).hint(indexSpec).lean(),
CheckModel.find({
...matchBase,
createdAt: { $gte: dateRange.start, $lte: dateRange.end },
})
.hint(indexSpec)
@@ -238,7 +242,8 @@ class MonitorModule {
const dateString = formatLookup[dateRange];
const results = await this.Check.aggregate(buildUptimeDetailsPipeline(monitorId, dates, dateString));
console.log("WTTTTFFF");
const results = await CheckModel.aggregate(buildUptimeDetailsPipeline(monitorId, dates, dateString));
const monitorData = results[0];
@@ -313,7 +318,7 @@ class MonitorModule {
};
const dateString = formatLookup[dateRange];
const hardwareStats = await this.Check.aggregate(buildHardwareDetailsPipeline(monitor, dates, dateString));
const hardwareStats = await CheckModel.aggregate(buildHardwareDetailsPipeline(monitor, dates, dateString));
const stats = hardwareStats[0];
+13 -8
View File
@@ -4,7 +4,7 @@ const buildUptimeDetailsPipeline = (monitorId, dates, dateString) => {
return [
{
$match: {
monitorId: new ObjectId(monitorId),
"metadata.monitorId": new ObjectId(monitorId),
updatedAt: { $gte: dates.start, $lte: dates.end },
},
},
@@ -172,8 +172,8 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
return [
{
$match: {
monitorId: monitor._id,
type: "hardware",
"metadata.monitorId": monitor._id,
"metadata.type": "hardware",
createdAt: { $gte: dates.start, $lte: dates.end },
},
},
@@ -233,11 +233,16 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
},
pipeline: [
{
$match: {
$expr: {
$and: [{ $eq: ["$monitorId", monitor._id] }, { $gte: ["$createdAt", dates.start] }, { $lte: ["$createdAt", dates.end] }],
},
},
$match: {
$expr: {
$and: [
{ $eq: ["$metadata.monitorId", monitor._id] },
{ $eq: ["$metadata.type", "hardware"] },
{ $gte: ["$createdAt", dates.start] },
{ $lte: ["$createdAt", dates.end] },
],
},
},
},
{
$group: {