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

[Drilldowns] Fix missing $store in filter state syncing edge case #61261

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,97 @@ describe('connect_to_app_state', () => {
});
});
});

describe('filters with different state', () => {
let queryServiceStart: QueryStart;
let filterManager: FilterManager;
let state: BaseStateContainer<QueryState>;
let stateSub: Subscription;
let stateChangeTriggered = jest.fn();
let filterManagerChangeSub: Subscription;
let filterManagerChangeTriggered = jest.fn();

let filter: Filter;

beforeEach(() => {
const queryService = new QueryService();
queryService.setup({
uiSettings: setupMock.uiSettings,
storage: new Storage(new StubBrowserStorage()),
});
queryServiceStart = queryService.start(startMock.savedObjects);
filterManager = queryServiceStart.filterManager;

state = createStateContainer({});
stateChangeTriggered = jest.fn();
stateSub = state.state$.subscribe(stateChangeTriggered);

filterManagerChangeTriggered = jest.fn();
filterManagerChangeSub = filterManager.getUpdates$().subscribe(filterManagerChangeTriggered);

filter = getFilter(FilterStateStore.GLOBAL_STATE, true, true, 'key1', 'value1');
});

// applies filter state changes, changes only internal $state.store value
function runChanges() {
filter = { ...filter, $state: { store: FilterStateStore.GLOBAL_STATE } };

state.set({
filters: [filter],
});

filter = { ...filter, $state: { store: FilterStateStore.APP_STATE } };

state.set({
filters: [filter],
});

filter = { ...filter };
delete filter.$state;

state.set({
filters: [filter],
});
}

test('when syncing all filters, changes to filter.state$ should be taken into account', () => {
const stop = connectToQueryState(queryServiceStart, state, {
filters: true,
});

runChanges();

expect(filterManagerChangeTriggered).toBeCalledTimes(3);

stop();
});

test('when syncing app state filters, changes to filter.state$ should be ignored', () => {
const stop = connectToQueryState(queryServiceStart, state, {
filters: FilterStateStore.APP_STATE,
});

runChanges();

expect(filterManagerChangeTriggered).toBeCalledTimes(1);

stop();
});

test('when syncing global state filters, changes to filter.state$ should be ignored', () => {
const stop = connectToQueryState(queryServiceStart, state, {
filters: FilterStateStore.GLOBAL_STATE,
});

runChanges();

expect(filterManagerChangeTriggered).toBeCalledTimes(1);

stop();
});

afterEach(() => {
stateSub.unsubscribe();
filterManagerChangeSub.unsubscribe();
});
});
24 changes: 20 additions & 4 deletions src/plugins/data/public/query/state_sync/connect_to_query_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,21 @@ export const connectToQueryState = <S extends QueryState>(
} else if (syncConfig.filters === FilterStateStore.GLOBAL_STATE) {
if (
!initialState.filters ||
!compareFilters(initialState.filters, filterManager.getGlobalFilters(), COMPARE_ALL_OPTIONS)
!compareFilters(initialState.filters, filterManager.getGlobalFilters(), {
Copy link
Contributor

@lizozom lizozom Mar 29, 2020

Choose a reason for hiding this comment

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

The fix does work as expected.

However, as far as I understand, the problem with the filters coming in from initialState.filters.
I think it would be more correct, if we updated any filter without a store value to FilterStateStore.APP_STATE.
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since $store was recently added and is optional would be hard to find all places where filters could come with missing $store. e.g., (link generations with missing $store in filters).

Also there are old kibana versions urls which are out of our control and old saved objects where filters are missing $store.

Apps work with missing $store, which is great. But would be also nice to fix this possible edge case with url syncing.

...COMPARE_ALL_OPTIONS,
state: false,
})
) {
initialState.filters = filterManager.getGlobalFilters();
initialDirty = true;
}
} else if (syncConfig.filters === FilterStateStore.APP_STATE) {
if (
!initialState.filters ||
!compareFilters(initialState.filters, filterManager.getAppFilters(), COMPARE_ALL_OPTIONS)
!compareFilters(initialState.filters, filterManager.getAppFilters(), {
...COMPARE_ALL_OPTIONS,
state: false,
})
) {
initialState.filters = filterManager.getAppFilters();
initialDirty = true;
Expand Down Expand Up @@ -173,11 +179,21 @@ export const connectToQueryState = <S extends QueryState>(
filterManager.setFilters(_.cloneDeep(filters));
}
} else if (syncConfig.filters === FilterStateStore.APP_STATE) {
if (!compareFilters(filters, filterManager.getAppFilters(), COMPARE_ALL_OPTIONS)) {
if (
!compareFilters(filters, filterManager.getAppFilters(), {
...COMPARE_ALL_OPTIONS,
state: false,
})
) {
filterManager.setAppFilters(_.cloneDeep(filters));
}
} else if (syncConfig.filters === FilterStateStore.GLOBAL_STATE) {
if (!compareFilters(filters, filterManager.getGlobalFilters(), COMPARE_ALL_OPTIONS)) {
if (
!compareFilters(filters, filterManager.getGlobalFilters(), {
...COMPARE_ALL_OPTIONS,
state: false,
})
) {
filterManager.setGlobalFilters(_.cloneDeep(filters));
}
}
Expand Down