Skip to content

Commit

Permalink
fix EQL rule creation from a timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpn committed Nov 23, 2024
1 parent 5d470f2 commit f7eeda7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 119 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import type { FieldConfig } from '../../../../shared_imports';
import { UseField } from '../../../../shared_imports';
import type { FieldValueQueryBar } from '../../../rule_creation_ui/components/query_bar_field';
import { QueryBarField } from '../../../rule_creation_ui/components/query_bar_field';
import { esqlQueryRequiredValidatorFactory } from './validators/esql_query_required_validator_factory';
import { esqlQueryValidatorFactory } from './validators/esql_query_validator_factory';
import { EsqlInfoIcon } from './esql_info_icon';
import * as i18n from './translations';
import { esqlQueryRequiredValidatorFactory } from './validators/esql_query_required_validator_factory';

interface EsqlQueryEditProps {
path: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type { FieldSpec } from '@kbn/data-plugin/common';
import type { SavedQuery } from '@kbn/data-plugin/public';
import type { DataViewBase } from '@kbn/es-query';
import { FormattedMessage } from '@kbn/i18n-react';
import { useSetFieldValueWithCallback } from '../../../../common/utils/use_set_field_value_cb';
import type { SetRuleQuery } from '../../../../detections/containers/detection_engine/rules/use_rule_from_timeline';
import { useRuleFromTimeline } from '../../../../detections/containers/detection_engine/rules/use_rule_from_timeline';
import { isMlRule } from '../../../../../common/machine_learning/helpers';
Expand Down Expand Up @@ -196,12 +195,6 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
const alertSuppressionUpsellingMessage = useUpsellingMessage('alert_suppression_rule_form');
const { getFields, reset, setFieldValue } = form;

const setRuleTypeCallback = useSetFieldValueWithCallback({
field: 'ruleType',
value: ruleType,
setFieldValue,
});

// Callback for when user toggles between Data Views and Index Patterns
const onChangeDataSource = useCallback(
(optionId: string) => {
Expand Down Expand Up @@ -258,19 +251,26 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
const handleSetRuleFromTimeline = useCallback<SetRuleQuery>(
({ index: timelineIndex, queryBar: timelineQueryBar, eqlOptions }) => {
const setQuery = () => {
setPersistentEqlQuery(timelineQueryBar);
setFieldValue('index', timelineIndex);
setFieldValue('queryBar', timelineQueryBar);
};
if (timelineQueryBar.query.language === 'eql') {
setPersistentEqlQuery(timelineQueryBar);
setPersistentEqlOptions(eqlOptions ?? {});
setRuleTypeCallback('eql', setQuery);
setFieldValue('eqlOptions', eqlOptions ?? {});
setFieldValue('ruleType', 'eql');

// Forms needs to be re-rendered with a new rule type first
// setTimeout is used to delay setting rule type specific values.
// Without that form turns out in an "impossible" state.
setTimeout(() => {
setPersistentEqlOptions(eqlOptions ?? {});
setQuery();
setFieldValue('eqlOptions', eqlOptions ?? {});
});
} else {
setQuery();
}
},
[setFieldValue, setRuleTypeCallback, setPersistentEqlQuery, setPersistentEqlOptions]
[setFieldValue, setPersistentEqlQuery, setPersistentEqlOptions]
);

const { onOpenTimeline, loading: timelineQueryLoading } =
Expand Down Expand Up @@ -620,20 +620,17 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
<>
<EuiSpacer size="s" />
{isEqlRule(ruleType) ? (
<>
<EqlQueryEdit
key="eqlQueryBar"
path="queryBar"
eqlOptionsPath="eqlOptions"
fieldsToValidateOnChange={ALERT_SUPPRESSION_FIELDS_FIELD_NAME}
required
showFilterBar
dataView={indexPattern}
loading={isIndexPatternLoading}
disabled={isLoading}
onValidityChange={setIsQueryBarValid}
/>
</>
<EqlQueryEdit
path="queryBar"
eqlOptionsPath="eqlOptions"
fieldsToValidateOnChange={ALERT_SUPPRESSION_FIELDS_FIELD_NAME}
required
showFilterBar
dataView={indexPattern}
loading={isIndexPatternLoading}
disabled={isLoading}
onValidityChange={setIsQueryBarValid}
/>
) : isEsqlRule(ruleType) ? (
<EsqlQueryEdit
path="queryBar"
Expand Down

0 comments on commit f7eeda7

Please sign in to comment.