Skip to content
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 14 commits into from
Jul 13, 2021

Conversation

dej611
Copy link
Contributor

@dej611 dej611 commented Jul 1, 2021

Summary

This PR adds one more validation case if the user passes too many fields/metrics to an operation.

Screenshot 2021-07-01 at 14 38 58

Screenshot 2021-07-01 at 14 39 17

Screenshot 2021-07-01 at 14 39 32

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@dej611 dej611 added release_note:fix Team:Visualizations Visualization editors, elastic-charts and infrastructure v8.0.0 Feature:Lens auto-backport Deprecated - use backport:version if exact versions are needed v7.15.0 labels Jul 1, 2021
@dej611 dej611 requested a review from a team July 1, 2021 12:46
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app (Team:KibanaApp)

@dej611 dej611 mentioned this pull request Jul 1, 2021
14 tasks
Copy link
Contributor

@wylieconlon wylieconlon left a 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.

@dej611 dej611 added release_note:skip Skip the PR/issue when compiling release notes v7.14.0 and removed release_note:fix labels Jul 2, 2021
@dej611 dej611 requested a review from wylieconlon July 2, 2021 14:34
@dej611
Copy link
Contributor Author

dej611 commented Jul 2, 2021

Add one more validation case, for the mixed mode in particular, and enriched tests with different type of parameters as well.

@flash1293
Copy link
Contributor

@elasticmachine merge upstream

Copy link
Contributor

@flash1293 flash1293 left a 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(
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, this branch is in the if (nodeOperation.input === 'fullReference') block.

I don't see in which case it would become necessary - the only one is this, but it shows two error for the same problem:
Screenshot 2021-07-06 at 11 55 05

Copy link
Contributor Author

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

if (functions.length > requiredFunctions) {
errors.push(
getMessageFromId({
messageId: 'tooManyFirstArguments',
Copy link
Contributor

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, this branch is in the if (nodeOperation.input === 'fullReference') block.

I don't see in which case it would become necessary - the only one is this, but it shows two error for the same problem:
Screenshot 2021-07-06 at 11 55 05

Copy link
Contributor

@wylieconlon wylieconlon left a 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.

@dej611
Copy link
Contributor Author

dej611 commented Jul 7, 2021

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.

@dej611
Copy link
Contributor Author

dej611 commented Jul 7, 2021

@elasticmachine merge upstream

@dej611
Copy link
Contributor Author

dej611 commented Jul 8, 2021

@elasticmachine merge upstream

@dej611
Copy link
Contributor Author

dej611 commented Jul 9, 2021

@elasticmachine merge upstream

@dej611 dej611 requested a review from wylieconlon July 9, 2021 13:17
@dej611
Copy link
Contributor Author

dej611 commented Jul 12, 2021

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
lens 1.5MB 1.5MB +4.1KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@wylieconlon wylieconlon left a 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.

@dej611 dej611 merged commit 26bc001 into elastic:master Jul 13, 2021
@dej611 dej611 deleted the fix/formula-multiple-fields branch July 13, 2021 08:09
kibanamachine added a commit to kibanamachine/kibana that referenced this pull request Jul 13, 2021
…4092)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
kibanamachine added a commit to kibanamachine/kibana that referenced this pull request Jul 13, 2021
…4092)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
@kibanamachine
Copy link
Contributor

💚 Backport successful

Status Branch Result
7.14
7.x

The backport PRs will be merged automatically after passing CI.

kibanamachine added a commit that referenced this pull request Jul 13, 2021
…105395)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
kibanamachine added a commit that referenced this pull request Jul 13, 2021
…105396)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
jloleysens added a commit to jloleysens/kibana that referenced this pull request Jul 13, 2021
…-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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed Feature:Lens release_note:skip Skip the PR/issue when compiling release notes Team:Visualizations Visualization editors, elastic-charts and infrastructure v7.14.0 v7.15.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants