Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alert template fields to external alert API #322

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/api/src/controllers/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export type AlertInput = {
type: AlertType;
threshold: number;

// Message template
templateTitle?: string | null;
templateBody?: string | null;

// Log alerts
groupBy?: string;
logViewId?: string;
Expand Down Expand Up @@ -86,6 +90,14 @@ const makeAlert = (alert: AlertInput) => {
source: alert.source,
threshold: alert.threshold,
type: alert.type,

// Message template
// If they're undefined/null, set it to null so we clear out the field
// due to mongoose behavior:
// https://mongoosejs.com/docs/migrating_to_6.html#removed-omitundefined
templateTitle: alert.templateTitle == null ? null : alert.templateTitle,
templateBody: alert.templateBody == null ? null : alert.templateBody,

// Log alerts
logView: alert.logViewId,
groupBy: alert.groupBy,
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,15 @@ export const makeExternalAlert = ({
chartId,
threshold = 8,
interval = '15m',
name,
message,
}: {
dashboardId: string;
chartId: string;
threshold?: number;
interval?: '15m' | '1m' | '5m' | '30m' | '1h' | '6h' | '12h' | '1d';
name?: string;
message?: string;
}): z.infer<typeof externalAlertSchema> => ({
channel: {
type: 'slack_webhook',
Expand All @@ -418,4 +422,6 @@ export const makeExternalAlert = ({
source: 'chart',
dashboardId,
chartId,
name,
message,
});
4 changes: 2 additions & 2 deletions packages/api/src/models/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export interface IAlert {
type: AlertType;

// Message template
templateTitle?: string;
templateBody?: string;
templateTitle?: string | null;
templateBody?: string | null;

// Log alerts
groupBy?: string;
Expand Down
22 changes: 21 additions & 1 deletion packages/api/src/routers/external-api/__tests__/alerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ describe('/api/v1/alerts', () => {
// Create alerts for all charts
const dashboard = initialDashboards.body.data[0];
await Promise.all(
dashboard.charts.map(chart =>
dashboard.charts.map((chart, i) =>
agent
.post('/api/v1/alerts')
.set('Authorization', `Bearer ${user?.accessKey}`)
.send(
makeExternalAlert({
dashboardId: dashboard._id,
chartId: chart.id,
...(i % 2 == 0
? {
name: 'test {{hello}}',
message: 'my message {{hello}}',
}
: undefined),
}),
)
.expect(200),
Expand Down Expand Up @@ -92,6 +98,8 @@ Array [
},
"chartId": "aaaaaaa",
"interval": "15m",
"message": "my message {{hello}}",
"name": "test {{hello}}",
"source": "chart",
"threshold": 8,
"threshold_type": "above",
Expand All @@ -103,6 +111,8 @@ Array [
},
"chartId": "bbbbbbb",
"interval": "15m",
"message": null,
"name": null,
"source": "chart",
"threshold": 8,
"threshold_type": "above",
Expand All @@ -114,6 +124,8 @@ Array [
},
"chartId": "ccccccc",
"interval": "15m",
"message": "my message {{hello}}",
"name": "test {{hello}}",
"source": "chart",
"threshold": 8,
"threshold_type": "above",
Expand All @@ -125,6 +137,8 @@ Array [
},
"chartId": "ddddddd",
"interval": "15m",
"message": null,
"name": null,
"source": "chart",
"threshold": 8,
"threshold_type": "above",
Expand All @@ -136,6 +150,8 @@ Array [
},
"chartId": "eeeeeee",
"interval": "15m",
"message": "my message {{hello}}",
"name": "test {{hello}}",
"source": "chart",
"threshold": 8,
"threshold_type": "above",
Expand Down Expand Up @@ -179,6 +195,8 @@ Object {
"webhookId": "65ad876b6b08426ab4ba7830",
},
"interval": "1h",
"message": null,
"name": null,
"source": "chart",
"threshold": 1000,
"threshold_type": "above",
Expand All @@ -200,6 +218,8 @@ Object {
"webhookId": "65ad876b6b08426ab4ba7830",
},
"interval": "1h",
"message": null,
"name": null,
"source": "chart",
"threshold": 1000,
"threshold_type": "above",
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/tasks/checkAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const buildAlertMessageTemplateTitle = ({
template,
view,
}: {
template?: string;
template?: string | null;
view: AlertMessageTemplateDefaultView;
}) => {
const { alert, dashboard, savedSearch, value } = view;
Expand Down Expand Up @@ -210,7 +210,7 @@ export const buildAlertMessageTemplateBody = async ({
template,
view,
}: {
template?: string;
template?: string | null;
view: AlertMessageTemplateDefaultView;
}) => {
const {
Expand Down
9 changes: 8 additions & 1 deletion packages/api/src/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ export const alertSchema = z
threshold: z.number().min(0),
type: z.enum(['presence', 'absence']),
source: z.enum(['LOG', 'CHART']).default('LOG'),
templateTitle: z.string().min(1).max(512).nullish(),
templateBody: z.string().min(1).max(4096).nullish(),
})
.and(zLogAlert.or(zChartAlert));

Expand All @@ -231,7 +233,6 @@ export const externalSearchAlertSchema = z.object({
source: z.literal('search'),
groupBy: z.string().optional(),
savedSearchId: objectIdSchema,
message: z.string().optional(),
});

export const externalChartAlertSchema = z.object({
Expand All @@ -247,6 +248,8 @@ export const externalAlertSchema = z
threshold: z.number().min(0),
threshold_type: z.enum(['above', 'below']),
source: z.enum(['search', 'chart']).default('search'),
name: z.string().min(1).max(512).nullish(),
message: z.string().min(1).max(4096).nullish(),
})
.and(externalSearchAlertSchema.or(externalChartAlertSchema));

Expand All @@ -268,6 +271,8 @@ export const translateExternalAlertToInternalAlert = (
...alertInput.channel,
type: 'webhook',
},
templateTitle: alertInput.name,
templateBody: alertInput.message,
...(alertInput.source === 'search' && alertInput.savedSearchId
? { source: 'LOG', logViewId: alertInput.savedSearchId }
: alertInput.source === 'chart' && alertInput.dashboardId
Expand All @@ -293,6 +298,8 @@ export const translateAlertDocumentToExternalAlert = (
...alertDoc.channel,
type: 'slack_webhook',
},
name: alertDoc.templateTitle,
message: alertDoc.templateBody,
...(alertDoc.source === 'LOG' && alertDoc.logView
? { source: 'search', savedSearchId: alertDoc.logView.toString() }
: alertDoc.source === 'CHART' && alertDoc.dashboardId
Expand Down
Loading