Skip to content

Commit

Permalink
add migration for alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Oct 2, 2024
1 parent 249a9d9 commit b624bd4
Show file tree
Hide file tree
Showing 12 changed files with 599 additions and 72 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { MigrationName1727193702212 } from "./1727193702212-MigrationName";
import { MigrationName1727194211048 } from "./1727194211048-MigrationName";
import { MigrationName1727194579925 } from "./1727194579925-MigrationName";
import { MigrationName1727894983857 } from "./1727894983857-MigrationName";
import { MigrationName1727906598804 } from "./1727906598804-MigrationName";

export default [
InitialMigration,
Expand Down Expand Up @@ -148,4 +149,5 @@ export default [
MigrationName1727194211048,
MigrationName1727194579925,
MigrationName1727894983857,
MigrationName1727906598804,
];
3 changes: 1 addition & 2 deletions Common/Types/Email/EmailTemplateType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ enum EmailTemplateType {
IncidentOwnerNotePosted = "IncidentOwnerNotePosted.hbs",
IncidentOwnerResourceCreated = "IncidentOwnerResourceCreated.hbs",


AlertOwnerAdded = "AlertOwnerAdded.hbs",
AlertOwnerStateChanged = "AlertOwnerStateChanged.hbs",
AlertOwnerNotePosted = "AlertOwnerNotePosted.hbs",
AlertOwnerResourceCreated = "AlertOwnerResourceCreated.hbs",

ScheduledMaintenanceOwnerNotePosted = "ScheduledMaintenanceOwnerNotePosted.hbs",
ScheduledMaintenanceOwnerAdded = "ScheduledMaintenanceOwnerAdded.hbs",
ScheduledMaintenanceOwnerStateChanged = "ScheduledMaintenanceOwnerStateChanged.hbs",
Expand Down
39 changes: 18 additions & 21 deletions Common/Types/Monitor/MonitorCriteriaInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface MonitorCriteriaInstanceType {
description: string;
changeMonitorStatus?: boolean | undefined;
createIncidents?: boolean | undefined;
createAlerts?: boolean | undefined;
createAlerts?: boolean | undefined;
id: string;
}

Expand All @@ -46,7 +46,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
createIncidents: false,
createAlerts: false,
createAlerts: false,
changeMonitorStatus: false,
incidents: [],
alerts: [],
Expand Down Expand Up @@ -77,7 +77,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
],
incidents: [],
alerts: [],
createAlerts: false,
createAlerts: false,
changeMonitorStatus: true,
createIncidents: false,
name: `Check if ${arg.monitorName} is online`,
Expand Down Expand Up @@ -106,7 +106,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
alerts: [],
changeMonitorStatus: true,
createIncidents: false,
createAlerts: false,
createAlerts: false,
name: `Check if ${arg.monitorName} is online`,
description: `This criteria checks if the ${arg.monitorName} is online`,
};
Expand All @@ -130,8 +130,8 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
incidents: [],
alerts: [],
createAlerts: false,
alerts: [],
createAlerts: false,
changeMonitorStatus: true,
createIncidents: false,
name: `Check if ${arg.monitorName} is online`,
Expand All @@ -158,7 +158,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
],
incidents: [],
alerts: [],
createAlerts: false,
createAlerts: false,
changeMonitorStatus: true,
createIncidents: false,
name: `Check if ${arg.monitorName} is online`,
Expand Down Expand Up @@ -187,7 +187,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
incidents: [],
alerts: [],
alerts: [],
createAlerts: false,
changeMonitorStatus: true,
createIncidents: false,
Expand Down Expand Up @@ -215,7 +215,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
],
incidents: [],
alerts: [],
createAlerts: false,
createAlerts: false,
changeMonitorStatus: true,
createIncidents: false,
name: `Check if ${arg.monitorName} is online`,
Expand Down Expand Up @@ -248,7 +248,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
],
incidents: [],
alerts: [],
createAlerts: false,
createAlerts: false,
changeMonitorStatus: true,
createIncidents: false,
name: `Check if ${arg.monitorName} is online`,
Expand Down Expand Up @@ -310,7 +310,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
],
changeMonitorStatus: true,
createIncidents: true,
createAlerts: false,
createAlerts: false,
alerts: [],
name: `Check if ${arg.monitorName} is offline`,
description: `This criteria checks if the ${arg.monitorName} is offline`,
Expand Down Expand Up @@ -338,7 +338,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
alerts: [],
createAlerts: false,
createAlerts: false,
incidents: [
{
title: `${arg.monitorName} is offline`,
Expand Down Expand Up @@ -379,7 +379,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
alerts: [],
createAlerts: false,
createAlerts: false,
changeMonitorStatus: true,
createIncidents: true,
name: `Check if ${arg.monitorName} is offline`,
Expand All @@ -400,7 +400,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
alerts: [],
createAlerts: false,
createAlerts: false,
incidents: [
{
title: `${arg.monitorName} is offline`,
Expand Down Expand Up @@ -431,7 +431,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
alerts: [],
createAlerts: false,
createAlerts: false,
incidents: [
{
title: `${arg.monitorName} is offline`,
Expand Down Expand Up @@ -465,7 +465,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
},
],
alerts: [],
createAlerts: false,
createAlerts: false,
incidents: [
{
title: `${arg.monitorName} is offline`,
Expand All @@ -489,7 +489,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
monitorStatusId: arg.monitorStatusId,
filterCondition: FilterCondition.Any,
alerts: [],
createAlerts: false,
createAlerts: false,
filters: [
{
checkOn: CheckOn.IsNotAValidCertificate,
Expand Down Expand Up @@ -659,10 +659,7 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
return this;
}


public setAlerts(
alerts: Array<CriteriaAlert>,
): MonitorCriteriaInstance {
public setAlerts(alerts: Array<CriteriaAlert>): MonitorCriteriaInstance {
if (this.data) {
this.data.alerts = [...alerts];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const MonitorCriteriaAlertsForm: FunctionComponent<ComponentProps> = (
return (
<MonitorCriteriaAlertForm
key={index}
alertSeverityDropdownOptions={
props.alertSeverityDropdownOptions
}
alertSeverityDropdownOptions={props.alertSeverityDropdownOptions}
onCallPolicyDropdownOptions={props.onCallPolicyDropdownOptions}
initialValue={i}
// onDelete={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ const MonitorCriteriaInstanceElement: FunctionComponent<ComponentProps> = (
(props.initialValue?.data?.incidents?.length || 0) > 0,
);


const [showAlertControl, setShowAlertControl] = useState<boolean>(
(props.initialValue?.data?.alerts?.length || 0) > 0,
);
Expand Down Expand Up @@ -293,9 +292,7 @@ const MonitorCriteriaInstanceElement: FunctionComponent<ComponentProps> = (
</div>
)}



<div className="mt-4">
<div className="mt-4">
<Toggle
value={showAlertControl}
title="When filters match, create an alert."
Expand Down Expand Up @@ -333,9 +330,7 @@ const MonitorCriteriaInstanceElement: FunctionComponent<ComponentProps> = (

<MonitorCriteriaAlertsForm
initialValue={monitorCriteriaInstance?.data?.alerts || []}
alertSeverityDropdownOptions={
props.alertSeverityDropdownOptions
}
alertSeverityDropdownOptions={props.alertSeverityDropdownOptions}
onCallPolicyDropdownOptions={props.onCallPolicyDropdownOptions}
onChange={(value: Array<CriteriaAlert>) => {
monitorCriteriaInstance.setAlerts(value);
Expand Down
9 changes: 3 additions & 6 deletions Dashboard/src/Components/Form/Monitor/MonitorSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ const MonitorStepsElement: FunctionComponent<ComponentProps> = (
const [incidentSeverityDropdownOptions, setIncidentSeverityDropdownOptions] =
React.useState<Array<DropdownOption>>([]);


const [alertSeverityDropdownOptions, setAlertSeverityDropdownOptions] =
const [alertSeverityDropdownOptions, setAlertSeverityDropdownOptions] =
React.useState<Array<DropdownOption>>([]);

const [onCallPolicyDropdownOptions, setOnCallPolicyDropdownOptions] =
Expand Down Expand Up @@ -99,8 +98,7 @@ const MonitorStepsElement: FunctionComponent<ComponentProps> = (
},
});


const alertSeverityList: ListResult<AlertSeverity> =
const alertSeverityList: ListResult<AlertSeverity> =
await ModelAPI.getList({
modelType: AlertSeverity,
query: {},
Expand Down Expand Up @@ -138,7 +136,6 @@ const MonitorStepsElement: FunctionComponent<ComponentProps> = (
);
}


if (alertSeverityList.data) {
setAlertSeverityDropdownOptions(
alertSeverityList.data.map((i: AlertSeverity) => {
Expand Down Expand Up @@ -184,7 +181,7 @@ const MonitorStepsElement: FunctionComponent<ComponentProps> = (
},
)!.id!,
defaultIncidentSeverityId: incidentSeverityList.data[0]!.id!,
defaultAlertSeverityId: alertSeverityList.data[0]!.id!
defaultAlertSeverityId: alertSeverityList.data[0]!.id!,
}),
);
}
Expand Down
10 changes: 3 additions & 7 deletions Worker/Jobs/AlertOwners/SendCreatedResourceNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ RunCron(
alert.description! || "",
MarkdownContentType.Email,
),
resourcesAffected:
alert.monitor?.name || "None",
resourcesAffected: alert.monitor?.name || "None",
alertSeverity: alert.alertSeverity!.name!,
declaredAt: OneUptimeDate.getDateAsFormattedHTMLInMultipleTimezones(
{
Expand All @@ -134,8 +133,7 @@ RunCron(
)) || "",
rootCause:
(await Markdown.convertToHTML(
alert.rootCause ||
"No root cause identified for this alert",
alert.rootCause || "No root cause identified for this alert",
MarkdownContentType.Email,
)) || "",
alertViewLink: (
Expand Down Expand Up @@ -178,9 +176,7 @@ RunCron(
NotificationSettingEventType.SEND_INCIDENT_CREATED_OWNER_NOTIFICATION,
});
} catch (e) {
logger.error(
"Error in sending alert created resource notification",
);
logger.error("Error in sending alert created resource notification");
logger.error(e);
}
}
Expand Down
7 changes: 2 additions & 5 deletions Worker/Jobs/AlertOwners/SendNotePostedNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ RunCron(
"AlertOwner:SendsNotePostedEmail",
{ schedule: EVERY_MINUTE, runOnStartup: false },
async () => {

const privateNotes: Array<AlertInternalNote> =
await AlertInternalNoteService.findBy({
query: {
Expand All @@ -47,7 +46,6 @@ RunCron(
},
);


for (const note of privateNotes) {
await AlertInternalNoteService.updateOneById({
id: note.id!,
Expand All @@ -60,7 +58,7 @@ RunCron(
});
}

const notes: Array<BaseModel> = [ ...privateNotes];
const notes: Array<BaseModel> = [...privateNotes];

for (const noteObject of notes) {
const note: BaseModel = noteObject as BaseModel;
Expand Down Expand Up @@ -124,8 +122,7 @@ RunCron(
(note.getColumnValue("note")! as string) || "",
MarkdownContentType.Email,
),
resourcesAffected:
alert.monitor?.name || "None",
resourcesAffected: alert.monitor?.name || "None",
alertSeverity: alert.alertSeverity!.name!,
alertViewLink: (
await AlertService.getAlertLinkInDashboard(
Expand Down
3 changes: 1 addition & 2 deletions Worker/Jobs/AlertOwners/SendOwnerAddedNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ RunCron(
alert.description! || "",
MarkdownContentType.Email,
),
resourcesAffected:
alert.monitor?.name || "None",
resourcesAffected: alert.monitor?.name || "None",
alertSeverity: alert.alertSeverity!.name!,
alertViewLink: (
await AlertService.getAlertLinkInDashboard(
Expand Down
31 changes: 13 additions & 18 deletions Worker/Jobs/AlertOwners/SendStateChangeNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@ RunCron(
const alertState: AlertState = alertStateTimeline.alertState!;

// get alert severity
const alertWithSeverity: Alert | null =
await AlertService.findOneById({
id: alert.id!,
props: {
isRoot: true,
},
select: {
_id: true,
alertSeverity: {
name: true,
},
const alertWithSeverity: Alert | null = await AlertService.findOneById({
id: alert.id!,
props: {
isRoot: true,
},
select: {
_id: true,
alertSeverity: {
name: true,
},
});
},
});

if (!alertWithSeverity) {
continue;
Expand All @@ -119,9 +118,7 @@ RunCron(
doesResourceHasOwners = false;

// find project owners.
owners = await ProjectService.getOwners(
alertStateTimeline.projectId!,
);
owners = await ProjectService.getOwners(alertStateTimeline.projectId!);
}

if (owners.length === 0) {
Expand All @@ -137,9 +134,7 @@ RunCron(
alert.description! || "",
MarkdownContentType.Email,
),
resourcesAffected:
alert
.monitor?.name || "",
resourcesAffected: alert.monitor?.name || "",
stateChangedAt:
OneUptimeDate.getDateAsFormattedHTMLInMultipleTimezones({
date: alertStateTimeline.createdAt!,
Expand Down
Loading

0 comments on commit b624bd4

Please sign in to comment.