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

[React@18 failing test] fix visualize app - new charts library visualize area charts date histogram when no time filter interval errors should show error when calendar interval invalid #196308

Merged
merged 4 commits into from
Oct 22, 2024

Conversation

Dosant
Copy link
Contributor

@Dosant Dosant commented Oct 15, 2024

Summary

close #196303

We're working on upgrading Kibana to React@18 (in Legacy Mode). There are a couple failing tests when running React@18 in Legacy mode and this is one of them

visualize app - new charts library visualize area charts date histogram when no time filter interval errors should show error when calendar interval invalid. Failure

Image


I investigated the problem and understand what is not working and suggesting this simple fix, but open to any other approaches or suggestions.

To Reproduce the failing tests:

  1. Create aggregation based viz, e.g. Area Chart
  2. Add data histogram agg
  3. Change minimum interval to a invalid value (for example, "f")
  4. Change minimum interval to another invalid value ("ff")

React@18 failure:

error.mov

The error is thrown from here in the reducer:

When we try to update the state using 2nd invalid value, the error is thrown when we try to serialize the current agg with previous invalid value.

This code is exececuted when we call agg.serialize:

if (!isDurationInterval(this._i)) {
throw new TypeError('"' + this._i + '" is not a valid interval.');
}

Why don't we see this failure in React@17?

In React@17 we don't see an error screen, but we only see a log in the console.

TypeError: "f" is not a valid interval.

It turns out that React@17 consistently executed that reducer twice. first time during dispatch and second time during rendering. This shouldn't be a problem because reducers are supposed to be pure (without side-effects). But in this case calling agg.serialize only throws an error when called the first time! So in React@17 the reducer was called the first time, the error was swallowed, then it was called the 2nd time and, since the TimeBucket was cached, there was no error anymore, so it never bubbled up during rendering.

The root cause of inconsitent behaviour is here:

if (buckets) return buckets;
buckets = new TimeBuckets({
'histogram:maxBars': getConfig(UI_SETTINGS.HISTOGRAM_MAX_BARS),
'histogram:barTarget': getConfig(UI_SETTINGS.HISTOGRAM_BAR_TARGET),
dateFormat: getConfig('dateFormat'),
'dateFormat:scaled': getConfig('dateFormat:scaled'),
});
updateTimeBuckets(this, calculateBounds, buckets);
return buckets;

when get() called first time we create buckets and cache them. but we cache them before calling updateTimeBuckets which is where the error happens.

To fix this issue, we should make the reducer pure. One approach is to swallow that error so that the call to agg.serialize() is consistent. Another approach could be to always throw that error, but then a larger refactor is needed and this likely a more risky and impactfull change.

@Dosant Dosant changed the title catch [React@18 failing test] fix visualize app - new charts library visualize area charts date histogram when no time filter interval errors should show error when calendar interval invalid Oct 16, 2024
@Dosant Dosant added release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) labels Oct 16, 2024
@Dosant Dosant marked this pull request as ready for review October 16, 2024 09:30
@Dosant Dosant requested review from a team as code owners October 16, 2024 09:30
@elasticmachine
Copy link
Contributor

Pinging @elastic/appex-sharedux (Team:SharedUX)

@Dosant
Copy link
Contributor Author

Dosant commented Oct 17, 2024

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

elasticmachine commented Oct 17, 2024

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #80 / home app sample data installing should install logs sample data set

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
data 419.9KB 419.9KB +39.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
data 55 56 +1

Total ESLint disabled count

id before after diff
data 57 58 +1

History

@Dosant
Copy link
Contributor Author

Dosant commented Oct 18, 2024

@elasticmachine merge upstream

try {
updateTimeBuckets(this, calculateBounds, buckets);
} catch (e) {
// swallow the error even though the agg is misconfigured
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is safe and shouldn't break anything because this is inside the accessor and previosly threw error only on a first attempt to access. Now at least it would be consistent

Copy link
Contributor

@davismcphee davismcphee left a comment

Choose a reason for hiding this comment

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

Code-only review, change LGTM from the Data Discovery side. We should also get approval from @elastic/kibana-visualizations too.

@Dosant
Copy link
Contributor Author

Dosant commented Oct 21, 2024

@elasticmachine merge upstream

@Dosant Dosant merged commit feb5b79 into elastic:main Oct 22, 2024
27 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/11457672672

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Oct 22, 2024
…ize area charts date histogram when no time filter interval errors should show error when calendar interval invalid (elastic#196308)

## Summary

close elastic#196303

We're working on upgrading Kibana to React@18 (in Legacy Mode). There
are a couple failing tests when running React@18 in Legacy mode and this
is one of them

visualize app - new charts library visualize area charts date histogram
when no time filter interval errors should show error when calendar
interval invalid.
[Failure](https://buildkite.com/elastic/kibana-pull-request/builds/236562#019222ec-e8d1-4465-ada3-fd923283b6f4)

![Image](https://github.com/user-attachments/assets/2cdaca3c-9ccf-4208-b6f3-6975588eb5fe)

----

I investigated the problem and understand what is not working and
suggesting this simple fix, but open to any other approaches or
suggestions.

To Reproduce the failing tests:

1. Create aggregation based viz, e.g. Area Chart
2. Add data histogram agg
3. Change minimum interval to a invalid value (for example, "f")
4. Change minimum interval to another invalid value ("ff")

React@18 failure:

https://github.com/user-attachments/assets/f8684b48-fb24-4500-a762-2a116ed55297

The error is thrown from here in the reducer:

https://github.com/elastic/kibana/blob/23e0e1e61c6df451cc38763b53a6e2db5518b9f4/src/plugins/vis_default_editor/public/components/sidebar/state/reducers.ts#L82

When we try to update the state using 2nd invalid value, the error is
thrown when we try to serialize the current agg with previous invalid
value.

This code is exececuted when we call `agg.serialize`:

https://github.com/elastic/kibana/blob/5ed698182887e18d2aa6c4b6782cc636a45a1472/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts#L200-L202

**Why don't we see this failure in React@17?**

In React@17 we don't see an error screen, but we only see a log in the
console.

> TypeError: "f" is not a valid interval.

It turns out that React@17 consistently executed that reducer twice.
first time during dispatch and second time during rendering. This
shouldn't be a problem because reducers are supposed to be pure (without
side-effects). **But in this case calling `agg.serialize` only throws an
error when called the first time**! So in React@17 the reducer was
called the first time, the error was swallowed, then it was called the
2nd time and, since the `TimeBucket` was cached, there was no error
anymore, so it never bubbled up during rendering.

The root cause of inconsitent behaviour is here:

https://github.com/elastic/kibana/blob/8afbbc008222dee377aab568a639466d49c56306/src/plugins/data/common/search/aggs/buckets/date_histogram.ts#L111-L121

when `get()` called first time we create buckets and cache them. but we
cache them before calling `updateTimeBuckets` which is where the error
happens.

To fix this issue, we should make the reducer pure. One approach is to
swallow that error so that the call to `agg.serialize()` is consistent.
Another approach could be to always throw that error, but then a larger
refactor is needed and this likely a more risky and impactfull change.

(cherry picked from commit feb5b79)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Oct 22, 2024
…visualize area charts date histogram when no time filter interval errors should show error when calendar interval invalid (#196308) (#197198)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[React@18 failing test] fix visualize app - new charts library
visualize area charts date histogram when no time filter interval errors
should show error when calendar interval invalid
(#196308)](#196308)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Anton
Dosov","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-22T09:50:15Z","message":"[React@18
failing test] fix visualize app - new charts library visualize area
charts date histogram when no time filter interval errors should show
error when calendar interval invalid (#196308)\n\n##
Summary\r\n\r\nclose
https://github.com/elastic/kibana/issues/196303\r\n\r\nWe're working on
upgrading Kibana to React@18 (in Legacy Mode). There\r\nare a couple
failing tests when running React@18 in Legacy mode and this\r\nis one of
them\r\n\r\nvisualize app - new charts library visualize area charts
date histogram\r\nwhen no time filter interval errors should show error
when calendar\r\ninterval
invalid.\r\n[Failure](https://buildkite.com/elastic/kibana-pull-request/builds/236562#019222ec-e8d1-4465-ada3-fd923283b6f4)\r\n\r\n\r\n![Image](https://github.com/user-attachments/assets/2cdaca3c-9ccf-4208-b6f3-6975588eb5fe)\r\n\r\n\r\n----
\r\n\r\n\r\nI investigated the problem and understand what is not
working and\r\nsuggesting this simple fix, but open to any other
approaches or\r\nsuggestions.\r\n\r\n\r\nTo Reproduce the failing tests:
\r\n\r\n1. Create aggregation based viz, e.g. Area Chart\r\n2. Add data
histogram agg\r\n3. Change minimum interval to a invalid value (for
example, \"f\")\r\n4. Change minimum interval to another invalid value
(\"ff\")\r\n\r\nReact@18 failure:
\r\n\r\n\r\nhttps://github.com/user-attachments/assets/f8684b48-fb24-4500-a762-2a116ed55297\r\n\r\nThe
error is thrown from here in the reducer:
\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/23e0e1e61c6df451cc38763b53a6e2db5518b9f4/src/plugins/vis_default_editor/public/components/sidebar/state/reducers.ts#L82\r\n\r\nWhen
we try to update the state using 2nd invalid value, the error
is\r\nthrown when we try to serialize the current agg with previous
invalid\r\nvalue.\r\n\r\nThis code is exececuted when we call
`agg.serialize`:\r\n
\r\n\r\nhttps://github.com/elastic/kibana/blob/5ed698182887e18d2aa6c4b6782cc636a45a1472/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts#L200-L202\r\n\r\n\r\n**Why
don't we see this failure in React@17?**\r\n\r\nIn React@17 we don't see
an error screen, but we only see a log in the\r\nconsole.\r\n\r\n>
TypeError: \"f\" is not a valid interval.\r\n\r\nIt turns out that
React@17 consistently executed that reducer twice.\r\nfirst time during
dispatch and second time during rendering. This\r\nshouldn't be a
problem because reducers are supposed to be pure
(without\r\nside-effects). **But in this case calling `agg.serialize`
only throws an\r\nerror when called the first time**! So in React@17 the
reducer was\r\ncalled the first time, the error was swallowed, then it
was called the\r\n2nd time and, since the `TimeBucket` was cached, there
was no error\r\nanymore, so it never bubbled up during
rendering.\r\n\r\nThe root cause of inconsitent behaviour is
here:\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/8afbbc008222dee377aab568a639466d49c56306/src/plugins/data/common/search/aggs/buckets/date_histogram.ts#L111-L121\r\n\r\nwhen
`get()` called first time we create buckets and cache them. but
we\r\ncache them before calling `updateTimeBuckets` which is where the
error\r\nhappens.\r\n\r\n\r\nTo fix this issue, we should make the
reducer pure. One approach is to\r\nswallow that error so that the call
to `agg.serialize()` is consistent.\r\nAnother approach could be to
always throw that error, but then a larger\r\nrefactor is needed and
this likely a more risky and impactfull
change.","sha":"feb5b79a96162b0259ecfd5d8216050cd074a3a4","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:SharedUX","backport:prev-minor"],"title":"[React@18
failing test] fix visualize app - new charts library visualize area
charts date histogram when no time filter interval errors should show
error when calendar interval
invalid","number":196308,"url":"https://github.com/elastic/kibana/pull/196308","mergeCommit":{"message":"[React@18
failing test] fix visualize app - new charts library visualize area
charts date histogram when no time filter interval errors should show
error when calendar interval invalid (#196308)\n\n##
Summary\r\n\r\nclose
https://github.com/elastic/kibana/issues/196303\r\n\r\nWe're working on
upgrading Kibana to React@18 (in Legacy Mode). There\r\nare a couple
failing tests when running React@18 in Legacy mode and this\r\nis one of
them\r\n\r\nvisualize app - new charts library visualize area charts
date histogram\r\nwhen no time filter interval errors should show error
when calendar\r\ninterval
invalid.\r\n[Failure](https://buildkite.com/elastic/kibana-pull-request/builds/236562#019222ec-e8d1-4465-ada3-fd923283b6f4)\r\n\r\n\r\n![Image](https://github.com/user-attachments/assets/2cdaca3c-9ccf-4208-b6f3-6975588eb5fe)\r\n\r\n\r\n----
\r\n\r\n\r\nI investigated the problem and understand what is not
working and\r\nsuggesting this simple fix, but open to any other
approaches or\r\nsuggestions.\r\n\r\n\r\nTo Reproduce the failing tests:
\r\n\r\n1. Create aggregation based viz, e.g. Area Chart\r\n2. Add data
histogram agg\r\n3. Change minimum interval to a invalid value (for
example, \"f\")\r\n4. Change minimum interval to another invalid value
(\"ff\")\r\n\r\nReact@18 failure:
\r\n\r\n\r\nhttps://github.com/user-attachments/assets/f8684b48-fb24-4500-a762-2a116ed55297\r\n\r\nThe
error is thrown from here in the reducer:
\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/23e0e1e61c6df451cc38763b53a6e2db5518b9f4/src/plugins/vis_default_editor/public/components/sidebar/state/reducers.ts#L82\r\n\r\nWhen
we try to update the state using 2nd invalid value, the error
is\r\nthrown when we try to serialize the current agg with previous
invalid\r\nvalue.\r\n\r\nThis code is exececuted when we call
`agg.serialize`:\r\n
\r\n\r\nhttps://github.com/elastic/kibana/blob/5ed698182887e18d2aa6c4b6782cc636a45a1472/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts#L200-L202\r\n\r\n\r\n**Why
don't we see this failure in React@17?**\r\n\r\nIn React@17 we don't see
an error screen, but we only see a log in the\r\nconsole.\r\n\r\n>
TypeError: \"f\" is not a valid interval.\r\n\r\nIt turns out that
React@17 consistently executed that reducer twice.\r\nfirst time during
dispatch and second time during rendering. This\r\nshouldn't be a
problem because reducers are supposed to be pure
(without\r\nside-effects). **But in this case calling `agg.serialize`
only throws an\r\nerror when called the first time**! So in React@17 the
reducer was\r\ncalled the first time, the error was swallowed, then it
was called the\r\n2nd time and, since the `TimeBucket` was cached, there
was no error\r\nanymore, so it never bubbled up during
rendering.\r\n\r\nThe root cause of inconsitent behaviour is
here:\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/8afbbc008222dee377aab568a639466d49c56306/src/plugins/data/common/search/aggs/buckets/date_histogram.ts#L111-L121\r\n\r\nwhen
`get()` called first time we create buckets and cache them. but
we\r\ncache them before calling `updateTimeBuckets` which is where the
error\r\nhappens.\r\n\r\n\r\nTo fix this issue, we should make the
reducer pure. One approach is to\r\nswallow that error so that the call
to `agg.serialize()` is consistent.\r\nAnother approach could be to
always throw that error, but then a larger\r\nrefactor is needed and
this likely a more risky and impactfull
change.","sha":"feb5b79a96162b0259ecfd5d8216050cd074a3a4"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/196308","number":196308,"mergeCommit":{"message":"[React@18
failing test] fix visualize app - new charts library visualize area
charts date histogram when no time filter interval errors should show
error when calendar interval invalid (#196308)\n\n##
Summary\r\n\r\nclose
https://github.com/elastic/kibana/issues/196303\r\n\r\nWe're working on
upgrading Kibana to React@18 (in Legacy Mode). There\r\nare a couple
failing tests when running React@18 in Legacy mode and this\r\nis one of
them\r\n\r\nvisualize app - new charts library visualize area charts
date histogram\r\nwhen no time filter interval errors should show error
when calendar\r\ninterval
invalid.\r\n[Failure](https://buildkite.com/elastic/kibana-pull-request/builds/236562#019222ec-e8d1-4465-ada3-fd923283b6f4)\r\n\r\n\r\n![Image](https://github.com/user-attachments/assets/2cdaca3c-9ccf-4208-b6f3-6975588eb5fe)\r\n\r\n\r\n----
\r\n\r\n\r\nI investigated the problem and understand what is not
working and\r\nsuggesting this simple fix, but open to any other
approaches or\r\nsuggestions.\r\n\r\n\r\nTo Reproduce the failing tests:
\r\n\r\n1. Create aggregation based viz, e.g. Area Chart\r\n2. Add data
histogram agg\r\n3. Change minimum interval to a invalid value (for
example, \"f\")\r\n4. Change minimum interval to another invalid value
(\"ff\")\r\n\r\nReact@18 failure:
\r\n\r\n\r\nhttps://github.com/user-attachments/assets/f8684b48-fb24-4500-a762-2a116ed55297\r\n\r\nThe
error is thrown from here in the reducer:
\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/23e0e1e61c6df451cc38763b53a6e2db5518b9f4/src/plugins/vis_default_editor/public/components/sidebar/state/reducers.ts#L82\r\n\r\nWhen
we try to update the state using 2nd invalid value, the error
is\r\nthrown when we try to serialize the current agg with previous
invalid\r\nvalue.\r\n\r\nThis code is exececuted when we call
`agg.serialize`:\r\n
\r\n\r\nhttps://github.com/elastic/kibana/blob/5ed698182887e18d2aa6c4b6782cc636a45a1472/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts#L200-L202\r\n\r\n\r\n**Why
don't we see this failure in React@17?**\r\n\r\nIn React@17 we don't see
an error screen, but we only see a log in the\r\nconsole.\r\n\r\n>
TypeError: \"f\" is not a valid interval.\r\n\r\nIt turns out that
React@17 consistently executed that reducer twice.\r\nfirst time during
dispatch and second time during rendering. This\r\nshouldn't be a
problem because reducers are supposed to be pure
(without\r\nside-effects). **But in this case calling `agg.serialize`
only throws an\r\nerror when called the first time**! So in React@17 the
reducer was\r\ncalled the first time, the error was swallowed, then it
was called the\r\n2nd time and, since the `TimeBucket` was cached, there
was no error\r\nanymore, so it never bubbled up during
rendering.\r\n\r\nThe root cause of inconsitent behaviour is
here:\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/8afbbc008222dee377aab568a639466d49c56306/src/plugins/data/common/search/aggs/buckets/date_histogram.ts#L111-L121\r\n\r\nwhen
`get()` called first time we create buckets and cache them. but
we\r\ncache them before calling `updateTimeBuckets` which is where the
error\r\nhappens.\r\n\r\n\r\nTo fix this issue, we should make the
reducer pure. One approach is to\r\nswallow that error so that the call
to `agg.serialize()` is consistent.\r\nAnother approach could be to
always throw that error, but then a larger\r\nrefactor is needed and
this likely a more risky and impactfull
change.","sha":"feb5b79a96162b0259ecfd5d8216050cd074a3a4"}}]}]
BACKPORT-->

Co-authored-by: Anton Dosov <[email protected]>
@Dosant Dosant added the React@18 label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) React@18 release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) v8.17.0 v9.0.0
Projects
None yet
5 participants