Skip to content

Commit

Permalink
feat: update incident log event type and add logging for subscriber n…
Browse files Browse the repository at this point in the history
…otifications in Incident services
  • Loading branch information
simlarsen committed Jan 13, 2025
1 parent 7538a47 commit fb5646e
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 33 deletions.
5 changes: 3 additions & 2 deletions Common/Models/DatabaseModels/IncidentLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import ColumnLength from "../../Types/Database/ColumnLength";

export enum IncidentLogEventType {
PublicNote = "PublicNote",
SubscriberEmailSent = "SubscriberEmailSent",
OwnerEmailSent = "OwnerEmailSent",
SubscriberNotificationSent = "SubscriberNotificationSent",
OwnerNotificationSent = "OwnerNotificationSent",
OwnerAdded = "OwnerAdded",
IncidentCreated = "IncidentCreated",
IncidentStateChanged = "IncidentStateChanged",
PrivateNote = "PrivateNote",
Expand Down
4 changes: 2 additions & 2 deletions Common/Server/Services/AlertLogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class Service extends DatabaseService<Model> {
super(Model);

if (IsBillingEnabled) {
this.hardDeleteItemsOlderThanInDays("createdAt", 120);
}
this.hardDeleteItemsOlderThanInDays("createdAt", 120);
}
}

public async createAlertLog(data: {
Expand Down
22 changes: 10 additions & 12 deletions Common/Server/Services/IncidentInternalNoteService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import User from "../../Models/DatabaseModels/User";
import Name from "../../Types/Name";
import ObjectID from "../../Types/ObjectID";
import DatabaseService from "./DatabaseService";
import Model from "Common/Models/DatabaseModels/IncidentInternalNote";
Expand All @@ -14,44 +13,43 @@ export class Service extends DatabaseService<Model> {
super(Model);
}


public override async onCreateSuccess(_onCreate: OnCreate<Model>, createdItem: Model): Promise<Model> {

const userId: ObjectID | null | undefined = createdItem.createdByUserId || createdItem.createdByUser?.id;
public override async onCreateSuccess(
_onCreate: OnCreate<Model>,
createdItem: Model,
): Promise<Model> {
const userId: ObjectID | null | undefined =
createdItem.createdByUserId || createdItem.createdByUser?.id;
let userName: string | null = null;

if (userId) {
const user: User | null = await UserService.findOneById({
id: userId,
select: {
name: true,
email: true
email: true,
},
props: {
isRoot: true
}
isRoot: true,
},
});

if (user && user.name && user.name.toString()) {
userName = user.name.toString();
}
}


await IncidentLogService.createIncidentLog({
incidentId: createdItem.id!,
projectId: createdItem.projectId!,
incidentLogEventType: IncidentLogEventType.PrivateNote,
displayColor: Blue500,
logInMarkdown: `**Private (Internal) Note Posted${userName ? " by "+userName.toString() : ""}**
logInMarkdown: `**Private (Internal) Note Posted${userName ? " by " + userName.toString() : ""}**
${createdItem.note}
`,
});


return createdItem;

}
}

Expand Down
4 changes: 2 additions & 2 deletions Common/Server/Services/IncidentLogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class Service extends DatabaseService<Model> {
super(Model);

if (IsBillingEnabled) {
this.hardDeleteItemsOlderThanInDays("createdAt", 120);
}
this.hardDeleteItemsOlderThanInDays("createdAt", 120);
}
}

public async createIncidentLog(data: {
Expand Down
22 changes: 10 additions & 12 deletions Common/Server/Services/IncidentPublicNoteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Blue500 } from "../../Types/BrandColors";
import ObjectID from "../../Types/ObjectID";
import User from "../../Models/DatabaseModels/User";
import UserService from "./UserService";
import Name from "../../Types/Name";

export class Service extends DatabaseService<Model> {
public constructor() {
Expand All @@ -29,44 +28,43 @@ export class Service extends DatabaseService<Model> {
};
}


public override async onCreateSuccess(_onCreate: OnCreate<Model>, createdItem: Model): Promise<Model> {

const userId: ObjectID | null | undefined = createdItem.createdByUserId || createdItem.createdByUser?.id;
public override async onCreateSuccess(
_onCreate: OnCreate<Model>,
createdItem: Model,
): Promise<Model> {
const userId: ObjectID | null | undefined =
createdItem.createdByUserId || createdItem.createdByUser?.id;
let userName: string | null = null;

if (userId) {
const user: User | null = await UserService.findOneById({
id: userId,
select: {
name: true,
email: true
email: true,
},
props: {
isRoot: true
}
isRoot: true,
},
});

if (user && user.name) {
userName = user.name.toString();
}
}


await IncidentLogService.createIncidentLog({
incidentId: createdItem.id!,
projectId: createdItem.projectId!,
incidentLogEventType: IncidentLogEventType.PublicNote,
displayColor: Blue500,
logInMarkdown: `**Note Posted on Status Page${userName ? " by "+userName.toString() : ""}**
logInMarkdown: `**Note Posted on Status Page${userName ? " by " + userName.toString() : ""}**
${createdItem.note}
`,
});


return createdItem;

}
}

Expand Down
2 changes: 1 addition & 1 deletion Common/Server/Services/IncidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export class Service extends DatabaseService<Model> {
incidentId: createdItem.id!,
projectId: createdItem.projectId!,
incidentLogEventType: IncidentLogEventType.IncidentCreated,
displayColor: Blue500,
displayColor: Blue500,
logInMarkdown: "Incident Created",
});

Expand Down
4 changes: 2 additions & 2 deletions Common/Server/Services/ScheduledMaintenanceLogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class Service extends DatabaseService<Model> {
super(Model);

if (IsBillingEnabled) {
this.hardDeleteItemsOlderThanInDays("createdAt", 120);
}
this.hardDeleteItemsOlderThanInDays("createdAt", 120);
}
}

public async createScheduledMaintenanceLog(data: {
Expand Down
13 changes: 13 additions & 0 deletions Worker/Jobs/Incident/SendNotificationToSubscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import StatusPage from "Common/Models/DatabaseModels/StatusPage";
import StatusPageResource from "Common/Models/DatabaseModels/StatusPageResource";
import StatusPageSubscriber from "Common/Models/DatabaseModels/StatusPageSubscriber";
import StatusPageEventType from "Common/Types/StatusPage/StatusPageEventType";
import IncidentLogService from "Common/Server/Services/IncidentLogService";
import { IncidentLogEventType } from "Common/Models/DatabaseModels/IncidentLog";
import { Blue500 } from "Common/Types/BrandColors";

RunCron(
"Incident:SendNotificationToSubscribers",
Expand Down Expand Up @@ -62,6 +65,8 @@ RunCron(
const httpProtocol: Protocol = await DatabaseConfig.getHttpProtocol();

for (const incident of incidents) {
const incidentLogText: string = `Notification sent to Status Page Subscribers on Incident Creation`;

if (!incident.monitors || incident.monitors.length === 0) {
continue;
}
Expand Down Expand Up @@ -263,6 +268,14 @@ RunCron(
logger.error(err);
}
}

await IncidentLogService.createIncidentLog({
incidentId: incident.id!,
projectId: incident.projectId!,
incidentLogEventType: IncidentLogEventType.SubscriberNotificationSent,
displayColor: Blue500,
logInMarkdown: incidentLogText,
});
}
},
);
17 changes: 17 additions & 0 deletions Worker/Jobs/IncidentOwners/SendCreatedResourceNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import IncidentState from "Common/Models/DatabaseModels/IncidentState";
import Monitor from "Common/Models/DatabaseModels/Monitor";
import Project from "Common/Models/DatabaseModels/Project";
import User from "Common/Models/DatabaseModels/User";
import IncidentLogService from "Common/Server/Services/IncidentLogService";
import { IncidentLogEventType } from "Common/Models/DatabaseModels/IncidentLog";
import { Blue500 } from "Common/Types/BrandColors";

RunCron(
"IncidentOwner:SendCreatedResourceEmail",
Expand Down Expand Up @@ -64,6 +67,9 @@ RunCron(
});

for (const incident of incidents) {
const incidentLogText: string = `Notification sent to owners of this Incident on Incident Creation`;
let moreIncidentLogInformationInMarkdown: string = "";

const incidentIdentifiedDate: Date =
await IncidentService.getIncidentIdentifiedDate(incident.id!);

Expand Down Expand Up @@ -182,13 +188,24 @@ RunCron(
eventType:
NotificationSettingEventType.SEND_INCIDENT_CREATED_OWNER_NOTIFICATION,
});

moreIncidentLogInformationInMarkdown += `User notified: ${user.name} (${user.email})\n`;
} catch (e) {
logger.error(
"Error in sending incident created resource notification",
);
logger.error(e);
}
}

await IncidentLogService.createIncidentLog({
incidentId: incident.id!,
projectId: incident.projectId!,
incidentLogEventType: IncidentLogEventType.OwnerNotificationSent,
displayColor: Blue500,
logInMarkdown: incidentLogText,
moreInformationInMarkdown: moreIncidentLogInformationInMarkdown,
});
}
},
);
22 changes: 22 additions & 0 deletions Worker/Jobs/IncidentOwners/SendNotePostedNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import IncidentInternalNote from "Common/Models/DatabaseModels/IncidentInternalN
import IncidentPublicNote from "Common/Models/DatabaseModels/IncidentPublicNote";
import Monitor from "Common/Models/DatabaseModels/Monitor";
import User from "Common/Models/DatabaseModels/User";
import IncidentLogService from "Common/Server/Services/IncidentLogService";
import { IncidentLogEventType } from "Common/Models/DatabaseModels/IncidentLog";
import { Blue500 } from "Common/Types/BrandColors";

RunCron(
"IncidentOwner:SendsNotePostedEmail",
Expand Down Expand Up @@ -94,6 +97,8 @@ RunCron(
const notes: Array<BaseModel> = [...publicNotes, ...privateNotes];

for (const noteObject of notes) {
let moreIncidentLogInformationInMarkdown: string = "";

const note: BaseModel = noteObject as BaseModel;

// get all scheduled events of all the projects.
Expand Down Expand Up @@ -206,7 +211,24 @@ RunCron(
eventType:
NotificationSettingEventType.SEND_INCIDENT_NOTE_POSTED_OWNER_NOTIFICATION,
});

moreIncidentLogInformationInMarkdown += `User notified: ${user.name} (${user.email})\n`;
}

const isPrivateNote: boolean = privateNoteIds.includes(
note._id!.toString(),
);

const incidentLogText: string = `Owners have been notified about the new ${isPrivateNote ? "private" : "public"} note posted on the incident.`;

await IncidentLogService.createIncidentLog({
incidentId: incident.id!,
projectId: incident.projectId!,
incidentLogEventType: IncidentLogEventType.OwnerNotificationSent,
displayColor: Blue500,
logInMarkdown: incidentLogText,
moreInformationInMarkdown: moreIncidentLogInformationInMarkdown,
});
}
},
);
11 changes: 11 additions & 0 deletions Worker/Jobs/IncidentOwners/SendOwnerAddedNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import IncidentOwnerTeam from "Common/Models/DatabaseModels/IncidentOwnerTeam";
import IncidentOwnerUser from "Common/Models/DatabaseModels/IncidentOwnerUser";
import Monitor from "Common/Models/DatabaseModels/Monitor";
import User from "Common/Models/DatabaseModels/User";
import IncidentLogService from "Common/Server/Services/IncidentLogService";
import { IncidentLogEventType } from "Common/Models/DatabaseModels/IncidentLog";
import { Blue500 } from "Common/Types/BrandColors";

RunCron(
"IncidentOwner:SendOwnerAddedEmail",
Expand Down Expand Up @@ -209,6 +212,14 @@ RunCron(
eventType:
NotificationSettingEventType.SEND_INCIDENT_OWNER_ADDED_NOTIFICATION,
});

await IncidentLogService.createIncidentLog({
incidentId: incident.id!,
projectId: incident.projectId!,
incidentLogEventType: IncidentLogEventType.OwnerAdded,
displayColor: Blue500,
logInMarkdown: `User added as owner to this Incident: ${user.name} (${user.email})`,
});
}
}
},
Expand Down
16 changes: 16 additions & 0 deletions Worker/Jobs/IncidentOwners/SendStateChangeNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import IncidentState from "Common/Models/DatabaseModels/IncidentState";
import IncidentStateTimeline from "Common/Models/DatabaseModels/IncidentStateTimeline";
import Monitor from "Common/Models/DatabaseModels/Monitor";
import User from "Common/Models/DatabaseModels/User";
import IncidentLogService from "Common/Server/Services/IncidentLogService";
import { IncidentLogEventType } from "Common/Models/DatabaseModels/IncidentLog";
import { Blue500 } from "Common/Types/BrandColors";

RunCron(
"IncidentOwner:SendStateChangeEmail",
Expand Down Expand Up @@ -52,6 +55,8 @@ RunCron(
});

for (const incidentStateTimeline of incidentStateTimelines) {
let moreIncidentLogInformationInMarkdown: string = "";

const incidentId: ObjectID = incidentStateTimeline.incidentId!;

if (!incidentId) {
Expand Down Expand Up @@ -207,7 +212,18 @@ RunCron(
eventType:
NotificationSettingEventType.SEND_INCIDENT_STATE_CHANGED_OWNER_NOTIFICATION,
});

moreIncidentLogInformationInMarkdown += `User notified: ${user.name} (${user.email})\n`;
}

await IncidentLogService.createIncidentLog({
incidentId: incident.id!,
projectId: incident.projectId!,
incidentLogEventType: IncidentLogEventType.OwnerNotificationSent,
displayColor: Blue500,
logInMarkdown: `Owners have been notified about the state change of the incident.`,
moreInformationInMarkdown: moreIncidentLogInformationInMarkdown,
});
}
},
);
13 changes: 13 additions & 0 deletions Worker/Jobs/IncidentPublicNote/SendNotificationToSubscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import StatusPage from "Common/Models/DatabaseModels/StatusPage";
import StatusPageResource from "Common/Models/DatabaseModels/StatusPageResource";
import StatusPageSubscriber from "Common/Models/DatabaseModels/StatusPageSubscriber";
import StatusPageEventType from "Common/Types/StatusPage/StatusPageEventType";
import IncidentLogService from "Common/Server/Services/IncidentLogService";
import { IncidentLogEventType } from "Common/Models/DatabaseModels/IncidentLog";
import { Blue500 } from "Common/Types/BrandColors";

RunCron(
"IncidentPublicNote:SendNotificationToSubscribers",
Expand Down Expand Up @@ -273,6 +276,16 @@ RunCron(
}
}
}

await IncidentLogService.createIncidentLog({
incidentId: incident.id!,
projectId: incident.projectId!,
incidentLogEventType: IncidentLogEventType.SubscriberNotificationSent,
displayColor: Blue500,
logInMarkdown: `**Notification sent to subscribers** for public note added to this Incident.`,
moreInformationInMarkdown: `**Public Note:**
${incidentPublicNote.note}`,
});
}
},
);
Loading

0 comments on commit fb5646e

Please sign in to comment.