Skip to content

Commit

Permalink
type collapse functions for convert-to-lens
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Oct 18, 2022
1 parent f86aafd commit d48b4b7
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -69,7 +70,10 @@ describe('getConfiguration', () => {
buckets,
maxAccessor,
columnsWithoutReferenced: [],
bucketCollapseFn: { [collapseFn]: [breakdownByAccessor] },
bucketCollapseFn: { [collapseFn]: [breakdownByAccessor] } as Record<
CollapseFunction,
string[]
>,
})
).toEqual({
breakdownByAccessor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -28,15 +32,15 @@ export const getConfiguration = (
};
maxAccessor: string;
columnsWithoutReferenced: Column[];
bucketCollapseFn?: Record<string, string[]>;
bucketCollapseFn?: Record<CollapseFunction, string[]>;
}
): 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -50,7 +51,7 @@ describe('getConfiguration', () => {
metrics: [metric],
buckets: { all: [bucket], customBuckets: { metric: bucket } },
columnsWithoutReferenced: [],
bucketCollapseFn: { [collapseFn]: [bucket] },
bucketCollapseFn: { [collapseFn]: [bucket] } as Record<CollapseFunction, string[]>,
})
).toEqual({
breakdownByAccessor: bucket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -26,15 +30,15 @@ export const getConfiguration = (
customBuckets: Record<string, string>;
};
columnsWithoutReferenced: Column[];
bucketCollapseFn?: Record<string, string[]>;
bucketCollapseFn?: Record<CollapseFunction, string[]>;
}
): 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { AggTypes } from '../../../common';
import { getConfiguration } from '.';
import { CollapseFunction } from '@kbn/visualizations-plugin/common';

const params = {
perPage: 20,
Expand Down Expand Up @@ -48,7 +49,7 @@ describe('getConfiguration', () => {
},
},
],
bucketCollapseFn: { sum: ['bucket-1'] },
bucketCollapseFn: { sum: ['bucket-1'] } as Record<CollapseFunction, string[]>,
})
).toEqual({
columns: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>
bucketCollapseFn?: Record<CollapseFunction, string[]>
) => {
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,
Expand Down Expand Up @@ -61,7 +68,7 @@ export const getConfiguration = (
customBuckets: Record<string, string>;
};
columnsWithoutReferenced: Column[];
bucketCollapseFn?: Record<string, string[]>;
bucketCollapseFn?: Record<CollapseFunction, string[]>;
}
): TableVisConfiguration => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, string>> = {
const functionMap: Partial<Record<string, CollapseFunction>> = {
mean: 'avg',
min: 'min',
max: 'max',
Expand Down
12 changes: 7 additions & 5 deletions src/plugins/vis_types/xy/public/convert_to_lens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,7 +25,7 @@ export interface Layer {
columnOrder: never[];
seriesIdsMap: Record<string, string>;
isReferenceLineLayer: boolean;
collapseFn?: string;
collapseFn?: CollapseFunction;
}

const SIBBLING_PIPELINE_AGGS: string[] = [
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,5 @@ export const GaugeColorModes = {
PALETTE: 'palette',
NONE: 'none',
} as const;

export const CollapseFunctions = ['sum', 'avg', 'min', 'max'] as const;
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import {
GaugeLabelMajorModes,
GaugeColorModes,
GaugeCentralMajorModes,
CollapseFunctions,
} from '../constants';

export type CollapseFunction = typeof CollapseFunctions[number];

export type FillType = $Values<typeof FillTypes>;
export type SeriesType = $Values<typeof SeriesTypes>;
export type YAxisMode = $Values<typeof YAxisModes>;
Expand All @@ -43,8 +46,6 @@ export type GaugeLabelMajorMode = $Values<typeof GaugeLabelMajorModes>;
export type GaugeCentralMajorMode = $Values<typeof GaugeCentralMajorModes>;
export type GaugeTicksPosition = $Values<typeof GaugeTicksPositions>;

type CollapseFunction = 'sum' | 'avg' | 'min' | 'max';

export interface AxisExtentConfig {
mode: 'full' | 'custom' | 'dataBounds';
lowerBound?: number;
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/visualizations/common/convert_to_lens/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<XYLayerConfig, 'layerType'>
Expand All @@ -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));
17 changes: 13 additions & 4 deletions src/plugins/visualizations/public/convert_to_lens/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -30,7 +36,7 @@ export const getBucketCollapseFn = (
customBucketsMap: Record<string, string>,
metricColumns: AggBasedColumn[]
) => {
const collapseFnMap: Record<string, string[]> = {
const collapseFnMap: Record<CollapseFunction, string[]> = {
min: [],
max: [],
sum: [],
Expand All @@ -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);
}
}
});
});
Expand Down

0 comments on commit d48b4b7

Please sign in to comment.