[8.11] [Lens] Handle invalid values gracefully for static value operation (#172198) (#172370)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[Lens] Handle invalid values gracefully for static value operation
(#172198)](https://github.com/elastic/kibana/pull/172198)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Marco
Liberati","email":"dej611@users.noreply.github.com"},"sourceCommit":{"committedDate":"2023-12-01T09:45:42Z","message":"[Lens]
Handle invalid values gracefully for static value operation
(#172198)\n\n## Summary\r\n\r\nFixes #171959 \r\n\r\nI've extended valid
static value check to 15 digits (which is the max\r\nsupport by JS as
implementing the [64-bit
IEEE\r\n574](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)).\r\n\r\n<img
width=\"1224\" alt=\"Screenshot 2023-11-30 at 10 23
13\"\r\nsrc=\"bf88c0c8-9e51-4c8f-912d-abd82f292eda\">\r\n\r\nNote:
an alternative approach would be to make it pass nonetheless
and\r\ntrunc the numeric value at 15th digit.\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Stratoula Kalafateli
<efstratia.kalafateli@elastic.co>","sha":"1f8c816901fa286b842ac652b0fce86608d01202","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Visualizations","Feature:Lens","backport:prev-minor","v8.12.0"],"number":172198,"url":"https://github.com/elastic/kibana/pull/172198","mergeCommit":{"message":"[Lens]
Handle invalid values gracefully for static value operation
(#172198)\n\n## Summary\r\n\r\nFixes #171959 \r\n\r\nI've extended valid
static value check to 15 digits (which is the max\r\nsupport by JS as
implementing the [64-bit
IEEE\r\n574](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)).\r\n\r\n<img
width=\"1224\" alt=\"Screenshot 2023-11-30 at 10 23
13\"\r\nsrc=\"bf88c0c8-9e51-4c8f-912d-abd82f292eda\">\r\n\r\nNote:
an alternative approach would be to make it pass nonetheless
and\r\ntrunc the numeric value at 15th digit.\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Stratoula Kalafateli
<efstratia.kalafateli@elastic.co>","sha":"1f8c816901fa286b842ac652b0fce86608d01202"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/172198","number":172198,"mergeCommit":{"message":"[Lens]
Handle invalid values gracefully for static value operation
(#172198)\n\n## Summary\r\n\r\nFixes #171959 \r\n\r\nI've extended valid
static value check to 15 digits (which is the max\r\nsupport by JS as
implementing the [64-bit
IEEE\r\n574](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)).\r\n\r\n<img
width=\"1224\" alt=\"Screenshot 2023-11-30 at 10 23
13\"\r\nsrc=\"bf88c0c8-9e51-4c8f-912d-abd82f292eda\">\r\n\r\nNote:
an alternative approach would be to make it pass nonetheless
and\r\ntrunc the numeric value at 15th digit.\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Stratoula Kalafateli
<efstratia.kalafateli@elastic.co>","sha":"1f8c816901fa286b842ac652b0fce86608d01202"}}]}]
BACKPORT-->

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2023-12-01 11:33:01 -05:00 committed by GitHub
parent 11eb765f54
commit bf69b7f459
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -66,8 +66,9 @@ export const staticValueOperation: OperationDefinition<
},
getErrorMessage(layer, columnId) {
const column = layer.columns[columnId] as StaticValueIndexPatternColumn;
const isValid = isValidNumber(column.params.value, false, undefined, undefined, 15);
return column.params.value != null && !isValidNumber(column.params.value)
return column.params.value != null && !isValid
? [
i18n.translate('xpack.lens.indexPattern.staticValueError', {
defaultMessage: 'The static value of {value} is not a valid number',
@ -89,7 +90,8 @@ export const staticValueOperation: OperationDefinition<
const params = currentColumn.params;
// TODO: improve this logic
const useDisplayLabel = currentColumn.label !== defaultLabel;
const label = isValidNumber(params.value)
const isValid = isValidNumber(params.value, false, undefined, undefined, 15);
const label = isValid
? useDisplayLabel
? currentColumn.label
: params?.value ?? defaultLabel
@ -98,11 +100,11 @@ export const staticValueOperation: OperationDefinition<
return [
{
type: 'function',
function: isValidNumber(params.value) ? 'mathColumn' : 'mapColumn',
function: isValid ? 'mathColumn' : 'mapColumn',
arguments: {
id: [columnId],
name: [label || defaultLabel],
expression: [String(isValidNumber(params.value) ? params.value! : defaultValue)],
expression: [String(isValid ? params.value! : defaultValue)],
},
},
];
@ -163,7 +165,10 @@ export const staticValueOperation: OperationDefinition<
const onChange = useCallback(
(newValue) => {
// even if debounced it's triggering for empty string with the previous valid value
if (currentColumn.params.value === newValue) {
if (
currentColumn.params.value === newValue ||
!isValidNumber(newValue, false, undefined, undefined, 15)
) {
return;
}
// Because of upstream specific UX flows, we need fresh layer state here
@ -209,13 +214,26 @@ export const staticValueOperation: OperationDefinition<
const onChangeHandler = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.currentTarget.value;
handleInputChange(isValidNumber(value) ? value : undefined);
handleInputChange(value);
},
[handleInputChange]
);
const inputValueIsValid = isValidNumber(inputValue, false, undefined, undefined, 15);
return (
<EuiFormRow label={paramEditorCustomProps?.labels?.[0] || defaultLabel} fullWidth>
<EuiFormRow
label={paramEditorCustomProps?.labels?.[0] || defaultLabel}
fullWidth
isInvalid={!inputValueIsValid}
error={
!inputValueIsValid &&
i18n.translate('xpack.lens.indexPattern.staticValueError', {
defaultMessage: 'The static value of {value} is not a valid number',
values: { value: inputValue ?? "''" },
})
}
>
<EuiFieldNumber
fullWidth
data-test-subj="lns-indexPattern-static_value-input"