Skip to content

Commit

Permalink
creating new object for alert condition initialization (#255) (#271)
Browse files Browse the repository at this point in the history
Signed-off-by: Amardeepsingh Siglani <[email protected]>

Signed-off-by: Amardeepsingh Siglani <[email protected]>
(cherry picked from commit ee374d5)

Co-authored-by: Amardeepsingh Siglani <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and amsiglan authored Dec 30, 2022
1 parent fc1acc4 commit 92949c7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ import {
} from '@elastic/eui';
import { Detector } from '../../../../../../../models/interfaces';
import { AlertCondition } from '../../../../../../../models/interfaces';
import { createSelectedOptions, parseAlertSeverityToOption } from '../../utils/helpers';
import { ALERT_SEVERITY_OPTIONS, EMPTY_DEFAULT_ALERT_CONDITION } from '../../utils/constants';
import {
createSelectedOptions,
getEmptyAlertCondition,
parseAlertSeverityToOption,
} from '../../utils/helpers';
import { ALERT_SEVERITY_OPTIONS } from '../../utils/constants';
import { CreateDetectorRulesOptions } from '../../../../../../models/types';
import { NotificationChannelOption, NotificationChannelTypeOptions } from '../../models/interfaces';
import { NOTIFICATIONS_HREF } from '../../../../../../utils/constants';
Expand Down Expand Up @@ -247,7 +251,7 @@ export default class AlertConditionPanel extends Component<

render() {
const {
alertCondition = EMPTY_DEFAULT_ALERT_CONDITION,
alertCondition = getEmptyAlertCondition(),
allNotificationChannels,
indexNum,
loadingNotifications,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import {
EuiText,
} from '@elastic/eui';
import { createDetectorSteps } from '../../../utils/constants';
import { EMPTY_DEFAULT_ALERT_CONDITION, MAX_ALERT_CONDITIONS } from '../utils/constants';
import { MAX_ALERT_CONDITIONS } from '../utils/constants';
import AlertConditionPanel from '../components/AlertCondition';
import { Detector } from '../../../../../../models/interfaces';
import { DetectorCreationStep } from '../../../models/types';
import { CreateDetectorRulesOptions } from '../../../../../models/types';
import { NotificationChannelTypeOptions } from '../models/interfaces';
import { getNotificationChannels, parseNotificationChannelsToOptions } from '../utils/helpers';
import {
getEmptyAlertCondition,
getNotificationChannels,
parseNotificationChannelsToOptions,
} from '../utils/helpers';
import { NotificationsService } from '../../../../../services';

interface ConfigureAlertsProps extends RouteComponentProps {
Expand Down Expand Up @@ -71,7 +75,7 @@ export default class ConfigureAlerts extends Component<ConfigureAlertsProps, Con
detector,
detector: { triggers },
} = this.props;
triggers.push(EMPTY_DEFAULT_ALERT_CONDITION);
triggers.push(getEmptyAlertCondition());
changeDetector({ ...detector, triggers });
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { AlertCondition, TriggerAction } from '../../../../../../models/interfaces';

export const MAX_ALERT_CONDITIONS = 10;
export const MIN_ALERT_CONDITIONS = 0;

Expand All @@ -27,35 +24,6 @@ export const RULE_SEVERITY_OPTIONS = {
INFORMATIONAL: { id: '5', value: 'informational', label: 'Info', text: 'Info' },
};

export const EMPTY_DEFAULT_TRIGGER_ACTION: TriggerAction = {
id: '',
name: '',
destination_id: '',
subject_template: {
source: '',
lang: 'mustache',
},
message_template: {
source: '',
lang: 'mustache',
},
throttle_enabled: false,
throttle: {
value: 10,
unit: 'MINUTES',
},
};

export const EMPTY_DEFAULT_ALERT_CONDITION: AlertCondition = {
name: '',
sev_levels: [],
tags: [],
actions: [EMPTY_DEFAULT_TRIGGER_ACTION],
types: [],
severity: '1',
ids: [],
};

export const MIN_NUM_NOTIFICATION_CHANNELS = 1;
export const MAX_NUM_NOTIFICATION_CHANNELS = 5;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ALERT_SEVERITY_OPTIONS, CHANNEL_TYPES } from './constants';
import { FeatureChannelList } from '../../../../../../server/models/interfaces/Notifications';
import { NotificationChannelTypeOptions } from '../models/interfaces';
import { NotificationsService } from '../../../../../services';
import { AlertCondition, TriggerAction } from '../../../../../../models/interfaces';

export const parseAlertSeverityToOption = (severity: string): EuiComboBoxOptionOption<string> => {
return Object.values(ALERT_SEVERITY_OPTIONS).find(
Expand Down Expand Up @@ -48,3 +49,34 @@ export function parseNotificationChannelsToOptions(
options: allOptions.filter((channel) => channel.type === type),
}));
}

export function getEmptyAlertCondition(): AlertCondition {
const emptyTriggerAction: TriggerAction = {
id: '',
name: '',
destination_id: '',
subject_template: {
source: '',
lang: 'mustache',
},
message_template: {
source: '',
lang: 'mustache',
},
throttle_enabled: false,
throttle: {
value: 10,
unit: 'MINUTES',
},
};

return {
name: '',
sev_levels: [],
tags: [],
actions: [emptyTriggerAction],
types: [],
severity: '1',
ids: [],
};
}
5 changes: 2 additions & 3 deletions public/pages/Findings/components/CreateAlertFlyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ import {
EuiFlyoutBody,
EuiFlyoutHeader,
EuiFormRow,
EuiLink,
EuiSpacer,
EuiText,
EuiTitle,
} from '@elastic/eui';
import AlertConditionPanel from '../../CreateDetector/components/ConfigureAlerts/components/AlertCondition';
import { AlertCondition, Detector } from '../../../../models/interfaces';
import { EMPTY_DEFAULT_ALERT_CONDITION } from '../../CreateDetector/components/ConfigureAlerts/utils/constants';
import { DetectorsService } from '../../../services';
import { RulesSharedState } from '../../../models/interfaces';
import { DEFAULT_EMPTY_DATA } from '../../../utils/constants';
import { NotificationChannelTypeOptions } from '../../CreateDetector/components/ConfigureAlerts/models/interfaces';
import { Finding } from '../models/interfaces';
import { getEmptyAlertCondition } from '../../CreateDetector/components/ConfigureAlerts/utils/helpers';

interface CreateAlertFlyoutProps extends RouteComponentProps {
closeFlyout: (refreshPage?: boolean) => void;
Expand All @@ -52,7 +51,7 @@ export default class CreateAlertFlyout extends Component<
constructor(props: CreateAlertFlyoutProps) {
super(props);
this.state = {
alertCondition: EMPTY_DEFAULT_ALERT_CONDITION,
alertCondition: getEmptyAlertCondition(),
loading: false,
detector: this.props.finding.detector._source,
submitting: false,
Expand Down

0 comments on commit 92949c7

Please sign in to comment.