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] Move editorFrame state to redux #100858

Merged
merged 25 commits into from
Jul 1, 2021

Conversation

mbondyra
Copy link
Contributor

@mbondyra mbondyra commented May 28, 2021

Summary

Second part of #89690

Most important changes

  1. Moving editor frame state from using useReducer to redux-toolkit store
  2. Expose resolved datasourcesMap and visualizationMap from the editor frame service to the mounter
  3. Moving all the loading logic from editor frame to the mounter - replace useEffects with linear code
  4. Remove unnecessary synchronization onChange of two properties from the store: lastKnownDoc and indexPatternsForTopNav
    a. Removing indexPatternsForTopNav from the global state and instead getting it in the lens_top_nav directly
    b. Removing lastKnownDoc and instead calculating it from the global state in the app - could be improved further and be kept as a selector instead of keeping it in the local app state
  5. framePublicAPI - remove methods addLayer and removeLayer and dispatch the store actions directly in chart_switcher
  6. Replace some mutations immer forced on - nothing big, but mentioning it to avoid confusion why these changes are there
  7. Change of behaviour: before we were able to save incorrect chart and then the error was shown in the dashboard. Now, if the expression is not built, the chart cannot be saved. (I wasn't planning on that change, it happened with the implementation changes, but I think it's an improvement)

Screenshot 2021-06-23 at 10 35 38

Screenshot 2021-06-23 at 10 38 50

Doubts:

  1. Store structure is just a single slice lensSlice

To do in the future:

  • I kept the subtypes ‘UPDATE_ALL_STATES’, but we probably want to keep redux toolkit naming convention lens/updateAllStates
  • moving loading logic to the middleware
  • create better selectors to avoid some unnecessary rerenders

  • calculate data structures which are derived from the whole state ( expression, suggestions, visualization groups, editorFrameAPI) in custom selectors using deep equality
  • get rid of the updaters - this is not redux way of doing this as updaters are not serialized
  • get rid of any not-serializable properties from state

TODO before merging:

  • Missing tests for mounter
  • missing tests for the store

@mbondyra mbondyra added Team:Visualizations Visualization editors, elastic-charts and infrastructure v8.0.0 release_note:skip Skip the PR/issue when compiling release notes Feature:Lens v7.14.0 auto-backport Deprecated - use backport:version if exact versions are needed labels May 28, 2021
@mbondyra mbondyra requested a review from a team May 28, 2021 10:48
@elasticmachine
Copy link
Contributor

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

@mbondyra mbondyra marked this pull request as draft May 28, 2021 10:48
@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch 3 times, most recently from 608bdbd to 3b0304e Compare June 15, 2021 11:31
@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch 4 times, most recently from 94dc761 to 6b22cc5 Compare June 18, 2021 13:38
})
);

{
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: why this isolated scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dej611 this code is not ready to be reviewed yet -> I marked it this way to remember about refactoring it. I'll let you know on Monday (hopefully) when it's ready!

@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch 2 times, most recently from 225115a to 44e03d1 Compare June 18, 2021 18:32
@elastic elastic deleted a comment from kibanamachine Jun 22, 2021
@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch 2 times, most recently from 889015b to 9fe53a5 Compare June 22, 2021 15:03
@mbondyra mbondyra marked this pull request as ready for review June 22, 2021 17:08
@mbondyra mbondyra added v7.15.0 and removed v7.14.0 labels Jun 22, 2021
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.

I'm not quite done reviewing this, but I did some testing today and wasn't able to break anything!

@@ -177,14 +208,6 @@ describe('Lens App', () => {
expect(services.data.query.filterManager.getFilters).not.toHaveBeenCalled();
});

it('displays errors from the frame in a toast', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like you haven't copied this unit test into the mounter- the onError callback was specifically called when initializeDatasources threw errors.

/**
* A map of all available palettes (keys being the ids).
*/
availablePalettes: PaletteRegistry;
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this availablePalettes deleted fully? I'm not seeing a replacement in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's because they are never used. They are being loaded again in the particular visualizations that need them, for example here:
https://github.com/elastic/kibana/blob/master/x-pack/plugins/lens/public/datatable_visualization/index.ts#L38

// This is a safeguard that prevents us from accidentally updating the
// wrong visualization. This occurs in some cases due to the uncoordinated
// way we manage state across plugins.
if (state.visualization.activeId !== payload.visualizationId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In theory both of these safeguards will be fixed by this PR!

});

it('should render render icon if there is no preview expression', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this is just changing the test order?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, reverting!

@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch from 9fe53a5 to c26a6f8 Compare June 23, 2021 09:01
@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch from c26a6f8 to 366d41a Compare June 23, 2021 09:16
@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch from c84c5dc to cd75cab Compare June 23, 2021 12:05
@mbondyra mbondyra force-pushed the lens/editor_frame_state_refactoring branch from bb8fcfa to 4be257c Compare June 23, 2021 13:05
@dej611
Copy link
Contributor

dej611 commented Jun 23, 2021

Still reviewing but I think I found a bug in the current PR: when creating a percentage chart, on dropping a field on the main chart area, only the very first time, it creates a new layer where the field is added in the Break down by dimension, leaving the existing layer Break down by empty.

redux_percentage

On master this works as expected adding the field as Break down by dimension in the current layer.

Perhaps this is a possible side effect of the removal of the addLayer from the frameAPI?

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.

I tested and went through the code - looks pretty good to me already. We can sync offline but I want to align about the next steps to make sure we are going into the right direction.

if (
!initialInput ||
(attributeService.inputIsRefType(initialInput) &&
initialInput.savedObjectId === persistedDoc?.savedObjectId)
) {
return;
return initializeDatasources(
Copy link
Contributor

Choose a reason for hiding this comment

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

It's fine for now, but I would like to move all of these things into the middleware at some point

})
);
if (initialContext) {
const selectedSuggestion = getVisualizeFieldSuggestions({
Copy link
Contributor

Choose a reason for hiding this comment

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

The whole block up to line 384 could be moved into the reduced because it's completely pure. The only thing we would need to make sure is making datasource and visualization map known to the reducer. But this will probably happen in a separate step, right?

import { uiActionsPluginMock } from '../../../../../../src/plugins/ui_actions/public/mocks';
import { chartPluginMock } from '../../../../../../src/plugins/charts/public/mocks';
import { expressionsPluginMock } from '../../../../../../src/plugins/expressions/public/mocks';
import { mockDataPlugin, mountWithProvider } from '../../mocks';
import { setState, setToggleFullscreen } from '../../state_management';
import { FrameLayout } from './frame_layout';

function generateSuggestion(state = {}): DatasourceSuggestion {
return {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't comment there but in line 71 there's still an onError prop - seems to be a leftover

@mbondyra
Copy link
Contributor Author

mbondyra commented Jun 23, 2021

@dej611 thank you for your finding. It's caused somewhere by this commit: 7a8327c
I'll try to investigate more on that!

Copy link
Contributor

@dej611 dej611 left a comment

Choose a reason for hiding this comment

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

Code LGTM 👍

@flash1293
Copy link
Contributor

@elasticmachine merge upstream

@flash1293
Copy link
Contributor

@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 +1.5KB
observability 478.9KB 478.9KB -21.0B
total +1.5KB
Unknown metric groups

References to deprecated APIs

id before after diff
lens 71 61 -10

History

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

@dej611 dej611 merged commit 6e3df60 into elastic:master Jul 1, 2021
kibanamachine added a commit to kibanamachine/kibana that referenced this pull request Jul 1, 2021
Co-authored-by: Joe Reuter <johannes.reuter@elastic.co>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: dej611 <dej611@gmail.com>
@kibanamachine
Copy link
Contributor

💚 Backport successful

Status Branch Result
7.x

This backport PR will be merged automatically after passing CI.

kibanamachine added a commit that referenced this pull request Jul 1, 2021
Co-authored-by: Joe Reuter <johannes.reuter@elastic.co>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: dej611 <dej611@gmail.com>

Co-authored-by: Marta Bondyra <marta.bondyra@gmail.com>
Co-authored-by: Joe Reuter <johannes.reuter@elastic.co>
Co-authored-by: dej611 <dej611@gmail.com>
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.15.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants