-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from 24 commits
4abf48a
1ecb7b2
69afa3f
0d81475
28869ba
8a84f85
dbc10d4
825412e
acef9bd
d759810
d53c9dd
956e0d4
4c77816
830567d
6267b33
c589153
4abfcc3
ce40b57
c9747f4
dde8743
653790a
42cac76
3bd31b9
a774fd2
c827bd8
847e4c3
703025d
52017cb
83320f3
14eda87
88c7fdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,24 +17,26 @@ | |||||
* under the License. | ||||||
*/ | ||||||
|
||||||
import { isString, isObject } from 'lodash'; | ||||||
import { isString, isObject, isArray } 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) || isArray(value)) return value; | ||||||
else return value.pattern; | ||||||
}, | ||||||
write( | ||||||
|
@@ -44,7 +46,12 @@ export const migrateIncludeExcludeFormat = { | |||||
) { | ||||||
const value = aggConfig.getParam(this.name); | ||||||
|
||||||
if (isObject(value)) { | ||||||
if (isArray(value) && value.length > 0 && isNumberType(aggConfig)) { | ||||||
const parsedValue = (value as number[]).filter((val: number) => val || val === 0); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This way we don't need to have a cast ( Using a custom typeguard Also I would shorten the call for |
||||||
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; | ||||||
|
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference isn't big:
In case we decide to reuse |
||
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 }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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, { useEffect } from 'react'; | ||
import { isArray } from 'lodash'; | ||
import { AggParamEditorProps } from '../agg_param_props'; | ||
import { StringParamEditor } from './string'; | ||
import { search } from '../../../../data/public'; | ||
import { SimpleNumberList } from './components/simple_number_list'; | ||
const { isNumberType } = search.aggs; | ||
|
||
export function IncludeExcludeParamEditor(props: AggParamEditorProps<string | Array<number | ''>>) { | ||
const { agg, value, setValue } = props; | ||
const isAggOfNumberType = isNumberType(agg); | ||
|
||
// This useEffect converts value from string type to number and back when the field type is changed | ||
useEffect(() => { | ||
if (isAggOfNumberType && !isArray(value) && value !== undefined) { | ||
const numberArray = value | ||
.split(',') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want to use |
||
.map(item => parseFloat(item)) | ||
.filter(number => !isNaN(number)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use |
||
setValue(numberArray.length ? numberArray : ['']); | ||
} else if (!isAggOfNumberType && isArray(value) && value !== undefined) { | ||
setValue((value as Array<number | ''>).filter(item => item !== '').toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The case should not be needed here, TypeScript should know that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also |
||
} | ||
}, [isAggOfNumberType, setValue, value]); | ||
|
||
return isAggOfNumberType ? ( | ||
<SimpleNumberList {...props} value={value as Array<number | ''>} /> | ||
) : ( | ||
<StringParamEditor {...props} value={value as string} /> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎨 We're trying to not introduce new lodash code if there is an easy vanilla JS equivalent. So I would recommend using
Array.isArray
instead of using a lodash function.