-
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
Changes from 4 commits
a89059d
0dcc0b8
b50ecd5
13eb57c
12521e6
950ff34
9748083
8aee877
8c042c1
2bc89e8
aa4b7f6
07fb0a0
7f571bf
d863ca4
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 |
---|---|---|
|
@@ -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,14 @@ interface ValidationErrors { | |
message: string; | ||
type: {}; | ||
}; | ||
tooManyFirstArguments: { | ||
message: string; | ||
type: { operation: string; type: string; count: number; text: string }; | ||
}; | ||
wrongArgument: { | ||
message: string; | ||
type: { operation: string; text: string; type: string }; | ||
}; | ||
} | ||
|
||
type ErrorTypes = keyof ValidationErrors; | ||
|
@@ -276,6 +284,20 @@ 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}, found {count}: {text}', | ||
values: { operation: out.operation, text: out.text, count: out.count, type: out.type }, | ||
}); | ||
break; | ||
case 'wrongArgument': | ||
message = i18n.translate('xpack.lens.indexPattern.formulaOperationwrongArgument', { | ||
defaultMessage: | ||
'The operation {operation} in the Formula does not support {type} parameters, found: {text}', | ||
values: { operation: out.operation, text: out.text, type: out.type }, | ||
}); | ||
break; | ||
// case 'mathRequiresFunction': | ||
// message = i18n.translate('xpack.lens.indexPattern.formulaMathRequiresFunctionLabel', { | ||
// defaultMessage; 'The function {name} requires an Elasticsearch function', | ||
|
@@ -531,7 +553,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 +583,37 @@ 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] : [], | ||
}) | ||
); | ||
} | ||
} | ||
if (functions.length) { | ||
errors.push( | ||
getMessageFromId({ | ||
messageId: 'wrongArgument', | ||
values: { | ||
operation: node.name, | ||
text: (functions as TinymathFunction[]).map(({ text }) => text).join(', '), | ||
type: 'metric', | ||
}, | ||
locations: node.location ? [node.location] : [], | ||
}) | ||
); | ||
} | ||
} else { | ||
// Named arguments only | ||
|
@@ -603,7 +656,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 +674,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( | ||
|
@@ -633,6 +701,22 @@ function runFullASTValidation( | |
}) | ||
); | ||
} else { | ||
const fields = variables.filter( | ||
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. Which case is caught here? 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. Probably need to add some more comment context on all these branches. 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. 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. You're right sorry. The check has to exclude the first argument.
I'll refactor the logic to group both first argument check and all arguents check for field values. |
||
(arg) => isArgumentValidType(arg, 'variable') && !isMathNode(arg) | ||
); | ||
if (fields.length) { | ||
errors.push( | ||
getMessageFromId({ | ||
messageId: 'wrongArgument', | ||
values: { | ||
operation: node.name, | ||
text: (fields as TinymathVariable[]).map(({ text }) => text).join(', '), | ||
type: 'field', | ||
}, | ||
locations: node.location ? [node.location] : [], | ||
}) | ||
); | ||
} | ||
const argumentsErrors = validateNameArguments( | ||
node, | ||
nodeOperation, | ||
|
@@ -736,7 +820,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; | ||
} | ||
|
||
|
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.