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 },