From e8a72d72e8731ca9e657814625cfc7242478b1f1 Mon Sep 17 00:00:00 2001 From: Riya Saxena Date: Tue, 18 Jun 2024 12:23:43 -0700 Subject: [PATCH] fixed bugs and removed console.log Signed-off-by: Riya Saxena --- .../containers/CreateCorrelationRule.tsx | 18 ++++++++++++++++++ public/store/CorrelationsStore.ts | 14 +------------- types/Correlations.ts | 2 +- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/public/pages/Correlations/containers/CreateCorrelationRule.tsx b/public/pages/Correlations/containers/CreateCorrelationRule.tsx index bd007454..5c9478f1 100644 --- a/public/pages/Correlations/containers/CreateCorrelationRule.tsx +++ b/public/pages/Correlations/containers/CreateCorrelationRule.tsx @@ -57,6 +57,8 @@ import { NotificationsCallOut } from '../../../../public/components/Notification import { BrowserServices } from '../../../../public/models/interfaces'; import { ExperimentalBanner } from '../components/ExperimentalBanner'; import { ALERT_SEVERITY_OPTIONS } from '../../CreateDetector/components/ConfigureAlerts/utils/constants'; +import uuid from 'uuid'; +import { randomUUID } from 'crypto'; export interface CreateCorrelationRuleProps extends DataSourceProps { indexService: IndexService; @@ -357,6 +359,8 @@ export const CreateCorrelationRule: React.FC = ( const submit = async (values: CorrelationRuleModel) => { + const randomTriggerId = uuid(); + const randomActionId = uuid(); let error; if ((error = validateCorrelationRule(values))) { errorNotificationToast(props.notifications, action, 'rule', error); @@ -375,6 +379,20 @@ export const CreateCorrelationRule: React.FC = ( }); } + // Modify or set default values for trigger if present + if (values.trigger) { + // Set default values for ids + values.trigger.id = randomTriggerId; + values.trigger.ids?.push(randomTriggerId); + // Set default values for actions if present + if (values.trigger.actions) { + values.trigger.actions.forEach((action) => { + action.id = randomActionId; + action.name = randomActionId; + }); + } + } + if (action === 'Edit') { await correlationStore.updateCorrelationRule(values as CorrelationRule); } else { diff --git a/public/store/CorrelationsStore.ts b/public/store/CorrelationsStore.ts index 08d6ab06..821cd629 100644 --- a/public/store/CorrelationsStore.ts +++ b/public/store/CorrelationsStore.ts @@ -19,7 +19,6 @@ import { NotificationsStart } from 'opensearch-dashboards/public'; import { errorNotificationToast } from '../utils/helpers'; import { DEFAULT_EMPTY_DATA } from '../utils/constants'; import { DataStore } from './DataStore'; -import uuid from 'uuid'; export interface ICorrelationsCache { [key: string]: CorrelationRule[]; @@ -57,7 +56,6 @@ export class CorrelationsStore implements ICorrelationsStore { } public async createCorrelationRule(correlationRule: CorrelationRule): Promise { - const randomUUID = uuid(); const response = await this.service.createCorrelationRule({ name: correlationRule.name, time_window: correlationRule.time_window, @@ -82,17 +80,7 @@ export class CorrelationsStore implements ICorrelationsStore { return correlationInput; }), - trigger: correlationRule.trigger ? { - name: correlationRule.trigger.name, - id: correlationRule.trigger.name ? randomUUID : undefined, - sev_levels: correlationRule.trigger.sev_levels, - ids: correlationRule.trigger.ids ? correlationRule.trigger.ids.map(() => randomUUID) : [], // Update ids with the same UUID - severity: correlationRule.trigger.severity, - actions: correlationRule.trigger.actions ? correlationRule.trigger.actions.map((action) => ({ - ...action, - id: uuid(), // Update each action's id with the same UUID - })) : undefined, - } : undefined, + trigger: correlationRule.trigger }); if (!response.ok) { errorNotificationToast(this.notifications, 'create', 'correlation rule', response.error); diff --git a/types/Correlations.ts b/types/Correlations.ts index 5bedf6ab..0e35c25e 100644 --- a/types/Correlations.ts +++ b/types/Correlations.ts @@ -54,7 +54,7 @@ export interface CorrelationRuleModel { name: string; time_window: number; // Time in milliseconds queries: CorrelationRuleQuery[]; - trigger?: CorrelationRuleTrigger | undefined; + trigger: CorrelationRuleTrigger | undefined; } export interface CorrelationRule extends CorrelationRuleModel {