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

[ML] Fixing wizard charts repeated loading #44409

Merged

Conversation

jgowdyelastic
Copy link
Member

@jgowdyelastic jgowdyelastic commented Aug 29, 2019

Fixes repeated chart loading when navigating through the wizards.

All chart loading functions are wrapped in memoize-one to cache the last results based on the search parameters. This reduces the amount of data loaded when changing steps. especially when you get to the summary step where all the chart data would be loaded again.

The metric_selection component for each wizard has been split into two. A "live" version, as in, one which updates as the user adds detectors. and a results version metric_selection_summary, which is embedded in the summary step and only used to display results.
Because of this split, there is now some duplicate code which can be removed in a followup PR.
The *_summary files are mainly a cut down version of the original metric_selection files.
The original metric_selection files have the results code removed.

The chart settings are now in a shared custom hook, used by all wizards.

Also now correctly displays chart loading errors in a toast.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

- [ ] This was checked for cross-browser compatibility, including a check against IE11
- [ ] Any text added follows EUI's writing guidelines, uses sentence case text and includes i18n support
- [ ] Documentation was added for features that require explanation or tutorials
- [ ] Unit or functional tests were updated or added to match the most common scenarios
- [ ] This was checked for keyboard-only and screenreader accessibility

For maintainers

- [ ] This was checked for breaking API changes and was labeled appropriately
- [ ] This includes a feature addition or change that requires a release note and was labeled appropriately

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@jgowdyelastic jgowdyelastic self-assigned this Aug 30, 2019
@jgowdyelastic jgowdyelastic added :ml bug Fixes for quality problems that affect the customer experience review v7.4.0 v7.5.0 v8.0.0 labels Aug 30, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/ml-ui

@jgowdyelastic jgowdyelastic changed the title [ML] Caching wizard chart data [ML] Fixing wizard charts repeated loading Aug 30, 2019
@jgowdyelastic jgowdyelastic added non-issue Indicates to automation that a pull request should not appear in the release notes release_note:skip Skip the PR/issue when compiling release notes labels Aug 30, 2019
@jgowdyelastic jgowdyelastic marked this pull request as ready for review August 30, 2019 13:18
@jgowdyelastic jgowdyelastic requested a review from a team as a code owner August 30, 2019 13:18
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Copy link
Contributor

@walterra walterra left a comment

Choose a reason for hiding this comment

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

Nice use of memoization! Posted a question about getChartSettings().

@@ -50,3 +54,43 @@ export const seriesStyle = {
visible: false,
},
};

export function useChartSettings() {
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 trying to wrap my head around the usage of this 😄 so I have this question: Why does this need to be a hook if you're returning the value directly from getChartSettings()? I'm not sure but could this have unexpected side effects? Just some food for thought:

  • You could rename getChartSettings() to updateChartSettings() and remove return cs;. This would mean changing all code that consumes the hook to react to chartSettings only and not rely on a value being returned immediately.
  • If we leave the code as is it might be worth adding a comment why this combination of it being a hook and returning a value immediately is necessary. Maybe rename getChartSettings() to updateAndGetChartSettings(), with just get it might be unexpected that this function has side effects.
  • And yet maybe another way to approach this: getChartSettings() could become a useEffect() itself so it would update on demand if jobCreator or chartInterval changes. Then you'd always get the most up to date chartSettings from the hook.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not surprised you questioned this. This ended up being the most fiddly and time consuming part of this refactor.
Originally had getChartSettings be a basic util function that took jobCreator and chartInterval as parameters. but i then decided that it could be a hook which could take advantage of the context, as per your last point. the problem here is that i then needed another useEffect in every file using this hook to trigger the loadCharts on a chartSettings change. and each of those files already have a lot of useEffects.

I think the solution is to go back to a normal, non-hook, util function that calculates and returns the chartSettings. Each file calling it will then need to have their own useState to wrap this so it can be passed into the ChartGrid component at the bottom of the file.
The fact that each file will need their own useState is the main reason i went with a custom hook, so they could share it. But I also need the calculated chartSettings immediately, hence why i was also returning a function that could supply it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed custom hook in 39d24ef

Copy link
Contributor

@peteharverson peteharverson left a comment

Choose a reason for hiding this comment

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

Tested, including IE11, and LGTM

Copy link
Contributor

@walterra walterra left a comment

Choose a reason for hiding this comment

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

Latest changes LGTM 👍

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@jgowdyelastic jgowdyelastic merged commit a3c2376 into elastic:master Sep 2, 2019
jgowdyelastic added a commit to jgowdyelastic/kibana that referenced this pull request Sep 2, 2019
* [ML] Caching wizard chart data

* using shared chart settings hook

* fixing settings hook and switching to memoize-one

* removing custom hook
jgowdyelastic added a commit to jgowdyelastic/kibana that referenced this pull request Sep 2, 2019
* [ML] Caching wizard chart data

* using shared chart settings hook

* fixing settings hook and switching to memoize-one

* removing custom hook
jgowdyelastic added a commit that referenced this pull request Sep 2, 2019
* [ML] Caching wizard chart data

* using shared chart settings hook

* fixing settings hook and switching to memoize-one

* removing custom hook
jgowdyelastic added a commit that referenced this pull request Sep 2, 2019
* [ML] Caching wizard chart data

* using shared chart settings hook

* fixing settings hook and switching to memoize-one

* removing custom hook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience :ml non-issue Indicates to automation that a pull request should not appear in the release notes release_note:skip Skip the PR/issue when compiling release notes review v7.4.0 v7.5.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants