monitorService types
This commit is contained in:
1 parent
bfbb4c390a
commit
bfd3eee94d
1 file changed
+39
-45
@@ -22,6 +22,8 @@ import type {
|
|||||||
import demoMonitorsData from "@/utils/demoMonitors.json" with { type: "json" };
|
import demoMonitorsData from "@/utils/demoMonitors.json" with { type: "json" };
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { ISuperSimpleQueue } from "../infrastructure/SuperSimpleQueue/SuperSimpleQueue.js";
|
import { ISuperSimpleQueue } from "../infrastructure/SuperSimpleQueue/SuperSimpleQueue.js";
|
||||||
|
import { IEmailService } from "../infrastructure/emailService.js";
|
||||||
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "MonitorService";
|
const SERVICE_NAME = "MonitorService";
|
||||||
type DateRangeKey = "recent" | "day" | "week" | "month" | "all";
|
type DateRangeKey = "recent" | "day" | "week" | "month" | "all";
|
||||||
@@ -31,7 +33,7 @@ export interface IMonitorService {
|
|||||||
|
|
||||||
// create
|
// create
|
||||||
createMonitor(teamId: string, userId: string, body: Partial<Monitor>): Promise<void>;
|
createMonitor(teamId: string, userId: string, body: Partial<Monitor>): Promise<void>;
|
||||||
createMonitors(monitors: Array<Monitor>, userId: string, teamId: string): Promise<Monitor[] | null>;
|
createMonitors(monitors: Array<Monitor>): Promise<Monitor[] | null>;
|
||||||
addDemoMonitors(args: { userId: string; teamId: string }): Promise<Monitor[]>;
|
addDemoMonitors(args: { userId: string; teamId: string }): Promise<Monitor[]>;
|
||||||
|
|
||||||
// read
|
// read
|
||||||
@@ -79,16 +81,16 @@ export interface IMonitorService {
|
|||||||
|
|
||||||
// other
|
// other
|
||||||
exportMonitorsToJSON(args: { teamId: string }): Promise<Monitor[]>;
|
exportMonitorsToJSON(args: { teamId: string }): Promise<Monitor[]>;
|
||||||
importMonitorsFromJSON(args: { teamId: string; userId: string; monitors: any[] }): Promise<{ imported: number; errors: string[] }>;
|
importMonitorsFromJSON(args: { teamId: string; userId: string; monitors: Monitor[] }): Promise<{ imported: number; errors: string[] }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MonitorService implements IMonitorService {
|
export class MonitorService implements IMonitorService {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private jobQueue: ISuperSimpleQueue;
|
private jobQueue: ISuperSimpleQueue;
|
||||||
private emailService: any;
|
private emailService: IEmailService;
|
||||||
private logger: any;
|
private logger: ILogger;
|
||||||
private games: any;
|
private games: GamesMap;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
private checksRepository: IChecksRepository;
|
private checksRepository: IChecksRepository;
|
||||||
private geoChecksRepository: IGeoChecksRepository;
|
private geoChecksRepository: IGeoChecksRepository;
|
||||||
@@ -109,9 +111,9 @@ export class MonitorService implements IMonitorService {
|
|||||||
incidentsRepository,
|
incidentsRepository,
|
||||||
}: {
|
}: {
|
||||||
jobQueue: ISuperSimpleQueue;
|
jobQueue: ISuperSimpleQueue;
|
||||||
emailService: any;
|
emailService: IEmailService;
|
||||||
logger: any;
|
logger: ILogger;
|
||||||
games: any;
|
games: GamesMap;
|
||||||
monitorsRepository: IMonitorsRepository;
|
monitorsRepository: IMonitorsRepository;
|
||||||
checksRepository: IChecksRepository;
|
checksRepository: IChecksRepository;
|
||||||
geoChecksRepository: IGeoChecksRepository;
|
geoChecksRepository: IGeoChecksRepository;
|
||||||
@@ -169,7 +171,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
this.jobQueue.addJob(monitor.id, monitor);
|
this.jobQueue.addJob(monitor.id, monitor);
|
||||||
};
|
};
|
||||||
|
|
||||||
createMonitors = async (monitors: Array<Monitor>, userId: string, teamId: string): Promise<Monitor[] | null> => {
|
createMonitors = async (monitors: Array<Monitor>): Promise<Monitor[] | null> => {
|
||||||
const createdMonitors = await this.monitorsRepository.createMonitors(monitors);
|
const createdMonitors = await this.monitorsRepository.createMonitors(monitors);
|
||||||
if (!monitors || monitors.length === 0) {
|
if (!monitors || monitors.length === 0) {
|
||||||
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createMonitors" });
|
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createMonitors" });
|
||||||
@@ -179,7 +181,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
return createdMonitors;
|
return createdMonitors;
|
||||||
};
|
};
|
||||||
|
|
||||||
addDemoMonitors = async ({ userId, teamId }: { userId: string; teamId: string }): Promise<any[]> => {
|
addDemoMonitors = async ({ userId, teamId }: { userId: string; teamId: string }): Promise<Monitor[]> => {
|
||||||
const demoData = demoMonitorsData;
|
const demoData = demoMonitorsData;
|
||||||
const monitors = demoData.map((monitor) => ({
|
const monitors = demoData.map((monitor) => ({
|
||||||
userId,
|
userId,
|
||||||
@@ -200,12 +202,10 @@ export class MonitorService implements IMonitorService {
|
|||||||
teamId,
|
teamId,
|
||||||
monitorId,
|
monitorId,
|
||||||
dateRange,
|
dateRange,
|
||||||
normalize,
|
|
||||||
}: {
|
}: {
|
||||||
teamId: string;
|
teamId: string;
|
||||||
monitorId: string;
|
monitorId: string;
|
||||||
dateRange: string;
|
dateRange: string;
|
||||||
normalize?: boolean;
|
|
||||||
}): Promise<UptimeDetailsResult> => {
|
}): Promise<UptimeDetailsResult> => {
|
||||||
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
|
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
|
||||||
if (!monitor) {
|
if (!monitor) {
|
||||||
@@ -386,7 +386,6 @@ export class MonitorService implements IMonitorService {
|
|||||||
filter,
|
filter,
|
||||||
field,
|
field,
|
||||||
order,
|
order,
|
||||||
explain,
|
|
||||||
}: {
|
}: {
|
||||||
teamId: string;
|
teamId: string;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
@@ -396,7 +395,6 @@ export class MonitorService implements IMonitorService {
|
|||||||
filter?: string;
|
filter?: string;
|
||||||
field?: string;
|
field?: string;
|
||||||
order?: "asc" | "desc";
|
order?: "asc" | "desc";
|
||||||
explain?: boolean;
|
|
||||||
}): Promise<MonitorsWithChecksByTeamIdResult> => {
|
}): Promise<MonitorsWithChecksByTeamIdResult> => {
|
||||||
const summary = await this.monitorsRepository.findMonitorsSummaryByTeamId(teamId, { type });
|
const summary = await this.monitorsRepository.findMonitorsSummaryByTeamId(teamId, { type });
|
||||||
const count = await this.monitorsRepository.findMonitorCountByTeamIdAndType(teamId, { type, filter });
|
const count = await this.monitorsRepository.findMonitorCountByTeamIdAndType(teamId, { type, filter });
|
||||||
@@ -443,47 +441,51 @@ export class MonitorService implements IMonitorService {
|
|||||||
|
|
||||||
pauseMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
pauseMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
||||||
const monitor = await this.monitorsRepository.togglePauseById(monitorId, teamId);
|
const monitor = await this.monitorsRepository.togglePauseById(monitorId, teamId);
|
||||||
monitor.isActive === true ? await this.jobQueue.resumeJob(monitor) : await this.jobQueue.pauseJob(monitor);
|
if (monitor.isActive) {
|
||||||
|
await this.jobQueue.resumeJob(monitor);
|
||||||
|
} else {
|
||||||
|
await this.jobQueue.pauseJob(monitor);
|
||||||
|
}
|
||||||
return monitor;
|
return monitor;
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
deleteMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
||||||
const monitor = await this.monitorsRepository.deleteById(monitorId, teamId);
|
const monitor = await this.monitorsRepository.deleteById(monitorId, teamId);
|
||||||
await this.monitorStatsRepository.deleteByMonitorId(monitor.id).catch((err: any) => {
|
await this.monitorStatsRepository.deleteByMonitorId(monitor.id).catch((err: unknown) => {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error deleting monitor stats for monitor ${monitor.id} with name ${monitor.name}`,
|
message: `Error deleting monitor stats for monitor ${monitor.id} with name ${monitor.name}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: err.stack,
|
stack: err instanceof Error ? err.stack : undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await this.checksRepository.deleteByMonitorId(monitor.id).catch((err: any) => {
|
await this.checksRepository.deleteByMonitorId(monitor.id).catch((err: unknown) => {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error deleting checks for monitor ${monitor.id} with name ${monitor.name}`,
|
message: `Error deleting checks for monitor ${monitor.id} with name ${monitor.name}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: err.stack,
|
stack: err instanceof Error ? err.stack : undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id).catch((err: any) => {
|
await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id).catch((err: unknown) => {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error removing monitor ${monitor.id} with name ${monitor.name} from status pages`,
|
message: `Error removing monitor ${monitor.id} with name ${monitor.name} from status pages`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: err.stack,
|
stack: err instanceof Error ? err.stack : undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.incidentsRepository.deleteByMonitorId(monitor.id, teamId).catch((err: any) => {
|
await this.incidentsRepository.deleteByMonitorId(monitor.id, teamId).catch((err: unknown) => {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error deleting incidents for monitor ${monitor.id} with name ${monitor.name}`,
|
message: `Error deleting incidents for monitor ${monitor.id} with name ${monitor.name}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: err.stack,
|
stack: err instanceof Error ? err.stack : undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.geoChecksRepository.deleteByMonitorId(monitor.id).catch((err: any) => {
|
await this.geoChecksRepository.deleteByMonitorId(monitor.id).catch((err: unknown) => {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error deleting geo checks for monitor ${monitor.id} with name ${monitor.name}`,
|
message: `Error deleting geo checks for monitor ${monitor.id} with name ${monitor.name}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: err.stack,
|
stack: err instanceof Error ? err.stack : undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -501,12 +503,12 @@ export class MonitorService implements IMonitorService {
|
|||||||
await this.geoChecksRepository.deleteByMonitorId(monitor.id);
|
await this.geoChecksRepository.deleteByMonitorId(monitor.id);
|
||||||
await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id);
|
await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id);
|
||||||
await this.monitorStatsRepository.deleteByMonitorId(monitor.id);
|
await this.monitorStatsRepository.deleteByMonitorId(monitor.id);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error deleting associated records for monitor ${monitor.id} with name ${monitor.name}`,
|
message: `Error deleting associated records for monitor ${monitor.id} with name ${monitor.name}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "deleteAllMonitors",
|
method: "deleteAllMonitors",
|
||||||
stack: error.stack,
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -514,7 +516,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
return deletedCount;
|
return deletedCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
exportMonitorsToJSON = async ({ teamId }: { teamId: string }): Promise<any[]> => {
|
exportMonitorsToJSON = async ({ teamId }: { teamId: string }): Promise<Monitor[]> => {
|
||||||
const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
|
const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
|
||||||
|
|
||||||
if (!monitors || monitors.length === 0) {
|
if (!monitors || monitors.length === 0) {
|
||||||
@@ -524,30 +526,22 @@ export class MonitorService implements IMonitorService {
|
|||||||
return monitors;
|
return monitors;
|
||||||
};
|
};
|
||||||
|
|
||||||
importMonitorsFromJSON = async ({
|
importMonitorsFromJSON = async ({ teamId, monitors }: { teamId: string; monitors: Monitor[] }): Promise<{ imported: number; errors: string[] }> => {
|
||||||
teamId,
|
|
||||||
userId,
|
|
||||||
monitors,
|
|
||||||
}: {
|
|
||||||
teamId: string;
|
|
||||||
userId: string;
|
|
||||||
monitors: any[];
|
|
||||||
}): Promise<{ imported: number; errors: string[] }> => {
|
|
||||||
const errors: string[] = [];
|
const errors: string[] = [];
|
||||||
|
|
||||||
const cleanedMonitors = monitors.map((monitor) => {
|
const cleanedMonitors = monitors.map((monitor) => {
|
||||||
const cleanData = { ...monitor };
|
const cleanData = {
|
||||||
delete cleanData.id;
|
...monitor,
|
||||||
delete cleanData._id;
|
_id: monitor.id,
|
||||||
delete cleanData.createdAt;
|
_createdAt: monitor.createdAt,
|
||||||
delete cleanData.updatedAt;
|
_updatedAt: monitor.updatedAt,
|
||||||
delete cleanData.recentChecks;
|
recentChecks: monitor.recentChecks,
|
||||||
// Monitors must belong to current team
|
};
|
||||||
cleanData.teamId = teamId;
|
cleanData.teamId = teamId;
|
||||||
return cleanData;
|
return cleanData;
|
||||||
});
|
});
|
||||||
|
|
||||||
const createdMonitors = await this.createMonitors(cleanedMonitors, userId, teamId);
|
const createdMonitors = await this.createMonitors(cleanedMonitors);
|
||||||
|
|
||||||
if (!createdMonitors || createdMonitors.length === 0) {
|
if (!createdMonitors || createdMonitors.length === 0) {
|
||||||
throw new AppError({
|
throw new AppError({
|
||||||
|
|||||||
Reference in new issue
Block a user