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

Shim getFormat function correctly #60032

Merged
merged 9 commits into from
Mar 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ export {
EsQuerySortValue,
SortDirection,
} from '../../../../../plugins/data/public';
export { getFormat } from 'ui/visualize/loader/pipeline_helpers/utilities';
// @ts-ignore
export { buildPointSeriesData } from 'ui/agg_response/point_series/point_series';
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { buildPointSeriesData, getFormat } from '../../kibana_services';
import { buildPointSeriesData, getServices } from '../../kibana_services';

function tableResponseHandler(table, dimensions) {
const converted = { tables: [] };
Expand All @@ -26,7 +26,7 @@ function tableResponseHandler(table, dimensions) {
if (split) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: I think we can further cleanup the discover part, since it doesn't use splitColumn, splitRow

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however, doesn't need to be in this PR, just loud thinking :)

converted.direction = dimensions.splitRow ? 'row' : 'column';
const splitColumnIndex = split[0].accessor;
const splitColumnFormatter = getFormat(split[0].format);
const splitColumnFormatter = getServices().data.fieldFormats.deserialize(split[0].format);
const splitColumn = table.columns[splitColumnIndex];
const splitMap = {};
let splitIndex = 0;
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import React from 'react';
import { shallow } from 'enzyme';

import { MetricVisComponent, MetricVisComponentProps } from './metric_vis_component';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { npStart } from 'ui/new_platform';
import { fieldFormats } from '../../../../../plugins/data/public';
import { identity } from 'lodash';
import { ExprVis } from '../../../visualizations/public/np_ready/public/expressions/vis';

jest.mock('ui/new_platform');
jest.mock('../services', () => ({
getFormatService: () => ({
deserialize: () => {
return {
convert: (x: unknown) => x,
};
},
}),
}));

type Props = MetricVisComponentProps;

Expand Down Expand Up @@ -66,12 +70,6 @@ describe('MetricVisComponent', function() {
return shallow(<MetricVisComponent {...props} />);
};

beforeAll(() => {
(npStart.plugins.data.fieldFormats.deserialize as jest.Mock).mockImplementation(() => {
return new (fieldFormats.FieldFormat.from(identity))();
});
});

it('should render component', () => {
expect(getComponent().exists()).toBe(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import { last, findIndex, isNaN } from 'lodash';
import React, { Component } from 'react';
import { isColorDark } from '@elastic/eui';
import { getFormat } from '../legacy_imports';
import { MetricVisValue } from './metric_vis_value';
import { Input } from '../metric_vis_fn';
import { FieldFormatsContentType, IFieldFormat } from '../../../../../plugins/data/public';
import { KibanaDatatable } from '../../../../../plugins/expressions/public';
import { getHeatmapColors } from '../../../../../plugins/charts/public';
import { VisParams, MetricVisMetric } from '../types';
import { getFormatService } from '../services';
import { SchemaConfig } from '../../../visualizations/public';
import { ExprVis } from '../../../visualizations/public/np_ready/public/expressions/vis';

Expand Down Expand Up @@ -122,13 +122,13 @@ export class MetricVisComponent extends Component<MetricVisComponentProps> {

if (dimensions.bucket) {
bucketColumnId = table.columns[dimensions.bucket.accessor].id;
bucketFormatter = getFormat(dimensions.bucket.format);
bucketFormatter = getFormatService().deserialize(dimensions.bucket.format);
}

dimensions.metrics.forEach((metric: SchemaConfig) => {
const columnIndex = metric.accessor;
const column = table?.columns[columnIndex];
const formatter = getFormat(metric.format);
const formatter = getFormatService().deserialize(metric.format);
table.rows.forEach((row, rowIndex) => {
let title = column.name;
let value: any = row[column.id];
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/vis_type_metric/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ const plugins: Readonly<MetricVisPluginSetupDependencies> = {
const pluginInstance = plugin({} as PluginInitializerContext);

export const setup = pluginInstance.setup(npSetup.core, plugins);
export const start = pluginInstance.start(npStart.core);
export const start = pluginInstance.start(npStart.core, { data: npStart.plugins.data });
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
import $ from 'jquery';

// TODO This is an integration test and thus requires a running platform. When moving to the new platform,
// this test has to be migrated to the newly created integration test environment.
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { npStart } from 'ui/new_platform';
// this test has to be migrated to a real unit test.
// @ts-ignore
import getStubIndexPattern from 'fixtures/stubbed_logstash_index_pattern';

import { Vis } from '../../visualizations/public';
import { fieldFormats } from '../../../../plugins/data/public';
import {
setup as visualizationsSetup,
start as visualizationsStart,
Expand All @@ -36,6 +33,16 @@ import { createMetricVisTypeDefinition } from './metric_vis_type';

jest.mock('ui/new_platform');

jest.mock('./services', () => ({
getFormatService: () => ({
deserialize: () => {
return {
convert: (x: unknown) => `<a href="http://ip.info?address={{${x}}">ip[${x}]</a>`,
};
},
}),
}));

jest.mock('../../vis_default_editor/public', () => ({
Schemas: class {},
}));
Expand All @@ -45,12 +52,6 @@ describe('metric_vis - createMetricVisTypeDefinition', () => {

beforeAll(() => {
visualizationsSetup.createReactVisualization(createMetricVisTypeDefinition());
(npStart.plugins.data.fieldFormats.getType as jest.Mock).mockImplementation(() => {
return fieldFormats.UrlFormat;
});
(npStart.plugins.data.fieldFormats.deserialize as jest.Mock).mockImplementation(mapping => {
return new fieldFormats.UrlFormat(mapping ? mapping.params : {});
});
});

const setup = () => {
Expand Down
11 changes: 9 additions & 2 deletions src/legacy/core_plugins/vis_type_metric/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { VisualizationsSetup } from '../../visualizations/public';
import { createMetricVisFn } from './metric_vis_fn';
import { createMetricVisTypeDefinition } from './metric_vis_type';
import { ChartsPluginSetup } from '../../../../plugins/charts/public';
import { DataPublicPluginStart } from '../../../../plugins/data/public';
import { setFormatService } from './services';

/** @internal */
export interface MetricVisPluginSetupDependencies {
Expand All @@ -32,6 +34,11 @@ export interface MetricVisPluginSetupDependencies {
charts: ChartsPluginSetup;
}

/** @internal */
export interface MetricVisPluginStartDependencies {
data: DataPublicPluginStart;
}

/** @internal */
export class MetricVisPlugin implements Plugin<void, void> {
initializerContext: PluginInitializerContext;
Expand All @@ -48,7 +55,7 @@ export class MetricVisPlugin implements Plugin<void, void> {
visualizations.createReactVisualization(createMetricVisTypeDefinition());
}

public start(core: CoreStart) {
// nothing to do here yet
public start(core: CoreStart, { data }: MetricVisPluginStartDependencies) {
setFormatService(data.fieldFormats);
}
}
25 changes: 25 additions & 0 deletions src/legacy/core_plugins/vis_type_metric/public/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { createGetterSetter } from '../../../../plugins/kibana_utils/common';
import { DataPublicPluginStart } from '../../../../plugins/data/public';

export const [getFormatService, setFormatService] = createGetterSetter<
DataPublicPluginStart['fieldFormats']
>('metric data.fieldFormats');
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import _ from 'lodash';
import aggTableTemplate from './agg_table.html';
import { getFormat } from '../legacy_imports';
import { getFormatService } from '../services';
import { i18n } from '@kbn/i18n';

export function KbnAggTable(config, RecursionHelper) {
Expand Down Expand Up @@ -127,7 +127,7 @@ export function KbnAggTable(config, RecursionHelper) {

if (!dimension) return;

const formatter = getFormat(dimension.format);
const formatter = getFormatService().deserialize(dimension.format);

const formattedColumn = {
id: col.id,
Expand Down Expand Up @@ -247,7 +247,7 @@ export function KbnAggTable(config, RecursionHelper) {
function addPercentageCol(columns, title, rows, insertAtIndex) {
const { id, sumTotal } = columns[insertAtIndex];
const newId = `${id}-percents`;
const formatter = getFormat({ id: 'percent' });
const formatter = getFormatService().deserialize({ id: 'percent' });
const i18nTitle = i18n.translate('visTypeTable.params.percentageTableColumnName', {
defaultMessage: '{title} percentages',
values: { title },
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/vis_type_table/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ const plugins: Readonly<TablePluginSetupDependencies> = {
const pluginInstance = plugin({} as PluginInitializerContext);

export const setup = pluginInstance.setup(npSetup.core, plugins);
export const start = pluginInstance.start(npStart.core);
export const start = pluginInstance.start(npStart.core, { data: npStart.plugins.data });
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@
*/

export { npSetup, npStart } from 'ui/new_platform';
export { getFormat } from 'ui/visualize/loader/pipeline_helpers/utilities';
11 changes: 9 additions & 2 deletions src/legacy/core_plugins/vis_type_table/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../..

import { createTableVisFn } from './table_vis_fn';
import { tableVisTypeDefinition } from './table_vis_type';
import { DataPublicPluginStart } from '../../../../plugins/data/public';
import { setFormatService } from './services';

/** @internal */
export interface TablePluginSetupDependencies {
expressions: ReturnType<ExpressionsPublicPlugin['setup']>;
visualizations: VisualizationsSetup;
}

/** @internal */
export interface TablePluginStartDependencies {
data: DataPublicPluginStart;
}

/** @internal */
export class TableVisPlugin implements Plugin<Promise<void>, void> {
initializerContext: PluginInitializerContext;
Expand All @@ -47,7 +54,7 @@ export class TableVisPlugin implements Plugin<Promise<void>, void> {
visualizations.createBaseVisualization(tableVisTypeDefinition);
}

public start(core: CoreStart) {
// nothing to do here yet
public start(core: CoreStart, { data }: TablePluginStartDependencies) {
setFormatService(data.fieldFormats);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@
* under the License.
*/

export { getFormat } from 'ui/visualize/loader/pipeline_helpers/utilities';
import { createGetterSetter } from '../../../../plugins/kibana_utils/common';
import { DataPublicPluginStart } from '../../../../plugins/data/public';

export const [getFormatService, setFormatService] = createGetterSetter<
DataPublicPluginStart['fieldFormats']
>('table data.fieldFormats');
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { Required } from '@kbn/utility-types';

import { getFormat } from './legacy_imports';
import { getFormatService } from './services';
import { Input } from './table_vis_fn';

export interface TableContext {
Expand Down Expand Up @@ -54,7 +54,7 @@ export function tableVisResponseHandler(table: Input, dimensions: any): TableCon
if (split) {
converted.direction = dimensions.splitRow ? 'row' : 'column';
const splitColumnIndex = split[0].accessor;
const splitColumnFormatter = getFormat(split[0].format);
const splitColumnFormatter = getFormatService().deserialize(split[0].format);
const splitColumn = table.columns[splitColumnIndex];
const splitMap = {};
let splitIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { take } from 'rxjs/operators';
import { render, unmountComponentAtNode } from 'react-dom';
import { I18nProvider } from '@kbn/i18n/react';

import { getFormat } from '../legacy_imports';
import { getFormatService } from '../services';

import { Label } from './label';
import { TagCloud } from './tag_cloud';
Expand Down Expand Up @@ -118,7 +118,7 @@ export function createTagCloudVisualization({ colors }) {

const bucket = this._visParams.bucket;
const metric = this._visParams.metric;
const bucketFormatter = bucket ? getFormat(bucket.format) : null;
const bucketFormatter = bucket ? getFormatService().deserialize(bucket.format) : null;
const tagColumn = bucket ? data.columns[bucket.accessor].id : -1;
const metricColumn = data.columns[metric.accessor].id;
const tags = data.rows.map((row, rowIndex) => {
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/vis_type_tagcloud/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ const plugins: Readonly<TagCloudPluginSetupDependencies> = {
const pluginInstance = plugin({} as PluginInitializerContext);

export const setup = pluginInstance.setup(npSetup.core, plugins);
export const start = pluginInstance.start(npStart.core);
export const start = pluginInstance.start(npStart.core, { data: npStart.plugins.data });
Loading