From c98253a1e4e4664d09a191d6b95cf4a93990a22b Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Sun, 3 Dec 2023 21:44:28 -0600 Subject: [PATCH 1/3] Fix problem with sample agg + space in agg accessor --- .../data_drift/use_data_drift_result.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts b/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts index 4588595ffcc4f..8ebe30a007c8c 100644 --- a/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts +++ b/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts @@ -459,10 +459,12 @@ const fetchComparisonDriftedData = async ({ ); const fieldsWithNoOverlap = new Set(); + const rangesAggs = rangesResp.aggregations.sample + ? rangesResp.aggregations.sample + : rangesResp.aggregations; for (const { field } of fields) { - if (rangesResp.aggregations[`${field}_ranges`]) { - const buckets = rangesResp.aggregations[`${field}_ranges`] - .buckets as AggregationsRangeBucketKeys[]; + if (rangesAggs[`${field}_ranges`]) { + const buckets = rangesAggs[`${field}_ranges`].buckets as AggregationsRangeBucketKeys[]; if (buckets) { const totalSumOfAllBuckets = buckets.reduce((acc, bucket) => acc + bucket.doc_count, 0); @@ -475,7 +477,7 @@ const fetchComparisonDriftedData = async ({ if (totalSumOfAllBuckets > 0) { driftedRequestAggs[`${field}_ks_test`] = { bucket_count_ks_test: { - buckets_path: `${field}_ranges>_count`, + buckets_path: `${field}_ranges > _count`, alternative: ['two_sided'], ...(totalSumOfAllBuckets > 0 ? { fractions: fractions.map((bucket) => Number(bucket.fraction.toFixed(3))) } @@ -779,7 +781,7 @@ export const useFetchDataComparisonResult = ( setProgressMessage( i18n.translate('xpack.dataVisualizer.dataDrift.progress.loadingReference', { - defaultMessage: `Loading reference data for {fieldsCount} fields.`, + defaultMessage: `Loading reference data for { fieldsCount } fields.`, values: { fieldsCount }, }) ); @@ -841,7 +843,7 @@ export const useFetchDataComparisonResult = ( setProgressMessage( i18n.translate('xpack.dataVisualizer.dataDrift.progress.loadingComparison', { - defaultMessage: `Loading comparison data for {fieldsCount} fields.`, + defaultMessage: `Loading comparison data for { fieldsCount } fields.`, values: { fieldsCount }, }) ); @@ -870,6 +872,7 @@ export const useFetchDataComparisonResult = ( signal, }), }); + if (isReturnedError(driftedRespAggs)) { setResult({ data: undefined, @@ -883,7 +886,7 @@ export const useFetchDataComparisonResult = ( setLoaded(0.5); setProgressMessage( i18n.translate('xpack.dataVisualizer.dataDrift.progress.loadedComparison', { - defaultMessage: `Loaded comparison data. Now loading histogram data.`, + defaultMessage: `Loaded comparison data.Now loading histogram data.`, }) ); From 78384286009202101bc54fcc79beae6021fbc148 Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Mon, 4 Dec 2023 09:22:12 -0600 Subject: [PATCH 2/3] Fix weird spacing change --- .../data_drift/use_data_drift_result.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts b/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts index 8ebe30a007c8c..245e3ad12612e 100644 --- a/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts +++ b/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts @@ -459,12 +459,12 @@ const fetchComparisonDriftedData = async ({ ); const fieldsWithNoOverlap = new Set(); - const rangesAggs = rangesResp.aggregations.sample + const rangesAggs = rangesResp?.aggregations?.sample ? rangesResp.aggregations.sample - : rangesResp.aggregations; + : rangesResp?.aggregations; for (const { field } of fields) { - if (rangesAggs[`${field}_ranges`]) { - const buckets = rangesAggs[`${field}_ranges`].buckets as AggregationsRangeBucketKeys[]; + if (isPopulatedObject(rangesAggs, [`${field}_ranges`])) { + const buckets = rangesAggs[`${field}_ranges`]; if (buckets) { const totalSumOfAllBuckets = buckets.reduce((acc, bucket) => acc + bucket.doc_count, 0); @@ -781,7 +781,7 @@ export const useFetchDataComparisonResult = ( setProgressMessage( i18n.translate('xpack.dataVisualizer.dataDrift.progress.loadingReference', { - defaultMessage: `Loading reference data for { fieldsCount } fields.`, + defaultMessage: `Loading reference data for {fieldsCount} fields.`, values: { fieldsCount }, }) ); @@ -843,7 +843,7 @@ export const useFetchDataComparisonResult = ( setProgressMessage( i18n.translate('xpack.dataVisualizer.dataDrift.progress.loadingComparison', { - defaultMessage: `Loading comparison data for { fieldsCount } fields.`, + defaultMessage: `Loading comparison data for {fieldsCount} fields.`, values: { fieldsCount }, }) ); @@ -886,7 +886,7 @@ export const useFetchDataComparisonResult = ( setLoaded(0.5); setProgressMessage( i18n.translate('xpack.dataVisualizer.dataDrift.progress.loadedComparison', { - defaultMessage: `Loaded comparison data.Now loading histogram data.`, + defaultMessage: `Loaded comparison data. Now loading histogram data.`, }) ); From 057df9eb61d01d9099623821d9d646c300e2a86e Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Mon, 4 Dec 2023 11:36:43 -0600 Subject: [PATCH 3/3] Fix type --- .../application/data_drift/use_data_drift_result.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts b/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts index 245e3ad12612e..88b597de5c128 100644 --- a/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts +++ b/x-pack/plugins/data_visualizer/public/application/data_drift/use_data_drift_result.ts @@ -29,7 +29,7 @@ import { isDefined } from '@kbn/ml-is-defined'; import { computeChi2PValue, type Histogram } from '@kbn/ml-chi2test'; import { mapAndFlattenFilters } from '@kbn/data-plugin/public'; -import type { AggregationsRangeBucketKeys } from '@elastic/elasticsearch/lib/api/types'; +import type { AggregationsMultiTermsBucketKeys } from '@elastic/elasticsearch/lib/api/types'; import { createMergedEsQuery } from '../index_data_visualizer/utils/saved_search_utils'; import { useDataVisualizerKibana } from '../kibana_context'; @@ -463,10 +463,15 @@ const fetchComparisonDriftedData = async ({ ? rangesResp.aggregations.sample : rangesResp?.aggregations; for (const { field } of fields) { - if (isPopulatedObject(rangesAggs, [`${field}_ranges`])) { - const buckets = rangesAggs[`${field}_ranges`]; + if ( + isPopulatedObject< + string, + estypes.AggregationsMultiBucketAggregateBase + >(rangesAggs, [`${field}_ranges`]) + ) { + const buckets = rangesAggs[`${field}_ranges`].buckets; - if (buckets) { + if (Array.isArray(buckets)) { const totalSumOfAllBuckets = buckets.reduce((acc, bucket) => acc + bucket.doc_count, 0); const fractions = buckets.map((bucket) => ({