From e8e5cec83dee15592a275c1056f2e0371ecddf70 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 12 Jun 2023 09:32:41 -0600 Subject: [PATCH] legacy agg vis tag cloud convert to lens (#159348) Part of https://github.com/elastic/kibana/issues/154307 Add convert to lens logic for tag cloud visualization --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Stratoula Kalafateli --- .../gauge/public/convert_to_lens/gauge.ts | 29 +----- .../gauge/public/convert_to_lens/goal.ts | 29 +----- .../vis_types/gauge/public/vis_type/gauge.tsx | 7 +- .../vis_types/gauge/public/vis_type/goal.tsx | 7 +- .../heatmap/public/convert_to_lens/index.ts | 17 +--- .../heatmap/public/vis_type/heatmap.ts | 7 +- .../metric/public/convert_to_lens/index.ts | 21 +--- .../metric/public/metric_vis_type.ts | 7 +- .../pie/public/convert_to_lens/index.ts | 17 +--- .../vis_types/pie/public/vis_type/pie.ts | 7 +- .../table/public/convert_to_lens/index.ts | 18 +--- .../vis_types/table/public/table_vis_type.ts | 7 +- src/plugins/vis_types/tagcloud/kibana.jsonc | 6 +- .../public/convert_to_lens/index.test.ts | 96 +++++++++++++++++++ .../tagcloud/public/convert_to_lens/index.ts | 78 +++++++++++++++ .../vis_types/tagcloud/public/plugin.ts | 15 ++- .../vis_types/tagcloud/public/services.ts | 13 +++ .../tagcloud/public/tag_cloud_type.ts | 17 +++- src/plugins/vis_types/tagcloud/tsconfig.json | 2 + .../timeseries/public/metrics_type.ts | 11 ++- .../xy/public/convert_to_lens/index.ts | 24 +---- .../vis_types/xy/public/vis_types/index.ts | 7 +- .../convert_to_lens/types/configurations.ts | 15 ++- .../common/convert_to_lens/utils.ts | 24 ++++- .../visualizations/tagcloud/suggestions.ts | 2 +- .../tagcloud/tagcloud_visualization.tsx | 16 +++- 26 files changed, 336 insertions(+), 163 deletions(-) create mode 100644 src/plugins/vis_types/tagcloud/public/convert_to_lens/index.test.ts create mode 100644 src/plugins/vis_types/tagcloud/public/convert_to_lens/index.ts create mode 100644 src/plugins/vis_types/tagcloud/public/services.ts diff --git a/src/plugins/vis_types/gauge/public/convert_to_lens/gauge.ts b/src/plugins/vis_types/gauge/public/convert_to_lens/gauge.ts index 17a14530bf9e5..41c228bd0b01d 100644 --- a/src/plugins/vis_types/gauge/public/convert_to_lens/gauge.ts +++ b/src/plugins/vis_types/gauge/public/convert_to_lens/gauge.ts @@ -7,32 +7,15 @@ */ import { v4 as uuidv4 } from 'uuid'; -import { - Column, - ColumnWithMeta, - PercentageModeConfigWithMinMax, -} from '@kbn/visualizations-plugin/common'; +import { PercentageModeConfigWithMinMax } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getDataViewByIndexPatternId, } from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { getDataViewsStart } from '../services'; import { ConvertGaugeVisToLensVisualization } from './types'; - -export const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { - if ((column as ColumnWithMeta).meta) { - return true; - } - return false; -}; - -export const excludeMetaFromColumn = (column: Column) => { - if (isColumnWithMeta(column)) { - const { meta, ...rest } = column; - return rest; - } - return column; -}; +import { getConfiguration } from './configurations/gauge'; export const convertToLens: ConvertGaugeVisToLensVisualization = async (vis, timefilter) => { if (!timefilter) { @@ -46,10 +29,8 @@ export const convertToLens: ConvertGaugeVisToLensVisualization = async (vis, tim return null; } - const [ - { getColumnsFromVis, createStaticValueColumn, getPalette, getPercentageModeConfig }, - { getConfiguration }, - ] = await Promise.all([convertToLensModule, import('./configurations/gauge')]); + const { getColumnsFromVis, createStaticValueColumn, getPalette, getPercentageModeConfig } = + await convertToLensModule; const percentageModeConfig = getPercentageModeConfig(vis.params.gauge, false); diff --git a/src/plugins/vis_types/gauge/public/convert_to_lens/goal.ts b/src/plugins/vis_types/gauge/public/convert_to_lens/goal.ts index 2e889466ca978..d3743f964264f 100644 --- a/src/plugins/vis_types/gauge/public/convert_to_lens/goal.ts +++ b/src/plugins/vis_types/gauge/public/convert_to_lens/goal.ts @@ -7,32 +7,15 @@ */ import { v4 as uuidv4 } from 'uuid'; -import { - Column, - ColumnWithMeta, - PercentageModeConfigWithMinMax, -} from '@kbn/visualizations-plugin/common'; +import { PercentageModeConfigWithMinMax } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getDataViewByIndexPatternId, } from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { getDataViewsStart } from '../services'; import { ConvertGoalVisToLensVisualization } from './types'; - -export const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { - if ((column as ColumnWithMeta).meta) { - return true; - } - return false; -}; - -export const excludeMetaFromColumn = (column: Column) => { - if (isColumnWithMeta(column)) { - const { meta, ...rest } = column; - return rest; - } - return column; -}; +import { getConfiguration } from './configurations/goal'; export const convertToLens: ConvertGoalVisToLensVisualization = async (vis, timefilter) => { if (!timefilter) { @@ -46,10 +29,8 @@ export const convertToLens: ConvertGoalVisToLensVisualization = async (vis, time return null; } - const [ - { getColumnsFromVis, getPalette, getPercentageModeConfig, createStaticValueColumn }, - { getConfiguration }, - ] = await Promise.all([convertToLensModule, import('./configurations/goal')]); + const { getColumnsFromVis, getPalette, getPercentageModeConfig, createStaticValueColumn } = + await convertToLensModule; const percentageModeConfig = getPercentageModeConfig(vis.params.gauge, false); diff --git a/src/plugins/vis_types/gauge/public/vis_type/gauge.tsx b/src/plugins/vis_types/gauge/public/vis_type/gauge.tsx index 264acd660252d..56edc040767dc 100644 --- a/src/plugins/vis_types/gauge/public/vis_type/gauge.tsx +++ b/src/plugins/vis_types/gauge/public/vis_type/gauge.tsx @@ -18,7 +18,6 @@ import { toExpressionAst } from '../to_ast'; import { getGaugeOptions } from '../editor/components'; import { GaugeVisParams } from '../types'; import { SplitTooltip } from './split_tooltip'; -import { convertGaugeToLens } from '../convert_to_lens'; export const getGaugeVisTypeDefinition = ( props: GaugeTypeProps @@ -130,8 +129,12 @@ export const getGaugeVisTypeDefinition = ( ], }, requiresSearch: true, - navigateToLens: async (vis, timefilter) => (vis ? convertGaugeToLens(vis, timefilter) : null), + navigateToLens: async (vis, timefilter) => { + const { convertGaugeToLens } = await import('../convert_to_lens'); + return vis ? convertGaugeToLens(vis, timefilter) : null; + }, getExpressionVariables: async (vis, timeFilter) => { + const { convertGaugeToLens } = await import('../convert_to_lens'); return { canNavigateToLens: Boolean(vis?.params ? await convertGaugeToLens(vis, timeFilter) : null), }; diff --git a/src/plugins/vis_types/gauge/public/vis_type/goal.tsx b/src/plugins/vis_types/gauge/public/vis_type/goal.tsx index 52fc8aa4e763f..97fdc26dd1f10 100644 --- a/src/plugins/vis_types/gauge/public/vis_type/goal.tsx +++ b/src/plugins/vis_types/gauge/public/vis_type/goal.tsx @@ -17,7 +17,6 @@ import { getGaugeOptions } from '../editor/components'; import { toExpressionAst } from '../to_ast'; import { GaugeVisParams, GaugeType, GaugeTypeProps } from '../types'; import { SplitTooltip } from './split_tooltip'; -import { convertGoalToLens } from '../convert_to_lens'; export const getGoalVisTypeDefinition = ( props: GaugeTypeProps @@ -122,8 +121,12 @@ export const getGoalVisTypeDefinition = ( ], }, requiresSearch: true, - navigateToLens: async (vis, timefilter) => (vis ? convertGoalToLens(vis, timefilter) : null), + navigateToLens: async (vis, timefilter) => { + const { convertGoalToLens } = await import('../convert_to_lens'); + return vis ? convertGoalToLens(vis, timefilter) : null; + }, getExpressionVariables: async (vis, timeFilter) => { + const { convertGoalToLens } = await import('../convert_to_lens'); return { canNavigateToLens: Boolean(vis?.params ? await convertGoalToLens(vis, timeFilter) : null), }; diff --git a/src/plugins/vis_types/heatmap/public/convert_to_lens/index.ts b/src/plugins/vis_types/heatmap/public/convert_to_lens/index.ts index bd7b72a1e7e2d..90875b6cb5f36 100644 --- a/src/plugins/vis_types/heatmap/public/convert_to_lens/index.ts +++ b/src/plugins/vis_types/heatmap/public/convert_to_lens/index.ts @@ -6,31 +6,16 @@ * Side Public License, v 1. */ -import { Column, ColumnWithMeta } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getDataViewByIndexPatternId, } from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { v4 as uuidv4 } from 'uuid'; import { getDataViewsStart } from '../services'; import { getConfiguration } from './configurations'; import { ConvertHeatmapToLensVisualization } from './types'; -export const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { - if ((column as ColumnWithMeta).meta) { - return true; - } - return false; -}; - -export const excludeMetaFromColumn = (column: Column) => { - if (isColumnWithMeta(column)) { - const { meta, ...rest } = column; - return rest; - } - return column; -}; - export const convertToLens: ConvertHeatmapToLensVisualization = async (vis, timefilter) => { if (!timefilter) { return null; diff --git a/src/plugins/vis_types/heatmap/public/vis_type/heatmap.ts b/src/plugins/vis_types/heatmap/public/vis_type/heatmap.ts index 97f6db9668a2a..c91f7dbad0a9e 100644 --- a/src/plugins/vis_types/heatmap/public/vis_type/heatmap.ts +++ b/src/plugins/vis_types/heatmap/public/vis_type/heatmap.ts @@ -14,7 +14,6 @@ import { VIS_EVENT_TO_TRIGGER, VisTypeDefinition } from '@kbn/visualizations-plu import { HeatmapTypeProps, HeatmapVisParams, AxisType, ScaleType } from '../types'; import { toExpressionAst } from '../to_ast'; import { getHeatmapOptions } from '../editor/components'; -import { convertToLens } from '../convert_to_lens'; export const getHeatmapVisTypeDefinition = ({ showElasticChartsOptions = false, @@ -145,8 +144,12 @@ export const getHeatmapVisTypeDefinition = ({ ], }, requiresSearch: true, - navigateToLens: async (vis, timefilter) => (vis ? convertToLens(vis, timefilter) : null), + navigateToLens: async (vis, timefilter) => { + const { convertToLens } = await import('../convert_to_lens'); + return vis ? convertToLens(vis, timefilter) : null; + }, getExpressionVariables: async (vis, timeFilter) => { + const { convertToLens } = await import('../convert_to_lens'); return { canNavigateToLens: Boolean(vis?.params ? await convertToLens(vis, timeFilter) : null), }; diff --git a/src/plugins/vis_types/metric/public/convert_to_lens/index.ts b/src/plugins/vis_types/metric/public/convert_to_lens/index.ts index d38fc97697774..ee810dfb742dd 100644 --- a/src/plugins/vis_types/metric/public/convert_to_lens/index.ts +++ b/src/plugins/vis_types/metric/public/convert_to_lens/index.ts @@ -7,28 +7,14 @@ */ import { v4 as uuidv4 } from 'uuid'; -import { Column, ColumnWithMeta } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getDataViewByIndexPatternId, } from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { getDataViewsStart } from '../services'; import { ConvertMetricVisToLensVisualization } from './types'; - -export const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { - if ((column as ColumnWithMeta).meta) { - return true; - } - return false; -}; - -export const excludeMetaFromColumn = (column: Column) => { - if (isColumnWithMeta(column)) { - const { meta, ...rest } = column; - return rest; - } - return column; -}; +import { getConfiguration } from './configurations'; export const convertToLens: ConvertMetricVisToLensVisualization = async (vis, timefilter) => { if (!timefilter) { @@ -42,8 +28,7 @@ export const convertToLens: ConvertMetricVisToLensVisualization = async (vis, ti return null; } - const [{ getColumnsFromVis, getPalette, getPercentageModeConfig }, { getConfiguration }] = - await Promise.all([convertToLensModule, import('./configurations')]); + const { getColumnsFromVis, getPalette, getPercentageModeConfig } = await convertToLensModule; const percentageModeConfig = getPercentageModeConfig(vis.params.metric); const layers = getColumnsFromVis( diff --git a/src/plugins/vis_types/metric/public/metric_vis_type.ts b/src/plugins/vis_types/metric/public/metric_vis_type.ts index 32a201e82f31d..7811d59d2e85f 100644 --- a/src/plugins/vis_types/metric/public/metric_vis_type.ts +++ b/src/plugins/vis_types/metric/public/metric_vis_type.ts @@ -13,7 +13,6 @@ import { AggGroupNames } from '@kbn/data-plugin/public'; import { MetricVisOptions } from './components'; import { toExpressionAst } from './to_ast'; import { VisParams } from './types'; -import { convertToLens } from './convert_to_lens'; export const createMetricVisTypeDefinition = (): VisTypeDefinition => ({ name: 'metric', @@ -103,8 +102,12 @@ export const createMetricVisTypeDefinition = (): VisTypeDefinition => ], }, requiresSearch: true, - navigateToLens: async (vis, timefilter) => (vis ? convertToLens(vis, timefilter) : null), + navigateToLens: async (vis, timefilter) => { + const { convertToLens } = await import('./convert_to_lens'); + return vis ? convertToLens(vis, timefilter) : null; + }, getExpressionVariables: async (vis, timeFilter) => { + const { convertToLens } = await import('./convert_to_lens'); return { canNavigateToLens: Boolean(vis?.params ? await convertToLens(vis, timeFilter) : null), }; diff --git a/src/plugins/vis_types/pie/public/convert_to_lens/index.ts b/src/plugins/vis_types/pie/public/convert_to_lens/index.ts index e997696e5fb01..c1ddfa46283e8 100644 --- a/src/plugins/vis_types/pie/public/convert_to_lens/index.ts +++ b/src/plugins/vis_types/pie/public/convert_to_lens/index.ts @@ -6,31 +6,16 @@ * Side Public License, v 1. */ -import { Column, ColumnWithMeta } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getDataViewByIndexPatternId, } from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { v4 as uuidv4 } from 'uuid'; import { getDataViewsStart } from '../services'; import { getConfiguration } from './configurations'; import { ConvertPieToLensVisualization } from './types'; -export const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { - if ((column as ColumnWithMeta).meta) { - return true; - } - return false; -}; - -export const excludeMetaFromColumn = (column: Column) => { - if (isColumnWithMeta(column)) { - const { meta, ...rest } = column; - return rest; - } - return column; -}; - export const convertToLens: ConvertPieToLensVisualization = async (vis, timefilter) => { if (!timefilter) { return null; diff --git a/src/plugins/vis_types/pie/public/vis_type/pie.ts b/src/plugins/vis_types/pie/public/vis_type/pie.ts index dfe88e3191a91..525ca28b23140 100644 --- a/src/plugins/vis_types/pie/public/vis_type/pie.ts +++ b/src/plugins/vis_types/pie/public/vis_type/pie.ts @@ -21,7 +21,6 @@ import { DEFAULT_PERCENT_DECIMALS } from '../../common'; import { PieTypeProps } from '../types'; import { toExpressionAst } from '../to_ast'; import { getPieOptions } from '../editor/components'; -import { convertToLens } from '../convert_to_lens'; export const getPieVisTypeDefinition = ({ showElasticChartsOptions = false, @@ -122,8 +121,12 @@ export const getPieVisTypeDefinition = ({ }, hierarchicalData: true, requiresSearch: true, - navigateToLens: async (vis, timefilter) => (vis ? convertToLens(vis, timefilter) : null), + navigateToLens: async (vis, timefilter) => { + const { convertToLens } = await import('../convert_to_lens'); + return vis ? convertToLens(vis, timefilter) : null; + }, getExpressionVariables: async (vis, timeFilter) => { + const { convertToLens } = await import('../convert_to_lens'); return { canNavigateToLens: Boolean(vis?.params ? await convertToLens(vis, timeFilter) : null), }; diff --git a/src/plugins/vis_types/table/public/convert_to_lens/index.ts b/src/plugins/vis_types/table/public/convert_to_lens/index.ts index 1866ac5f67cf7..3e7ff478102c0 100644 --- a/src/plugins/vis_types/table/public/convert_to_lens/index.ts +++ b/src/plugins/vis_types/table/public/convert_to_lens/index.ts @@ -7,32 +7,18 @@ */ import { METRIC_TYPES } from '@kbn/data-plugin/common'; -import { Column, ColumnWithMeta, SchemaConfig } from '@kbn/visualizations-plugin/common'; +import { SchemaConfig } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getVisSchemas, getDataViewByIndexPatternId, } from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { v4 as uuidv4 } from 'uuid'; import { getDataViewsStart } from '../services'; import { getConfiguration } from './configurations'; import { ConvertTableToLensVisualization } from './types'; -export const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { - if ((column as ColumnWithMeta).meta) { - return true; - } - return false; -}; - -export const excludeMetaFromColumn = (column: Column) => { - if (isColumnWithMeta(column)) { - const { meta, ...rest } = column; - return rest; - } - return column; -}; - export const convertToLens: ConvertTableToLensVisualization = async (vis, timefilter) => { if (!timefilter) { return null; diff --git a/src/plugins/vis_types/table/public/table_vis_type.ts b/src/plugins/vis_types/table/public/table_vis_type.ts index 27479d23b99de..7d40b536908be 100644 --- a/src/plugins/vis_types/table/public/table_vis_type.ts +++ b/src/plugins/vis_types/table/public/table_vis_type.ts @@ -13,7 +13,6 @@ import { VIS_EVENT_TO_TRIGGER, VisTypeDefinition } from '@kbn/visualizations-plu import { TableVisParams, VIS_TYPE_TABLE } from '../common'; import { TableOptions } from './components/table_vis_options_lazy'; import { toExpressionAst } from './to_ast'; -import { convertToLens } from './convert_to_lens'; export const tableVisTypeDefinition: VisTypeDefinition = { name: VIS_TYPE_TABLE, @@ -103,8 +102,12 @@ export const tableVisTypeDefinition: VisTypeDefinition = { hasPartialRows: (vis) => vis.params.showPartialRows, hierarchicalData: (vis) => vis.params.showPartialRows || vis.params.showMetricsAtAllLevels, requiresSearch: true, - navigateToLens: async (vis, timefilter) => (vis ? convertToLens(vis, timefilter) : null), + navigateToLens: async (vis, timefilter) => { + const { convertToLens } = await import('./convert_to_lens'); + return vis ? convertToLens(vis, timefilter) : null; + }, getExpressionVariables: async (vis, timeFilter) => { + const { convertToLens } = await import('./convert_to_lens'); return { canNavigateToLens: Boolean(vis?.params ? await convertToLens(vis, timeFilter) : null), }; diff --git a/src/plugins/vis_types/tagcloud/kibana.jsonc b/src/plugins/vis_types/tagcloud/kibana.jsonc index 755ca5fb5a384..8694f3f27c2f5 100644 --- a/src/plugins/vis_types/tagcloud/kibana.jsonc +++ b/src/plugins/vis_types/tagcloud/kibana.jsonc @@ -11,11 +11,13 @@ "data", "expressions", "visualizations", - "charts" + "charts", + "dataViews" ], "requiredBundles": [ "kibanaReact", - "visDefaultEditor" + "visDefaultEditor", + "kibanaUtils" ] } } diff --git a/src/plugins/vis_types/tagcloud/public/convert_to_lens/index.test.ts b/src/plugins/vis_types/tagcloud/public/convert_to_lens/index.test.ts new file mode 100644 index 0000000000000..a4c43be97bd3f --- /dev/null +++ b/src/plugins/vis_types/tagcloud/public/convert_to_lens/index.test.ts @@ -0,0 +1,96 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +jest.mock('uuid', () => ({ + v4: jest.fn().mockReturnValue('12345'), +})); + +jest.mock('@kbn/visualizations-plugin/public', () => ({ + convertToLensModule: Promise.resolve({ + getColumnsFromVis: jest.fn(() => { + return [ + { + metrics: ['0cdf0372-a78f-438f-9fc0-df9ad83796df'], + buckets: { + all: ['8a2556cf-dfb2-49f1-83cb-8892e1eace1c'], + }, + columns: [ + { + columnId: '8a2556cf-dfb2-49f1-83cb-8892e1eace1c', + meta: { + aggId: '2', + }, + }, + ], + }, + ]; + }), + }), + getDataViewByIndexPatternId: jest.fn(() => ({ id: 'myDataViewId' })), +})); + +jest.mock('../services', () => ({ + getDataViewsStart: () => ({ get: () => ({}), getDefault: () => ({}) }), +})); + +import type { Vis } from '@kbn/visualizations-plugin/public'; +import type { TagCloudVisParams } from '../types'; +import type { TimefilterContract } from '@kbn/data-plugin/public'; +import { convertToLens } from '.'; + +test('should convert legacy tag cloud vis into navigate to lens context', async () => { + const mockVis = { + data: { + indexPattern: { + id: 'myDataViewId', + }, + }, + params: { + maxFontSize: 81, + minFontSize: 27, + orientation: 'right angled', + palette: { + name: 'status', + type: 'palette', + }, + scale: 'linear', + showLabel: true, + }, + } as unknown as Vis; + expect(await convertToLens(mockVis, {} as unknown as TimefilterContract)).toEqual({ + type: 'lnsTagcloud', + layers: [ + { + indexPatternId: 'myDataViewId', + layerId: '12345', + columns: [ + { + columnId: '8a2556cf-dfb2-49f1-83cb-8892e1eace1c', + }, + ], + columnOrder: [], + ignoreGlobalFilters: false, + }, + ], + configuration: { + layerId: '12345', + layerType: 'data', + valueAccessor: '0cdf0372-a78f-438f-9fc0-df9ad83796df', + tagAccessor: '8a2556cf-dfb2-49f1-83cb-8892e1eace1c', + maxFontSize: 81, + minFontSize: 27, + orientation: 'right angled', + palette: { + name: 'status', + type: 'palette', + }, + showLabel: true, + }, + indexPatternIds: ['myDataViewId'], + }); +}); diff --git a/src/plugins/vis_types/tagcloud/public/convert_to_lens/index.ts b/src/plugins/vis_types/tagcloud/public/convert_to_lens/index.ts new file mode 100644 index 0000000000000..1df8dc5942c4c --- /dev/null +++ b/src/plugins/vis_types/tagcloud/public/convert_to_lens/index.ts @@ -0,0 +1,78 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { v4 as uuidv4 } from 'uuid'; +import { + convertToLensModule, + getDataViewByIndexPatternId, +} from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; +import type { TimefilterContract } from '@kbn/data-plugin/public'; +import type { Vis } from '@kbn/visualizations-plugin/public'; +import { NavigateToLensContext, TagcloudVisConfiguration } from '@kbn/visualizations-plugin/common'; +import type { TagCloudVisParams } from '../types'; +import { getDataViewsStart } from '../services'; + +export const convertToLens = async ( + vis: Vis, + timefilter?: TimefilterContract +) => { + if (!timefilter) { + return null; + } + + const dataViews = getDataViewsStart(); + const dataView = await getDataViewByIndexPatternId(vis.data.indexPattern?.id, dataViews); + + if (!dataView) { + return null; + } + + const { getColumnsFromVis } = await convertToLensModule; + const layers = getColumnsFromVis(vis, timefilter, dataView, { + splits: ['segment'], + }); + + if (layers === null) { + return null; + } + + const [layerConfig] = layers; + + if (!layerConfig.buckets.all.length || !layerConfig.metrics.length) { + return null; + } + + const layerId = uuidv4(); + + const indexPatternId = dataView.id!; + return { + type: 'lnsTagcloud', + layers: [ + { + indexPatternId, + layerId, + columns: layerConfig.columns.map(excludeMetaFromColumn), + columnOrder: [], + ignoreGlobalFilters: false, + }, + ], + configuration: { + layerId, + layerType: 'data', + valueAccessor: layerConfig.metrics[0], + tagAccessor: layerConfig.buckets.all[0], + maxFontSize: vis.params.maxFontSize, + minFontSize: vis.params.minFontSize, + orientation: vis.params.orientation, + palette: vis.params.palette, + showLabel: vis.params.showLabel, + }, + indexPatternIds: [indexPatternId], + } as NavigateToLensContext; +}; diff --git a/src/plugins/vis_types/tagcloud/public/plugin.ts b/src/plugins/vis_types/tagcloud/public/plugin.ts index c42a62d162766..ec5ae6afaaad2 100644 --- a/src/plugins/vis_types/tagcloud/public/plugin.ts +++ b/src/plugins/vis_types/tagcloud/public/plugin.ts @@ -9,9 +9,10 @@ import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '@kbn/core/public'; import { VisualizationsSetup } from '@kbn/visualizations-plugin/public'; import { ChartsPluginSetup } from '@kbn/charts-plugin/public'; - +import { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; import { getTagCloudVisTypeDefinition } from './tag_cloud_type'; import { TagcloudPublicConfig } from '../config'; +import { setDataViewsStart } from './services'; /** @internal */ export interface TagCloudPluginSetupDependencies { @@ -19,13 +20,19 @@ export interface TagCloudPluginSetupDependencies { charts: ChartsPluginSetup; } +export interface TagCloudPluginStartDependencies { + dataViews: DataViewsPublicPluginStart; +} + /** @internal */ export interface TagCloudVisDependencies { palettes: ChartsPluginSetup['palettes']; } /** @internal */ -export class TagCloudPlugin implements Plugin { +export class TagCloudPlugin + implements Plugin +{ initializerContext: PluginInitializerContext; constructor(initializerContext: PluginInitializerContext) { @@ -45,5 +52,7 @@ export class TagCloudPlugin implements Plugin { }); } - public start(core: CoreStart) {} + public start(core: CoreStart, { dataViews }: TagCloudPluginStartDependencies) { + setDataViewsStart(dataViews); + } } diff --git a/src/plugins/vis_types/tagcloud/public/services.ts b/src/plugins/vis_types/tagcloud/public/services.ts new file mode 100644 index 0000000000000..736ad70d49419 --- /dev/null +++ b/src/plugins/vis_types/tagcloud/public/services.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { createGetterSetter } from '@kbn/kibana-utils-plugin/public'; +import { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; + +export const [getDataViewsStart, setDataViewsStart] = + createGetterSetter('dataViews'); diff --git a/src/plugins/vis_types/tagcloud/public/tag_cloud_type.ts b/src/plugins/vis_types/tagcloud/public/tag_cloud_type.ts index 417ec9430333d..2b54293b2e4ef 100644 --- a/src/plugins/vis_types/tagcloud/public/tag_cloud_type.ts +++ b/src/plugins/vis_types/tagcloud/public/tag_cloud_type.ts @@ -9,7 +9,9 @@ import { i18n } from '@kbn/i18n'; import { AggGroupNames } from '@kbn/data-plugin/public'; import { VIS_EVENT_TO_TRIGGER } from '@kbn/visualizations-plugin/public'; - +import type { TimefilterContract } from '@kbn/data-plugin/public'; +import type { Vis } from '@kbn/visualizations-plugin/public'; +import type { TagCloudVisParams } from './types'; import { getTagCloudOptions } from './components/get_tag_cloud_options'; import { toExpressionAst } from './to_ast'; import { TagCloudVisDependencies } from './plugin'; @@ -80,5 +82,18 @@ export const getTagCloudVisTypeDefinition = ({ palettes }: TagCloudVisDependenci ], }, requiresSearch: true, + navigateToLens: async (vis?: Vis, timefilter?: TimefilterContract) => { + const { convertToLens } = await import('./convert_to_lens'); + return vis ? convertToLens(vis, timefilter) : null; + }, + getExpressionVariables: async ( + vis?: Vis, + timeFilter?: TimefilterContract + ) => { + const { convertToLens } = await import('./convert_to_lens'); + return { + canNavigateToLens: Boolean(vis?.params ? await convertToLens(vis, timeFilter) : null), + }; + }, }; }; diff --git a/src/plugins/vis_types/tagcloud/tsconfig.json b/src/plugins/vis_types/tagcloud/tsconfig.json index 45cefb2106423..844e910554e1f 100644 --- a/src/plugins/vis_types/tagcloud/tsconfig.json +++ b/src/plugins/vis_types/tagcloud/tsconfig.json @@ -19,6 +19,8 @@ "@kbn/i18n", "@kbn/coloring", "@kbn/config-schema", + "@kbn/kibana-utils-plugin", + "@kbn/data-views-plugin", ], "exclude": [ "target/**/*", diff --git a/src/plugins/vis_types/timeseries/public/metrics_type.ts b/src/plugins/vis_types/timeseries/public/metrics_type.ts index 4409faf7c0827..51b0fcce5a58d 100644 --- a/src/plugins/vis_types/timeseries/public/metrics_type.ts +++ b/src/plugins/vis_types/timeseries/public/metrics_type.ts @@ -28,7 +28,6 @@ import { toExpressionAst } from './to_ast'; import { getDataViewsStart, getUISettings } from './services'; import type { TimeseriesVisDefaultParams, TimeseriesVisParams } from './types'; import type { IndexPatternValue, Panel } from '../common/types'; -import { convertTSVBtoLensConfiguration } from './convert_to_lens'; export const withReplacedIds = ( vis: Vis @@ -168,6 +167,7 @@ export const metricsVisDefinition: VisTypeDefinition< return []; }, getExpressionVariables: async (vis, timeFilter) => { + const { convertTSVBtoLensConfiguration } = await import('./convert_to_lens'); return { canNavigateToLens: Boolean( vis?.params @@ -176,9 +176,12 @@ export const metricsVisDefinition: VisTypeDefinition< ), }; }, - navigateToLens: async (vis, timeFilter) => - vis?.params ? await convertTSVBtoLensConfiguration(vis, timeFilter?.getAbsoluteTime()) : null, - + navigateToLens: async (vis, timeFilter) => { + const { convertTSVBtoLensConfiguration } = await import('./convert_to_lens'); + return vis?.params + ? await convertTSVBtoLensConfiguration(vis, timeFilter?.getAbsoluteTime()) + : null; + }, inspectorAdapters: () => ({ requests: new RequestAdapter(), }), 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 88aee4e1eb7b6..a7a0990f2ea4c 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,15 +7,17 @@ */ import { METRIC_TYPES } from '@kbn/data-plugin/public'; -import { CollapseFunction, Column, ColumnWithMeta } from '@kbn/visualizations-plugin/common'; +import { CollapseFunction, Column } from '@kbn/visualizations-plugin/common'; import { convertToLensModule, getVisSchemas, getDataViewByIndexPatternId, } from '@kbn/visualizations-plugin/public'; +import { excludeMetaFromColumn } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { getDataViewsStart } from '../services'; import { getSeriesParams } from '../utils/get_series_params'; import { ConvertXYToLensVisualization } from './types'; +import { getConfiguration } from './configurations'; export interface Layer { indexPatternId: string; @@ -36,21 +38,6 @@ const SIBBLING_PIPELINE_AGGS: string[] = [ METRIC_TYPES.MIN_BUCKET, ]; -export const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { - if ((column as ColumnWithMeta).meta) { - return true; - } - return false; -}; - -export const excludeMetaFromColumn = (column: Column) => { - if (isColumnWithMeta(column)) { - const { meta, ...rest } = column; - return rest; - } - return column; -}; - export const convertToLens: ConvertXYToLensVisualization = async (vis, timefilter) => { if (!timefilter) { return null; @@ -86,10 +73,7 @@ export const convertToLens: ConvertXYToLensVisualization = async (vis, timefilte (param) => param.show && visSchemas.metric.some((m) => m.aggId?.split('.')[0] === param.data.id) ); - const [{ getColumnsFromVis, createStaticValueColumn }, { getConfiguration }] = await Promise.all([ - convertToLensModule, - import('./configurations'), - ]); + const { getColumnsFromVis, createStaticValueColumn } = await convertToLensModule; const dataLayers = getColumnsFromVis( vis, timefilter, diff --git a/src/plugins/vis_types/xy/public/vis_types/index.ts b/src/plugins/vis_types/xy/public/vis_types/index.ts index 2f7a03b6aaf1c..5fcc07fdb78e6 100644 --- a/src/plugins/vis_types/xy/public/vis_types/index.ts +++ b/src/plugins/vis_types/xy/public/vis_types/index.ts @@ -12,7 +12,6 @@ import { areaVisTypeDefinition } from './area'; import { lineVisTypeDefinition } from './line'; import { histogramVisTypeDefinition } from './histogram'; import { horizontalBarVisTypeDefinition } from './horizontal_bar'; -import { convertToLens } from '../convert_to_lens'; export const visTypesDefinitions = [ areaVisTypeDefinition, @@ -22,8 +21,12 @@ export const visTypesDefinitions = [ ].map>((defenition) => { return { ...defenition, - navigateToLens: async (vis, timefilter) => (vis ? convertToLens(vis, timefilter) : null), + navigateToLens: async (vis, timefilter) => { + const { convertToLens } = await import('../convert_to_lens'); + return vis ? convertToLens(vis, timefilter) : null; + }, getExpressionVariables: async (vis, timeFilter) => { + const { convertToLens } = await import('../convert_to_lens'); return { canNavigateToLens: Boolean(vis?.params ? await convertToLens(vis, timeFilter) : null), }; 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 2676620bb1a6c..9949644aa1e24 100644 --- a/src/plugins/visualizations/common/convert_to_lens/types/configurations.ts +++ b/src/plugins/visualizations/common/convert_to_lens/types/configurations.ts @@ -333,10 +333,23 @@ export type HeatmapConfiguration = HeatmapLayerState & { palette?: Palette; }; +export interface TagcloudVisConfiguration { + layerId: string; + layerType: LayerType; + valueAccessor: string; + tagAccessor: string; + maxFontSize: number; + minFontSize: number; + orientation: string; + palette: PaletteOutput; + showLabel: boolean; +} + export type Configuration = | XYConfiguration | TableVisConfiguration | PartitionVisConfiguration | MetricVisConfiguration | GaugeVisConfiguration - | HeatmapConfiguration; + | HeatmapConfiguration + | TagcloudVisConfiguration; diff --git a/src/plugins/visualizations/common/convert_to_lens/utils.ts b/src/plugins/visualizations/common/convert_to_lens/utils.ts index 88c2802c421ec..7994d21d83abb 100644 --- a/src/plugins/visualizations/common/convert_to_lens/utils.ts +++ b/src/plugins/visualizations/common/convert_to_lens/utils.ts @@ -9,7 +9,14 @@ import type { DataViewField } from '@kbn/data-views-plugin/common'; import { CollapseFunctions } from './constants'; import type { SupportedMetric } from './lib/convert/supported_metrics'; -import type { CollapseFunction, Layer, XYAnnotationsLayerConfig, XYLayerConfig } from './types'; +import type { + CollapseFunction, + Column, + ColumnWithMeta, + Layer, + XYAnnotationsLayerConfig, + XYLayerConfig, +} from './types'; export const isAnnotationsLayer = ( layer: Pick @@ -45,3 +52,18 @@ export const isFieldValid = ( export const isCollapseFunction = (candidate: string | undefined): candidate is CollapseFunction => Boolean(candidate && CollapseFunctions.includes(candidate as CollapseFunction)); + +const isColumnWithMeta = (column: Column): column is ColumnWithMeta => { + if ((column as ColumnWithMeta).meta) { + return true; + } + return false; +}; + +export const excludeMetaFromColumn = (column: Column) => { + if (isColumnWithMeta(column)) { + const { meta, ...rest } = column; + return rest; + } + return column; +}; diff --git a/x-pack/plugins/lens/public/visualizations/tagcloud/suggestions.ts b/x-pack/plugins/lens/public/visualizations/tagcloud/suggestions.ts index b4866bf95a407..c85f7b0b28fe2 100644 --- a/x-pack/plugins/lens/public/visualizations/tagcloud/suggestions.ts +++ b/x-pack/plugins/lens/public/visualizations/tagcloud/suggestions.ts @@ -11,7 +11,7 @@ import type { SuggestionRequest, VisualizationSuggestion } from '../../types'; import type { TagcloudState } from './types'; import { DEFAULT_STATE, TAGCLOUD_LABEL } from './constants'; -export function suggestions({ +export function getSuggestions({ table, state, keptLayerIds, diff --git a/x-pack/plugins/lens/public/visualizations/tagcloud/tagcloud_visualization.tsx b/x-pack/plugins/lens/public/visualizations/tagcloud/tagcloud_visualization.tsx index b74351bc185d8..09e988b445795 100644 --- a/x-pack/plugins/lens/public/visualizations/tagcloud/tagcloud_visualization.tsx +++ b/x-pack/plugins/lens/public/visualizations/tagcloud/tagcloud_visualization.tsx @@ -24,7 +24,7 @@ import { IconChartTagcloud } from '@kbn/chart-icons'; import { SystemPaletteExpressionFunctionDefinition } from '@kbn/charts-plugin/common'; import type { OperationMetadata, Visualization } from '../..'; import type { TagcloudState } from './types'; -import { suggestions } from './suggestions'; +import { getSuggestions } from './suggestions'; import { TagcloudToolbar } from './tagcloud_toolbar'; import { TagsDimensionEditor } from './tags_dimension_editor'; import { DEFAULT_STATE, TAGCLOUD_LABEL } from './constants'; @@ -79,7 +79,19 @@ export const getTagcloudVisualization = ({ }; }, - getSuggestions: suggestions, + getSuggestions, + + getSuggestionFromConvertToLensContext({ suggestions, context }) { + return !suggestions.length + ? undefined + : { + ...suggestions[0], + visualizationState: { + ...(suggestions[0].visualizationState as TagcloudState), + ...(context.configuration as unknown as TagcloudState), + }, + }; + }, triggers: [VIS_EVENT_TO_TRIGGER.filter],