Skip to content

Commit

Permalink
[Table Vis] Shim new platform - table_vis_fn.js -> table_vis_fn.ts , …
Browse files Browse the repository at this point in the history
…use ExpressionFunction (#42585)

* [Table Vis] Shim new platform - table_vis_fn.js -> table_vis_fn.ts , use ExpressionFunction

* fix PR comments
  • Loading branch information
alexwizp authored Aug 7, 2019
1 parent 8b57570 commit 08bd1ad
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 37 deletions.
6 changes: 2 additions & 4 deletions src/legacy/core_plugins/vis_type_table/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ import {

import { LegacyDependenciesPluginSetup, LegacyDependenciesPlugin } from './shim';

// @ts-ignore
import { createTableVisFn } from './table_vis_fn';
// @ts-ignore
import { createTableVisTypeDefinition } from './table_vis_type';

/** @private */
/** @internal */
export interface TableVisualizationDependencies extends LegacyDependenciesPluginSetup {
uiSettings: UiSettingsClientContract;
}
Expand Down Expand Up @@ -63,7 +61,7 @@ export class TableVisPlugin implements Plugin<Promise<void>, void> {
...(await __LEGACY.setup()),
};

data.expressions.registerFunction(() => createTableVisFn(visualizationDependencies));
data.expressions.registerFunction(createTableVisFn);

visualizations.types.VisTypesRegistryProvider.register(() =>
createTableVisTypeDefinition(visualizationDependencies)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
* under the License.
*/

import { functionWrapper } from '../../interpreter/test_helpers';
import { createTableVisFn } from './table_vis_fn';

// @ts-ignore
import { functionWrapper } from '../../interpreter/test_helpers';

jest.mock('ui/new_platform');

const mockResponseHandler = jest.fn().mockReturnValue(Promise.resolve({
tables: [{ columns: [], rows: [] }],
}));
const mockResponseHandler = jest.fn().mockReturnValue(
Promise.resolve({
tables: [{ columns: [], rows: [] }],
})
);

jest.mock('ui/vis/response_handlers/legacy', () => ({
legacyResponseHandlerProvider: () => ({ handler: mockResponseHandler }),
}));
Expand All @@ -42,7 +47,7 @@ describe('interpreter/functions#table', () => {
showMetricsAtAllLevels: false,
sort: {
columnIndex: null,
direction: null
direction: null,
},
showTotal: false,
totalFunc: 'sum',
Expand All @@ -51,14 +56,14 @@ describe('interpreter/functions#table', () => {
{
accessor: 0,
format: {
id: 'number'
id: 'number',
},
params: {},
aggType: 'count'
}
aggType: 'count',
},
],
buckets: []
}
buckets: [],
},
};

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,53 @@
import { i18n } from '@kbn/i18n';
import { createTableVisResponseHandler } from './table_vis_request_handler';

export const createTableVisFn = (dependencies) => ({
name: 'kibana_table',
import { ExpressionFunction, KibanaDatatable, Render } from '../../interpreter/types';

const name = 'kibana_table';

type Context = KibanaDatatable;

interface Arguments {
visConfig: string | null;
}

type VisParams = Required<Arguments>;

interface RenderValue {
visData: Context;
visType: 'table';
visConfig: VisParams;
params: {
listenOnChange: boolean;
};
}

type Return = Promise<Render<RenderValue>>;

export const createTableVisFn = (): ExpressionFunction<
typeof name,
Context,
Arguments,
Return
> => ({
name,
type: 'render',
context: {
types: [
'kibana_datatable'
],
types: ['kibana_datatable'],
},
help: i18n.translate('visTypeTable.function.help', {
defaultMessage: 'Table visualization'
defaultMessage: 'Table visualization',
}),
args: {
visConfig: {
types: ['string', 'null'],
default: '"{}"',
help: '',
},
},
async fn(context, args) {
const visConfig = JSON.parse(args.visConfig);
const responseHandler = createTableVisResponseHandler(dependencies);
const visConfig = args.visConfig && JSON.parse(args.visConfig);
const responseHandler = createTableVisResponseHandler();
const convertedData = await responseHandler(context, visConfig.dimensions);

return {
Expand All @@ -51,7 +78,7 @@ export const createTableVisFn = (dependencies) => ({
visConfig,
params: {
listenOnChange: true,
}
},
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

// @ts-ignore
import { legacyResponseHandlerProvider } from 'ui/vis/response_handlers/legacy';

export const createTableVisResponseHandler = () => legacyResponseHandlerProvider().handler;
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
*/

import { i18n } from '@kbn/i18n';
import { createTableVisResponseHandler } from './table_vis_request_handler';
import { Vis } from 'ui/vis';

// @ts-ignore
import { Schemas } from 'ui/vis/editors/default/schemas';
import { createTableVisResponseHandler } from './table_vis_request_handler';

import { TableVisualizationDependencies } from './plugin';
import tableVisTemplate from './table_vis.html';

export const createTableVisTypeDefinition = (dependencies) => {
const responseHandler = createTableVisResponseHandler(dependencies);
export const createTableVisTypeDefinition = (dependencies: TableVisualizationDependencies) => {
const responseHandler = createTableVisResponseHandler();

return dependencies.createAngularVisualization({
type: 'table',
Expand All @@ -43,7 +47,7 @@ export const createTableVisTypeDefinition = (dependencies) => {
showMetricsAtAllLevels: false,
sort: {
columnIndex: null,
direction: null
direction: null,
},
showTotal: false,
totalFunc: 'sum',
Expand All @@ -67,17 +71,15 @@ export const createTableVisTypeDefinition = (dependencies) => {
},
},
min: 1,
defaults: [
{ type: 'count', schema: 'metric' }
]
defaults: [{ type: 'count', schema: 'metric' }],
},
{
group: 'buckets',
name: 'bucket',
title: i18n.translate('visTypeTable.tableVisEditorConfig.schemas.bucketTitle', {
defaultMessage: 'Split rows',
}),
aggFilter: ['!filter']
aggFilter: ['!filter'],
},
{
group: 'buckets',
Expand All @@ -87,14 +89,13 @@ export const createTableVisTypeDefinition = (dependencies) => {
}),
min: 0,
max: 1,
aggFilter: ['!filter']
}
])
aggFilter: ['!filter'],
},
]),
},
responseHandler,
hierarchicalData: function (vis) {
hierarchicalData: (vis: Vis) => {
return Boolean(vis.params.showPartialRows || vis.params.showMetricsAtAllLevels);
}
},
});
};

0 comments on commit 08bd1ad

Please sign in to comment.