Skip to content

Commit

Permalink
style: rename 'buildAlertMessageTemplateBody'
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 committed Feb 27, 2024
1 parent 8245c93 commit 7a99b4f
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions packages/api/src/tasks/checkAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,6 @@ const notifyChannel = async ({
throw new Error(`Unsupported channel type: ${channel}`);
}
};
const renderAlertTemplate = (
template: string,
view: AlertMessageTemplateDefaultView,
) => {
// Init the template engine
// need to seprate environments
const handlebars = Handlebars.create();
const compiledTemplate = handlebars.compile(template);
return compiledTemplate(view);
};
export const buildAlertMessageTemplateHdxLink = ({
alert,
dashboard,
Expand Down Expand Up @@ -224,22 +214,22 @@ export const buildAlertMessageTemplateTitle = ({
view: AlertMessageTemplateDefaultView;
}) => {
const { alert, dashboard, savedSearch, value } = view;
const handlebars = Handlebars.create();
if (alert.source === 'search') {
if (savedSearch == null) {
throw new Error('Source is LOG but logView is null');
}

// TODO: using template engine to render the title
return template
? renderAlertTemplate(template, view)
? handlebars.compile(template)(view)
: `Alert for "${savedSearch.name}" - ${value} lines found`;
} else if (alert.source === 'chart') {
if (dashboard == null) {
throw new Error('Source is CHART but dashboard is null');
}
const chart = dashboard.charts[0];
return template
? renderAlertTemplate(template, view)
? handlebars.compile(template)(view)
: `Alert for "${chart.name}" in "${dashboard.name}" - ${value} ${
doesExceedThreshold(
alert.threshold_type === 'above',
Expand Down Expand Up @@ -276,7 +266,7 @@ export const translateExternalActionsToInternal = (template: string) => {
};

// this method will build the body of the alert message and will be used to send the alert to the channel
export const buildAlertMessageTemplateBody = async ({
export const renderAlertTemplate = async ({
template,
title,
view,
Expand All @@ -299,7 +289,7 @@ export const buildAlertMessageTemplateBody = async ({
const defaultExternalAction = getDefaultExternalAction(alert);
const targetTemplate =
defaultExternalAction !== null
? // if the default external action is used, we won't use the actions from the template
? // if the default external action is used, actions in the template won't be used
`${template ?? ''} ${translateExternalActionsToInternal(
defaultExternalAction,
)}`
Expand Down Expand Up @@ -462,7 +452,7 @@ const fireChannelEvent = async ({
value: totalCount,
};

await buildAlertMessageTemplateBody({
await renderAlertTemplate({
title: buildAlertMessageTemplateTitle({
template: alert.name,
view: templateView,
Expand Down

0 comments on commit 7a99b4f

Please sign in to comment.