From 389e163a4f8cc17b2445b2d3ec216ecd0a65b2aa Mon Sep 17 00:00:00 2001 From: Peter Pisljar Date: Thu, 29 Jul 2021 16:45:10 +0200 Subject: [PATCH] adds dimensionName to datatable column meta information (#106514) --- .../expression_types/specs/datatable.ts | 4 + .../public/__snapshots__/pie_fn.test.ts.snap | 20 +++++ .../vis_type_pie/public/pie_fn.test.ts | 17 +++++ src/plugins/vis_type_pie/public/pie_fn.ts | 31 +++++++- .../common/expression_functions/index.ts | 10 +++ .../expression_functions/range.ts | 2 +- .../expression_functions/vis_dimension.ts | 2 +- src/plugins/visualizations/common/index.ts | 2 + .../common/prepare_log_table.test.ts | 74 +++++++++++++++++++ .../common/prepare_log_table.ts | 35 +++++++++ src/plugins/visualizations/kibana.json | 2 +- src/plugins/visualizations/public/index.ts | 4 +- src/plugins/visualizations/public/plugin.ts | 4 +- 13 files changed, 198 insertions(+), 9 deletions(-) create mode 100644 src/plugins/visualizations/common/expression_functions/index.ts rename src/plugins/visualizations/{public => common}/expression_functions/range.ts (97%) rename src/plugins/visualizations/{public => common}/expression_functions/vis_dimension.ts (98%) create mode 100644 src/plugins/visualizations/common/prepare_log_table.test.ts create mode 100644 src/plugins/visualizations/common/prepare_log_table.ts diff --git a/src/plugins/expressions/common/expression_types/specs/datatable.ts b/src/plugins/expressions/common/expression_types/specs/datatable.ts index 3fdb6bd27e0ba..a094ce39d6caa 100644 --- a/src/plugins/expressions/common/expression_types/specs/datatable.ts +++ b/src/plugins/expressions/common/expression_types/specs/datatable.ts @@ -69,6 +69,10 @@ export interface DatatableColumnMeta { * index/table this column is based on */ index?: string; + /** + * names the domain this column represents + */ + dimensionName?: string; /** * serialized field format */ diff --git a/src/plugins/vis_type_pie/public/__snapshots__/pie_fn.test.ts.snap b/src/plugins/vis_type_pie/public/__snapshots__/pie_fn.test.ts.snap index dc83d9fdf48ac..6c43072b97c28 100644 --- a/src/plugins/vis_type_pie/public/__snapshots__/pie_fn.test.ts.snap +++ b/src/plugins/vis_type_pie/public/__snapshots__/pie_fn.test.ts.snap @@ -1,5 +1,25 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`interpreter/functions#pie logs correct datatable to inspector 1`] = ` +Object { + "columns": Array [ + Object { + "id": "col-0-1", + "meta": Object { + "dimensionName": "Slice size", + }, + "name": "Count", + }, + ], + "rows": Array [ + Object { + "col-0-1": 0, + }, + ], + "type": "datatable", +} +`; + exports[`interpreter/functions#pie returns an object with the correct structure 1`] = ` Object { "as": "pie_vis", diff --git a/src/plugins/vis_type_pie/public/pie_fn.test.ts b/src/plugins/vis_type_pie/public/pie_fn.test.ts index d387d4035e8ab..3dcef406379c2 100644 --- a/src/plugins/vis_type_pie/public/pie_fn.test.ts +++ b/src/plugins/vis_type_pie/public/pie_fn.test.ts @@ -8,6 +8,7 @@ import { functionWrapper } from '../../expressions/common/expression_functions/specs/tests/utils'; import { createPieVisFn } from './pie_fn'; +import { Datatable } from '../../expressions/common/expression_types/specs'; describe('interpreter/functions#pie', () => { const fn = functionWrapper(createPieVisFn()); @@ -50,4 +51,20 @@ describe('interpreter/functions#pie', () => { const actual = await fn(context, visConfig); expect(actual).toMatchSnapshot(); }); + + it('logs correct datatable to inspector', async () => { + let loggedTable: Datatable; + const handlers = { + inspectorAdapters: { + tables: { + logDatatable: (name: string, datatable: Datatable) => { + loggedTable = datatable; + }, + }, + }, + }; + await fn(context, visConfig, handlers as any); + + expect(loggedTable!).toMatchSnapshot(); + }); }); diff --git a/src/plugins/vis_type_pie/public/pie_fn.ts b/src/plugins/vis_type_pie/public/pie_fn.ts index 1b5b8574f9311..65ac648ca2868 100644 --- a/src/plugins/vis_type_pie/public/pie_fn.ts +++ b/src/plugins/vis_type_pie/public/pie_fn.ts @@ -7,8 +7,9 @@ */ import { i18n } from '@kbn/i18n'; -import { ExpressionFunctionDefinition, Datatable, Render } from '../../expressions/public'; +import { ExpressionFunctionDefinition, Datatable, Render } from '../../expressions/common'; import { PieVisParams, PieVisConfig } from './types'; +import { prepareLogTable } from '../../visualizations/public'; export const vislibPieName = 'pie_vis'; @@ -133,7 +134,33 @@ export const createPieVisFn = (): VisTypePieExpressionFunctionDefinition => ({ } as PieVisParams; if (handlers?.inspectorAdapters?.tables) { - handlers.inspectorAdapters.tables.logDatatable('default', context); + const logTable = prepareLogTable(context, [ + [ + [args.metric], + i18n.translate('visTypePie.function.dimension.metric', { + defaultMessage: 'Slice size', + }), + ], + [ + args.buckets, + i18n.translate('visTypePie.function.adimension.buckets', { + defaultMessage: 'Slice', + }), + ], + [ + args.splitColumn, + i18n.translate('visTypePie.function.dimension.splitcolumn', { + defaultMessage: 'Column split', + }), + ], + [ + args.splitRow, + i18n.translate('visTypePie.function.dimension.splitrow', { + defaultMessage: 'Row split', + }), + ], + ]); + handlers.inspectorAdapters.tables.logDatatable('default', logTable); } return { diff --git a/src/plugins/visualizations/common/expression_functions/index.ts b/src/plugins/visualizations/common/expression_functions/index.ts new file mode 100644 index 0000000000000..123fa28f18338 --- /dev/null +++ b/src/plugins/visualizations/common/expression_functions/index.ts @@ -0,0 +1,10 @@ +/* + * 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. + */ + +export * from './range'; +export * from './vis_dimension'; diff --git a/src/plugins/visualizations/public/expression_functions/range.ts b/src/plugins/visualizations/common/expression_functions/range.ts similarity index 97% rename from src/plugins/visualizations/public/expression_functions/range.ts rename to src/plugins/visualizations/common/expression_functions/range.ts index c5408a399e235..04751ef01985e 100644 --- a/src/plugins/visualizations/public/expression_functions/range.ts +++ b/src/plugins/visualizations/common/expression_functions/range.ts @@ -7,7 +7,7 @@ */ import { i18n } from '@kbn/i18n'; -import { ExpressionFunctionDefinition, Datatable, Range } from '../../../expressions/public'; +import { ExpressionFunctionDefinition, Datatable, Range } from '../../../expressions/common'; interface Arguments { from: number; diff --git a/src/plugins/visualizations/public/expression_functions/vis_dimension.ts b/src/plugins/visualizations/common/expression_functions/vis_dimension.ts similarity index 98% rename from src/plugins/visualizations/public/expression_functions/vis_dimension.ts rename to src/plugins/visualizations/common/expression_functions/vis_dimension.ts index cc29ee6ea3987..6886fa94f878e 100644 --- a/src/plugins/visualizations/public/expression_functions/vis_dimension.ts +++ b/src/plugins/visualizations/common/expression_functions/vis_dimension.ts @@ -12,7 +12,7 @@ import { ExpressionValueBoxed, Datatable, DatatableColumn, -} from '../../../expressions/public'; +} from '../../../expressions/common'; interface Arguments { accessor: string | number; diff --git a/src/plugins/visualizations/common/index.ts b/src/plugins/visualizations/common/index.ts index c93e0ac3cda5a..bde292cb69697 100644 --- a/src/plugins/visualizations/common/index.ts +++ b/src/plugins/visualizations/common/index.ts @@ -8,3 +8,5 @@ /** @public types */ export * from './types'; +export * from './prepare_log_table'; +export * from './expression_functions'; diff --git a/src/plugins/visualizations/common/prepare_log_table.test.ts b/src/plugins/visualizations/common/prepare_log_table.test.ts new file mode 100644 index 0000000000000..dc02adbd458ee --- /dev/null +++ b/src/plugins/visualizations/common/prepare_log_table.test.ts @@ -0,0 +1,74 @@ +/* + * 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 { prepareLogTable } from './prepare_log_table'; + +describe('prepareLogTable', () => { + test('returns first matching dimension name', () => { + const datatable = { + columns: [ + { + meta: {}, + }, + { + meta: {}, + }, + { + meta: {}, + }, + ], + }; + const logTable = prepareLogTable(datatable as any, [ + [[{ accessor: 0 } as any], 'dimension1'], + [[{ accessor: 2 } as any], 'dimension3'], + [[{ accessor: 1 } as any], 'dimension2'], + ]); + expect(logTable).toMatchInlineSnapshot( + { + columns: [ + { + meta: { + dimensionName: 'dimension1', + }, + }, + { + meta: { + dimensionName: 'dimension2', + }, + }, + { + meta: { + dimensionName: 'dimension3', + }, + }, + ], + }, + ` + Object { + "columns": Array [ + Object { + "meta": Object { + "dimensionName": "dimension1", + }, + }, + Object { + "meta": Object { + "dimensionName": "dimension2", + }, + }, + Object { + "meta": Object { + "dimensionName": "dimension3", + }, + }, + ], + } + ` + ); + }); +}); diff --git a/src/plugins/visualizations/common/prepare_log_table.ts b/src/plugins/visualizations/common/prepare_log_table.ts new file mode 100644 index 0000000000000..f179eb8a1c10a --- /dev/null +++ b/src/plugins/visualizations/common/prepare_log_table.ts @@ -0,0 +1,35 @@ +/* + * 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 { ExpressionValueVisDimension } from './expression_functions/vis_dimension'; +import { Datatable } from '../../expressions/common/expression_types/specs'; + +export type Dimension = [ExpressionValueVisDimension[] | undefined, string]; + +const getDimensionName = (columnIndex: number, dimensions: Dimension[]) => { + for (const dimension of dimensions) { + if (dimension[0]?.find((d) => d.accessor === columnIndex)) { + return dimension[1]; + } + } +}; + +export const prepareLogTable = (datatable: Datatable, dimensions: Dimension[]) => { + return { + ...datatable, + columns: datatable.columns.map((column, columnIndex) => { + return { + ...column, + meta: { + ...column.meta, + dimensionName: getDimensionName(columnIndex, dimensions), + }, + }; + }), + }; +}; diff --git a/src/plugins/visualizations/kibana.json b/src/plugins/visualizations/kibana.json index aa78e45481aa4..daf2c90c2eb79 100644 --- a/src/plugins/visualizations/kibana.json +++ b/src/plugins/visualizations/kibana.json @@ -13,7 +13,7 @@ ], "optionalPlugins": ["usageCollection"], "requiredBundles": ["kibanaUtils", "discover"], - "extraPublicDirs": ["common/constants"], + "extraPublicDirs": ["common/constants", "common/prepare_log_table", "common/expression_functions"], "owner": { "name": "Kibana App", "githubTeam": "kibana-app" diff --git a/src/plugins/visualizations/public/index.ts b/src/plugins/visualizations/public/index.ts index 081023d0039e7..a01a2477dd311 100644 --- a/src/plugins/visualizations/public/index.ts +++ b/src/plugins/visualizations/public/index.ts @@ -41,5 +41,5 @@ export { } from './types'; export { VisualizationListItem, VisualizationStage } from './vis_types/vis_type_alias_registry'; export { VISUALIZE_ENABLE_LABS_SETTING } from '../common/constants'; -export { SavedVisState, VisParams } from '../common'; -export { ExpressionValueVisDimension } from './expression_functions/vis_dimension'; +export { SavedVisState, VisParams, prepareLogTable } from '../common'; +export { ExpressionValueVisDimension } from '../common/expression_functions/vis_dimension'; diff --git a/src/plugins/visualizations/public/plugin.ts b/src/plugins/visualizations/public/plugin.ts index 17dcb28e15e5d..e3dcf79a63a76 100644 --- a/src/plugins/visualizations/public/plugin.ts +++ b/src/plugins/visualizations/public/plugin.ts @@ -31,8 +31,8 @@ import { createVisEmbeddableFromObject, } from './embeddable'; import { TypesService } from './vis_types/types_service'; -import { range as rangeExpressionFunction } from './expression_functions/range'; -import { visDimension as visDimensionExpressionFunction } from './expression_functions/vis_dimension'; +import { range as rangeExpressionFunction } from '../common/expression_functions/range'; +import { visDimension as visDimensionExpressionFunction } from '../common/expression_functions/vis_dimension'; import { createStartServicesGetter, StartServicesGetter } from '../../kibana_utils/public'; import { createSavedVisLoader, SavedVisualizationsLoader } from './saved_visualizations';