module -> class

This commit is contained in:
Alex Holliday committed 2025-07-30 12:25:12 -07:00
1 parent b383ce5b11
commit de742068a7
11 files changed
+580 -714

No files matched your search

+21 -2
View File
@@ -32,8 +32,11 @@ import mjml2html from "mjml";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import crypto from "crypto"; import crypto from "crypto";
import { fileURLToPath } from "url";
import { ObjectId } from "mongodb";
// DB Modules // DB Modules
import { NormalizeData } from "../utils/dataUtils.js"; import { NormalizeData, NormalizeDataUptimeDetails } from "../utils/dataUtils.js";
import { GenerateAvatarImage } from "../utils/imageProcessing.js"; import { GenerateAvatarImage } from "../utils/imageProcessing.js";
import { ParseBoolean } from "../utils/utils.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 StatusPage from "../db/models/StatusPage.js";
import Team from "../db/models/Team.js"; import Team from "../db/models/Team.js";
import MaintenanceWindow from "../db/models/MaintenanceWindow.js"; import MaintenanceWindow from "../db/models/MaintenanceWindow.js";
import MonitorStats from "../db/models/MonitorStats.js";
import InviteModule from "../db/mongo/modules/inviteModule.js"; import InviteModule from "../db/mongo/modules/inviteModule.js";
import CheckModule from "../db/mongo/modules/checkModule.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 UserModule from "../db/mongo/modules/userModule.js";
import HardwareCheckModule from "../db/mongo/modules/hardwareCheckModule.js"; import HardwareCheckModule from "../db/mongo/modules/hardwareCheckModule.js";
import MaintenanceWindowModule from "../db/mongo/modules/maintenanceWindowModule.js"; import MaintenanceWindowModule from "../db/mongo/modules/maintenanceWindowModule.js";
import MonitorModule from "../db/mongo/modules/monitorModule.js";
export const initializeServices = async ({ logger, envSettings, settingsService }) => { export const initializeServices = async ({ logger, envSettings, settingsService }) => {
const serviceRegistry = new ServiceRegistry({ logger }); const serviceRegistry = new ServiceRegistry({ logger });
ServiceRegistry.instance = serviceRegistry; ServiceRegistry.instance = serviceRegistry;
@@ -71,6 +75,20 @@ export const initializeServices = async ({ logger, envSettings, settingsService
const userModule = new UserModule({ User, Team, GenerateAvatarImage, ParseBoolean, stringService }); const userModule = new UserModule({ User, Team, GenerateAvatarImage, ParseBoolean, stringService });
const hardwareCheckModule = new HardwareCheckModule({ HardwareCheck, Monitor, logger }); const hardwareCheckModule = new HardwareCheckModule({ HardwareCheck, Monitor, logger });
const maintenanceWindowModule = new MaintenanceWindowModule({ MaintenanceWindow }); 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({ const db = new MongoDB({
logger, logger,
envSettings, envSettings,
@@ -80,6 +98,7 @@ export const initializeServices = async ({ logger, envSettings, settingsService
userModule, userModule,
hardwareCheckModule, hardwareCheckModule,
maintenanceWindowModule, maintenanceWindowModule,
monitorModule,
}); });
await db.connect(); await db.connect();
+2 -2
View File
@@ -32,7 +32,7 @@ class MonitorController extends BaseController {
} }
async verifyTeamAccess(teamId, monitorId) { async verifyTeamAccess(teamId, monitorId) {
const monitor = await this.db.getMonitorById(monitorId); const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor.teamId.equals(teamId)) { if (!monitor.teamId.equals(teamId)) {
throw this.errorService.createAuthorizationError(); throw this.errorService.createAuthorizationError();
} }
@@ -150,7 +150,7 @@ class MonitorController extends BaseController {
await getCertificateParamValidation.validateAsync(req.params); await getCertificateParamValidation.validateAsync(req.params);
const { monitorId } = 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); const certificate = await fetchMonitorCertificate(sslChecker, monitor);
return res.success({ return res.success({
@@ -158,7 +158,7 @@ class NotificationController extends BaseController {
throw this.errorService.createBadRequestError("Team ID is required"); 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)) { if (!monitor.teamId.equals(teamId)) {
throw this.errorService.createAuthorizationError(); throw this.errorService.createAuthorizationError();
+12 -12
View File
@@ -6,23 +6,12 @@ import AppSettings from "../models/AppSettings.js";
//**************************************** //****************************************
import * as recoveryModule from "./modules/recoveryModule.js"; import * as recoveryModule from "./modules/recoveryModule.js";
//****************************************
// Monitors
//****************************************
import * as monitorModule from "./modules/monitorModule.js";
//**************************************** //****************************************
// Page Speed Checks // Page Speed Checks
//**************************************** //****************************************
import * as pageSpeedCheckModule from "./modules/pageSpeedCheckModule.js"; import * as pageSpeedCheckModule from "./modules/pageSpeedCheckModule.js";
//****************************************
// Maintenance Window
//****************************************
import * as maintenanceWindowModule from "./modules/maintenanceWindowModule.js";
//**************************************** //****************************************
// Notifications // Notifications
//**************************************** //****************************************
@@ -41,7 +30,17 @@ import * as diagnosticModule from "./modules/diagnosticModule.js";
class MongoDB { class MongoDB {
static SERVICE_NAME = "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.logger = logger;
this.envSettings = envSettings; this.envSettings = envSettings;
this.userModule = userModule; this.userModule = userModule;
@@ -52,6 +51,7 @@ class MongoDB {
this.hardwareCheckModule = hardwareCheckModule; this.hardwareCheckModule = hardwareCheckModule;
this.checkModule = checkModule; this.checkModule = checkModule;
this.maintenanceWindowModule = maintenanceWindowModule; this.maintenanceWindowModule = maintenanceWindowModule;
this.monitorModule = monitorModule;
Object.assign(this, notificationModule); Object.assign(this, notificationModule);
Object.assign(this, settingsModule); Object.assign(this, settingsModule);
this.statusPageModule = statusPageModule; this.statusPageModule = statusPageModule;
File diff suppressed because it is too large. Load diff
+3 -3
View File
@@ -23,7 +23,7 @@ class CheckService {
throw this.errorService.createBadRequestError("No team ID in request"); 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) { if (!monitor) {
throw this.errorService.createNotFoundError("Monitor not found"); throw this.errorService.createNotFoundError("Monitor not found");
@@ -95,7 +95,7 @@ class CheckService {
throw this.errorService.createBadRequestError("No monitor ID in request"); 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) { if (!monitor) {
throw this.errorService.createNotFoundError("Monitor not found"); throw this.errorService.createNotFoundError("Monitor not found");
} }
@@ -118,7 +118,7 @@ class CheckService {
throw this.errorService.createBadRequestError("No team ID in request"); 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) { if (!monitor) {
throw this.errorService.createNotFoundError("Monitor not found"); throw this.errorService.createNotFoundError("Monitor not found");
@@ -16,7 +16,7 @@ class MaintenanceWindowService {
createMaintenanceWindow = async ({ teamId, body }) => { createMaintenanceWindow = async ({ teamId, body }) => {
const monitorIds = body.monitors; 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)); const unauthorizedMonitors = monitors.filter((monitor) => !monitor.teamId.equals(teamId));
+10 -10
View File
@@ -20,20 +20,20 @@ class MonitorService {
} }
verifyTeamAccess = async ({ teamId, monitorId }) => { verifyTeamAccess = async ({ teamId, monitorId }) => {
const monitor = await this.db.getMonitorById(monitorId); const monitor = await this.db.monitorModule.getMonitorById(monitorId);
if (!monitor?.teamId?.equals(teamId)) { if (!monitor?.teamId?.equals(teamId)) {
throw this.errorService.createAuthorizationError(); throw this.errorService.createAuthorizationError();
} }
}; };
getAllMonitors = async () => { getAllMonitors = async () => {
const monitors = await this.db.getAllMonitors(); const monitors = await this.db.monitorModule.getAllMonitors();
return monitors; return monitors;
}; };
getUptimeDetailsById = async ({ teamId, monitorId, dateRange, normalize }) => { getUptimeDetailsById = async ({ teamId, monitorId, dateRange, normalize }) => {
await this.verifyTeamAccess({ teamId, monitorId }); await this.verifyTeamAccess({ teamId, monitorId });
const data = await this.db.getUptimeDetailsById({ const data = await this.db.monitorModule.getUptimeDetailsById({
monitorId, monitorId,
dateRange, dateRange,
normalize, normalize,
@@ -44,7 +44,7 @@ class MonitorService {
getMonitorStatsById = async ({ teamId, monitorId, limit, sortOrder, dateRange, numToDisplay, normalize }) => { getMonitorStatsById = async ({ teamId, monitorId, limit, sortOrder, dateRange, numToDisplay, normalize }) => {
await this.verifyTeamAccess({ teamId, monitorId }); await this.verifyTeamAccess({ teamId, monitorId });
const monitorStats = await this.db.getMonitorStatsById({ const monitorStats = await this.db.monitorModule.getMonitorStatsById({
monitorId, monitorId,
limit, limit,
sortOrder, sortOrder,
@@ -58,14 +58,14 @@ class MonitorService {
getHardwareDetailsById = async ({ teamId, monitorId, dateRange }) => { getHardwareDetailsById = async ({ teamId, monitorId, dateRange }) => {
await this.verifyTeamAccess({ teamId, monitorId }); await this.verifyTeamAccess({ teamId, monitorId });
const monitor = await this.db.getHardwareDetailsById({ monitorId, dateRange }); const monitor = await this.db.monitorModule.getHardwareDetailsById({ monitorId, dateRange });
return monitor; return monitor;
}; };
getMonitorById = async ({ teamId, monitorId }) => { getMonitorById = async ({ teamId, monitorId }) => {
await this.verifyTeamAccess({ teamId, monitorId }); await this.verifyTeamAccess({ teamId, monitorId });
const monitor = await this.db.getMonitorById(monitorId); const monitor = await this.db.monitorModule.getMonitorById(monitorId);
return monitor; return monitor;
}; };
@@ -203,7 +203,7 @@ class MonitorService {
}; };
getMonitorsByTeamId = async ({ teamId, limit, type, page, rowsPerPage, filter, field, order }) => { getMonitorsByTeamId = async ({ teamId, limit, type, page, rowsPerPage, filter, field, order }) => {
const monitors = await this.db.getMonitorsByTeamId({ const monitors = await this.db.monitorModule.getMonitorsByTeamId({
limit, limit,
type, type,
page, page,
@@ -217,7 +217,7 @@ class MonitorService {
}; };
getMonitorsAndSummaryByTeamId = async ({ teamId, type, explain }) => { getMonitorsAndSummaryByTeamId = async ({ teamId, type, explain }) => {
const result = await this.db.getMonitorsAndSummaryByTeamId({ const result = await this.db.monitorModule.getMonitorsAndSummaryByTeamId({
type, type,
explain, explain,
teamId, teamId,
@@ -226,7 +226,7 @@ class MonitorService {
}; };
getMonitorsWithChecksByTeamId = async ({ teamId, limit, type, page, rowsPerPage, filter, field, order, explain }) => { 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, limit,
type, type,
page, page,
@@ -241,7 +241,7 @@ class MonitorService {
}; };
exportMonitorsToCSV = async ({ teamId }) => { exportMonitorsToCSV = async ({ teamId }) => {
const monitors = await this.db.getMonitorsByTeamId({ teamId }); const monitors = await this.db.monitorModule.getMonitorsByTeamId({ teamId });
if (!monitors || monitors.length === 0) { if (!monitors || monitors.length === 0) {
throw this.errorService.createNotFoundError("No monitors to export"); throw this.errorService.createNotFoundError("No monitors to export");
+1 -1
View File
@@ -181,7 +181,7 @@ class UserService {
} }
// 1. Find all the monitors associated with the team ID if superadmin // 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, teamId: teamId,
}); });
@@ -33,7 +33,7 @@ class SuperSimpleQueue {
this.scheduler.start(); this.scheduler.start();
this.scheduler.addTemplate("monitor-job", this.helper.getMonitorJob()); 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) { for (const monitor of monitors) {
await this.addJob(monitor._id, monitor); await this.addJob(monitor._id, monitor);
} }
@@ -122,7 +122,7 @@ class StatusService {
this.insertCheck(networkResponse); this.insertCheck(networkResponse);
try { try {
const { monitorId, status, code } = networkResponse; const { monitorId, status, code } = networkResponse;
const monitor = await this.db.getMonitorById(monitorId); const monitor = await this.db.monitorModule.getMonitorById(monitorId);
// Update running stats // Update running stats
this.updateRunningStats({ monitor, networkResponse }); this.updateRunningStats({ monitor, networkResponse });