incidents repo and data reversal

This commit is contained in:
Alex Holliday committed 2026-01-06 20:49:06 +00:00
1 parent 605b4713af
commit 51081a6a0e
32 files changed
+537 -241

No files matched your search

+102 -124
View File
@@ -5,37 +5,37 @@ import {
Monitor,
IUser,
} from "@/db/models/index.js";
import type { ResolutionType } from "@/db/models/index.js";
import mongoose from "mongoose";
import type { ResolutionType } from "@/types/domain/index.js";
import { getChildLogger } from "@/logger/Logger.js";
import { ThresholdEvaluationResult } from "@/services/infrastructure/StatusService.js";
import { getStartDate } from "@/utils/TimeUtils.js";
import type {
Monitor as MonitorEntity,
CheckEntity,
Incident as IncidentEntity,
IncidentWithDetails,
} from "@/types/domain/index.js";
type IncidentPopulated = Omit<IIncident, "monitorId" | "resolvedBy"> & {
monitorId: IMonitor;
resolvedBy?: IUser | null;
};
import type { IIncidentsRepository } from "@/repositories/index.js";
export interface IIncidentService {
handleStatusChange: (
updatedMonitor: MonitorEntity,
lastCheck: CheckEntity
) => Promise<IIncident | null>;
) => Promise<IncidentEntity | null>;
handleThresholdBreach: (
updatedMonitor: MonitorEntity,
lastCheck: CheckEntity,
evalResult: ThresholdEvaluationResult
) => Promise<IIncident | null>;
) => Promise<IncidentEntity | null>;
create: (
teamId: mongoose.Types.ObjectId,
monitorId: mongoose.Types.ObjectId,
startCheckId: mongoose.Types.ObjectId
) => Promise<IIncident>;
get: (teamId: string, incidentId: string) => Promise<IIncident | null>;
teamId: string,
monitorId: string,
startCheckId: string
) => Promise<IncidentEntity>;
get: (
teamId: string,
incidentId: string
) => Promise<IncidentWithDetails | null>;
getAll: (
teamId: string,
monitorId: string,
@@ -44,7 +44,7 @@ export interface IIncidentService {
range: string,
resolved?: boolean,
resolutionType?: ResolutionType
) => Promise<{ incidents: IIncident[]; count: number }>;
) => Promise<{ incidents: IncidentEntity[]; count: number }>;
resolve: (
teamId: string,
incidentId: string,
@@ -52,11 +52,8 @@ export interface IIncidentService {
lastCheck?: CheckEntity,
resolvedBy?: string,
resolutionNote?: string
) => Promise<IIncident | null>;
delete: (
teamId: mongoose.Types.ObjectId,
incidentId: mongoose.Types.ObjectId
) => Promise<boolean>;
) => Promise<IncidentEntity | null>;
delete: (teamId: string, incidentId: string) => Promise<boolean>;
export: (teamId: string) => Promise<Array<Record<string, unknown>>>;
cleanupOrphanedIncidents: () => Promise<boolean>;
}
@@ -65,8 +62,10 @@ const SERVICE_NAME = "IncidentService";
const logger = getChildLogger(SERVICE_NAME);
class IncidentService implements IIncidentService {
public SERVICE_NAME: string;
constructor() {
private incidentsRepository: IIncidentsRepository;
constructor(incidentsRepository: IIncidentsRepository) {
this.SERVICE_NAME = SERVICE_NAME;
this.incidentsRepository = incidentsRepository;
}
handleStatusChange = async (
@@ -75,25 +74,27 @@ class IncidentService implements IIncidentService {
) => {
if (updatedMonitor.status === "down") {
const incident = await this.create(
new mongoose.Types.ObjectId(updatedMonitor.teamId),
new mongoose.Types.ObjectId(updatedMonitor.id),
new mongoose.Types.ObjectId(lastCheck.id)
updatedMonitor.teamId,
updatedMonitor.id,
lastCheck.id
);
return incident;
} else if (updatedMonitor.status === "up") {
const incident = await Incident.findOne({
monitorId: updatedMonitor.id,
teamId: updatedMonitor.teamId,
resolved: false,
});
const incident =
await this.incidentsRepository.findByMonitorIdAndResolved(
updatedMonitor.id,
updatedMonitor.teamId,
false
);
if (!incident) {
return null;
}
const resolvedIncident = await this.resolve(
updatedMonitor.teamId.toString(),
incident._id.toString(),
updatedMonitor.teamId,
incident.id,
"auto",
lastCheck
);
@@ -107,42 +108,38 @@ class IncidentService implements IIncidentService {
lastCheck: CheckEntity,
evalResult: ThresholdEvaluationResult
) => {
const existing = await Incident.findOne({
monitorId: updatedMonitor.id,
teamId: updatedMonitor.teamId,
resolved: false,
});
const existing = await this.incidentsRepository.findByMonitorIdAndResolved(
updatedMonitor.id,
updatedMonitor.teamId,
false
);
if (evalResult.hasBreach) {
if (!existing) {
const incident = await this.create(
new mongoose.Types.ObjectId(updatedMonitor.teamId),
new mongoose.Types.ObjectId(updatedMonitor.id),
new mongoose.Types.ObjectId(lastCheck.id)
);
const incident = await this.incidentsRepository.create({
teamId: updatedMonitor.teamId,
monitorId: updatedMonitor.id,
startCheck: lastCheck.id,
});
if (evalResult.notes.length) {
await Incident.updateOne(
{ _id: incident._id },
await this.incidentsRepository.updateById(
incident.id,
updatedMonitor.teamId,
{
$set: {
resolutionNote: `threshold breach: ${evalResult.notes.join(
", "
)}`,
},
resolutionNote: `threshold breach: ${evalResult.notes.join(
", "
)}`,
}
);
}
return incident;
}
if (evalResult.notes.length) {
await Incident.updateOne(
{ _id: existing._id },
await this.incidentsRepository.updateById(
existing.id,
updatedMonitor.teamId,
{
$set: {
resolutionNote: `threshold breach: ${evalResult.notes.join(
", "
)}`,
},
resolutionNote: `threshold breach: ${evalResult.notes.join(", ")}`,
}
);
}
@@ -150,7 +147,7 @@ class IncidentService implements IIncidentService {
} else if (existing) {
const resolved = await this.resolve(
updatedMonitor.teamId,
existing._id.toString(),
existing.id,
"auto",
lastCheck
);
@@ -160,38 +157,32 @@ class IncidentService implements IIncidentService {
return null;
};
create = async (
teamId: mongoose.Types.ObjectId,
monitorId: mongoose.Types.ObjectId,
startCheckId: mongoose.Types.ObjectId
) => {
const existing = await Incident.findOne({
create = async (teamId: string, monitorId: string, startCheckId: string) => {
const existing = await this.incidentsRepository.findByMonitorIdAndResolved(
monitorId,
teamId,
resolved: false,
});
false
);
if (existing) {
return existing;
}
let data: Partial<IIncident> = {
teamId: new mongoose.Types.ObjectId(teamId),
monitorId: new mongoose.Types.ObjectId(monitorId),
startedAt: new Date(),
startCheck: new mongoose.Types.ObjectId(startCheckId),
let data: Partial<IncidentEntity> = {
teamId,
monitorId,
startCheck: startCheckId,
};
const incident = await Incident.create(data);
const incident = await this.incidentsRepository.create(data);
return incident;
};
get = async (teamId: string, incidentId: string) => {
const incident = await Incident.findOne({
_id: new mongoose.Types.ObjectId(incidentId),
teamId: new mongoose.Types.ObjectId(teamId),
})
.populate("monitorId")
.populate("resolvedBy");
const incident =
await this.incidentsRepository.findByIdWithMonitorAndResolvedBy(
incidentId,
teamId
);
return incident;
};
@@ -205,22 +196,24 @@ class IncidentService implements IIncidentService {
resolutionType?: ResolutionType
) => {
const startDate = getStartDate(range);
const match = {
teamId: teamId,
...(monitorId && { monitorId }),
createdAt: { $gte: startDate },
...(resolved !== undefined && { resolved }),
...(resolutionType !== undefined && { resolutionType }),
};
const [count, incidents] = await Promise.all([
Incident.countDocuments(match),
Incident.find(match)
.populate("monitorId")
.populate("resolvedBy")
.sort({ createdAt: -1 })
.skip(page * rowsPerPage)
.limit(rowsPerPage),
this.incidentsRepository.count(
teamId,
startDate,
monitorId,
resolved,
resolutionType
),
this.incidentsRepository.findAllWithMonitorAndResolvedBy(
teamId,
page,
rowsPerPage,
startDate,
monitorId,
resolved,
resolutionType
),
]);
return { count, incidents };
};
@@ -233,53 +226,38 @@ class IncidentService implements IIncidentService {
resolvedBy?: string,
resolutionNote?: string
) => {
const incident = await Incident.findOneAndUpdate(
{ _id: incidentId, teamId, resolved: false },
const resolvedIncident = await this.incidentsRepository.updateById(
incidentId,
teamId,
{
$set: {
resolved: true,
endedAt: new Date(),
endCheck: lastCheck
? new mongoose.Types.ObjectId(lastCheck.id)
: undefined,
resolvedBy: resolvedBy ? resolvedBy : undefined,
resolutionType,
resolutionNote: resolutionNote ? resolutionNote : undefined,
},
},
{ new: true }
resolved: true,
endedAt: new Date(),
endCheck: lastCheck ? lastCheck.id : undefined,
resolvedBy: resolvedBy ? resolvedBy : undefined,
resolutionType,
resolutionNote: resolutionNote ? resolutionNote : undefined,
}
);
return incident;
return resolvedIncident;
};
delete = async (
teamId: mongoose.Types.ObjectId,
incidentId: mongoose.Types.ObjectId
) => {
const result = await Incident.deleteOne({
_id: new mongoose.Types.ObjectId(incidentId),
teamId: new mongoose.Types.ObjectId(teamId),
});
return result.deletedCount === 1;
delete = async (teamId: string, incidentId: string) => {
return await this.incidentsRepository.deleteById(incidentId, teamId);
};
export = async (teamId: string) => {
const incidents = await Incident.find({
teamId,
})
.populate("monitorId")
.populate("resolvedBy")
.lean<IncidentPopulated[]>();
const incidents = await this.incidentsRepository.findAllByTeamId(teamId);
return incidents.map((incident) => ({
monitor: incident.monitorId?.name ?? "",
monitorId: incident.monitorId?._id?.toString?.() ?? "",
monitor: incident.monitor.name ?? "",
monitorId: incident.monitor.id ?? "",
resolved: incident.resolved,
resolutionType: incident.resolutionType ?? "Unresolved",
startedAt: incident.startedAt,
endedAt: incident.endedAt ?? "Ongoing",
resolvedBy: incident.resolvedBy ? incident.resolvedBy.email ?? "" : "",
resolvedBy: incident.resolvedByUser
? incident.resolvedByUser.email ?? ""
: "",
}));
};
@@ -15,7 +15,10 @@ import SettingsService from "@/services/system/SettingsService.js";
import ApiError from "@/utils/ApiError.js";
import { getChildLogger } from "@/logger/Logger.js";
import type { IEmailTransport } from "./NotificationServices/Email.js";
import type { Monitor as MonitorEntity } from "@/types/domain/index.js";
import type {
Monitor as MonitorEntity,
Incident,
} from "@/types/domain/index.js";
const SERVICE_NAME = "NotificationService";
const logger = getChildLogger(SERVICE_NAME);
@@ -29,7 +32,7 @@ export interface ITestResult {
export interface INotificationService {
handleNotifications: (
monitor: MonitorEntity,
incident: IIncident
incident: Incident
) => Promise<void>;
testNotificationChannels: (
monitorId: string,
@@ -58,7 +61,7 @@ class NotificationService implements INotificationService {
this.webhookService = new WebhookService();
}
handleNotifications = async (monitor: MonitorEntity, incident: IIncident) => {
handleNotifications = async (monitor: MonitorEntity, incident: Incident) => {
const notificationIds = monitor.notificationChannels || [];
if (notificationIds.length === 0) {
@@ -89,7 +92,7 @@ class NotificationService implements INotificationService {
private sendForChannel = async (
channel: INotificationChannel,
monitor: MonitorEntity,
incident: IIncident
incident: Incident
): Promise<boolean> => {
try {
switch (channel.type) {
@@ -1,5 +1,5 @@
import { INotificationChannel, IIncident } from "@/db/models/index.js";
import type { Monitor } from "@/types/domain/index.js";
import { INotificationChannel } from "@/db/models/index.js";
import type { Monitor, Incident } from "@/types/domain/index.js";
import { IAlert, IMessageService } from "./IMessageService.js";
import got from "got";
import ApiError from "@/utils/ApiError.js";
@@ -49,7 +49,7 @@ class DiscordService implements IMessageService {
};
};
buildAlert = (monitor: Monitor, incident: IIncident) => {
buildAlert = (monitor: Monitor, incident: Incident) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";
@@ -1,5 +1,5 @@
import { INotificationChannel, IIncident } from "@/db/models/index.js";
import type { Monitor } from "@/types/domain/index.js";
import { INotificationChannel } from "@/db/models/index.js";
import type { Monitor, Incident } from "@/types/domain/index.js";
import { IMessageService, IAlert } from "./IMessageService.js";
import nodemailer, { Transporter } from "nodemailer";
@@ -121,7 +121,7 @@ class EmailService implements IEmailService {
}
};
buildAlert = (monitor: Monitor, incident: IIncident) => {
buildAlert = (monitor: Monitor, incident: Incident) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";
@@ -1,9 +1,9 @@
import {
INotificationChannel,
IIncident,
import { INotificationChannel } from "@/db/models/index.js";
import type {
Monitor,
Incident,
ResolutionType,
} from "@/db/models/index.js";
import type { Monitor } from "@/types/domain/index.js";
} from "@/types/domain/index.js";
export interface IAlert {
name: string;
@@ -18,7 +18,7 @@ export interface IAlert {
alertTime: Date;
}
export interface IMessageService {
buildAlert: (monitor: Monitor, incident: IIncident) => IAlert;
buildAlert: (monitor: Monitor, incident: Incident) => IAlert;
sendMessage: (
alert: IAlert,
channel: INotificationChannel
@@ -1,7 +1,7 @@
import { INotificationChannel, IIncident } from "@/db/models/index.js";
import { INotificationChannel } from "@/db/models/index.js";
import { IAlert, IMessageService } from "./IMessageService.js";
import got from "got";
import { Monitor } from "@/types/domain/index.js";
import { Monitor, Incident } from "@/types/domain/index.js";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "SlackService";
@@ -110,7 +110,7 @@ class SlackService implements IMessageService {
];
};
buildAlert = (monitor: Monitor, incident: IIncident) => {
buildAlert = (monitor: Monitor, incident: Incident) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";
@@ -1,9 +1,9 @@
import { INotificationChannel, IIncident } from "@/db/models/index.js";
import { INotificationChannel } from "@/db/models/index.js";
import { IAlert, IMessageService } from "./IMessageService.js";
import ApiError from "@/utils/ApiError.js";
import got from "got";
import { getChildLogger } from "@/logger/Logger.js";
import type { Monitor } from "@/types/domain/index.js";
import type { Monitor, Incident } from "@/types/domain/index.js";
const SERVICE_NAME = "WebhookService";
const logger = getChildLogger(SERVICE_NAME);
@@ -14,7 +14,7 @@ class WebhookService implements IMessageService {
this.SERVICE_NAME = SERVICE_NAME;
}
buildAlert = (monitor: Monitor, incident: IIncident) => {
buildAlert = (monitor: Monitor, incident: Incident) => {
const name = monitor?.name || "Unnamed monitor";
const monitorStatus = monitor?.status || "unknown status";
const url = monitor?.url || "no URL";