diff --git a/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.test.ts b/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.test.ts index a8070e90449ec..6d88ae3f3b0d5 100644 --- a/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.test.ts +++ b/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.test.ts @@ -8,6 +8,7 @@ import { ColorSchemas } from '@kbn/charts-plugin/common'; import { CustomPaletteParams, PaletteOutput } from '@kbn/coloring'; +import { CollapseFunction } from '@kbn/visualizations-plugin/common'; import { GaugeVisParams } from '../../types'; import { getConfiguration } from './goal'; @@ -69,7 +70,10 @@ describe('getConfiguration', () => { buckets, maxAccessor, columnsWithoutReferenced: [], - bucketCollapseFn: { [collapseFn]: [breakdownByAccessor] }, + bucketCollapseFn: { [collapseFn]: [breakdownByAccessor] } as Record< + CollapseFunction, + string[] + >, }) ).toEqual({ breakdownByAccessor, diff --git a/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.ts b/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.ts index 995dc5302288e..46e1f5f05399e 100644 --- a/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.ts +++ b/src/plugins/vis_types/gauge/public/convert_to_lens/configurations/goal.ts @@ -7,7 +7,11 @@ */ import { CustomPaletteParams, PaletteOutput } from '@kbn/coloring'; -import { Column, MetricVisConfiguration } from '@kbn/visualizations-plugin/common'; +import { + CollapseFunction, + Column, + MetricVisConfiguration, +} from '@kbn/visualizations-plugin/common'; import { GaugeVisParams } from '../../types'; export const getConfiguration = ( @@ -28,15 +32,15 @@ export const getConfiguration = ( }; maxAccessor: string; columnsWithoutReferenced: Column[]; - bucketCollapseFn?: Record; + bucketCollapseFn?: Record; } ): MetricVisConfiguration => { const [metricAccessor] = metrics; const [breakdownByAccessor] = buckets.all; const collapseFn = bucketCollapseFn - ? Object.keys(bucketCollapseFn).find((key) => - bucketCollapseFn[key].includes(breakdownByAccessor) - ) + ? (Object.keys(bucketCollapseFn).find((key) => + bucketCollapseFn[key as CollapseFunction].includes(breakdownByAccessor) + ) as CollapseFunction) : undefined; return { layerId, diff --git a/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.test.ts b/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.test.ts index 5ccfb169c91ae..f3c80eb87d5e7 100644 --- a/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.test.ts +++ b/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.test.ts @@ -8,6 +8,7 @@ import { ColorSchemas } from '@kbn/charts-plugin/common'; import { CustomPaletteParams, PaletteOutput } from '@kbn/coloring'; +import { CollapseFunction } from '@kbn/visualizations-plugin/common'; import { getConfiguration } from '.'; import { VisParams } from '../../types'; @@ -50,7 +51,7 @@ describe('getConfiguration', () => { metrics: [metric], buckets: { all: [bucket], customBuckets: { metric: bucket } }, columnsWithoutReferenced: [], - bucketCollapseFn: { [collapseFn]: [bucket] }, + bucketCollapseFn: { [collapseFn]: [bucket] } as Record, }) ).toEqual({ breakdownByAccessor: bucket, diff --git a/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.ts b/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.ts index 7b1b42a0211f5..04969e0c99f65 100644 --- a/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.ts +++ b/src/plugins/vis_types/metric/public/convert_to_lens/configurations/index.ts @@ -7,7 +7,11 @@ */ import { CustomPaletteParams, PaletteOutput } from '@kbn/coloring'; -import { Column, MetricVisConfiguration } from '@kbn/visualizations-plugin/common'; +import { + CollapseFunction, + Column, + MetricVisConfiguration, +} from '@kbn/visualizations-plugin/common'; import { VisParams } from '../../types'; export const getConfiguration = ( @@ -26,15 +30,15 @@ export const getConfiguration = ( customBuckets: Record; }; columnsWithoutReferenced: Column[]; - bucketCollapseFn?: Record; + bucketCollapseFn?: Record; } ): MetricVisConfiguration => { const [metricAccessor] = metrics; const [breakdownByAccessor] = buckets.all; const collapseFn = bucketCollapseFn - ? Object.keys(bucketCollapseFn).find((key) => - bucketCollapseFn[key].includes(breakdownByAccessor) - ) + ? (Object.keys(bucketCollapseFn).find((key) => + bucketCollapseFn[key as CollapseFunction].includes(breakdownByAccessor) + ) as CollapseFunction) : undefined; return { layerId, diff --git a/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.test.ts b/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.test.ts index d2e68436700d3..19e4a004bd6fc 100644 --- a/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.test.ts +++ b/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.test.ts @@ -8,6 +8,7 @@ import { AggTypes } from '../../../common'; import { getConfiguration } from '.'; +import { CollapseFunction } from '@kbn/visualizations-plugin/common'; const params = { perPage: 20, @@ -48,7 +49,7 @@ describe('getConfiguration', () => { }, }, ], - bucketCollapseFn: { sum: ['bucket-1'] }, + bucketCollapseFn: { sum: ['bucket-1'] } as Record, }) ).toEqual({ columns: [ diff --git a/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.ts b/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.ts index 5079b25258a75..b532a79df7d6b 100644 --- a/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.ts +++ b/src/plugins/vis_types/table/public/convert_to_lens/configurations/index.ts @@ -6,19 +6,26 @@ * Side Public License, v 1. */ -import { Column, PagingState, TableVisConfiguration } from '@kbn/visualizations-plugin/common'; +import { + CollapseFunction, + Column, + PagingState, + TableVisConfiguration, +} from '@kbn/visualizations-plugin/common'; import { TableVisParams } from '../../../common'; const getColumns = ( params: TableVisParams, metrics: string[], columns: Column[], - bucketCollapseFn?: Record + bucketCollapseFn?: Record ) => { const { showTotal, totalFunc } = params; return columns.map(({ columnId }) => { const collapseFn = bucketCollapseFn - ? Object.keys(bucketCollapseFn).find((key) => bucketCollapseFn[key].includes(columnId)) + ? (Object.keys(bucketCollapseFn).find((key) => + bucketCollapseFn[key as CollapseFunction].includes(columnId) + ) as CollapseFunction) : undefined; return { columnId, @@ -61,7 +68,7 @@ export const getConfiguration = ( customBuckets: Record; }; columnsWithoutReferenced: Column[]; - bucketCollapseFn?: Record; + bucketCollapseFn?: Record; } ): TableVisConfiguration => { return { diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/series_agg.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/series_agg.ts index 030bcd887f9cb..92ad627c55970 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/series_agg.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/series_agg.ts @@ -5,9 +5,10 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ +import { CollapseFunction } from '@kbn/visualizations-plugin/common'; import type { Metric } from '../../../../common/types'; -const functionMap: Partial> = { +const functionMap: Partial> = { mean: 'avg', min: 'min', max: 'max', diff --git a/src/plugins/vis_types/xy/public/convert_to_lens/index.ts b/src/plugins/vis_types/xy/public/convert_to_lens/index.ts index 3b4339828c6d5..88965cc77f118 100644 --- a/src/plugins/vis_types/xy/public/convert_to_lens/index.ts +++ b/src/plugins/vis_types/xy/public/convert_to_lens/index.ts @@ -7,7 +7,7 @@ */ import { METRIC_TYPES } from '@kbn/data-plugin/public'; -import { Column, ColumnWithMeta } from '@kbn/visualizations-plugin/common'; +import { CollapseFunction, Column, ColumnWithMeta } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getVisSchemas, @@ -25,7 +25,7 @@ export interface Layer { columnOrder: never[]; seriesIdsMap: Record; isReferenceLineLayer: boolean; - collapseFn?: string; + collapseFn?: CollapseFunction; } const SIBBLING_PIPELINE_AGGS: string[] = [ @@ -175,9 +175,11 @@ export const convertToLens: ConvertXYToLensVisualization = async (vis, timefilte } }); const collapseFn = l.bucketCollapseFn - ? Object.keys(l.bucketCollapseFn).find((key) => - l.bucketCollapseFn[key].includes(l.buckets.customBuckets[l.metrics[0]]) - ) + ? (Object.keys(l.bucketCollapseFn).find((key) => + l.bucketCollapseFn[key as CollapseFunction].includes( + l.buckets.customBuckets[l.metrics[0]] + ) + ) as CollapseFunction) : undefined; return { indexPatternId, diff --git a/src/plugins/visualizations/common/convert_to_lens/constants.ts b/src/plugins/visualizations/common/convert_to_lens/constants.ts index b3af9b9394d67..00890884fd722 100644 --- a/src/plugins/visualizations/common/convert_to_lens/constants.ts +++ b/src/plugins/visualizations/common/convert_to_lens/constants.ts @@ -134,3 +134,5 @@ export const GaugeColorModes = { PALETTE: 'palette', NONE: 'none', } as const; + +export const CollapseFunctions = ['sum', 'avg', 'min', 'max'] as const; diff --git a/src/plugins/visualizations/common/convert_to_lens/types/configurations.ts b/src/plugins/visualizations/common/convert_to_lens/types/configurations.ts index 60f97a588c2a9..4f7a5ad715215 100644 --- a/src/plugins/visualizations/common/convert_to_lens/types/configurations.ts +++ b/src/plugins/visualizations/common/convert_to_lens/types/configurations.ts @@ -26,8 +26,11 @@ import { GaugeLabelMajorModes, GaugeColorModes, GaugeCentralMajorModes, + CollapseFunctions, } from '../constants'; +export type CollapseFunction = typeof CollapseFunctions[number]; + export type FillType = $Values; export type SeriesType = $Values; export type YAxisMode = $Values; @@ -43,8 +46,6 @@ export type GaugeLabelMajorMode = $Values; export type GaugeCentralMajorMode = $Values; export type GaugeTicksPosition = $Values; -type CollapseFunction = 'sum' | 'avg' | 'min' | 'max'; - export interface AxisExtentConfig { mode: 'full' | 'custom' | 'dataBounds'; lowerBound?: number; diff --git a/src/plugins/visualizations/common/convert_to_lens/utils.ts b/src/plugins/visualizations/common/convert_to_lens/utils.ts index 51c7e8239a439..6a875bf63bea4 100644 --- a/src/plugins/visualizations/common/convert_to_lens/utils.ts +++ b/src/plugins/visualizations/common/convert_to_lens/utils.ts @@ -7,8 +7,9 @@ */ import type { DataViewField } from '@kbn/data-views-plugin/common'; +import { CollapseFunctions } from './constants'; import type { SupportedMetric } from './lib/convert/supported_metrics'; -import type { Layer, XYAnnotationsLayerConfig, XYLayerConfig } from './types'; +import type { CollapseFunction, Layer, XYAnnotationsLayerConfig, XYLayerConfig } from './types'; export const isAnnotationsLayer = ( layer: Pick @@ -31,3 +32,6 @@ export const isFieldValid = ( return true; }; + +export const isCollapseFunction = (candidate: string | undefined): candidate is CollapseFunction => + Boolean(candidate && CollapseFunctions.includes(candidate as CollapseFunction)); diff --git a/src/plugins/visualizations/public/convert_to_lens/utils.ts b/src/plugins/visualizations/public/convert_to_lens/utils.ts index 0cab4f698fb2f..ba05d29cdeea9 100644 --- a/src/plugins/visualizations/public/convert_to_lens/utils.ts +++ b/src/plugins/visualizations/public/convert_to_lens/utils.ts @@ -8,7 +8,13 @@ import type { DataView } from '@kbn/data-views-plugin/common'; import { IAggConfig, METRIC_TYPES } from '@kbn/data-plugin/public'; -import { AggBasedColumn, SchemaConfig, SupportedAggregation } from '../../common'; +import { + AggBasedColumn, + CollapseFunction, + isCollapseFunction, + SchemaConfig, + SupportedAggregation, +} from '../../common'; import { convertBucketToColumns } from '../../common/convert_to_lens/lib/buckets'; import { isSiblingPipeline } from '../../common/convert_to_lens/lib/utils'; import { BucketColumn } from '../../common/convert_to_lens/lib'; @@ -30,7 +36,7 @@ export const getBucketCollapseFn = ( customBucketsMap: Record, metricColumns: AggBasedColumn[] ) => { - const collapseFnMap: Record = { + const collapseFnMap: Record = { min: [], max: [], sum: [], @@ -45,8 +51,11 @@ export const getBucketCollapseFn = ( const collapseFn = metrics .find((m) => m.aggId === metricColumn.meta.aggId) ?.aggType.split('_')[0]; - if (collapseFn) { - collapseFnMap[collapseFn].push(bucket.columnId); + + if (isCollapseFunction(collapseFn)) { + if (collapseFn) { + collapseFnMap[collapseFn].push(bucket.columnId); + } } }); });