Skip to content

Commit

Permalink
Add anomaly_threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaindik committed Nov 26, 2024
1 parent 8bddcba commit 8013dcd
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { UseField } from '../../../../shared_imports';
import { AnomalyThresholdSlider } from '../../../rule_creation_ui/components/anomaly_threshold_slider';

const componentProps = {
describedByIds: ['anomalyThreshold'],
};

interface AnomalyThresholdEditProps {
path: string;
}

export function AnomalyThresholdEdit({ path }: AnomalyThresholdEditProps): JSX.Element {
return (
<UseField path={path} component={AnomalyThresholdSlider} componentProps={componentProps} />
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export { AnomalyThresholdEdit } from './anomaly_threshold_edit';
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { StepRuleDescription } from '../description_step';
import type { QueryBarDefineRuleProps } from '../query_bar';
import { QueryBarDefineRule } from '../query_bar';
import { SelectRuleType } from '../select_rule_type';
import { AnomalyThresholdSlider } from '../anomaly_threshold_slider';
import { PickTimeline } from '../../../rule_creation/components/pick_timeline';
import { StepContentWrapper } from '../../../rule_creation/components/step_content_wrapper';
import { ThresholdInput } from '../threshold_input';
Expand Down Expand Up @@ -96,6 +95,7 @@ import {
import { ThresholdAlertSuppressionEdit } from '../../../rule_creation/components/threshold_alert_suppression_edit';
import { usePersistentAlertSuppressionState } from './use_persistent_alert_suppression_state';
import { MachineLearningJobIdEdit } from '../../../rule_creation/components/machine_learning_job_id_edit';
import { AnomalyThresholdEdit } from '../../../rule_creation/components/anomaly_threshold';

const CommonUseField = getUseField({ component: Field });

Expand Down Expand Up @@ -806,13 +806,7 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
<RuleTypeEuiFormRow $isVisible={isMlRule(ruleType)} fullWidth>
<>
<MachineLearningJobIdEdit path="machineLearningJobId" />
<UseField
path="anomalyThreshold"
component={AnomalyThresholdSlider}
componentProps={{
describedByIds: ['detectionEngineStepDefineRuleAnomalyThreshold'],
}}
/>
<AnomalyThresholdEdit path="anomalyThreshold" />
</>
</RuleTypeEuiFormRow>
<RuleTypeEuiFormRow
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { AnomalyThresholdEdit } from '../../../../../../../rule_creation/components/anomaly_threshold';

export function AnomalyThresholdAdapter(): JSX.Element {
return <AnomalyThresholdEdit path="anomalyThreshold" />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import type { FormData, FormSchema } from '../../../../../../../../shared_imports';
import { schema } from '../../../../../../../rule_creation_ui/components/step_define_rule/schema';
import { RuleFieldEditFormWrapper } from '../rule_field_edit_form_wrapper';
import { AnomalyThresholdAdapter } from './anomaly_threshold_adapter';
import type { AnomalyThreshold } from '../../../../../../../../../common/api/detection_engine';

interface AnomalyThresholdFormData {
anomalyThreshold: AnomalyThreshold;
}

export function AnomalyThresholdForm(): JSX.Element {
return (
<RuleFieldEditFormWrapper
component={AnomalyThresholdAdapter}
ruleFieldFormSchema={anomalyThresholdSchema}
deserializer={deserializer}
serializer={serializer}
/>
);
}

function deserializer(defaultValue: FormData): AnomalyThresholdFormData {
return {
anomalyThreshold: defaultValue.anomaly_threshold,
};
}

function serializer(formData: FormData): {
anomaly_threshold: AnomalyThresholdFormData;
} {
return {
anomaly_threshold: formData.anomalyThreshold,
};
}

const anomalyThresholdSchema = {
anomalyThreshold: schema.anomalyThreshold,
} as FormSchema<AnomalyThresholdFormData>;
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import React from 'react';
import type { UpgradeableMachineLearningFields } from '../../../../model/prebuilt_rule_upgrade/fields';
import { AnomalyThresholdForm } from './fields/anomaly_threshold/anomaly_threshold_form';
import { AlertSuppressionEditForm } from './fields/alert_suppression';
import { MachineLearningJobIdForm } from './fields/machine_learning_job_id/machine_learning_job_id_form';
import { assertUnreachable } from '../../../../../../../common/utility_types';

interface MachineLearningRuleFieldEditProps {
fieldName: UpgradeableMachineLearningFields;
Expand All @@ -18,9 +20,11 @@ export function MachineLearningRuleFieldEdit({ fieldName }: MachineLearningRuleF
switch (fieldName) {
case 'alert_suppression':
return <AlertSuppressionEditForm />;
case 'anomaly_threshold':
return <AnomalyThresholdForm />;
case 'machine_learning_job_id':
return <MachineLearningJobIdForm />;
default:
return null; // Will be replaced with `assertUnreachable(fieldName)` once all fields are implemented
return assertUnreachable(fieldName);
}
}

0 comments on commit 8013dcd

Please sign in to comment.