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

Query Filter \ Filter Manager: de-angularize and move to data plugin #37311

Merged

Conversation

lizozom
Copy link
Contributor

@lizozom lizozom commented May 29, 2019

Important: Much of the code was merged accidentally by another PR

Summary

This PR a full rewrite of query_filter into the data/filter/filter_manager new platform service.
This was done in such a way that the API is fully backwards compatible.

Previously, initializing queryFilter was done this way:

// Replace this import 
import { FilterBarQueryFilterProvider } from 'ui/filter_manager/query_filter';
const queryFilter = Private(FilterBarQueryFilterProvider);
queryFilter.addFilters(...);

While this is still supported, you can also use the FilterManager instance directly:

import { FilterStateManager } from 'plugins/data';
import { data } from 'plugins/data/setup';
const filterManager = data.filter.filterManager;

// You have to hook the filter manager instance into your app's state.
// FilterStateManager is a helper that hooks it into the existing global \ app scope)
const filterStateManager = new FilterStateManager(globalState, getAppState, filterManager);

filterManager.addFilters(...);

This PR also includes a very simple de-angularization of FilterManagerProvider.
It is now used in the following manner:

const filterGen = getFilterGenerator(queryFilter);

But it should be merged with FilterManager.

Notes and comments

  • The code still relies on Private caching to instantiate a single instance of FilterManager per app.
  • The es-query Filter type definition changed, and $state is now optional. This is due to the fact that some places create filters without state. FilterManager however, makes sure that all filters have state.
  • globalState, getAppState and indexPatterns are still angular injected dependencies.
  • FilterStateManager is just a convenience wrapper around app+global states, and might be removed when a new state manager is introduced. It uses setTimeout instead of rootScope to watch state variables. It can be changed, but everything works this way as well.
  • In FilterManager, there was a very old optimization that didn't re-fetch the data, when specific filter updates occurred:
!onlyDisabled(nextVal, prevVal) && !onlyStateChanged(newFilters, oldFilters)

I went over this with @spalger and this optimization seems irrelevant. I removed it and for now we just fire fetch and update events at the same time (to avoid changing subscribers).

  • In query_filter there several methods that were not being used by anything, but were there for completeness (like toggleAll and disableAll for example). I chose not to implement them for the time being, but they may be implemented again if needed.
  • I had to add a try-catch clause to ui/errors due to a new error that started to happen once I started loading the filter mappers from .ts files. If any one has suggestions about this error, I'd love to hear!
TypeError: Cannot redefine property: prototype
  • Full jest test coverage for FilterManager and FilterStateManager was added, and older query_filter tests were updated.

Checklist

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

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@lizozom
Copy link
Contributor Author

lizozom commented Jun 27, 2019

retest

@lizozom
Copy link
Contributor Author

lizozom commented Jun 27, 2019

retest

@elasticmachine

This comment has been minimized.

@elasticmachine
Copy link
Contributor

💔 Build Failed

@lizozom
Copy link
Contributor Author

lizozom commented Jun 27, 2019

retest

Copy link
Contributor

@stacey-gammon stacey-gammon left a comment

Choose a reason for hiding this comment

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

Looks good! I pulled down and tested quite a bit, using input controls with pinned filters by default and not pinned filters, a combination of unpinned and pinned filters created by visualizations and also by filtering on maps and saved searches (found an existing bug - #39802).

All seems to be working as expected! All my comments are minor.

this.globalState = globalState;
this.filterManager = filterManager;

this.watchFilterState();
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about requiring users to directly call watchFilterState? Just thinking that it might be clearer to keep the side affects out of the constructor? I had a similar comment on a recent PR of mine, in DashboardStateManager, that the constructor has side affects. It did read a little funny in the test file that you need to instantiate the class, but then do nothing with it.

}
);

this.filters = newFilters;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe move inside the if statement since if they aren't updated, no reason to set them?

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@lizozom
Copy link
Contributor Author

lizozom commented Jun 30, 2019

retest

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Liza Katz added 2 commits June 30, 2019 13:04
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@lizozom lizozom added release_note:enhancement and removed release_note:skip Skip the PR/issue when compiling release notes labels Jul 1, 2019
Copy link
Contributor

@stacey-gammon stacey-gammon left a comment

Choose a reason for hiding this comment

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

Here's my official LGTM! ... though I did not pull down and test the latest minor changes.

@lizozom lizozom merged commit 531e2b0 into elastic:master Jul 1, 2019
lizozom pushed a commit to lizozom/kibana that referenced this pull request Jul 1, 2019
…lastic#37311)

* Embeddable and Visualize to use type definitions from kbn-es-query package

* moved state watching to new filter manager and removed query filter dependency on rootScope

* Deleted unused pinFilter

* Extracted merge fitlers function

* Changed mappedFilters name

* Move state management to new filter management as well. query filter is now just a wrapper.

* Code works correctly with new setup of query filter

* improved typing

* Moved mapping into new filter manager

* removed promise dependency from query filter

* Extracted filter state manager from query filter

* Moved addFiltersAndChangeTimeFilter logic into new filter manager

* Fixed addFiltersAndChangeTimeFilter implementation

* Moved all logic to new filter manager - query filter is just a wrapper now!

* Connected query_filter observable management to new filter manager as well

* Usage example in discover

* filter state management tests + fire less events from filter state manager

* Tests for filter manager and filter state manager

* Moved new filter manager to data plugin

* Got rid of FilterManagerProvider and turned it into a getFilterGenerator function that works with the new FilterManager.
Need to merge this code with FilterManager.

* Added tests that make sure that filter manager listens to filter state manager

* fix typo

* Fixed action add filter test

* Fixed increasePredecessorCount test

* Fixed increaseSuccessorCount test

* Fixed setPredecessorCount test

* Fixed setQueryParameters test

* Fixed setSuccessorCount test

* Fixed doc table filter actions test

* Fixed filter generator tests

* rename argument

* Moved push filters into vis filters (only place where its used)

* Filter type fix, test fix

* Removed irrelevant filter tests (for deleted methods)
    Added state to filters

* Fixed most of get filters errors by sleeping (TODO!)

* Added destroy methods
Improved merge logic
Improved remove filter tests to use add filter (to avoid having for filter state manager to catch up)

* Fixed discover functional tests
Added default empty filter state in filter generator
Some more browser test adjustments

* Allow filter $state to be empty

* Fixed types

* Code review with @splager
- subscribeWithScope
- query filter to return angular promises
- remove unneeded Promise.resolve

* Separate class\type defenitions from plugin instance setup in shim plugin definition
This helps avoiding circular dependency issues that were obsereved in filter-manager branch (due to code starting to use the data plugin).

* typescript fun

* Fixed merge

* updated some get filter tests to work with add filter, and therefore moved them to add_filters.js

* Remove shouldFetch and add comment explaning the observable.

* Minor code style fixes

* Code review fixes and changes

* Moved Karma tests to jest

* Fixed merge

* Fixed some type errors

* Improved watchFilterState logic

* Removed addFiltersAndChangeTimeFilter test for now, as I'm having TS difficulties

* filters can also be null

* Further watch state fine tuning

* Get data instance inside provider to avoid circular deps

* mock chrome

* Removed change to range filter

* Deleted unnecessary injects

* dedup setFilters as well

* deleted unused subscription

* Added tests for setFilters de-dupe

* Update src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.ts

Co-Authored-By: Stacey Gammon <[email protected]>

* Update src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts

Co-Authored-By: Stacey Gammon <[email protected]>

* Code review minor fixes
lizozom pushed a commit that referenced this pull request Jul 1, 2019
…37311) (#40012)

* Embeddable and Visualize to use type definitions from kbn-es-query package

* moved state watching to new filter manager and removed query filter dependency on rootScope

* Deleted unused pinFilter

* Extracted merge fitlers function

* Changed mappedFilters name

* Move state management to new filter management as well. query filter is now just a wrapper.

* Code works correctly with new setup of query filter

* improved typing

* Moved mapping into new filter manager

* removed promise dependency from query filter

* Extracted filter state manager from query filter

* Moved addFiltersAndChangeTimeFilter logic into new filter manager

* Fixed addFiltersAndChangeTimeFilter implementation

* Moved all logic to new filter manager - query filter is just a wrapper now!

* Connected query_filter observable management to new filter manager as well

* Usage example in discover

* filter state management tests + fire less events from filter state manager

* Tests for filter manager and filter state manager

* Moved new filter manager to data plugin

* Got rid of FilterManagerProvider and turned it into a getFilterGenerator function that works with the new FilterManager.
Need to merge this code with FilterManager.

* Added tests that make sure that filter manager listens to filter state manager

* fix typo

* Fixed action add filter test

* Fixed increasePredecessorCount test

* Fixed increaseSuccessorCount test

* Fixed setPredecessorCount test

* Fixed setQueryParameters test

* Fixed setSuccessorCount test

* Fixed doc table filter actions test

* Fixed filter generator tests

* rename argument

* Moved push filters into vis filters (only place where its used)

* Filter type fix, test fix

* Removed irrelevant filter tests (for deleted methods)
    Added state to filters

* Fixed most of get filters errors by sleeping (TODO!)

* Added destroy methods
Improved merge logic
Improved remove filter tests to use add filter (to avoid having for filter state manager to catch up)

* Fixed discover functional tests
Added default empty filter state in filter generator
Some more browser test adjustments

* Allow filter $state to be empty

* Fixed types

* Code review with @splager
- subscribeWithScope
- query filter to return angular promises
- remove unneeded Promise.resolve

* Separate class\type defenitions from plugin instance setup in shim plugin definition
This helps avoiding circular dependency issues that were obsereved in filter-manager branch (due to code starting to use the data plugin).

* typescript fun

* Fixed merge

* updated some get filter tests to work with add filter, and therefore moved them to add_filters.js

* Remove shouldFetch and add comment explaning the observable.

* Minor code style fixes

* Code review fixes and changes

* Moved Karma tests to jest

* Fixed merge

* Fixed some type errors

* Improved watchFilterState logic

* Removed addFiltersAndChangeTimeFilter test for now, as I'm having TS difficulties

* filters can also be null

* Further watch state fine tuning

* Get data instance inside provider to avoid circular deps

* mock chrome

* Removed change to range filter

* Deleted unnecessary injects

* dedup setFilters as well

* deleted unused subscription

* Added tests for setFilters de-dupe

* Update src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.ts

Co-Authored-By: Stacey Gammon <[email protected]>

* Update src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts

Co-Authored-By: Stacey Gammon <[email protected]>

* Code review minor fixes
spalger pushed a commit that referenced this pull request Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants