-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Lens] Formula: add validation for multiple field/metrics #104092
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a89059d
:bug: Add validation for multiple field/metrics
dej611 0dcc0b8
:fire: Remove debug comment
dej611 b50ecd5
:ok_hand: Added more validation cases
dej611 13eb57c
Merge branch 'master' into fix/formula-multiple-fields
kibanamachine 12521e6
:ok_hand: Integrate feedback
dej611 950ff34
Merge branch 'fix/formula-multiple-fields' of github.com:dej611/kiban…
dej611 9748083
:bug: Fix missing ref case
dej611 8aee877
Merge remote-tracking branch 'upstream/master' into fix/formula-multi…
dej611 8c042c1
Merge branch 'master' into fix/formula-multiple-fields
kibanamachine 2bc89e8
:bug: Fix more edges cases
dej611 aa4b7f6
Merge branch 'fix/formula-multiple-fields' of github.com:dej611/kiban…
dej611 07fb0a0
Merge branch 'master' into fix/formula-multiple-fields
kibanamachine 7f571bf
Merge branch 'master' into fix/formula-multiple-fields
kibanamachine d863ca4
Merge branch 'master' into fix/formula-multiple-fields
kibanamachine 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
|
||
import { isObject, partition } from 'lodash'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { parse, TinymathLocation } from '@kbn/tinymath'; | ||
import { parse, TinymathLocation, TinymathVariable } from '@kbn/tinymath'; | ||
import type { TinymathAST, TinymathFunction, TinymathNamedArgument } from '@kbn/tinymath'; | ||
import { esKuery, esQuery } from '../../../../../../../../src/plugins/data/public'; | ||
import { | ||
|
@@ -63,6 +63,10 @@ interface ValidationErrors { | |
message: string; | ||
type: {}; | ||
}; | ||
tooManyFirstArguments: { | ||
message: string; | ||
type: { operation: string; type: string; count: number; text: string }; | ||
}; | ||
} | ||
|
||
type ErrorTypes = keyof ValidationErrors; | ||
|
@@ -276,6 +280,13 @@ function getMessageFromId<K extends ErrorTypes>({ | |
defaultMessage: 'Use only one of kql= or lucene=, not both', | ||
}); | ||
break; | ||
case 'tooManyFirstArguments': | ||
message = i18n.translate('xpack.lens.indexPattern.formulaOperationTooManyFirstArguments', { | ||
defaultMessage: | ||
'The operation {operation} in the Formula requires a single {type}, but found {count}: {text}', | ||
values: { operation: out.operation, text: out.text, count: out.count, type: out.type }, | ||
}); | ||
break; | ||
// case 'mathRequiresFunction': | ||
// message = i18n.translate('xpack.lens.indexPattern.formulaMathRequiresFunctionLabel', { | ||
// defaultMessage; 'The function {name} requires an Elasticsearch function', | ||
|
@@ -531,7 +542,7 @@ function runFullASTValidation( | |
} else { | ||
if (nodeOperation.input === 'field') { | ||
if (shouldHaveFieldArgument(node)) { | ||
if (!isFirstArgumentValidType(firstArg, 'variable')) { | ||
if (!isArgumentValidType(firstArg, 'variable')) { | ||
if (isMathNode(firstArg)) { | ||
errors.push( | ||
getMessageFromId({ | ||
|
@@ -561,6 +572,24 @@ function runFullASTValidation( | |
}) | ||
); | ||
} | ||
} else { | ||
const fields = variables.filter( | ||
(arg) => isArgumentValidType(arg, 'variable') && !isMathNode(arg) | ||
); | ||
if (fields.length > 1) { | ||
errors.push( | ||
getMessageFromId({ | ||
messageId: 'tooManyFirstArguments', | ||
values: { | ||
operation: node.name, | ||
type: 'field', | ||
text: (fields as TinymathVariable[]).map(({ text }) => text).join(', '), | ||
count: fields.length, | ||
}, | ||
locations: node.location ? [node.location] : [], | ||
}) | ||
); | ||
} | ||
} | ||
} else { | ||
// Named arguments only | ||
|
@@ -603,7 +632,7 @@ function runFullASTValidation( | |
// What about fn(7 + 1)? We may want to allow that | ||
// In general this should be handled down the Esaggs route rather than here | ||
if ( | ||
!isFirstArgumentValidType(firstArg, 'function') || | ||
!isArgumentValidType(firstArg, 'function') || | ||
(isMathNode(firstArg) && validateMathNodes(firstArg, missingVariablesSet).length) | ||
) { | ||
errors.push( | ||
|
@@ -621,6 +650,21 @@ function runFullASTValidation( | |
locations: node.location ? [node.location] : [], | ||
}) | ||
); | ||
} else { | ||
if (functions.length > 1) { | ||
errors.push( | ||
getMessageFromId({ | ||
messageId: 'tooManyFirstArguments', | ||
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 message still says |
||
values: { | ||
operation: node.name, | ||
type: 'metric', | ||
text: (functions as TinymathFunction[]).map(({ text }) => text).join(', '), | ||
count: functions.length, | ||
}, | ||
locations: node.location ? [node.location] : [], | ||
}) | ||
); | ||
} | ||
} | ||
if (!canHaveParams(nodeOperation) && namedArguments.length) { | ||
errors.push( | ||
|
@@ -736,7 +780,7 @@ export function hasFunctionFieldArgument(type: string) { | |
return !['count'].includes(type); | ||
} | ||
|
||
export function isFirstArgumentValidType(arg: TinymathAST, type: TinymathNodeTypes['type']) { | ||
export function isArgumentValidType(arg: TinymathAST | string, type: TinymathNodeTypes['type']) { | ||
return isObject(arg) && arg.type === type; | ||
} | ||
|
||
|
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.
I'm not sure whether this is a future-proof way to check - the way our full reference operations are set up, they could theoretically accept multiple parameters. Should we check the
requiredReferences
here?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.
This code is generic enough to check functions in different position in the node, as
moving_average( median( ...), median( ... ) )
as well asmoving_average( median( ...), window = 7, shift="7d", median( ... ) )
.I could parametrize the threshold to be
requiredReferences.filter( /* check for functions */).length
here, but in general the formula code assumes only one argument to be a reference (to a field or operation) and it may be required to change in the future if we introduce operations with different assumptions.The parametric change here is pretty easy and I'll push a fix on that.