Skip to content

Commit

Permalink
Filters: Extract getting data source props into a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaindik committed Sep 8, 2024
1 parent 4b06a98 commit 0bead12
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
*/

import type { Filter } from '@kbn/es-query';
import type { RuleFieldsDiff, ThreeWayDiff } from '../../../../../common/api/detection_engine';
import { ThreeWayDiffOutcome } from '../../../../../common/api/detection_engine';
import type {
DiffableAllFields,
RuleFieldsDiff,
ThreeWayDiff,
} from '../../../../../common/api/detection_engine';
import { DataSourceType, ThreeWayDiffOutcome } from '../../../../../common/api/detection_engine';
import type { FieldsGroupDiff } from '../../model/rule_details/rule_field_diff';
import {
ABOUT_UPGRADE_FIELD_ORDER,
Expand All @@ -16,6 +20,7 @@ import {
SETUP_UPGRADE_FIELD_ORDER,
} from './constants';
import * as i18n from './translations';
import { assertUnreachable } from '../../../../../common/utility_types';

export const getSectionedFieldDiffs = (fields: FieldsGroupDiff[]) => {
const aboutFields = [];
Expand Down Expand Up @@ -83,3 +88,34 @@ export function typeCheckFilters(filters: unknown[]): Filter[] {
return false;
}) as Filter[];
}

type DataSourceProps =
| {
index: undefined;
dataViewId: undefined;
}
| {
index: string[];
dataViewId: undefined;
}
| {
index: undefined;
dataViewId: string;
};

/**
* Extracts `index` and `dataViewId` from a `data_source` object for use in the `Filters` component.
*/
export function getDataSourceProps(dataSource: DiffableAllFields['data_source']): DataSourceProps {
if (!dataSource) {
return { index: undefined, dataViewId: undefined };
}

if (dataSource.type === DataSourceType.index_patterns) {
return { index: dataSource.index_patterns, dataViewId: undefined };
} else if (dataSource.type === DataSourceType.data_view) {
return { index: undefined, dataViewId: dataSource.data_view_id };
}

return assertUnreachable(dataSource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import React from 'react';
import { EuiDescriptionList } from '@elastic/eui';
import type { EuiDescriptionListProps } from '@elastic/eui';
import { DataSourceType } from '../../../../../../../../../common/api/detection_engine';
import type { DiffableAllFields } from '../../../../../../../../../common/api/detection_engine';
import * as descriptionStepI18n from '../../../../../../../rule_creation_ui/components/description_step/translations';
import { Query, Filters } from '../../../../rule_definition_section';
import { typeCheckFilters } from '../../../../helpers';
import { getDataSourceProps, typeCheckFilters } from '../../../../helpers';

interface EqlQueryReadOnlyProps {
eqlQuery: DiffableAllFields['eql_query'];
Expand All @@ -30,15 +29,11 @@ export function EqlQueryReadOnly({ eqlQuery, dataSource }: EqlQueryReadOnlyProps
const filters = typeCheckFilters(eqlQuery.filters);

if (filters.length > 0) {
const index =
dataSource?.type === DataSourceType.index_patterns ? dataSource.index_patterns : undefined;

const dataViewId =
dataSource?.type === DataSourceType.data_view ? dataSource.data_view_id : undefined;
const dataSourceProps = getDataSourceProps(dataSource);

listItems.push({
title: descriptionStepI18n.FILTERS_LABEL,
description: <Filters filters={filters} index={index} dataViewId={dataViewId} />,
description: <Filters filters={filters} {...dataSourceProps} />,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
import React from 'react';
import { EuiDescriptionList } from '@elastic/eui';
import type { EuiDescriptionListProps } from '@elastic/eui';
import { DataSourceType } from '../../../../../../../../../common/api/detection_engine';
import type {
DiffableAllFields,
InlineKqlQuery,
} from '../../../../../../../../../common/api/detection_engine';
import { Query, Filters } from '../../../../rule_definition_section';
import * as ruleDetailsI18n from '../../../../translations';
import * as descriptionStepI18n from '../../../../../../../rule_creation_ui/components/description_step/translations';
import { getQueryLanguageLabel, typeCheckFilters } from '../../../../helpers';
import { getDataSourceProps, getQueryLanguageLabel, typeCheckFilters } from '../../../../helpers';

const defaultI18nLabels = {
query: descriptionStepI18n.QUERY_LABEL,
Expand Down Expand Up @@ -53,15 +52,11 @@ export function InlineKqlQueryReadOnly({
const filters = typeCheckFilters(kqlQuery.filters);

if (filters.length > 0) {
const index =
dataSource?.type === DataSourceType.index_patterns ? dataSource.index_patterns : undefined;

const dataViewId =
dataSource?.type === DataSourceType.data_view ? dataSource.data_view_id : undefined;
const dataSourceProps = getDataSourceProps(dataSource);

listItems.push({
title: i18nLabels.filters,
description: <Filters filters={filters} index={index} dataViewId={dataViewId} />,
description: <Filters filters={filters} {...dataSourceProps} />,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import React from 'react';
import { EuiDescriptionList } from '@elastic/eui';
import type { EuiDescriptionListProps } from '@elastic/eui';
import { DataSourceType } from '../../../../../../../../../common/api/detection_engine';
import type {
SavedKqlQuery,
DiffableRule,
Expand All @@ -18,7 +17,7 @@ import { Query, SavedQueryName, Filters } from '../../../../rule_definition_sect
import * as ruleDetailsI18n from '../../../../translations';
import * as descriptionStepI18n from '../../../../../../../rule_creation_ui/components/description_step/translations';
import { useGetSavedQuery } from '../../../../../../../../detections/pages/detection_engine/rules/use_get_saved_query';
import { getQueryLanguageLabel } from '../../../../helpers';
import { getDataSourceProps, getQueryLanguageLabel } from '../../../../helpers';

interface SavedQueryProps {
kqlQuery: SavedKqlQuery;
Expand Down Expand Up @@ -55,17 +54,11 @@ export function SavedKqlQueryReadOnly({ kqlQuery, dataSource, ruleType }: SavedQ
}

if (savedQuery.attributes.filters) {
const index =
dataSource?.type === DataSourceType.index_patterns ? dataSource.index_patterns : undefined;

const dataViewId =
dataSource?.type === DataSourceType.data_view ? dataSource.data_view_id : undefined;
const dataSourceProps = getDataSourceProps(dataSource);

listItems.push({
title: descriptionStepI18n.SAVED_QUERY_FILTERS_LABEL,
description: (
<Filters filters={savedQuery.attributes.filters} index={index} dataViewId={dataViewId} />
),
description: <Filters filters={savedQuery.attributes.filters} {...dataSourceProps} />,
});
}

Expand Down

0 comments on commit 0bead12

Please sign in to comment.