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

Enable include/exclude in Terms agg for numeric fields #59425

Merged
merged 31 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4abf48a
Enable include/exclude in Terms agg for numeric fields
DianaDerevyankina Mar 5, 2020
1ecb7b2
Merge branch 'master' into 4576
elasticmachine Mar 6, 2020
69afa3f
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Mar 10, 2020
0d81475
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Mar 12, 2020
28869ba
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Mar 19, 2020
8a84f85
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Mar 23, 2020
dbc10d4
Added a new component that allows adding multiple values
DianaDerevyankina Mar 23, 2020
825412e
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Mar 30, 2020
acef9bd
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Mar 31, 2020
d759810
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Apr 1, 2020
d53c9dd
Added some validation to include/exclude fields
DianaDerevyankina Apr 1, 2020
956e0d4
Removed unnecessary comments and accepted API changes
DianaDerevyankina Apr 2, 2020
4c77816
Fixed i18n ID issue
DianaDerevyankina Apr 2, 2020
830567d
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Apr 3, 2020
6267b33
Refactored some code and fixed discard button issue
DianaDerevyankina Apr 3, 2020
c589153
Merge branch 'master' into 4576
elasticmachine Apr 6, 2020
4abfcc3
Added SimpleNumberList component and value parsing in include_exclude…
DianaDerevyankina Apr 7, 2020
ce40b57
Merge branch 'master' into 4576
DianaDerevyankina Apr 7, 2020
c9747f4
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Apr 7, 2020
dde8743
Fixed merge conflict
DianaDerevyankina Apr 8, 2020
653790a
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Apr 8, 2020
42cac76
Fixed merge conflict
DianaDerevyankina Apr 8, 2020
3bd31b9
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina Apr 9, 2020
a774fd2
Refactored some code
DianaDerevyankina Apr 9, 2020
c827bd8
Got rid of lodash isArray, added Number.isFinite where needed and cha…
DianaDerevyankina Apr 10, 2020
847e4c3
Merge branch 'master' into 4576
elasticmachine Apr 10, 2020
703025d
Merge branch 'DianaDerevyankina' into 4576
DianaDerevyankina Apr 14, 2020
52017cb
Merge branch 'DianaDerevyankina' into 4576
DianaDerevyankina Apr 17, 2020
83320f3
Added some more test cases to cover migrate_include_exclude_format wr…
DianaDerevyankina Apr 17, 2020
14eda87
Merge branch '4576' of https://github.com/DianaDerevyankina/kibana in…
DianaDerevyankina Apr 17, 2020
88c7fdb
Merge branch 'DianaDerevyankina' into 4576
DianaDerevyankina Apr 20, 2020
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
2 changes: 2 additions & 0 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ import {
convertIPRangeToString,
intervalOptions, // only used in Discover
isDateHistogramBucketAggConfig,
isNumberType,
isStringType,
isType,
parentPipelineType,
Expand Down Expand Up @@ -393,6 +394,7 @@ export const search = {
InvalidEsCalendarIntervalError,
InvalidEsIntervalFormatError,
isDateHistogramBucketAggConfig,
isNumberType,
isStringType,
isType,
isValidEsInterval,
Expand Down
33 changes: 17 additions & 16 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1545,8 +1545,9 @@ export const search: {
InvalidEsCalendarIntervalError: typeof InvalidEsCalendarIntervalError;
InvalidEsIntervalFormatError: typeof InvalidEsIntervalFormatError;
isDateHistogramBucketAggConfig: typeof isDateHistogramBucketAggConfig;
isNumberType: (agg: import("./search").AggConfig) => boolean;
isStringType: (agg: import("./search").AggConfig) => boolean;
isType: (type: string) => (agg: import("./search").AggConfig) => boolean;
isType: (...types: string[]) => (agg: import("./search").AggConfig) => boolean;
isValidEsInterval: typeof isValidEsInterval;
isValidInterval: typeof isValidInterval;
parentPipelineType: string;
Expand Down Expand Up @@ -1889,21 +1890,21 @@ export type TSearchStrategyProvider<T extends TStrategyTypes> = (context: ISearc
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "getRoutes" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:388:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:391:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:400:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:409:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:410:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:413:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:392:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:403:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:407:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:408:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:411:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:412:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:415:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:33:33 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:37:1 - (ae-forgotten-export) The symbol "QueryStateChange" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/types.ts:52:5 - (ae-forgotten-export) The symbol "createFiltersFromEvent" needs to be exported by the entry point index.d.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ import { isString, isObject } from 'lodash';
import { IBucketAggConfig, BucketAggType, BucketAggParam } from './bucket_agg_type';
import { IAggConfig } from '../agg_config';

export const isType = (type: string) => {
export const isType = (...types: string[]) => {
return (agg: IAggConfig): boolean => {
const field = agg.params.field;

return field && field.type === type;
return types.some(type => field && field.type === type);
};
};

export const isNumberType = isType('number');
export const isStringType = isType('string');
export const isStringOrNumberType = isType('string', 'number');

export const migrateIncludeExcludeFormat = {
serialize(this: BucketAggParam<IBucketAggConfig>, value: any, agg: IBucketAggConfig) {
if (this.shouldShow && !this.shouldShow(agg)) return;
if (!value || isString(value)) return value;
if (!value || isString(value) || Array.isArray(value)) return value;
else return value.pattern;
},
write(
Expand All @@ -44,7 +46,12 @@ export const migrateIncludeExcludeFormat = {
) {
const value = aggConfig.getParam(this.name);

if (isObject(value)) {
if (Array.isArray(value) && value.length > 0 && isNumberType(aggConfig)) {
const parsedValue = value.filter((val): val is number => Number.isFinite(val));
if (parsedValue.length) {
output.params[this.name] = parsedValue;
}
} else if (isObject(value)) {
output.params[this.name] = value.pattern;
} else if (value && isStringType(aggConfig)) {
output.params[this.name] = value;
Expand Down
60 changes: 60 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/terms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,65 @@ describe('Terms Agg', () => {
expect(params.include).toBe('404');
expect(params.exclude).toBe('400');
});

test('accepts string from string field type and writes this value', () => {
const aggConfigs = getAggConfigs({
include: 'include value',
exclude: 'exclude value',
field: {
name: 'string_field',
type: 'string',
},
orderAgg: {
type: 'count',
},
});

const { [BUCKET_TYPES.TERMS]: params } = aggConfigs.aggs[0].toDsl();

expect(params.field).toBe('string_field');
expect(params.include).toBe('include value');
expect(params.exclude).toBe('exclude value');
});

test('accepts empty array from number field type and does not write a value', () => {
const aggConfigs = getAggConfigs({
include: [],
exclude: [],
field: {
name: 'empty_number_field',
type: 'number',
},
orderAgg: {
type: 'count',
},
});

const { [BUCKET_TYPES.TERMS]: params } = aggConfigs.aggs[0].toDsl();

expect(params.field).toBe('empty_number_field');
expect(params.include).toBe(undefined);
expect(params.exclude).toBe(undefined);
});

test('filters array with empty strings from number field type and writes only numbers', () => {
const aggConfigs = getAggConfigs({
include: [1.1, 2, '', 3.33, ''],
exclude: ['', 4, 5.555, '', 6],
field: {
name: 'number_field',
type: 'number',
},
orderAgg: {
type: 'count',
},
});

const { [BUCKET_TYPES.TERMS]: params } = aggConfigs.aggs[0].toDsl();

expect(params.field).toBe('number_field');
expect(params.include).toStrictEqual([1.1, 2, 3.33]);
expect(params.exclude).toStrictEqual([4, 5.555, 6]);
});
});
});
9 changes: 6 additions & 3 deletions src/plugins/data/public/search/aggs/buckets/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import { i18n } from '@kbn/i18n';
import { BucketAggType, IBucketAggConfig } from './bucket_agg_type';
import { BUCKET_TYPES } from './bucket_agg_types';
import { createFilterTerms } from './create_filter/terms';
import { isStringType, migrateIncludeExcludeFormat } from './migrate_include_exclude_format';
import {
isStringOrNumberType,
migrateIncludeExcludeFormat,
} from './migrate_include_exclude_format';
import { IAggConfigs } from '../agg_configs';

import { Adapters } from '../../../../../inspector/public';
Expand Down Expand Up @@ -266,7 +269,7 @@ export const getTermsBucketAgg = ({ getInternalStartServices }: TermsBucketAggDe
}),
type: 'string',
advanced: true,
shouldShow: isStringType,
shouldShow: isStringOrNumberType,
...migrateIncludeExcludeFormat,
},
{
Expand All @@ -276,7 +279,7 @@ export const getTermsBucketAgg = ({ getInternalStartServices }: TermsBucketAggDe
}),
type: 'string',
advanced: true,
shouldShow: isStringType,
shouldShow: isStringOrNumberType,
...migrateIncludeExcludeFormat,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const buckets = {
size: controls.SizeParamEditor,
},
[BUCKET_TYPES.TERMS]: {
include: controls.IncludeExcludeParamEditor,
exclude: controls.IncludeExcludeParamEditor,
orderBy: controls.OrderByParamEditor,
orderAgg: controls.OrderAggParamEditor,
order: wrapWithInlineComp(controls.OrderParamEditor),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function getNextModel(list: NumberRowModel[], range: NumberListRange): NumberRow
};
}

function getInitModelList(list: Array<number | undefined>): NumberRowModel[] {
function getInitModelList(list: Array<number | undefined | ''>): NumberRowModel[] {
return list.length
? list.map(num => ({
value: (num === undefined ? EMPTY_STRING : num) as NumberRowModel['value'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React, { Fragment, useCallback, useEffect, useMemo, useState } from 'react';
import { isArray } from 'lodash';
import { EuiButtonEmpty, EuiFlexItem, EuiFormRow, EuiSpacer, htmlIdGenerator } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { EMPTY_STRING, getInitModelList, getRange, parse } from './number_list/utils';
import { NumberRow, NumberRowModel } from './number_list/number_row';
import { AggParamEditorProps } from '../../agg_param_props';

const generateId = htmlIdGenerator();

function SimpleNumberList({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maryia-lapata @DianaDerevyankina Could you please explain shortly what's the different between SimpleNumberList and NumberList, we use in other places in the editor (like percentiles)? Is the difference really large enough that it justifies having a separate component, or were there just minor things, that we could potentially solve adding a property to NumberList?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference isn't big:

  • NumberList creates next value as incremented the last one, SimpleNumberList creates just empty input;
  • NumberList saves an empty value as undefined to value, SimpleNumberList - as an empty string;
  • NumberList can influence on the form validity and it uses useValidation hook as well as it validates each input, SimpleNumberList - not;
  • there is no initial value in SimpleNumberList.

In case we decide to reuse NumberList, it's needed to make it more generic. I think the component is already a bit complex (I wouldn't want to make it a magic component), and I'd leave a separate SimpleNumberList component.

agg,
aggParam,
value,
setValue,
setTouched,
}: AggParamEditorProps<Array<number | ''>>) {
const [numbers, setNumbers] = useState(
getInitModelList(value && isArray(value) ? value : [EMPTY_STRING])
);
const numberRange = useMemo(() => getRange('[-Infinity,Infinity]'), []);

// This useEffect is needed to discard changes, it sets numbers a mapped value if they are different
useEffect(() => {
if (
isArray(value) &&
(value.length !== numbers.length ||
!value.every((numberValue, index) => numberValue === numbers[index].value))
) {
setNumbers(
value.map(numberValue => ({
id: generateId(),
value: numberValue,
isInvalid: false,
}))
);
}
}, [numbers, value]);

const onUpdate = useCallback(
(numberList: NumberRowModel[]) => {
setNumbers(numberList);
setValue(numberList.map(({ value: numberValue }) => numberValue));
},
[setValue]
);

const onChangeValue = useCallback(
(numberField: { id: string; value: string }) => {
onUpdate(
numbers.map(number =>
number.id === numberField.id
? {
id: numberField.id,
value: parse(numberField.value),
DianaDerevyankina marked this conversation as resolved.
Show resolved Hide resolved
isInvalid: false,
}
: number
)
);
},
[numbers, onUpdate]
);

// Add an item to the end of the list
const onAdd = useCallback(() => {
const newArray = [
...numbers,
{
id: generateId(),
value: EMPTY_STRING as '',
isInvalid: false,
},
];
onUpdate(newArray);
}, [numbers, onUpdate]);

const onDelete = useCallback(
(id: string) => onUpdate(numbers.filter(number => number.id !== id)),
[numbers, onUpdate]
);

return (
<EuiFormRow
id={`${aggParam.name}-${agg.id}}`}
label={aggParam.displayName || aggParam.name}
fullWidth={true}
compressed
>
<>
{numbers.map((number, arrayIndex) => (
<Fragment key={number.id}>
<NumberRow
isInvalid={number.isInvalid}
disableDelete={numbers.length === 1}
model={number}
labelledbyId={`${aggParam.name}-${agg.id}-legend`}
range={numberRange}
onDelete={onDelete}
onChange={onChangeValue}
onBlur={setTouched}
autoFocus={numbers.length !== 1 && arrayIndex === numbers.length - 1}
/>
{numbers.length - 1 !== arrayIndex && <EuiSpacer size="s" />}
</Fragment>
))}
<EuiSpacer size="s" />
<EuiFlexItem>
<EuiButtonEmpty iconType="plusInCircleFilled" onClick={onAdd} size="xs">
<FormattedMessage
id="visDefaultEditor.controls.includeExclude.addUnitButtonLabel"
defaultMessage="Add value"
/>
</EuiButtonEmpty>
</EuiFlexItem>
</>
</EuiFormRow>
);
}

export { SimpleNumberList };
Loading