From dbf7ebb92359701e40b85c272070f57624926587 Mon Sep 17 00:00:00 2001 From: Matias Chomicki Date: Thu, 22 Jun 2023 12:44:02 +0200 Subject: [PATCH] Elasticsearch: Fix infinite loop when using mixed datasources (#70386) * Elasticsearch context: do not init query when it is already initialized * Fix buggy conditional --- .../QueryEditor/BucketAggregationsEditor/state/reducer.ts | 3 +-- .../components/QueryEditor/ElasticsearchQueryContext.tsx | 4 ++-- .../QueryEditor/MetricAggregationsEditor/state/reducer.ts | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts index d289f7dc32f..935f7a10e07 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts @@ -107,10 +107,9 @@ export const createReducer = } if (initQuery.match(action)) { - if (state?.length || 0 > 0) { + if (state && state.length > 0) { return state; } - return [{ ...defaultBucketAgg('2'), field: defaultTimeField }]; } diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx index 9e25bb96620..f91fe3d209d 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx @@ -59,11 +59,11 @@ export const ElasticsearchProvider = ({ // This initializes the query by dispatching an init action to each reducer. // useStatelessReducer will then call `onChange` with the newly generated query useEffect(() => { - if (shouldRunInit) { + if (shouldRunInit && isUninitialized) { dispatch(initQuery()); setShouldRunInit(false); } - }, [shouldRunInit, dispatch]); + }, [shouldRunInit, dispatch, isUninitialized]); if (isUninitialized) { return null; diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts index e3da5ccd2e6..a1b8fcb860a 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts @@ -155,7 +155,7 @@ export const reducer = (state: ElasticsearchQuery['metrics'], action: Action): E } if (initQuery.match(action)) { - if (state?.length || 0 > 0) { + if (state && state.length > 0) { return state; } return [defaultMetricAgg('1')];