Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some of filter bar's utils to appropriate locations #51162

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import { FormattedMessage } from '@kbn/i18n/react';
import React, { Component } from 'react';
import { IndexPattern } from '../../index_patterns';
import { FilterLabel } from '../filter_bar/filter_editor/lib/filter_label';
import { mapAndFlattenFilters, esFilters } from '../../../../../../plugins/data/public';
import { getDisplayValueFromFilter } from '../filter_bar/filter_editor/lib/get_display_value';
import { mapAndFlattenFilters, esFilters, utils } from '../../../../../../plugins/data/public';

interface Props {
filters: esFilters.Filter[];
Expand All @@ -58,7 +57,7 @@ export class ApplyFiltersPopoverContent extends Component<Props, State> {
};
}
private getLabel(filter: esFilters.Filter) {
const valueLabel = getDisplayValueFromFilter(filter, this.props.indexPatterns);
const valueLabel = utils.getDisplayValueFromFilter(filter, this.props.indexPatterns);
return <FilterLabel filter={filter} valueLabel={valueLabel} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ import { EuiButtonEmpty, EuiFlexGroup, EuiFlexItem, EuiPopover } from '@elastic/
import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react';
import classNames from 'classnames';
import React, { useState } from 'react';
import { IndexPattern } from '../../index_patterns';

import { FilterEditor } from './filter_editor';
import { FilterItem } from './filter_item';
import { FilterOptions } from './filter_options';
import { useKibana } from '../../../../../../plugins/kibana_react/public';
import { esFilters } from '../../../../../../plugins/data/public';
import { IIndexPattern, esFilters } from '../../../../../../plugins/data/public';

interface Props {
filters: esFilters.Filter[];
onFiltersUpdated?: (filters: esFilters.Filter[]) => void;
className: string;
indexPatterns: IndexPattern[];
indexPatterns: IIndexPattern[];
intl: InjectedIntl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,36 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react';
import { get } from 'lodash';
import React, { Component } from 'react';
import { Field, IndexPattern } from '../../../index_patterns';
import { GenericComboBox, GenericComboBoxProps } from './generic_combo_box';
import {
buildCustomFilter,
buildFilter,
getFieldFromFilter,
getFilterableFields,
getFilterParams,
getIndexPatternFromFilter,
getOperatorFromFilter,
getOperatorOptions,
getQueryDslFromFilter,
isFilterValid,
} from './lib/filter_editor_utils';
import { Operator } from './lib/filter_operators';
import { PhraseValueInput } from './phrase_value_input';
import { PhrasesValuesInput } from './phrases_values_input';
import { RangeValueInput } from './range_value_input';
import { esFilters } from '../../../../../../../plugins/data/public';
import {
esFilters,
utils,
IIndexPattern,
IFieldType,
} from '../../../../../../../plugins/data/public';

interface Props {
filter: esFilters.Filter;
indexPatterns: IndexPattern[];
indexPatterns: IIndexPattern[];
onSubmit: (filter: esFilters.Filter) => void;
onCancel: () => void;
intl: InjectedIntl;
}

interface State {
selectedIndexPattern?: IndexPattern;
selectedField?: Field;
selectedIndexPattern?: IIndexPattern;
selectedField?: IFieldType;
selectedOperator?: Operator;
params: any;
useCustomLabel: boolean;
Expand All @@ -82,10 +81,10 @@ class FilterEditorUI extends Component<Props, State> {
selectedIndexPattern: this.getIndexPatternFromFilter(),
selectedField: this.getFieldFromFilter(),
selectedOperator: this.getSelectedOperator(),
params: getFilterParams(props.filter),
params: esFilters.getFilterParams(props.filter),
useCustomLabel: props.filter.meta.alias !== null,
customLabel: props.filter.meta.alias,
queryDsl: JSON.stringify(getQueryDslFromFilter(props.filter), null, 2),
queryDsl: JSON.stringify(esFilters.cleanFilter(props.filter), null, 2),
isCustomEditorOpen: this.isUnknownFilterType(),
};
}
Expand Down Expand Up @@ -377,7 +376,7 @@ class FilterEditorUI extends Component<Props, State> {
}

private getIndexPatternFromFilter() {
return getIndexPatternFromFilter(this.props.filter, this.props.indexPatterns);
return utils.getIndexPatternFromFilter(this.props.filter, this.props.indexPatterns);
}

private getFieldFromFilter() {
Expand Down Expand Up @@ -412,14 +411,14 @@ class FilterEditorUI extends Component<Props, State> {
return isFilterValid(indexPattern, field, operator, params);
}

private onIndexPatternChange = ([selectedIndexPattern]: IndexPattern[]) => {
private onIndexPatternChange = ([selectedIndexPattern]: IIndexPattern[]) => {
const selectedField = undefined;
const selectedOperator = undefined;
const params = undefined;
this.setState({ selectedIndexPattern, selectedField, selectedOperator, params });
};

private onFieldChange = ([selectedField]: Field[]) => {
private onFieldChange = ([selectedField]: IFieldType[]) => {
const selectedOperator = undefined;
const params = undefined;
this.setState({ selectedField, selectedOperator, params });
Expand Down Expand Up @@ -475,13 +474,21 @@ class FilterEditorUI extends Component<Props, State> {
const { index, disabled, negate } = this.props.filter.meta;
const newIndex = index || this.props.indexPatterns[0].id!;
const body = JSON.parse(queryDsl);
const filter = buildCustomFilter(newIndex, body, disabled, negate, alias, $state.store);
const filter = esFilters.buildCustomFilter(
newIndex,
body,
disabled,
negate,
alias,
$state.store
);
this.props.onSubmit(filter);
} else if (indexPattern && field && operator) {
const filter = buildFilter(
const filter = esFilters.buildFilter(
indexPattern,
field,
operator,
operator.type,
operator.negate,
this.props.filter.meta.disabled,
params,
alias,
Expand All @@ -492,11 +499,11 @@ class FilterEditorUI extends Component<Props, State> {
};
}

function IndexPatternComboBox(props: GenericComboBoxProps<IndexPattern>) {
function IndexPatternComboBox(props: GenericComboBoxProps<IIndexPattern>) {
return GenericComboBox(props);
}

function FieldComboBox(props: GenericComboBoxProps<Field>) {
function FieldComboBox(props: GenericComboBoxProps<IFieldType>) {
return GenericComboBox(props);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,15 @@ import {
} from '../../../../../../../../plugins/data/public/stubs';
import { IndexPattern, Field } from '../../../../index';
import {
buildFilter,
getFieldFromFilter,
getFilterableFields,
getFilterParams,
getIndexPatternFromFilter,
getOperatorFromFilter,
getOperatorOptions,
getQueryDslFromFilter,
isFilterValid,
} from './filter_editor_utils';
import {
doesNotExistOperator,
existsOperator,
isBetweenOperator,
isOneOfOperator,
isOperator,
} from './filter_operators';

import { existsOperator, isBetweenOperator, isOneOfOperator, isOperator } from './filter_operators';

import { esFilters } from '../../../../../../../../plugins/data/public';

jest.mock('ui/new_platform');
Expand All @@ -53,21 +45,6 @@ const mockedFields = stubFields as Field[];
const mockedIndexPattern = stubIndexPattern as IndexPattern;

describe('Filter editor utils', () => {
describe('getQueryDslFromFilter', () => {
it('should return query DSL without meta and $state', () => {
const queryDsl = getQueryDslFromFilter(phraseFilter);
expect(queryDsl).not.toHaveProperty('meta');
expect(queryDsl).not.toHaveProperty('$state');
});
});

describe('getIndexPatternFromFilter', () => {
it('should return the index pattern from the filter', () => {
const indexPattern = getIndexPatternFromFilter(phraseFilter, [mockedIndexPattern]);
expect(indexPattern).toBe(mockedIndexPattern);
});
});

describe('getFieldFromFilter', () => {
it('should return the field from the filter', () => {
const field = getFieldFromFilter(phraseFilter, mockedIndexPattern);
Expand Down Expand Up @@ -138,28 +115,6 @@ describe('Filter editor utils', () => {
});
});

describe('getFilterParams', () => {
it('should retrieve params from phrase filter', () => {
const params = getFilterParams(phraseFilter);
expect(params).toBe('ios');
});

it('should retrieve params from phrases filter', () => {
const params = getFilterParams(phrasesFilter);
expect(params).toEqual(['win xp', 'osx']);
});

it('should retrieve params from range filter', () => {
const params = getFilterParams(rangeFilter);
expect(params).toEqual({ from: 0, to: 10 });
});

it('should return undefined for exists filter', () => {
const params = getFilterParams(existsFilter);
expect(params).toBeUndefined();
});
});

describe('getFilterableFields', () => {
it('returns the list of fields from the given index pattern', () => {
const fieldOptions = getFilterableFields(mockedIndexPattern);
Expand Down Expand Up @@ -245,129 +200,4 @@ describe('Filter editor utils', () => {
expect(isValid).toBe(true);
});
});

describe('buildFilter', () => {
it('should build phrase filters', () => {
const params = 'foo';
const alias = 'bar';
const state = esFilters.FilterStateStore.APP_STATE;
const filter = buildFilter(
mockedIndexPattern,
mockedFields[0],
isOperator,
false,
params,
alias,
state
);
expect(filter.meta.negate).toBe(isOperator.negate);
expect(filter.meta.alias).toBe(alias);

expect(filter.$state).toBeDefined();
if (filter.$state) {
expect(filter.$state.store).toBe(state);
}
});

it('should build phrases filters', () => {
const params = ['foo', 'bar'];
const alias = 'bar';
const state = esFilters.FilterStateStore.APP_STATE;
const filter = buildFilter(
mockedIndexPattern,
mockedFields[0],
isOneOfOperator,
false,
params,
alias,
state
);
expect(filter.meta.type).toBe(isOneOfOperator.type);
expect(filter.meta.negate).toBe(isOneOfOperator.negate);
expect(filter.meta.alias).toBe(alias);
expect(filter.$state).toBeDefined();
if (filter.$state) {
expect(filter.$state.store).toBe(state);
}
});

it('should build range filters', () => {
const params = { from: 'foo', to: 'qux' };
const alias = 'bar';
const state = esFilters.FilterStateStore.APP_STATE;
const filter = buildFilter(
mockedIndexPattern,
mockedFields[0],
isBetweenOperator,
false,
params,
alias,
state
);
expect(filter.meta.negate).toBe(isBetweenOperator.negate);
expect(filter.meta.alias).toBe(alias);
expect(filter.$state).toBeDefined();
if (filter.$state) {
expect(filter.$state.store).toBe(state);
}
});

it('should build exists filters', () => {
const params = undefined;
const alias = 'bar';
const state = esFilters.FilterStateStore.APP_STATE;
const filter = buildFilter(
mockedIndexPattern,
mockedFields[0],
existsOperator,
false,
params,
alias,
state
);
expect(filter.meta.negate).toBe(existsOperator.negate);
expect(filter.meta.alias).toBe(alias);
expect(filter.$state).toBeDefined();
if (filter.$state) {
expect(filter.$state.store).toBe(state);
}
});

it('should include disabled state', () => {
const params = undefined;
const alias = 'bar';
const state = esFilters.FilterStateStore.APP_STATE;
const filter = buildFilter(
mockedIndexPattern,
mockedFields[0],
doesNotExistOperator,
true,
params,
alias,
state
);
expect(filter.meta.disabled).toBe(true);
});

it('should negate based on operator', () => {
const params = undefined;
const alias = 'bar';
const state = esFilters.FilterStateStore.APP_STATE;
const filter = buildFilter(
mockedIndexPattern,
mockedFields[0],
doesNotExistOperator,
false,
params,
alias,
state
);
expect(filter.meta.negate).toBe(doesNotExistOperator.negate);
expect(filter.meta.alias).toBe(alias);
expect(filter.$state).toBeDefined();
if (filter.$state) {
expect(filter.$state.store).toBe(state);
}
});
});
});
Loading