-
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
Merged
Merged
Changes from 26 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 1ecb7b2
Merge branch 'master' into 4576
elasticmachine 69afa3f
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina 0d81475
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina 28869ba
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina 8a84f85
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina dbc10d4
Added a new component that allows adding multiple values
DianaDerevyankina 825412e
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina acef9bd
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina d759810
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina d53c9dd
Added some validation to include/exclude fields
DianaDerevyankina 956e0d4
Removed unnecessary comments and accepted API changes
DianaDerevyankina 4c77816
Fixed i18n ID issue
DianaDerevyankina 830567d
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina 6267b33
Refactored some code and fixed discard button issue
DianaDerevyankina c589153
Merge branch 'master' into 4576
elasticmachine 4abfcc3
Added SimpleNumberList component and value parsing in include_exclude…
DianaDerevyankina ce40b57
Merge branch 'master' into 4576
DianaDerevyankina c9747f4
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina dde8743
Fixed merge conflict
DianaDerevyankina 653790a
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina 42cac76
Fixed merge conflict
DianaDerevyankina 3bd31b9
Merge branch 'DziyanaDzeraviankina' into 4576
DianaDerevyankina a774fd2
Refactored some code
DianaDerevyankina c827bd8
Got rid of lodash isArray, added Number.isFinite where needed and cha…
DianaDerevyankina 847e4c3
Merge branch 'master' into 4576
elasticmachine 703025d
Merge branch 'DianaDerevyankina' into 4576
DianaDerevyankina 52017cb
Merge branch 'DianaDerevyankina' into 4576
DianaDerevyankina 83320f3
Added some more test cases to cover migrate_include_exclude_format wr…
DianaDerevyankina 14eda87
Merge branch '4576' of https://github.com/DianaDerevyankina/kibana in…
DianaDerevyankina 88c7fdb
Merge branch 'DianaDerevyankina' into 4576
DianaDerevyankina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/plugins/vis_default_editor/public/components/controls/components/simple_number_list.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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 }; |
49 changes: 49 additions & 0 deletions
49
src/plugins/vis_default_editor/public/components/controls/include_exclude.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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 { 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 && !Array.isArray(value) && value !== undefined) { | ||
const numberArray = value | ||
.split('|') | ||
.map(item => parseFloat(item)) | ||
.filter(number => Number.isFinite(number)); | ||
setValue(numberArray.length ? numberArray : ['']); | ||
} else if (!isAggOfNumberType && Array.isArray(value) && value !== undefined) { | ||
setValue(value.filter(item => item !== '').join('|')); | ||
} | ||
}, [isAggOfNumberType, setValue, value]); | ||
|
||
return isAggOfNumberType ? ( | ||
<SimpleNumberList {...props} value={value as Array<number | ''>} /> | ||
) : ( | ||
<StringParamEditor {...props} value={value as string} /> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@maryia-lapata @DianaDerevyankina Could you please explain shortly what's the different between
SimpleNumberList
andNumberList
, 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 toNumberList
?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.
The difference isn't big:
NumberList
creates next value as incremented the last one,SimpleNumberList
creates just empty input;NumberList
saves an empty value asundefined
to value,SimpleNumberList
- as an empty string;NumberList
can influence on the form validity and it usesuseValidation
hook as well as it validates each input,SimpleNumberList
- not;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 amagic component
), and I'd leave a separateSimpleNumberList
component.