module -> class

This commit is contained in:
Alex Holliday
2025-07-30 12:25:12 -07:00
parent b383ce5b11
commit de742068a7
11 changed files with 580 additions and 714 deletions

View File

@@ -32,8 +32,11 @@ import mjml2html from "mjml";
import jwt from "jsonwebtoken";
import crypto from "crypto";
import { fileURLToPath } from "url";
import { ObjectId } from "mongodb";
// DB Modules
import { NormalizeData } from "../utils/dataUtils.js";
import { NormalizeData, NormalizeDataUptimeDetails } from "../utils/dataUtils.js";
import { GenerateAvatarImage } from "../utils/imageProcessing.js";
import { ParseBoolean } from "../utils/utils.js";
@@ -47,6 +50,7 @@ import InviteToken from "../db/models/InviteToken.js";
import StatusPage from "../db/models/StatusPage.js";
import Team from "../db/models/Team.js";
import MaintenanceWindow from "../db/models/MaintenanceWindow.js";
import MonitorStats from "../db/models/MonitorStats.js";
import InviteModule from "../db/mongo/modules/inviteModule.js";
import CheckModule from "../db/mongo/modules/checkModule.js";
@@ -54,7 +58,7 @@ import StatusPageModule from "../db/mongo/modules/statusPageModule.js";
import UserModule from "../db/mongo/modules/userModule.js";
import HardwareCheckModule from "../db/mongo/modules/hardwareCheckModule.js";
import MaintenanceWindowModule from "../db/mongo/modules/maintenanceWindowModule.js";
import MonitorModule from "../db/mongo/modules/monitorModule.js";
export const initializeServices = async ({ logger, envSettings, settingsService }) => {
const serviceRegistry = new ServiceRegistry({ logger });
ServiceRegistry.instance = serviceRegistry;
@@ -71,6 +75,20 @@ export const initializeServices = async ({ logger, envSettings, settingsService
const userModule = new UserModule({ User, Team, GenerateAvatarImage, ParseBoolean, stringService });
const hardwareCheckModule = new HardwareCheckModule({ HardwareCheck, Monitor, logger });
const maintenanceWindowModule = new MaintenanceWindowModule({ MaintenanceWindow });
const monitorModule = new MonitorModule({
Monitor,
MonitorStats,
Check,
PageSpeedCheck,
HardwareCheck,
stringService,
fs,
path,
fileURLToPath,
ObjectId,
NormalizeData,
NormalizeDataUptimeDetails,
});
const db = new MongoDB({
logger,
envSettings,
@@ -80,6 +98,7 @@ export const initializeServices = async ({ logger, envSettings, settingsService
userModule,
hardwareCheckModule,
maintenanceWindowModule,
monitorModule,
});
await db.connect();

View File

@@ -32,7 +32,7 @@ class MonitorController extends BaseController {
}
async verifyTeamAccess(teamId, monitorId) {
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor.teamId.equals(teamId)) {
throw this.errorService.createAuthorizationError();
}
@@ -150,7 +150,7 @@ class MonitorController extends BaseController {
await getCertificateParamValidation.validateAsync(req.params);
const { monitorId } = req.params;
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
return res.success({

View File

@@ -158,7 +158,7 @@ class NotificationController extends BaseController {
throw this.errorService.createBadRequestError("Team ID is required");
}
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor.teamId.equals(teamId)) {
throw this.errorService.createAuthorizationError();

View File

@@ -6,23 +6,12 @@ import AppSettings from "../models/AppSettings.js";
//****************************************
import * as recoveryModule from "./modules/recoveryModule.js";
//****************************************
// Monitors
//****************************************
import * as monitorModule from "./modules/monitorModule.js";
//****************************************
// Page Speed Checks
//****************************************
import * as pageSpeedCheckModule from "./modules/pageSpeedCheckModule.js";
//****************************************
// Maintenance Window
//****************************************
import * as maintenanceWindowModule from "./modules/maintenanceWindowModule.js";
//****************************************
// Notifications
//****************************************
@@ -41,7 +30,17 @@ import * as diagnosticModule from "./modules/diagnosticModule.js";
class MongoDB {
static SERVICE_NAME = "MongoDB";
constructor({ logger, envSettings, checkModule, inviteModule, statusPageModule, userModule, hardwareCheckModule, maintenanceWindowModule }) {
constructor({
logger,
envSettings,
checkModule,
inviteModule,
statusPageModule,
userModule,
hardwareCheckModule,
maintenanceWindowModule,
monitorModule,
}) {
this.logger = logger;
this.envSettings = envSettings;
this.userModule = userModule;
@@ -52,6 +51,7 @@ class MongoDB {
this.hardwareCheckModule = hardwareCheckModule;
this.checkModule = checkModule;
this.maintenanceWindowModule = maintenanceWindowModule;
this.monitorModule = monitorModule;
Object.assign(this, notificationModule);
Object.assign(this, settingsModule);
this.statusPageModule = statusPageModule;

File diff suppressed because it is too large Load Diff

View File

@@ -23,7 +23,7 @@ class CheckService {
throw this.errorService.createBadRequestError("No team ID in request");
}
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor) {
throw this.errorService.createNotFoundError("Monitor not found");
@@ -95,7 +95,7 @@ class CheckService {
throw this.errorService.createBadRequestError("No monitor ID in request");
}
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor) {
throw this.errorService.createNotFoundError("Monitor not found");
}
@@ -118,7 +118,7 @@ class CheckService {
throw this.errorService.createBadRequestError("No team ID in request");
}
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor) {
throw this.errorService.createNotFoundError("Monitor not found");

View File

@@ -16,7 +16,7 @@ class MaintenanceWindowService {
createMaintenanceWindow = async ({ teamId, body }) => {
const monitorIds = body.monitors;
const monitors = await this.db.getMonitorsByIds(monitorIds);
const monitors = await this.db.monitorModule.getMonitorsByIds(monitorIds);
const unauthorizedMonitors = monitors.filter((monitor) => !monitor.teamId.equals(teamId));

View File

@@ -20,20 +20,20 @@ class MonitorService {
}
verifyTeamAccess = async ({ teamId, monitorId }) => {
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor?.teamId?.equals(teamId)) {
throw this.errorService.createAuthorizationError();
}
};
getAllMonitors = async () => {
const monitors = await this.db.getAllMonitors();
const monitors = await this.db.monitorModule.getAllMonitors();
return monitors;
};
getUptimeDetailsById = async ({ teamId, monitorId, dateRange, normalize }) => {
await this.verifyTeamAccess({ teamId, monitorId });
const data = await this.db.getUptimeDetailsById({
const data = await this.db.monitorModule.getUptimeDetailsById({
monitorId,
dateRange,
normalize,
@@ -44,7 +44,7 @@ class MonitorService {
getMonitorStatsById = async ({ teamId, monitorId, limit, sortOrder, dateRange, numToDisplay, normalize }) => {
await this.verifyTeamAccess({ teamId, monitorId });
const monitorStats = await this.db.getMonitorStatsById({
const monitorStats = await this.db.monitorModule.getMonitorStatsById({
monitorId,
limit,
sortOrder,
@@ -58,14 +58,14 @@ class MonitorService {
getHardwareDetailsById = async ({ teamId, monitorId, dateRange }) => {
await this.verifyTeamAccess({ teamId, monitorId });
const monitor = await this.db.getHardwareDetailsById({ monitorId, dateRange });
const monitor = await this.db.monitorModule.getHardwareDetailsById({ monitorId, dateRange });
return monitor;
};
getMonitorById = async ({ teamId, monitorId }) => {
await this.verifyTeamAccess({ teamId, monitorId });
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
return monitor;
};
@@ -203,7 +203,7 @@ class MonitorService {
};
getMonitorsByTeamId = async ({ teamId, limit, type, page, rowsPerPage, filter, field, order }) => {
const monitors = await this.db.getMonitorsByTeamId({
const monitors = await this.db.monitorModule.getMonitorsByTeamId({
limit,
type,
page,
@@ -217,7 +217,7 @@ class MonitorService {
};
getMonitorsAndSummaryByTeamId = async ({ teamId, type, explain }) => {
const result = await this.db.getMonitorsAndSummaryByTeamId({
const result = await this.db.monitorModule.getMonitorsAndSummaryByTeamId({
type,
explain,
teamId,
@@ -226,7 +226,7 @@ class MonitorService {
};
getMonitorsWithChecksByTeamId = async ({ teamId, limit, type, page, rowsPerPage, filter, field, order, explain }) => {
const result = await this.db.getMonitorsWithChecksByTeamId({
const result = await this.db.monitorModule.getMonitorsWithChecksByTeamId({
limit,
type,
page,
@@ -241,7 +241,7 @@ class MonitorService {
};
exportMonitorsToCSV = async ({ teamId }) => {
const monitors = await this.db.getMonitorsByTeamId({ teamId });
const monitors = await this.db.monitorModule.getMonitorsByTeamId({ teamId });
if (!monitors || monitors.length === 0) {
throw this.errorService.createNotFoundError("No monitors to export");

View File

@@ -181,7 +181,7 @@ class UserService {
}
// 1. Find all the monitors associated with the team ID if superadmin
const result = await this.db.getMonitorsByTeamId({
const result = await this.db.monitorModule.getMonitorsByTeamId({
teamId: teamId,
});

View File

@@ -33,7 +33,7 @@ class SuperSimpleQueue {
this.scheduler.start();
this.scheduler.addTemplate("monitor-job", this.helper.getMonitorJob());
const monitors = await this.db.getAllMonitors();
const monitors = await this.db.monitorModule.getAllMonitors();
for (const monitor of monitors) {
await this.addJob(monitor._id, monitor);
}

View File

@@ -122,7 +122,7 @@ class StatusService {
this.insertCheck(networkResponse);
try {
const { monitorId, status, code } = networkResponse;
const monitor = await this.db.getMonitorById(monitorId);
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
// Update running stats
this.updateRunningStats({ monitor, networkResponse });