-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Dashboard] fix searchSessionId not updated when pinned filter changes #151390
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97dbb7f
[Dashboard] fix searchSessionId not updated when pinned filter changes
nreese 7abb62a
do not refresh on pin/unpin action
nreese 0f1a68b
tslint
nreese 1be70cc
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine 0a51080
tslint
nreese b983a17
Merge branch 'main' into issue_151219
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...hboard_container/embeddable/integrations/diff_state/dashboard_diffing_integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { buildExistsFilter, disableFilter, pinFilter, toggleFilterNegated } from '@kbn/es-query'; | ||
import type { DataViewFieldBase, DataViewBase } from '@kbn/es-query'; | ||
import { getShouldRefresh } from './dashboard_diffing_integration'; | ||
import { DashboardContainer } from '../../dashboard_container'; | ||
import { DashboardContainerByValueInput } from '../../../../../common'; | ||
|
||
describe('getShouldRefresh', () => { | ||
const dashboardContainerMock = { | ||
untilInitialized: () => {}, | ||
} as unknown as DashboardContainer; | ||
|
||
const existsFilter = buildExistsFilter( | ||
{ | ||
name: 'myFieldName', | ||
} as DataViewFieldBase, | ||
{ | ||
id: 'myDataViewId', | ||
} as DataViewBase | ||
); | ||
|
||
test('should return true when pinned filters change', async () => { | ||
const pinnedFilter = pinFilter(existsFilter); | ||
const lastInput = { | ||
filters: [pinnedFilter], | ||
} as unknown as DashboardContainerByValueInput; | ||
const input = { | ||
filters: [toggleFilterNegated(pinnedFilter)], | ||
} as unknown as DashboardContainerByValueInput; | ||
expect(await getShouldRefresh.bind(dashboardContainerMock)(lastInput, { ...lastInput })).toBe( | ||
false | ||
); | ||
expect(await getShouldRefresh.bind(dashboardContainerMock)(lastInput, input)).toBe(true); | ||
}); | ||
|
||
test('should return false when disabled filters change', async () => { | ||
const disabledFilter = disableFilter(existsFilter); | ||
const lastInput = { | ||
filters: [disabledFilter], | ||
} as unknown as DashboardContainerByValueInput; | ||
const input = { | ||
filters: [toggleFilterNegated(disabledFilter)], | ||
} as unknown as DashboardContainerByValueInput; | ||
expect(await getShouldRefresh.bind(dashboardContainerMock)(lastInput, input)).toBe(false); | ||
}); | ||
|
||
test('should return false when pinned filter changes to unpinned', async () => { | ||
const lastInput = { | ||
filters: [existsFilter], | ||
} as unknown as DashboardContainerByValueInput; | ||
const input = { | ||
filters: [pinFilter(existsFilter)], | ||
} as unknown as DashboardContainerByValueInput; | ||
expect(await getShouldRefresh.bind(dashboardContainerMock)(lastInput, input)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,12 @@ import { DashboardContainer } from '../../dashboard_container'; | |
import { pluginServices } from '../../../../services/plugin_services'; | ||
import { DashboardContainerByValueInput } from '../../../../../common'; | ||
import { CHANGE_CHECK_DEBOUNCE } from '../../../../dashboard_constants'; | ||
import { isKeyEqual } from './dashboard_diffing_functions'; | ||
import type { DashboardDiffFunctions } from './dashboard_diffing_functions'; | ||
import { | ||
isKeyEqual, | ||
shouldRefreshDiffingFunctions, | ||
unsavedChangesDiffingFunctions, | ||
} from './dashboard_diffing_functions'; | ||
import { dashboardContainerReducers } from '../../../state/dashboard_container_reducers'; | ||
|
||
/** | ||
|
@@ -54,6 +59,23 @@ export const keysNotConsideredUnsavedChanges: Array<keyof DashboardContainerByVa | |
'viewMode', | ||
]; | ||
|
||
/** | ||
* input keys that will cause a new session to be created. | ||
*/ | ||
const refetchKeys: Array<keyof DashboardContainerByValueInput> = [ | ||
'query', | ||
'filters', | ||
'timeRange', | ||
'timeslice', | ||
'timeRestore', | ||
'lastReloadRequestTime', | ||
|
||
// also refetch when chart settings change | ||
'syncColors', | ||
'syncCursor', | ||
'syncTooltips', | ||
]; | ||
|
||
/** | ||
* Does an initial diff between @param initialInput and @param initialLastSavedInput, and created a middleware | ||
* which listens to the redux store and checks for & publishes the unsaved changes on dispatches. | ||
|
@@ -139,44 +161,67 @@ function backupUnsavedChanges( | |
} | ||
|
||
/** | ||
* Does a shallow diff between @param lastExplicitInput and @param currentExplicitInput and | ||
* Does a shallow diff between @param lastInput and @param input and | ||
* @returns an object out of the keys which are different. | ||
*/ | ||
export async function getUnsavedChanges( | ||
this: DashboardContainer, | ||
lastInput: DashboardContainerByValueInput, | ||
input: DashboardContainerByValueInput | ||
): Promise<Partial<DashboardContainerByValueInput>> { | ||
const allKeys = [...new Set([...Object.keys(lastInput), ...Object.keys(input)])] as Array< | ||
keyof DashboardContainerByValueInput | ||
>; | ||
return await getInputChanges(this, lastInput, input, allKeys, unsavedChangesDiffingFunctions); | ||
} | ||
|
||
export async function getShouldRefresh( | ||
this: DashboardContainer, | ||
lastInput: DashboardContainerByValueInput, | ||
input: DashboardContainerByValueInput | ||
): Promise<boolean> { | ||
const inputChanges = await getInputChanges( | ||
this, | ||
lastInput, | ||
input, | ||
refetchKeys, | ||
shouldRefreshDiffingFunctions | ||
); | ||
return Object.keys(inputChanges).length > 0; | ||
} | ||
|
||
async function getInputChanges( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call moving this to a shared function. |
||
container: DashboardContainer, | ||
lastInput: DashboardContainerByValueInput, | ||
input: DashboardContainerByValueInput, | ||
keys?: Array<keyof DashboardContainerByValueInput> | ||
keys: Array<keyof DashboardContainerByValueInput>, | ||
diffingFunctions: DashboardDiffFunctions | ||
): Promise<Partial<DashboardContainerByValueInput>> { | ||
await this.untilInitialized(); | ||
const allKeys = | ||
keys ?? | ||
([...new Set([...Object.keys(lastInput), ...Object.keys(input)])] as Array< | ||
keyof DashboardContainerByValueInput | ||
>); | ||
const keyComparePromises = allKeys.map( | ||
await container.untilInitialized(); | ||
const keyComparePromises = keys.map( | ||
(key) => | ||
new Promise<{ key: keyof DashboardContainerByValueInput; isEqual: boolean }>((resolve) => | ||
isKeyEqual(key, { | ||
container: this, | ||
|
||
currentValue: input[key], | ||
currentInput: input, | ||
|
||
lastValue: lastInput[key], | ||
lastInput, | ||
}).then((isEqual) => resolve({ key, isEqual })) | ||
isKeyEqual( | ||
key, | ||
{ | ||
container, | ||
|
||
currentValue: input[key], | ||
currentInput: input, | ||
|
||
lastValue: lastInput[key], | ||
lastInput, | ||
}, | ||
diffingFunctions | ||
).then((isEqual) => resolve({ key, isEqual })) | ||
) | ||
); | ||
const unsavedChanges = (await Promise.allSettled(keyComparePromises)).reduce( | ||
(changes, current) => { | ||
if (current.status === 'fulfilled') { | ||
const { key, isEqual } = current.value; | ||
if (!isEqual) (changes as { [key: string]: unknown })[key] = input[key]; | ||
} | ||
return changes; | ||
}, | ||
{} as Partial<DashboardContainerByValueInput> | ||
); | ||
return unsavedChanges; | ||
const inputChanges = (await Promise.allSettled(keyComparePromises)).reduce((changes, current) => { | ||
if (current.status === 'fulfilled') { | ||
const { key, isEqual } = current.value; | ||
if (!isEqual) (changes as { [key: string]: unknown })[key] = input[key]; | ||
} | ||
return changes; | ||
}, {} as Partial<DashboardContainerByValueInput>); | ||
return inputChanges; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test additions!