-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[discover] fix too many queries sent (#8347)
Fixes the issue where too many queries were being fired. A number of things were being called because the dependency array was triggering updates and re-calls. Forcing a re-render of elements that had the possibility of updating the query manager. Includes some tests. Signed-off-by: Kawika Avilla <[email protected]> --------- Signed-off-by: Kawika Avilla <[email protected]> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d1caa92
commit 0882435
Showing
8 changed files
with
243 additions
and
57 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fix: | ||
- Prevent too many queries sent from dataset selector ([#8347](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8347)) |
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
116 changes: 116 additions & 0 deletions
116
src/plugins/data/public/ui/dataset_selector/dataset_selector.test.tsx
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,116 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { DatasetSelector } from './dataset_selector'; | ||
import { getQueryService } from '../../services'; | ||
import { CoreStart } from 'opensearch-dashboards/public'; | ||
import { DataStorage, Dataset } from '../../../common'; | ||
import { IDataPluginServices, DataPublicPluginStart } from '../../types'; | ||
import { DatasetTypeConfig } from '../..'; | ||
|
||
jest.mock('../../services', () => ({ | ||
getQueryService: jest.fn(), | ||
})); | ||
|
||
describe('DatasetSelector', () => { | ||
const nextTick = () => new Promise((res) => process.nextTick(res)); | ||
|
||
const fetchMock = jest.fn().mockResolvedValue({ children: [] }); | ||
const setDatasetsMock = jest.fn(); | ||
const mockDatasetTypeConfig: DatasetTypeConfig = { | ||
id: 'mockType', | ||
title: 'Mock Type', | ||
meta: { icon: { type: 'mockIcon' }, tooltip: 'Mock Tooltip' }, | ||
fetch: fetchMock, | ||
toDataset: jest.fn(), | ||
fetchFields: jest.fn(), | ||
supportedLanguages: jest.fn().mockReturnValue(['mockLanguage']), | ||
}; | ||
|
||
const getTypeMock = jest.fn().mockReturnValue(mockDatasetTypeConfig); | ||
|
||
const mockServices: IDataPluginServices = { | ||
appName: 'testApp', | ||
uiSettings: {} as CoreStart['uiSettings'], | ||
savedObjects: {} as CoreStart['savedObjects'], | ||
notifications: ({ | ||
toasts: { | ||
addSuccess: jest.fn(), | ||
addError: jest.fn(), | ||
}, | ||
} as unknown) as CoreStart['notifications'], | ||
http: {} as CoreStart['http'], | ||
storage: {} as DataStorage, | ||
data: {} as DataPublicPluginStart, | ||
overlays: ({ | ||
openModal: jest.fn(), | ||
} as unknown) as CoreStart['overlays'], | ||
}; | ||
|
||
beforeEach(() => { | ||
(getQueryService as jest.Mock).mockReturnValue({ | ||
queryString: { | ||
getDatasetService: jest.fn().mockReturnValue({ | ||
getType: getTypeMock, | ||
}), | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should fetch datasets once on mount', async () => { | ||
const props = { | ||
selectedDataset: undefined as Dataset | undefined, | ||
setSelectedDataset: jest.fn(), | ||
services: mockServices, | ||
}; | ||
|
||
const wrapper = mount(<DatasetSelector {...props} />); | ||
|
||
await nextTick(); | ||
expect(fetchMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not fetch datasets on re-render', async () => { | ||
const props = { | ||
selectedDataset: undefined as Dataset | undefined, | ||
setSelectedDataset: jest.fn(), | ||
services: mockServices, | ||
}; | ||
|
||
const wrapper = mount(<DatasetSelector {...props} />); | ||
await nextTick(); | ||
|
||
// Simulate a re-render | ||
wrapper.setProps({}); | ||
await nextTick(); | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not update datasets state on re-render', async () => { | ||
const props = { | ||
selectedDataset: undefined as Dataset | undefined, | ||
setSelectedDataset: jest.fn(), | ||
services: mockServices, | ||
}; | ||
|
||
const wrapper = mount(<DatasetSelector {...props} />); | ||
await nextTick(); | ||
|
||
setDatasetsMock.mockClear(); | ||
|
||
// Simulate a re-render | ||
wrapper.setProps({}); | ||
await nextTick(); | ||
|
||
expect(setDatasetsMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
72 changes: 72 additions & 0 deletions
72
src/plugins/data/public/ui/dataset_selector/index.test.tsx
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,72 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { DatasetSelector as ConnectedDatasetSelector } from './index'; | ||
import { DatasetSelector } from './dataset_selector'; | ||
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; | ||
import { Dataset } from '../../../common'; | ||
|
||
jest.mock('../../../../opensearch_dashboards_react/public', () => ({ | ||
useOpenSearchDashboards: jest.fn(), | ||
})); | ||
|
||
jest.mock('./dataset_selector', () => ({ | ||
DatasetSelector: jest.fn(() => null), | ||
})); | ||
|
||
describe('ConnectedDatasetSelector', () => { | ||
const mockQueryString = { | ||
getQuery: jest.fn().mockReturnValue({}), | ||
getDefaultQuery: jest.fn().mockReturnValue({}), | ||
getInitialQueryByDataset: jest.fn().mockReturnValue({}), | ||
setQuery: jest.fn(), | ||
}; | ||
const mockOnSubmit = jest.fn(); | ||
const mockServices = { | ||
data: { | ||
query: { | ||
// @ts-ignore | ||
queryString: mockQueryString, | ||
}, | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
(useOpenSearchDashboards as jest.Mock).mockReturnValue({ services: mockServices }); | ||
}); | ||
|
||
it('should render DatasetSelector with correct props', () => { | ||
const wrapper = mount(<ConnectedDatasetSelector onSubmit={mockOnSubmit} />); | ||
expect(wrapper.find(DatasetSelector).props()).toEqual({ | ||
selectedDataset: undefined, | ||
setSelectedDataset: expect.any(Function), | ||
services: mockServices, | ||
}); | ||
}); | ||
|
||
it('should initialize selectedDataset correctly', () => { | ||
const mockDataset: Dataset = { id: 'initial', title: 'Initial Dataset', type: 'test' }; | ||
mockQueryString.getQuery.mockReturnValueOnce({ dataset: mockDataset }); | ||
|
||
const wrapper = mount(<ConnectedDatasetSelector onSubmit={mockOnSubmit} />); | ||
expect(wrapper.find(DatasetSelector).prop('selectedDataset')).toEqual(mockDataset); | ||
}); | ||
|
||
it('should call handleDatasetChange only once when dataset changes', () => { | ||
const wrapper = mount(<ConnectedDatasetSelector onSubmit={mockOnSubmit} />); | ||
const setSelectedDataset = wrapper.find(DatasetSelector).prop('setSelectedDataset') as ( | ||
dataset?: Dataset | ||
) => void; | ||
|
||
const newDataset: Dataset = { id: 'test', title: 'Test Dataset', type: 'test' }; | ||
setSelectedDataset(newDataset); | ||
|
||
expect(mockQueryString.getInitialQueryByDataset).toHaveBeenCalledTimes(1); | ||
expect(mockQueryString.setQuery).toHaveBeenCalledTimes(1); | ||
expect(mockOnSubmit).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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