Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] adds dimensionName to datatable column meta information (#106514) #107174

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
20 changes: 20 additions & 0 deletions src/plugins/vis_type_pie/public/__snapshots__/pie_fn.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/plugins/vis_type_pie/public/pie_fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
});
});
31 changes: 29 additions & 2 deletions src/plugins/vis_type_pie/public/pie_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/visualizations/common/expression_functions/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ExpressionValueBoxed,
Datatable,
DatatableColumn,
} from '../../../expressions/public';
} from '../../../expressions/common';

interface Arguments {
accessor: string | number;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/visualizations/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

/** @public types */
export * from './types';
export * from './prepare_log_table';
export * from './expression_functions';
74 changes: 74 additions & 0 deletions src/plugins/visualizations/common/prepare_log_table.test.ts
Original file line number Diff line number Diff line change
@@ -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",
},
},
],
}
`
);
});
});
35 changes: 35 additions & 0 deletions src/plugins/visualizations/common/prepare_log_table.ts
Original file line number Diff line number Diff line change
@@ -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),
},
};
}),
};
};
2 changes: 1 addition & 1 deletion src/plugins/visualizations/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/visualizations/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
4 changes: 2 additions & 2 deletions src/plugins/visualizations/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down