Skip to content

Commit

Permalink
Fix errors in conditions for activating vizAugmenter
Browse files Browse the repository at this point in the history
Signed-off-by: Miki <[email protected]>
  • Loading branch information
AMoo-Miki committed Oct 4, 2023
1 parent 0ffd2ab commit b02e48a
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/plugins/vis_augmenter/public/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down

0 comments on commit b02e48a

Please sign in to comment.