Skip to content

Commit

Permalink
[Async search Discover] Poll for results when current request complet…
Browse files Browse the repository at this point in the history
…es (opensearch-project#8555)

* poll for results when current request completes

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* Changeset file for PR opensearch-project#8555 created/updated

* added localization to error string

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* simplified error message for user

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

---------

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
amsiglan and opensearch-changeset-bot[bot] committed Oct 19, 2024
1 parent c9068a7 commit 8469cdf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
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 {
// 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;
};

0 comments on commit 8469cdf

Please sign in to comment.