From a7414f07c987d64d50fe1d72b08eadb033381dae Mon Sep 17 00:00:00 2001 From: "Qingyang(Abby) Hu" Date: Sat, 19 Oct 2024 11:28:30 -0700 Subject: [PATCH] Fix random big number during loading in query editor result (#8650) * Fix random big number during loading in query editor result Signed-off-by: abbyhu2000 * Changeset file for PR #8650 created/updated * Fix initial loading number Signed-off-by: abbyhu2000 --------- Signed-off-by: abbyhu2000 Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/8650.yml | 2 + .../language_service/lib/query_result.tsx | 62 ++++++++++--------- 2 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 changelogs/fragments/8650.yml diff --git a/changelogs/fragments/8650.yml b/changelogs/fragments/8650.yml new file mode 100644 index 000000000000..3f53ba7a41fa --- /dev/null +++ b/changelogs/fragments/8650.yml @@ -0,0 +1,2 @@ +fix: +- Fix random big number when loading in query result ([#8650](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8650)) \ No newline at end of file diff --git a/src/plugins/data/public/query/query_string/language_service/lib/query_result.tsx b/src/plugins/data/public/query/query_string/language_service/lib/query_result.tsx index 042d2e11a664..074ba4b5d0d5 100644 --- a/src/plugins/data/public/query/query_string/language_service/lib/query_result.tsx +++ b/src/plugins/data/public/query/query_string/language_service/lib/query_result.tsx @@ -41,40 +41,42 @@ export function QueryResult(props: { queryStatus: QueryStatus }) { setPopover(!isPopoverOpen); }; - const updateElapsedTime = () => { - const time = Date.now() - (props.queryStatus.startTime || 0); - if (time > BUFFER_TIME) { - setElapsedTime(time); - } else { - setElapsedTime(0); - } - }; - useEffect(() => { + const updateElapsedTime = () => { + const currentTime = Date.now(); + if (!props.queryStatus.startTime) { + return; + } + const elapsed = currentTime - props.queryStatus.startTime; + setElapsedTime(elapsed); + }; + const interval = setInterval(updateElapsedTime, 1000); - return () => clearInterval(interval); - }); + return () => { + clearInterval(interval); + setElapsedTime(0); + }; + }, [props.queryStatus.startTime]); - if (props.queryStatus.status === ResultStatus.LOADING) { - if (elapsedTime < BUFFER_TIME) { - return null; + if (elapsedTime > BUFFER_TIME) { + if (props.queryStatus.status === ResultStatus.LOADING) { + const time = Math.floor(elapsedTime / 1000); + return ( + {}} + isLoading + data-test-subj="queryResultLoading" + > + {i18n.translate('data.query.languageService.queryResults.loadTime', { + defaultMessage: 'Loading {time} s', + values: { time }, + })} + + ); } - const time = Math.floor(elapsedTime / 1000); - return ( - {}} - isLoading - data-test-subj="queryResultLoading" - > - {i18n.translate('data.query.languageService.queryResults.loadTime', { - defaultMessage: 'Loading {time} s', - values: { time }, - })} - - ); } if (props.queryStatus.status === ResultStatus.READY) { @@ -85,7 +87,7 @@ export function QueryResult(props: { queryStatus: QueryStatus }) { }); } else if (props.queryStatus.elapsedMs < 1000) { message = i18n.translate( - 'data.query.languageService.queryResults.completeTimeInMiliseconds', + 'data.query.languageService.queryResults.completeTimeInMilliseconds', { defaultMessage: 'Completed in {timeMS} ms', values: { timeMS: props.queryStatus.elapsedMs },