Skip to content

Commit

Permalink
Updated context type being passed to detector notification form
Browse files Browse the repository at this point in the history
  • Loading branch information
nishtham-amazon committed Dec 21, 2024
1 parent 00d2e7f commit 4da8746
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
14 changes: 8 additions & 6 deletions public/components/Notifications/NotificationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
EuiCompressedTextArea,
EuiCompressedCheckbox,
} from '@elastic/eui';
import React, { useState } from 'react';
import React, { useCallback, useState } from 'react';
import { NOTIFICATIONS_HREF } from '../../utils/constants';
import { NotificationsCallOut } from '../NotificationsCallOut';
import {
Expand All @@ -28,7 +28,7 @@ import {
TriggerContext,
} from '../../../types';
import { getIsNotificationPluginInstalled } from '../../utils/helpers';
import Mustache from 'mustache';
import { render } from 'mustache';

export interface NotificationFormProps {
allNotificationChannels: NotificationChannelTypeOptions[];
Expand Down Expand Up @@ -58,8 +58,10 @@ export const NotificationForm: React.FC<NotificationFormProps> = ({
const hasNotificationPlugin = getIsNotificationPluginInstalled();
const [shouldSendNotification, setShouldSendNotification] = useState(!!action?.destination_id);
const selectedNotificationChannelOption: NotificationChannelOption[] = [];
const onDisplayPreviewChange = (e) => setDisplayPreview(e.target.checked);
const [displayPreview, setDisplayPreview] = useState(false);
const onDisplayPreviewChange = useCallback((e) => setDisplayPreview(e.target.checked), [
displayPreview,
]);
if (shouldSendNotification && action?.destination_id) {
allNotificationChannels.forEach((typeOption) => {
const matchingChannel = typeOption.options.find(
Expand All @@ -70,7 +72,7 @@ export const NotificationForm: React.FC<NotificationFormProps> = ({
}
let preview = '';
try {
preview = `${Mustache.render(action?.subject_template.source, context)}\n\n${Mustache.render(
preview = `${render(action?.subject_template.source, context)}\n\n${render(
action?.message_template.source,
context
)}`;
Expand Down Expand Up @@ -194,7 +196,7 @@ export const NotificationForm: React.FC<NotificationFormProps> = ({
onChange={onDisplayPreviewChange}
/>
</EuiFlexItem>
{displayPreview ? (
{displayPreview && (
<EuiFlexItem>
<EuiCompressedFormRow label="Message preview" fullWidth>
<EuiCompressedTextArea
Expand All @@ -205,7 +207,7 @@ export const NotificationForm: React.FC<NotificationFormProps> = ({
/>
</EuiCompressedFormRow>
</EuiFlexItem>
) : null}
)}
</EuiFlexGroup>
</EuiAccordion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
Detector,
NotificationChannelOption,
NotificationChannelTypeOptions,
TriggerContext,
} from '../../../../../../../types';
import { NotificationForm } from '../../../../../../components/Notifications/NotificationForm';
import { ALERT_SEVERITY_OPTIONS, DEFAULT_MESSAGE_SOURCE } from '../../../../../../utils/constants';
Expand Down Expand Up @@ -99,23 +100,25 @@ export default class AlertConditionPanel extends Component<
});
}

getTriggerContext = () => {
getTriggerContext = (): TriggerContext => {
const lineBreakAndTab = '\n\t';
const { alertCondition, detector } = this.props;
const detectorInput = detector.inputs[0].detector_input;
const detectorIndices = `${lineBreakAndTab}${detectorInput.indices.join(
`,${lineBreakAndTab}`
)}`;
return {
trigger: {
name: alertCondition.name,
severity:
parseAlertSeverityToOption(alertCondition.severity)?.label || alertCondition.severity,
},
detector: {
name: detector.name,
description: detectorInput.description,
datasources: detectorIndices,
ctx: {
trigger: {
name: alertCondition.name,
severity:
parseAlertSeverityToOption(alertCondition.severity)?.label || alertCondition.severity,
},
detector: {
name: detector.name,
description: detectorInput.description,
datasources: detectorIndices,
},
},
};
};
Expand Down Expand Up @@ -307,7 +310,6 @@ export default class AlertConditionPanel extends Component<
};

render() {
const context = this.getTriggerContext();
const {
alertCondition = getEmptyAlertCondition(),
allNotificationChannels,
Expand Down Expand Up @@ -559,9 +561,7 @@ export default class AlertConditionPanel extends Component<

<NotificationForm
action={alertCondition.actions[0]}
context={{
ctx: context,
}}
context={this.getTriggerContext()}
allNotificationChannels={allNotificationChannels}
loadingNotifications={loadingNotifications}
prepareMessage={this.prepareMessage}
Expand Down
4 changes: 0 additions & 4 deletions public/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ export const dataSourceObservable = new BehaviorSubject<DataSourceOption>({});

export const DATA_SOURCE_NOT_SET_ERROR = 'Data source is not set';

/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
export const DEFAULT_MESSAGE_SOURCE = {
MESSAGE_BODY: `- Triggered alert condition: {{ctx.trigger.name}}
- Severity: {{ctx.trigger.severity}}
Expand Down
18 changes: 10 additions & 8 deletions types/Alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ export interface TriggerAction {
}

export interface TriggerContext {
trigger: {
name: string;
severity: string;
};
detector: {
name: string;
description: string;
datasources: string;
ctx: {
trigger: {
name: string;
severity: string;
};
detector: {
name: string;
description: string;
datasources: string;
};
};
}

Expand Down

0 comments on commit 4da8746

Please sign in to comment.