delete many

This commit is contained in:
Alex Holliday committed 2026-01-06 21:08:41 +00:00
1 parent ba7cc3bb0c
commit 052b9377bc
4 files changed
+33 -16

No files matched your search

+4 -2
View File
@@ -64,7 +64,6 @@ export const initServices = async () => {
monitorRepository,
monitorStatsRepository
);
// If desired in future, use repository for stats as well
const settingsService = new SettingsService();
const stripeService = new StripeService();
const billingService = new BillingService();
@@ -74,7 +73,10 @@ export const initServices = async () => {
userService,
settingsService
);
const incidentService = new IncidentService(incidentsRepository);
const incidentService = new IncidentService(
incidentsRepository,
monitorRepository
);
const statsAggregationService = new StatsAggregationService();
const jobGenerator = new JobGenerator(
networkService,
@@ -44,6 +44,7 @@ export interface IIncidentsRepository {
): Promise<IncidentEntity | null>;
// delete
deleteById(incidentId: string, teamId: string): Promise<boolean>;
deleteManyExcludedByMonitorIds(monitorIds: string[]): Promise<number>;
}
export default IIncidentsRepository;
@@ -187,6 +187,15 @@ class MongoIncidentsRepository implements IIncidentsRepository {
});
return result.deletedCount === 1;
};
deleteManyExcludedByMonitorIds = async (monitorIds: string[]) => {
const result = await Incident.deleteMany({
monitorId: {
$nin: monitorIds.map((id) => new mongoose.Types.ObjectId(id)),
},
});
return result.deletedCount || 0;
};
}
export default MongoIncidentsRepository;
+19 -14
View File
@@ -1,10 +1,4 @@
import {
IIncident,
Incident,
IMonitor,
Monitor,
IUser,
} from "@/db/models/index.js";
import { Incident, Monitor } from "@/db/models/index.js";
import type { ResolutionType } from "@/types/domain/index.js";
import { getChildLogger } from "@/logger/Logger.js";
import { ThresholdEvaluationResult } from "@/services/infrastructure/StatusService.js";
@@ -15,7 +9,10 @@ import type {
Incident as IncidentEntity,
IncidentWithDetails,
} from "@/types/domain/index.js";
import type { IIncidentsRepository } from "@/repositories/index.js";
import type {
IIncidentsRepository,
IMonitorRepository,
} from "@/repositories/index.js";
export interface IIncidentService {
handleStatusChange: (
@@ -63,9 +60,14 @@ const logger = getChildLogger(SERVICE_NAME);
class IncidentService implements IIncidentService {
public SERVICE_NAME: string;
private incidentsRepository: IIncidentsRepository;
constructor(incidentsRepository: IIncidentsRepository) {
private monitorRepository: IMonitorRepository;
constructor(
incidentsRepository: IIncidentsRepository,
monitorRepository: IMonitorRepository
) {
this.SERVICE_NAME = SERVICE_NAME;
this.incidentsRepository = incidentsRepository;
this.monitorRepository = monitorRepository;
}
handleStatusChange = async (
@@ -263,11 +265,14 @@ class IncidentService implements IIncidentService {
async cleanupOrphanedIncidents() {
try {
const monitorIds = await Monitor.find().distinct("_id");
const result = await Incident.deleteMany({
monitorId: { $nin: monitorIds },
});
logger.info(`Deleted ${result.deletedCount} orphaned Incidents.`);
const monitorIds = (await this.monitorRepository.findAll()).map(
(m) => m.id
);
const deletedCount =
await this.incidentsRepository.deleteManyExcludedByMonitorIds(
monitorIds
);
logger.info(`Deleted ${deletedCount} orphaned Incidents.`);
return true;
} catch (error) {
logger.error("Error cleaning up orphaned Incidents:", error);