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

[Async search Discover] Poll for results when current request completes #8555

Merged
merged 4 commits into from
Oct 18, 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/8555.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Refactored polling logic to poll for results once current request completes ([#8555](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8555))
52 changes: 23 additions & 29 deletions src/plugins/data/common/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,37 @@
* under the License.
*/

import { timer } from 'rxjs';
import { filter, mergeMap, take, takeWhile } from 'rxjs/operators';
import {
PollQueryResultsHandler,
FetchStatusResponse,
QueryFailedStatusResponse,
} from '../data_frames';
import { i18n } from '@osd/i18n';
import { PollQueryResultsHandler, FetchStatusResponse } from '../data_frames';

export interface QueryStatusOptions {
pollQueryResults: PollQueryResultsHandler;
queryId?: string;
interval?: number;
}

export const handleQueryResults = <T>(
export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));

export const handleQueryResults = async <T>(
options: QueryStatusOptions
): Promise<FetchStatusResponse> => {
const { pollQueryResults, interval = 5000, queryId } = options;
const { pollQueryResults, interval = 5000 } = options;
let queryResultsRes: FetchStatusResponse;
let queryStatus;
do {

Check warning on line 48 in src/plugins/data/common/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/utils/helpers.ts#L48

Added line #L48 was not covered by tests
// Wait for the given interval in ms before polling for the query status/results
await delay(interval);
queryResultsRes = await pollQueryResults();
queryStatus = queryResultsRes?.status?.toUpperCase();

Check warning on line 52 in src/plugins/data/common/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/utils/helpers.ts#L50-L52

Added lines #L50 - L52 were not covered by tests
} while (queryStatus !== 'SUCCESS' && queryStatus !== 'FAILED');

if (queryStatus === 'FAILED') {
throw new Error(

Check warning on line 56 in src/plugins/data/common/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/utils/helpers.ts#L56

Added line #L56 was not covered by tests
i18n.translate('data.search.request.failed', {
defaultMessage: 'An error occurred while executing the search query',
})
);
}

return timer(0, interval)
.pipe(
mergeMap(() => pollQueryResults()),
takeWhile((response: FetchStatusResponse) => {
const status = response?.status?.toUpperCase();
return status !== 'SUCCESS' && status !== 'FAILED';
}, true),
filter((response: FetchStatusResponse) => {
const status = response?.status?.toUpperCase();
if (status === 'FAILED') {
throw (
(response as QueryFailedStatusResponse).body.error ??
new Error(`Failed to fetch results ${queryId ?? ''}`)
);
}
return status === 'SUCCESS';
}),
take(1)
)
.toPromise();
return queryResultsRes;

Check warning on line 63 in src/plugins/data/common/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/utils/helpers.ts#L63

Added line #L63 was not covered by tests
};
Loading