diff --git a/changelogs/fragments/8555.yml b/changelogs/fragments/8555.yml new file mode 100644 index 000000000000..8314c88c1872 --- /dev/null +++ b/changelogs/fragments/8555.yml @@ -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)) \ No newline at end of file diff --git a/src/plugins/data/common/utils/helpers.ts b/src/plugins/data/common/utils/helpers.ts index c3fc8112ceec..2d1ab2ab1417 100644 --- a/src/plugins/data/common/utils/helpers.ts +++ b/src/plugins/data/common/utils/helpers.ts @@ -28,13 +28,8 @@ * 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; @@ -42,29 +37,28 @@ export interface QueryStatusOptions { interval?: number; } -export const handleQueryResults = ( +export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms)); + +export const handleQueryResults = async ( options: QueryStatusOptions ): Promise => { - const { pollQueryResults, interval = 5000, queryId } = options; + const { pollQueryResults, interval = 5000 } = options; + let queryResultsRes: FetchStatusResponse; + let queryStatus; + do { + // Wait for the given interval in ms before polling for the query status/results + await delay(interval); + queryResultsRes = await pollQueryResults(); + queryStatus = queryResultsRes?.status?.toUpperCase(); + } while (queryStatus !== 'SUCCESS' && queryStatus !== 'FAILED'); + + if (queryStatus === 'FAILED') { + throw new Error( + 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; };