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

[Backport 2.x] [Discover] Dataset search on page load issues #8877

Merged
merged 1 commit into from
Nov 15, 2024
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
2 changes: 2 additions & 0 deletions changelogs/fragments/8871.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Search on page load out of sync state when clicking submit. ([#8871](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8871))
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,48 @@ describe('useSearch', () => {
});
});

it('should initialize with uninitialized state when dataset type config search on page load is disabled', async () => {
const services = createMockServices();
(services.uiSettings.get as jest.Mock).mockReturnValueOnce(true);
(services.data.query.queryString.getDatasetService as jest.Mock).mockReturnValue({
meta: { searchOnLoad: false },
});
(services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({
pause: true,
value: 10,
});

const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper });
expect(result.current.data$.getValue()).toEqual(
expect.objectContaining({ status: ResultStatus.UNINITIALIZED })
);

await act(async () => {
await waitForNextUpdate();
});
});

it('should initialize with uninitialized state when dataset type config search on page load is enabled but the UI setting is disabled', async () => {
const services = createMockServices();
(services.uiSettings.get as jest.Mock).mockReturnValueOnce(false);
(services.data.query.queryString.getDatasetService as jest.Mock).mockReturnValue({
meta: { searchOnLoad: true },
});
(services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({
pause: true,
value: 10,
});

const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper });
expect(result.current.data$.getValue()).toEqual(
expect.objectContaining({ status: ResultStatus.UNINITIALIZED })
);

await act(async () => {
await waitForNextUpdate();
});
});

it('should update startTime when hook rerenders', async () => {
const services = createMockServices();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,23 @@ export const useSearch = (services: DiscoverViewServices) => {
requests: new RequestAdapter(),
};

const getDatasetAutoSearchOnPageLoadPreference = () => {
// Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to true
const datasetType = data.query.queryString.getQuery().dataset?.type;

const datasetService = data.query.queryString.getDatasetService();

return !datasetType || (datasetService?.getType(datasetType)?.meta?.searchOnLoad ?? true);
};

const shouldSearchOnPageLoad = useCallback(() => {
// Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to UI Settings
const { queryString } = data.query;
const { dataset } = queryString.getQuery();
const typeConfig = dataset ? queryString.getDatasetService().getType(dataset.type) : undefined;
const datasetPreference =
typeConfig?.meta?.searchOnLoad ?? uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING);

// A saved search is created on every page load, so we check the ID to see if we're loading a
// previously saved search or if it is just transient
return (
services.uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) ||
datasetPreference ||
uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) ||
savedSearch?.id !== undefined ||
timefilter.getRefreshInterval().pause === false
);
}, [savedSearch, services.uiSettings, timefilter]);
}, [data.query, savedSearch, uiSettings, timefilter]);

const startTime = Date.now();
const data$ = useMemo(
Expand Down Expand Up @@ -346,9 +345,6 @@ export const useSearch = (services: DiscoverViewServices) => {
]);

useEffect(() => {
if (!getDatasetAutoSearchOnPageLoadPreference()) {
skipInitialFetch.current = true;
}
const fetch$ = merge(
refetch$,
filterManager.getFetches$(),
Expand Down Expand Up @@ -379,8 +375,6 @@ export const useSearch = (services: DiscoverViewServices) => {
return () => {
subscription.unsubscribe();
};
// disabling the eslint since we are not adding getDatasetAutoSearchOnPageLoadPreference since this changes when dataset changes and these chnages are already part of data.query.queryString
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
data$,
data.query.queryString,
Expand Down
Loading