initial commit
This commit is contained in:
1 parent
ef15e5d6e0
commit
6bc9194e8f
9 files changed
+92
-3
No files matched your search
@@ -202,6 +202,8 @@ export const initializeServices = async ({
|
|||||||
incidentService,
|
incidentService,
|
||||||
maintenanceWindowsRepository,
|
maintenanceWindowsRepository,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
|
teamsRepository,
|
||||||
|
monitorStatsRepository,
|
||||||
});
|
});
|
||||||
|
|
||||||
const superSimpleQueue = await SuperSimpleQueue.create({
|
const superSimpleQueue = await SuperSimpleQueue.create({
|
||||||
|
|||||||
@@ -24,5 +24,6 @@ export interface IIncidentsRepository {
|
|||||||
updateById(incidentId: string, teamId: string, updateData: Partial<Incident>): Promise<Incident>;
|
updateById(incidentId: string, teamId: string, updateData: Partial<Incident>): Promise<Incident>;
|
||||||
// delete
|
// delete
|
||||||
deleteByMonitorId(monitorId: string, teamId: string): Promise<number>;
|
deleteByMonitorId(monitorId: string, teamId: string): Promise<number>;
|
||||||
|
deleteByMonitorIdsNotIn(monitorIds: string[]): Promise<number>;
|
||||||
// other
|
// other
|
||||||
}
|
}
|
||||||
@@ -7,5 +7,7 @@ export interface IMonitorStatsRepository {
|
|||||||
// update
|
// update
|
||||||
// delete
|
// delete
|
||||||
deleteByMonitorId(monitorId: string): Promise<MonitorStats>;
|
deleteByMonitorId(monitorId: string): Promise<MonitorStats>;
|
||||||
|
deleteByMonitorIds(monitorIds: string[]): Promise<number>;
|
||||||
|
deleteByMonitorIdsNotIn(monitorIds: string[]): Promise<number>;
|
||||||
// other
|
// other
|
||||||
}
|
}
|
||||||
@@ -52,6 +52,18 @@ class MongoMonitorStatsRepository implements IMonitorStatsRepository {
|
|||||||
}
|
}
|
||||||
return this.toEntity(deleted);
|
return this.toEntity(deleted);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
deleteByMonitorIds = async (monitorIds: string[]): Promise<number> => {
|
||||||
|
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||||
|
const result = await MonitorStatsModel.deleteMany({ monitorId: { $in: objectIds } });
|
||||||
|
return result.deletedCount ?? 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
deleteByMonitorIdsNotIn = async (monitorIds: string[]): Promise<number> => {
|
||||||
|
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||||
|
const result = await MonitorStatsModel.deleteMany({ monitorId: { $nin: objectIds } });
|
||||||
|
return result.deletedCount ?? 0;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MongoMonitorStatsRepository;
|
export default MongoMonitorStatsRepository;
|
||||||
@@ -41,4 +41,6 @@ export interface IMonitorsRepository {
|
|||||||
findMonitorsSummaryByTeamId(teamId: string, config?: SummaryConfig): Promise<MonitorsSummary>;
|
findMonitorsSummaryByTeamId(teamId: string, config?: SummaryConfig): Promise<MonitorsSummary>;
|
||||||
findGroupsByTeamId(teamId: string): Promise<string[]>;
|
findGroupsByTeamId(teamId: string): Promise<string[]>;
|
||||||
removeNotificationFromMonitors(notificationId: string): Promise<void>;
|
removeNotificationFromMonitors(notificationId: string): Promise<void>;
|
||||||
|
deleteByTeamIdsNotIn(teamIds: string[]): Promise<number>;
|
||||||
|
findAllMonitorIds(): Promise<string[]>;
|
||||||
}
|
}
|
||||||
@@ -431,6 +431,17 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
createdAt: toDateString(doc.createdAt),
|
createdAt: toDateString(doc.createdAt),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
deleteByTeamIdsNotIn = async (teamIds: string[]): Promise<number> => {
|
||||||
|
const objectIds = teamIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||||
|
const result = await MonitorModel.deleteMany({ teamId: { $nin: objectIds } });
|
||||||
|
return result.deletedCount ?? 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
findAllMonitorIds = async (): Promise<string[]> => {
|
||||||
|
const monitors = await MonitorModel.find({}, { _id: 1 }).lean();
|
||||||
|
return monitors.map((doc) => doc._id.toString());
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MongoMonitorsRepository;
|
export default MongoMonitorsRepository;
|
||||||
@@ -6,4 +6,5 @@ export interface ITeamsRepository {
|
|||||||
// update
|
// update
|
||||||
// delete
|
// delete
|
||||||
// other
|
// other
|
||||||
|
findAllTeamIds(): Promise<string[]>;
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,11 @@ class MongoTeamsRepository implements ITeamsRepository {
|
|||||||
const team = await TeamModel.create({ email });
|
const team = await TeamModel.create({ email });
|
||||||
return this.toEntity(team);
|
return this.toEntity(team);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
findAllTeamIds = async (): Promise<string[]> => {
|
||||||
|
const teams = await TeamModel.find({}, { _id: 1 }).lean();
|
||||||
|
return teams.map((team) => this.toStringId(team._id));
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MongoTeamsRepository;
|
export default MongoTeamsRepository;
|
||||||
@@ -4,7 +4,7 @@ import { AppError } from "@/utils/AppError.js";
|
|||||||
import { INetworkService, INotificationsService, IStatusService } from "@/service/index.js";
|
import { INetworkService, INotificationsService, IStatusService } from "@/service/index.js";
|
||||||
import type { StatusChangeResult, MonitorStatusResponse, HardwareStatusPayload, MonitorStatus } from "@/types/index.js";
|
import type { StatusChangeResult, MonitorStatusResponse, HardwareStatusPayload, MonitorStatus } from "@/types/index.js";
|
||||||
import IncidentService from "@/service/business/incidentService.js";
|
import IncidentService from "@/service/business/incidentService.js";
|
||||||
import { IMaintenanceWindowsRepository, IMonitorsRepository } from "@/repositories/index.js";
|
import { IMaintenanceWindowsRepository, IMonitorsRepository, ITeamsRepository, IMonitorStatsRepository } from "@/repositories/index.js";
|
||||||
|
|
||||||
export interface MonitorActionDecision {
|
export interface MonitorActionDecision {
|
||||||
shouldCreateIncident: boolean;
|
shouldCreateIncident: boolean;
|
||||||
@@ -32,6 +32,8 @@ class SuperSimpleQueueHelper {
|
|||||||
private incidentService: IncidentService;
|
private incidentService: IncidentService;
|
||||||
private maintenanceWindowsRepository: IMaintenanceWindowsRepository;
|
private maintenanceWindowsRepository: IMaintenanceWindowsRepository;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
|
private teamsRepository: ITeamsRepository;
|
||||||
|
private monitorStatsRepository: IMonitorStatsRepository;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
logger,
|
logger,
|
||||||
@@ -43,6 +45,8 @@ class SuperSimpleQueueHelper {
|
|||||||
incidentService,
|
incidentService,
|
||||||
maintenanceWindowsRepository,
|
maintenanceWindowsRepository,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
|
teamsRepository,
|
||||||
|
monitorStatsRepository,
|
||||||
}: {
|
}: {
|
||||||
logger: any;
|
logger: any;
|
||||||
networkService: INetworkService;
|
networkService: INetworkService;
|
||||||
@@ -53,6 +57,8 @@ class SuperSimpleQueueHelper {
|
|||||||
incidentService: IncidentService;
|
incidentService: IncidentService;
|
||||||
maintenanceWindowsRepository: IMaintenanceWindowsRepository;
|
maintenanceWindowsRepository: IMaintenanceWindowsRepository;
|
||||||
monitorsRepository: IMonitorsRepository;
|
monitorsRepository: IMonitorsRepository;
|
||||||
|
teamsRepository: ITeamsRepository;
|
||||||
|
monitorStatsRepository: IMonitorStatsRepository;
|
||||||
}) {
|
}) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.networkService = networkService;
|
this.networkService = networkService;
|
||||||
@@ -63,6 +69,8 @@ class SuperSimpleQueueHelper {
|
|||||||
this.incidentService = incidentService;
|
this.incidentService = incidentService;
|
||||||
this.maintenanceWindowsRepository = maintenanceWindowsRepository;
|
this.maintenanceWindowsRepository = maintenanceWindowsRepository;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
|
this.teamsRepository = teamsRepository;
|
||||||
|
this.monitorStatsRepository = monitorStatsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
@@ -156,10 +164,55 @@ class SuperSimpleQueueHelper {
|
|||||||
getCleanupOrphanedJob = () => {
|
getCleanupOrphanedJob = () => {
|
||||||
return async () => {
|
return async () => {
|
||||||
try {
|
try {
|
||||||
// Remove orphaned monitors
|
this.logger.info({
|
||||||
// remove orphaned monitorStats
|
message: "Starting cleanup of orphaned data",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get all valid team IDs
|
||||||
|
const validTeamIds = await this.teamsRepository.findAllTeamIds();
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Found ${validTeamIds.length} valid teams`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove orphaned monitors (monitors without a valid team)
|
||||||
|
const deletedMonitorCount = await this.monitorsRepository.deleteByTeamIdsNotIn(validTeamIds);
|
||||||
|
if (deletedMonitorCount > 0) {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Deleted ${deletedMonitorCount} orphaned monitors`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove orphaned monitorStats (stats without a valid monitor)
|
||||||
|
const allMonitorIds = await this.monitorsRepository.findAllMonitorIds();
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Found ${allMonitorIds.length} valid monitors`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
|
||||||
|
const deletedStatsCount = await this.monitorStatsRepository.deleteByMonitorIdsNotIn(allMonitorIds);
|
||||||
|
if (deletedStatsCount > 0) {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Deleted ${deletedStatsCount} orphaned monitor stats`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Remove orphaned checks
|
// Remove orphaned checks
|
||||||
// Remove orphaned incidents
|
// Remove orphaned incidents
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
message: "Cleanup of orphaned data completed",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
|
|||||||
Reference in new issue
Block a user