From 421a75536298c52417bbf8929d6049a231c60e5e Mon Sep 17 00:00:00 2001 From: Miki Date: Wed, 4 Oct 2023 13:05:44 -0700 Subject: [PATCH] Fix errors in conditions for activating `vizAugmenter` (#5213) Signed-off-by: Miki Signed-off-by: Willie Hung --- CHANGELOG.md | 1 + .../vis_augmenter/public/utils/utils.ts | 52 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17750e4af783..e60c9cb01909 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Discover] Fix misc navigation issues ([#5168](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5168)) - [Discover] Fix mobile view ([#5168](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5168)) - Fix `visAugmenter` forming empty key-value pairs in its calls to the `SavedObject` API ([#5190](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5190)) +- Fix errors in conditions for activating `vizAugmenter` ([#5213](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5213)) - [Data Explorer][Discover] Automatically load solo added default index pattern ([#5171](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5171)) - [Data Explorer][Discover] Allow data grid to auto adjust size based on fetched data count ([#5191](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5191)) - [BUG][Data Explorer][Discover] Allow filter and query persist when refresh page or paste url to a new tab ([#5206](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5206)) diff --git a/src/plugins/vis_augmenter/public/utils/utils.ts b/src/plugins/vis_augmenter/public/utils/utils.ts index c8ebde337757..ce44964e6173 100644 --- a/src/plugins/vis_augmenter/public/utils/utils.ts +++ b/src/plugins/vis_augmenter/public/utils/utils.ts @@ -24,32 +24,38 @@ import { getUISettings } from '../services'; import { IUiSettingsClient } from '../../../../core/public'; export const isEligibleForVisLayers = (vis: Vis, uiSettingsClient?: IUiSettingsClient): boolean => { - // Only support date histogram and ensure there is only 1 x-axis and it has to be on the bottom. - // Additionally to have a valid x-axis, there needs to be a segment aggregation - const hasValidXaxis = - vis.data?.aggs !== undefined && - vis.data.aggs?.byTypeName('date_histogram').length === 1 && - vis.params.categoryAxes.length === 1 && - vis.params.categoryAxes[0].position === 'bottom' && - vis.data.aggs?.bySchemaName('segment').length > 0; - // Support 1 segment for x axis bucket (that is date_histogram) and support metrics for - // multiple supported yaxis only. If there are other aggregation types, this is not - // valid for augmentation - const hasCorrectAggregationCount = - vis.data?.aggs !== undefined && - vis.data.aggs?.bySchemaName('metric').length > 0 && - vis.data.aggs?.bySchemaName('metric').length === vis.data.aggs?.aggs.length - 1; - const hasOnlyLineSeries = - vis.params?.seriesParams !== undefined && - vis.params?.seriesParams?.every( - (seriesParam: { type: string }) => seriesParam.type === 'line' - ) && - vis.params?.type === 'line'; + // Only support a date histogram + const dateHistograms = vis.data?.aggs?.byTypeName?.('date_histogram'); + if (!Array.isArray(dateHistograms) || dateHistograms.length !== 1) return false; + + // Ensure there is only 1 x-axis and it has to be on the bottom + const xAxis = vis.params?.categoryAxes; + if (!Array.isArray(xAxis) || xAxis.length !== 1 || xAxis[0]?.position !== 'bottom') return false; + + // Additionally, to have a valid x-axis, there needs to be a segment aggregation + const segmentAggs = vis.data.aggs!.bySchemaName('segment'); + if (!Array.isArray(segmentAggs) || segmentAggs.length === 0) return false; + + // Require metrics for multiple supported y-axis only and no other aggregation types + const metricAggs = vis.data.aggs!.bySchemaName('metric'); + if ( + !Array.isArray(metricAggs) || + metricAggs.length === 0 || + metricAggs.length !== vis.data.aggs!.aggs?.length - 1 + ) + return false; + + // Must have only line series + if ( + !Array.isArray(vis.params.seriesParams) || + vis.params.type !== 'line' || + vis.params.seriesParams.some((seriesParam: { type: string }) => seriesParam.type !== 'line') + ) + return false; // Checks if the augmentation setting is enabled const config = uiSettingsClient ?? getUISettings(); - const isAugmentationEnabled = config.get(PLUGIN_AUGMENTATION_ENABLE_SETTING); - return isAugmentationEnabled && hasValidXaxis && hasCorrectAggregationCount && hasOnlyLineSeries; + return config.get(PLUGIN_AUGMENTATION_ENABLE_SETTING); }; /**