-
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
[Lens] Formula: add validation for multiple field/metrics #104092
Conversation
Pinging @elastic/kibana-app (Team:KibanaApp) |
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.
While this logic appears to work for cases where the user has duplicate types, like sum(bytes, bytes)
or moving_average(count(), sum())
, it does not catch mixed types like sum(bytes, count())
which are also invalid.
Also, I think we should backport this 7.14 and mark this as a release_note:skip
, I would mark it as a bugfix only if we ship the broken state to users.
Add one more validation case, for the mixed mode in particular, and enriched tests with different type of parameters as well. |
@elasticmachine merge upstream |
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.
Didn't manage to break it, but I'm unsure about some things - left some questions.
@@ -633,6 +701,22 @@ function runFullASTValidation( | |||
}) | |||
); | |||
} else { | |||
const fields = variables.filter( |
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.
Which case is caught 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.
Probably need to add some more comment context on all these branches.
In this specific branch it is checking that only a single field
is passed to field
-only operations.
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.
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.
You're right sorry. The check has to exclude the first argument.
This check is here to validate this scenario:
moving_average(average(bytes), bytes)
I'll refactor the logic to group both first argument check and all arguents check for field values.
@@ -621,6 +674,21 @@ function runFullASTValidation( | |||
locations: node.location ? [node.location] : [], | |||
}) | |||
); | |||
} else { | |||
if (functions.length > 1) { |
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 as moving_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.
…a into fix/formula-multiple-fields
if (functions.length > requiredFunctions) { | ||
errors.push( | ||
getMessageFromId({ | ||
messageId: 'tooManyFirstArguments', |
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 message still says a single ...
, but I think it's fine for now - as you mentioned the parsing logic can't handle this either at the moment.
@@ -633,6 +701,22 @@ function runFullASTValidation( | |||
}) | |||
); | |||
} else { | |||
const fields = variables.filter( |
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.
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.
On a meta level, I think this code is hard to deal with because of groupArgsByType
. For example sum(bytes, 5)
is probably worth throwing an error for, same with moving_average(sum(bytes), 5)
, but scanning through the code I'm not there's a simple validator for both. What I was hoping for was a simple test that would catch all of the "extra arguments" cases, like what we do for Math: if (node.args.length > positionalArguments.length) {
. So if I can suggest a refactoring here, it would be that I'd iterate over each argument in position order instead of using the grouped arguments. This would simplify the code and also catch edge cases like the one I've mentioned above.
I think this makes sense over a general refactoring of the validation code, rather than this specific PR to fix some edge cases. I've refactored the code now and added many more test cases with both positional and type-based issues and it all passes with the new implementation. |
@elasticmachine merge upstream |
…a into fix/formula-multiple-fields
@elasticmachine merge upstream |
@elasticmachine merge upstream |
@elasticmachine merge upstream |
💚 Build SucceededMetrics [docs]Async chunks
History
To update your PR or re-run it, just comment with: |
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.
Tested and LGTM. It's still missing the case where you do sum(bytes, 5)
but this is worth getting into 7.14 and refactoring later as it significantly improves the semantics.
…4092) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
…4092) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
…-png-pdf-report-type * 'master' of github.com:elastic/kibana: (292 commits) bring back KQL autocomplete in timeline + fix last updated (elastic#105380) [Maps] Change TOC pop-up wording to reflect filter change, not search bar change (elastic#105163) Updating urls to upstream elastic repo (elastic#105250) [Maps] Move new vector layer wizard card down (elastic#104797) Exclude registering the cases feature if not enabled (elastic#105292) [Uptime] Alerts - Monitor status alert - check monitor status by monitor.timespan (elastic#104541) updated UI copy (elastic#105184) Log a warning when documents of unknown types are detected during migration (elastic#105213) [Logs UI] Register log threshold rule as lifecycle rule (elastic#104341) [Ingest pipelines] add network direction processor (elastic#103436) [Console] Autocomplete definitions (manual backport) (elastic#105086) [Security Solution] User can make Exceptions for Memory protection alerts (elastic#102196) [Lens] Formula: add validation for multiple field/metrics (elastic#104092) Removing async from file upload and data visualizer plugins start lifecycle (elastic#105197) Fix error when validating the form with non blocking validations (elastic#103629) [ML] Fix "View by" swim lane with applied filter and sorting by score (elastic#105217) Update dependency @elastic/charts to v32 (elastic#104625) [CTI] shortens large numbers on Dashboard Link Panel (elastic#105269) [Security Solution][Endpoint][Host Isolation] Fixes bug to remove excess host metadata status toasts on non user initiated errors (elastic#105331) [Cases] Fix pushing alerts count on every push to external service (elastic#105030) ... # Conflicts: # x-pack/plugins/reporting/common/types.ts
Summary
This PR adds one more validation case if the user passes too many fields/metrics to an operation.
Checklist
Delete any items that are not applicable to this PR.
For maintainers