From 5eafb7b8144bcb82329d7be08bd6dff370bd47eb Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Thu, 10 Oct 2024 18:56:31 -0700 Subject: [PATCH 1/4] poll for results when current request completes Signed-off-by: Amardeepsingh Siglani --- src/plugins/data/common/utils/helpers.ts | 42 +++++++++++------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/plugins/data/common/utils/helpers.ts b/src/plugins/data/common/utils/helpers.ts index c3fc8112ceec..20451757430e 100644 --- a/src/plugins/data/common/utils/helpers.ts +++ b/src/plugins/data/common/utils/helpers.ts @@ -28,8 +28,6 @@ * under the License. */ -import { timer } from 'rxjs'; -import { filter, mergeMap, take, takeWhile } from 'rxjs/operators'; import { PollQueryResultsHandler, FetchStatusResponse, @@ -42,29 +40,27 @@ 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; + 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 ( + (queryResultsRes as QueryFailedStatusResponse).body.error ?? + new Error(`Failed to fetch results ${queryId ?? ''}`) + ); + } - 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; }; From dfa18e5388f8ca28537344e8b5f25d49e8e958be Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 02:00:00 +0000 Subject: [PATCH 2/4] Changeset file for PR #8555 created/updated --- changelogs/fragments/8555.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/8555.yml 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 From 21e7f86e20b33ea57b65c0f6a1a73a78e007fe5f Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Fri, 11 Oct 2024 12:01:50 -0700 Subject: [PATCH 3/4] added localization to error string Signed-off-by: Amardeepsingh Siglani --- src/plugins/data/common/utils/helpers.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugins/data/common/utils/helpers.ts b/src/plugins/data/common/utils/helpers.ts index 20451757430e..2a4fefb59944 100644 --- a/src/plugins/data/common/utils/helpers.ts +++ b/src/plugins/data/common/utils/helpers.ts @@ -28,6 +28,7 @@ * under the License. */ +import { i18n } from '@osd/i18n'; import { PollQueryResultsHandler, FetchStatusResponse, @@ -58,7 +59,12 @@ export const handleQueryResults = async ( if (queryStatus === 'FAILED') { throw ( (queryResultsRes as QueryFailedStatusResponse).body.error ?? - new Error(`Failed to fetch results ${queryId ?? ''}`) + new Error( + i18n.translate('data.search.request.failed', { + defaultMessage: 'Failed to fetch results for queryId: {queryId}', + values: { queryId: queryId ?? '' }, + }) + ) ); } From 15ebeef1cb2c064cb8c77237aa41b42bc1d23f27 Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Fri, 11 Oct 2024 13:15:54 -0700 Subject: [PATCH 4/4] simplified error message for user Signed-off-by: Amardeepsingh Siglani --- src/plugins/data/common/utils/helpers.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/plugins/data/common/utils/helpers.ts b/src/plugins/data/common/utils/helpers.ts index 2a4fefb59944..2d1ab2ab1417 100644 --- a/src/plugins/data/common/utils/helpers.ts +++ b/src/plugins/data/common/utils/helpers.ts @@ -29,11 +29,7 @@ */ import { i18n } from '@osd/i18n'; -import { - PollQueryResultsHandler, - FetchStatusResponse, - QueryFailedStatusResponse, -} from '../data_frames'; +import { PollQueryResultsHandler, FetchStatusResponse } from '../data_frames'; export interface QueryStatusOptions { pollQueryResults: PollQueryResultsHandler; @@ -46,7 +42,7 @@ 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 { @@ -57,14 +53,10 @@ export const handleQueryResults = async ( } while (queryStatus !== 'SUCCESS' && queryStatus !== 'FAILED'); if (queryStatus === 'FAILED') { - throw ( - (queryResultsRes as QueryFailedStatusResponse).body.error ?? - new Error( - i18n.translate('data.search.request.failed', { - defaultMessage: 'Failed to fetch results for queryId: {queryId}', - values: { queryId: queryId ?? '' }, - }) - ) + throw new Error( + i18n.translate('data.search.request.failed', { + defaultMessage: 'An error occurred while executing the search query', + }) ); }