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