From b02e48a6bcc589b3f5fcabfc77201ad914eabcf6 Mon Sep 17 00:00:00 2001 From: Miki Date: Wed, 4 Oct 2023 11:26:33 -0700 Subject: [PATCH] Fix errors in conditions for activating `vizAugmenter` Signed-off-by: Miki --- .../vis_augmenter/public/utils/utils.ts | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/plugins/vis_augmenter/public/utils/utils.ts b/src/plugins/vis_augmenter/public/utils/utils.ts index c8ebde337757..7ff1e5c5dd5d 100644 --- a/src/plugins/vis_augmenter/public/utils/utils.ts +++ b/src/plugins/vis_augmenter/public/utils/utils.ts @@ -24,32 +24,39 @@ 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); }; /**