remvoe dead code from monitor service
This commit is contained in:
1 parent
6886fc75de
commit
1b746d0f0a
3 files changed
+10
-121
No files matched your search
@@ -364,7 +364,6 @@ export const initializeServices = async ({
|
|||||||
});
|
});
|
||||||
const monitorService = new MonitorService({
|
const monitorService = new MonitorService({
|
||||||
jobQueue: superSimpleQueue,
|
jobQueue: superSimpleQueue,
|
||||||
emailService,
|
|
||||||
logger,
|
logger,
|
||||||
games,
|
games,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import demoMonitorsData from "@/utils/demoMonitors.json" with { type: "json" };
|
|||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import type { ImportedMonitor } from "@/validation/monitorValidation.js";
|
import type { ImportedMonitor } from "@/validation/monitorValidation.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";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "MonitorService";
|
const SERVICE_NAME = "MonitorService";
|
||||||
@@ -91,7 +90,6 @@ export class MonitorService implements IMonitorService {
|
|||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private jobQueue: ISuperSimpleQueue;
|
private jobQueue: ISuperSimpleQueue;
|
||||||
private emailService: IEmailService;
|
|
||||||
private logger: ILogger;
|
private logger: ILogger;
|
||||||
private games: GamesMap;
|
private games: GamesMap;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
@@ -103,7 +101,6 @@ export class MonitorService implements IMonitorService {
|
|||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
jobQueue,
|
jobQueue,
|
||||||
emailService,
|
|
||||||
logger,
|
logger,
|
||||||
games,
|
games,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
@@ -114,7 +111,6 @@ export class MonitorService implements IMonitorService {
|
|||||||
incidentsRepository,
|
incidentsRepository,
|
||||||
}: {
|
}: {
|
||||||
jobQueue: ISuperSimpleQueue;
|
jobQueue: ISuperSimpleQueue;
|
||||||
emailService: IEmailService;
|
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
games: GamesMap;
|
games: GamesMap;
|
||||||
monitorsRepository: IMonitorsRepository;
|
monitorsRepository: IMonitorsRepository;
|
||||||
@@ -125,7 +121,6 @@ export class MonitorService implements IMonitorService {
|
|||||||
incidentsRepository: IIncidentsRepository;
|
incidentsRepository: IIncidentsRepository;
|
||||||
}) {
|
}) {
|
||||||
this.jobQueue = jobQueue;
|
this.jobQueue = jobQueue;
|
||||||
this.emailService = emailService;
|
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.games = games;
|
this.games = games;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
@@ -185,8 +180,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
addDemoMonitors = async ({ userId, teamId }: { userId: string; teamId: string }): Promise<Monitor[]> => {
|
addDemoMonitors = async ({ userId, teamId }: { userId: string; teamId: string }): Promise<Monitor[]> => {
|
||||||
const demoData = demoMonitorsData;
|
const monitors = demoMonitorsData.map((monitor) => ({
|
||||||
const monitors = demoData.map((monitor) => ({
|
|
||||||
userId,
|
userId,
|
||||||
teamId,
|
teamId,
|
||||||
name: monitor.name,
|
name: monitor.name,
|
||||||
@@ -214,7 +208,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
if (!monitor) {
|
if (!monitor) {
|
||||||
throw new AppError({ message: `Monitor with ID ${monitorId} not found.`, status: 404 });
|
throw new AppError({ message: `Monitor with ID ${monitorId} not found.`, status: 404 });
|
||||||
}
|
}
|
||||||
const rangeKey = (dateRange as DateRangeKey) ?? "recent";
|
const rangeKey = dateRange as DateRangeKey;
|
||||||
const { start, end } = this.getDateRange(rangeKey);
|
const { start, end } = this.getDateRange(rangeKey);
|
||||||
const checksData = await this.checksRepository.findByDateRangeAndMonitorId(monitor.id, start, end, this.getDateFormat(rangeKey), {
|
const checksData = await this.checksRepository.findByDateRangeAndMonitorId(monitor.id, start, end, this.getDateFormat(rangeKey), {
|
||||||
type: monitor.type,
|
type: monitor.type,
|
||||||
@@ -263,16 +257,12 @@ export class MonitorService implements IMonitorService {
|
|||||||
throw new AppError({ message: `${monitor.type} monitors are not supported for hardware details`, status: 400 });
|
throw new AppError({ message: `${monitor.type} monitors are not supported for hardware details`, status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const rangeKey = (dateRange as DateRangeKey) ?? "recent";
|
const rangeKey = dateRange as DateRangeKey;
|
||||||
const { start, end } = this.getDateRange(rangeKey);
|
const { start, end } = this.getDateRange(rangeKey);
|
||||||
const checksData = await this.checksRepository.findByDateRangeAndMonitorId(monitor.id, start, end, this.getDateFormat(rangeKey), {
|
const checksData = await this.checksRepository.findByDateRangeAndMonitorId(monitor.id, start, end, this.getDateFormat(rangeKey), {
|
||||||
type: monitor.type,
|
type: monitor.type,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (checksData.monitorType !== "hardware") {
|
|
||||||
throw new AppError({ message: "Unable to load hardware stats for this monitor", status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const stats = {
|
const stats = {
|
||||||
aggregateData: checksData.aggregateData,
|
aggregateData: checksData.aggregateData,
|
||||||
upChecks: checksData.upChecks,
|
upChecks: checksData.upChecks,
|
||||||
@@ -305,16 +295,12 @@ export class MonitorService implements IMonitorService {
|
|||||||
throw new AppError({ message: `${monitor.type} monitors are not supported for pagespeed details`, status: 400 });
|
throw new AppError({ message: `${monitor.type} monitors are not supported for pagespeed details`, status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const rangeKey = (dateRange as DateRangeKey) ?? "recent";
|
const rangeKey = dateRange as DateRangeKey;
|
||||||
const { start, end } = this.getDateRange(rangeKey);
|
const { start, end } = this.getDateRange(rangeKey);
|
||||||
const checksData = await this.checksRepository.findByDateRangeAndMonitorId(monitor.id, start, end, this.getDateFormat(rangeKey), {
|
const checksData = await this.checksRepository.findByDateRangeAndMonitorId(monitor.id, start, end, this.getDateFormat(rangeKey), {
|
||||||
type: monitor.type,
|
type: monitor.type,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (checksData.monitorType !== "pagespeed") {
|
|
||||||
throw new AppError({ message: "Unable to load pagespeed stats for this monitor", status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const monitorStats = await this.monitorStatsRepository.findByMonitorId(monitor.id);
|
const monitorStats = await this.monitorStatsRepository.findByMonitorId(monitor.id);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -346,7 +332,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
return { groupedGeoChecks: [] };
|
return { groupedGeoChecks: [] };
|
||||||
}
|
}
|
||||||
|
|
||||||
const rangeKey = (dateRange as DateRangeKey) ?? "recent";
|
const rangeKey = dateRange as DateRangeKey;
|
||||||
const { start, end } = this.getDateRange(rangeKey);
|
const { start, end } = this.getDateRange(rangeKey);
|
||||||
const groupedGeoChecks = await this.geoChecksRepository.findGroupedByMonitorIdAndDateRange(
|
const groupedGeoChecks = await this.geoChecksRepository.findGroupedByMonitorIdAndDateRange(
|
||||||
monitor.id,
|
monitor.id,
|
||||||
@@ -360,8 +346,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
getMonitorById = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
getMonitorById = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
||||||
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
|
return await this.monitorsRepository.findById(monitorId, teamId);
|
||||||
return monitor;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
getMonitorsByTeamId = async ({
|
getMonitorsByTeamId = async ({
|
||||||
@@ -373,11 +358,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
type?: MonitorType | MonitorType[];
|
type?: MonitorType | MonitorType[];
|
||||||
filter?: string;
|
filter?: string;
|
||||||
}): Promise<Monitor[] | null> => {
|
}): Promise<Monitor[] | null> => {
|
||||||
const monitors = await this.monitorsRepository.findByTeamId(teamId, {
|
return await this.monitorsRepository.findByTeamId(teamId, { type, filter });
|
||||||
type,
|
|
||||||
filter,
|
|
||||||
});
|
|
||||||
return monitors;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
getMonitorsWithChecksByTeamId = async ({
|
getMonitorsWithChecksByTeamId = async ({
|
||||||
@@ -432,8 +413,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
getGroupsByTeamId = async ({ teamId }: { teamId: string }): Promise<string[]> => {
|
getGroupsByTeamId = async ({ teamId }: { teamId: string }): Promise<string[]> => {
|
||||||
const groups = await this.monitorsRepository.findGroupsByTeamId(teamId);
|
return await this.monitorsRepository.findGroupsByTeamId(teamId);
|
||||||
return groups;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: Partial<Monitor> }) => {
|
editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: Partial<Monitor> }) => {
|
||||||
|
|||||||
@@ -63,11 +63,6 @@ const createJobQueueMock = () => ({
|
|||||||
deleteJob: jest.fn(),
|
deleteJob: jest.fn(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const createEmailServiceMock = () => ({
|
|
||||||
buildEmail: jest.fn(),
|
|
||||||
sendEmail: jest.fn(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const TEAM_ID = "team-1";
|
const TEAM_ID = "team-1";
|
||||||
const MONITOR_ID = "monitor-1";
|
const MONITOR_ID = "monitor-1";
|
||||||
const USER_ID = "user-1";
|
const USER_ID = "user-1";
|
||||||
@@ -104,7 +99,6 @@ const createService = (
|
|||||||
const logger = overrides.logger ?? createMockLogger();
|
const logger = overrides.logger ?? createMockLogger();
|
||||||
const service = new MonitorService({
|
const service = new MonitorService({
|
||||||
jobQueue: jobQueue as any,
|
jobQueue: jobQueue as any,
|
||||||
emailService: createEmailServiceMock() as any,
|
|
||||||
logger: logger as any,
|
logger: logger as any,
|
||||||
games: (overrides.games ?? { cs2: { name: "Counter-Strike 2" } }) as any,
|
games: (overrides.games ?? { cs2: { name: "Counter-Strike 2" } }) as any,
|
||||||
monitorsRepository: overrides.monitorsRepository ?? createMonitorsRepositoryMock(),
|
monitorsRepository: overrides.monitorsRepository ?? createMonitorsRepositoryMock(),
|
||||||
@@ -296,27 +290,6 @@ describe("MonitorService", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("defaults to 'recent' when dateRange is nullish", async () => {
|
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
|
||||||
const checksRepository = createChecksRepositoryMock();
|
|
||||||
const monitorStatsRepository = createMonitorStatsRepositoryMock();
|
|
||||||
const monitor = makeMonitor();
|
|
||||||
(monitorsRepository.findById as jest.Mock).mockResolvedValue(monitor);
|
|
||||||
(checksRepository.findByDateRangeAndMonitorId as jest.Mock).mockResolvedValue({
|
|
||||||
monitorType: "http",
|
|
||||||
groupedChecks: [],
|
|
||||||
groupedUpChecks: [],
|
|
||||||
groupedDownChecks: [],
|
|
||||||
uptimePercentage: 1,
|
|
||||||
avgResponseTime: 50,
|
|
||||||
});
|
|
||||||
(monitorStatsRepository.findByMonitorId as jest.Mock).mockResolvedValue(null);
|
|
||||||
|
|
||||||
const { service } = createService({ monitorsRepository, checksRepository, monitorStatsRepository });
|
|
||||||
await service.getUptimeDetailsById({ teamId: TEAM_ID, monitorId: MONITOR_ID, dateRange: undefined as any });
|
|
||||||
expect(checksRepository.findByDateRangeAndMonitorId).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("uses different date ranges correctly", async () => {
|
it("uses different date ranges correctly", async () => {
|
||||||
for (const dateRange of ["recent", "day", "week", "month", "all"] as const) {
|
for (const dateRange of ["recent", "day", "week", "month", "all"] as const) {
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
const monitorsRepository = createMonitorsRepositoryMock();
|
||||||
@@ -386,40 +359,6 @@ describe("MonitorService", () => {
|
|||||||
"monitors are not supported for hardware details"
|
"monitors are not supported for hardware details"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws 500 when checksData monitorType is not hardware", async () => {
|
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
|
||||||
const checksRepository = createChecksRepositoryMock();
|
|
||||||
const monitor = makeMonitor({ type: "hardware" });
|
|
||||||
(monitorsRepository.findById as jest.Mock).mockResolvedValue(monitor);
|
|
||||||
(checksRepository.findByDateRangeAndMonitorId as jest.Mock).mockResolvedValue({
|
|
||||||
monitorType: "http",
|
|
||||||
});
|
|
||||||
const { service } = createService({ monitorsRepository, checksRepository });
|
|
||||||
|
|
||||||
await expect(service.getHardwareDetailsById({ teamId: TEAM_ID, monitorId: MONITOR_ID, dateRange: "recent" })).rejects.toThrow(
|
|
||||||
"Unable to load hardware stats for this monitor"
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("defaults to 'recent' when dateRange is nullish", async () => {
|
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
|
||||||
const checksRepository = createChecksRepositoryMock();
|
|
||||||
const monitorStatsRepository = createMonitorStatsRepositoryMock();
|
|
||||||
const monitor = makeMonitor({ type: "hardware" });
|
|
||||||
(monitorsRepository.findById as jest.Mock).mockResolvedValue(monitor);
|
|
||||||
(checksRepository.findByDateRangeAndMonitorId as jest.Mock).mockResolvedValue({
|
|
||||||
monitorType: "hardware",
|
|
||||||
aggregateData: { totalChecks: 1 },
|
|
||||||
upChecks: { totalChecks: 1 },
|
|
||||||
checks: [],
|
|
||||||
});
|
|
||||||
(monitorStatsRepository.findByMonitorId as jest.Mock).mockResolvedValue(null);
|
|
||||||
const { service } = createService({ monitorsRepository, checksRepository, monitorStatsRepository });
|
|
||||||
|
|
||||||
await service.getHardwareDetailsById({ teamId: TEAM_ID, monitorId: MONITOR_ID, dateRange: undefined as any });
|
|
||||||
expect(checksRepository.findByDateRangeAndMonitorId).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getPageSpeedDetailsById", () => {
|
describe("getPageSpeedDetailsById", () => {
|
||||||
@@ -464,22 +403,7 @@ describe("MonitorService", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws 500 when checksData monitorType is not pagespeed", async () => {
|
it("handles pagespeed with different date range", async () => {
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
|
||||||
const checksRepository = createChecksRepositoryMock();
|
|
||||||
const monitor = makeMonitor({ type: "pagespeed" });
|
|
||||||
(monitorsRepository.findById as jest.Mock).mockResolvedValue(monitor);
|
|
||||||
(checksRepository.findByDateRangeAndMonitorId as jest.Mock).mockResolvedValue({
|
|
||||||
monitorType: "http",
|
|
||||||
});
|
|
||||||
const { service } = createService({ monitorsRepository, checksRepository });
|
|
||||||
|
|
||||||
await expect(service.getPageSpeedDetailsById({ teamId: TEAM_ID, monitorId: MONITOR_ID, dateRange: "recent" })).rejects.toThrow(
|
|
||||||
"Unable to load pagespeed stats for this monitor"
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("defaults to 'recent' when dateRange is nullish", async () => {
|
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
const monitorsRepository = createMonitorsRepositoryMock();
|
||||||
const checksRepository = createChecksRepositoryMock();
|
const checksRepository = createChecksRepositoryMock();
|
||||||
const monitorStatsRepository = createMonitorStatsRepositoryMock();
|
const monitorStatsRepository = createMonitorStatsRepositoryMock();
|
||||||
@@ -492,7 +416,7 @@ describe("MonitorService", () => {
|
|||||||
(monitorStatsRepository.findByMonitorId as jest.Mock).mockResolvedValue(null);
|
(monitorStatsRepository.findByMonitorId as jest.Mock).mockResolvedValue(null);
|
||||||
const { service } = createService({ monitorsRepository, checksRepository, monitorStatsRepository });
|
const { service } = createService({ monitorsRepository, checksRepository, monitorStatsRepository });
|
||||||
|
|
||||||
await service.getPageSpeedDetailsById({ teamId: TEAM_ID, monitorId: MONITOR_ID, dateRange: undefined as any });
|
await service.getPageSpeedDetailsById({ teamId: TEAM_ID, monitorId: MONITOR_ID, dateRange: "week" });
|
||||||
expect(checksRepository.findByDateRangeAndMonitorId).toHaveBeenCalled();
|
expect(checksRepository.findByDateRangeAndMonitorId).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -558,19 +482,6 @@ describe("MonitorService", () => {
|
|||||||
expect(geoChecksRepository.findGroupedByMonitorIdAndDateRange).toHaveBeenCalled();
|
expect(geoChecksRepository.findGroupedByMonitorIdAndDateRange).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("defaults to 'recent' when dateRange is nullish", async () => {
|
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
|
||||||
const geoChecksRepository = createGeoChecksRepositoryMock();
|
|
||||||
const monitor = makeMonitor({ type: "http", geoCheckEnabled: true });
|
|
||||||
(monitorsRepository.findById as jest.Mock).mockResolvedValue(monitor);
|
|
||||||
(geoChecksRepository.findGroupedByMonitorIdAndDateRange as jest.Mock).mockResolvedValue([]);
|
|
||||||
|
|
||||||
const { service } = createService({ monitorsRepository, geoChecksRepository });
|
|
||||||
const result = await service.getGeoChecksByMonitorId({ teamId: TEAM_ID, monitorId: MONITOR_ID, dateRange: undefined as any });
|
|
||||||
expect(result.groupedGeoChecks).toEqual([]);
|
|
||||||
expect(geoChecksRepository.findGroupedByMonitorIdAndDateRange).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("works with ping monitor type", async () => {
|
it("works with ping monitor type", async () => {
|
||||||
const monitorsRepository = createMonitorsRepositoryMock();
|
const monitorsRepository = createMonitorsRepositoryMock();
|
||||||
const geoChecksRepository = createGeoChecksRepositoryMock();
|
const geoChecksRepository = createGeoChecksRepositoryMock();
|
||||||
@@ -1391,7 +1302,6 @@ describe("MonitorService", () => {
|
|||||||
const logger = createMockLogger();
|
const logger = createMockLogger();
|
||||||
const service = new MonitorService({
|
const service = new MonitorService({
|
||||||
jobQueue: jobQueue as any,
|
jobQueue: jobQueue as any,
|
||||||
emailService: createEmailServiceMock() as any,
|
|
||||||
logger: logger as any,
|
logger: logger as any,
|
||||||
games: {} as any,
|
games: {} as any,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
|
|||||||
Reference in new issue
Block a user