Skip to content

Commit

Permalink
Expose NP FieldFormats service to server side (#55419)
Browse files Browse the repository at this point in the history
* Expose NP FieldFormats service to server side

* fix CI

* fix PR comments

* fix PR comments

* fix CI

* getFieldFormatsRegistry -> getFieldFormatRegistry

* fix CI

* memoize - add resolve cache function

* fix Jest

* move IFieldFormatMetaParams to types.ts

* FieldFormatRegistry -> FieldFormatsRegistry

* update src/core/MIGRATION.md

* update public contract

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
alexwizp and elasticmachine authored Jan 27, 2020
1 parent 9301531 commit 5d6dbf0
Show file tree
Hide file tree
Showing 70 changed files with 525 additions and 741 deletions.
10 changes: 5 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@ module.exports = {
{
target: [
'(src|x-pack)/plugins/**/*',
'!(src|x-pack)/plugins/*/server/**/*',
'!(src|x-pack)/plugins/**/server/**/*',

'src/legacy/core_plugins/**/*',
'!src/legacy/core_plugins/*/server/**/*',
'!src/legacy/core_plugins/*/index.{js,ts,tsx}',
'!src/legacy/core_plugins/**/server/**/*',
'!src/legacy/core_plugins/**/index.{js,ts,tsx}',

'x-pack/legacy/plugins/**/*',
'!x-pack/legacy/plugins/*/server/**/*',
'!x-pack/legacy/plugins/*/index.{js,ts,tsx}',
'!x-pack/legacy/plugins/**/server/**/*',
'!x-pack/legacy/plugins/**/index.{js,ts,tsx}',

'examples/**/*',
'!examples/**/server/**/*',
Expand Down
4 changes: 2 additions & 2 deletions src/core/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1231,8 +1231,8 @@ This table shows where these uiExports have moved to in the New Platform. In mos
| `docViews` | | |
| `embeddableActions` | | Should be an API on the embeddables plugin. |
| `embeddableFactories` | | Should be an API on the embeddables plugin. |
| `fieldFormatEditors` | | |
| `fieldFormats` | | |
| `fieldFormatEditors` | | |
| `fieldFormats` | [`plugins.data.fieldFormats`](./src/plugins/data/public/field_formats) | |
| `hacks` | n/a | Just run the code in your plugin's `start` method. |
| `home` | [`plugins.home.featureCatalogue.register`](./src/plugins/home/public/feature_catalogue) | Must add `home` as a dependency in your kibana.json. |
| `indexManagement` | | Should be an API on the indexManagement plugin. |
Expand Down
2 changes: 0 additions & 2 deletions src/legacy/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { migrations } from './migrations';
import { importApi } from './server/routes/api/import';
import { exportApi } from './server/routes/api/export';
import { managementApi } from './server/routes/api/management';
import { registerFieldFormats } from './server/field_formats/register';
import * as systemApi from './server/lib/system_api';
import mappings from './mappings.json';
import { getUiSettingDefaults } from './ui_setting_defaults';
Expand Down Expand Up @@ -331,7 +330,6 @@ export default function(kibana) {
importApi(server);
exportApi(server);
managementApi(server);
registerFieldFormats(server);
registerCspCollector(usageCollection, server);
server.expose('systemApi', systemApi);
server.injectUiAppVars('kibana', () => injectVars(server));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { isColorDark } from '@elastic/eui';

import { getHeatmapColors, getFormat, Vis } from '../legacy_imports';
import { MetricVisValue } from './metric_vis_value';
import { FieldFormat, ContentType } from '../../../../../plugins/data/public';
import { fieldFormats } from '../../../../../plugins/data/public';
import { Context } from '../metric_vis_fn';
import { KibanaDatatable } from '../../../../../plugins/expressions/public';
import { VisParams, MetricVisMetric } from '../types';
Expand Down Expand Up @@ -100,9 +100,9 @@ export class MetricVisComponent extends Component<MetricVisComponentProps> {
}

private getFormattedValue = (
fieldFormatter: FieldFormat,
fieldFormatter: fieldFormats.FieldFormat,
value: any,
format: ContentType = 'text'
format: fieldFormats.ContentType = 'text'
) => {
if (isNaN(value)) return '-';
return fieldFormatter.convert(value, format);
Expand All @@ -119,7 +119,7 @@ export class MetricVisComponent extends Component<MetricVisComponentProps> {
const metrics: MetricVisMetric[] = [];

let bucketColumnId: string;
let bucketFormatter: FieldFormat;
let bucketFormatter: fieldFormats.FieldFormat;

if (dimensions.bucket) {
bucketColumnId = table.columns[dimensions.bucket.accessor].id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { npStart } from 'ui/new_platform';
import getStubIndexPattern from 'fixtures/stubbed_logstash_index_pattern';

import { Vis } from '../../visualizations/public';
import { UrlFormat } from '../../../../plugins/data/public';
import { fieldFormats } from '../../../../plugins/data/public';
import {
setup as visualizationsSetup,
start as visualizationsStart,
Expand All @@ -39,7 +39,7 @@ describe('metric_vis - createMetricVisTypeDefinition', () => {
beforeAll(() => {
visualizationsSetup.types.createReactVisualization(metricVisTypeDefinition);
(npStart.plugins.data.fieldFormats.getType as jest.Mock).mockImplementation(() => {
return UrlFormat;
return fieldFormats.UrlFormat;
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { calculateLabel } from '../../../../common/calculate_label';
import { isSortable } from './is_sortable';
import { EuiToolTip, EuiIcon } from '@elastic/eui';
import { replaceVars } from '../../lib/replace_vars';
import { FIELD_FORMAT_IDS } from '../../../../../../../plugins/data/public';
import { fieldFormats } from '../../../../../../../plugins/data/public';
import { FormattedMessage } from '@kbn/i18n/react';

import { METRIC_TYPES } from '../../../../common/metric_types';
Expand All @@ -49,8 +49,8 @@ export class TableVis extends Component {
constructor(props) {
super(props);

const fieldFormats = npStart.plugins.data.fieldFormats;
const DateFormat = fieldFormats.getType(FIELD_FORMAT_IDS.DATE);
const fieldFormatsService = npStart.plugins.data.fieldFormats;
const DateFormat = fieldFormatsService.getType(fieldFormats.FIELD_FORMAT_IDS.DATE);

this.dateFormatter = new DateFormat({}, this.props.getConfig);
}
Expand Down
20 changes: 0 additions & 20 deletions src/legacy/ui/field_formats/index.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/legacy/ui/field_formats/mixin/field_formats_mixin.ts

This file was deleted.

56 changes: 0 additions & 56 deletions src/legacy/ui/field_formats/mixin/field_formats_service.test.ts

This file was deleted.

79 changes: 0 additions & 79 deletions src/legacy/ui/field_formats/mixin/field_formats_service.ts

This file was deleted.

12 changes: 6 additions & 6 deletions src/legacy/ui/public/agg_types/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import { AggConfigs } from './agg_configs';
import { Schema } from '../vis/editors/default/schemas';
import {
ISearchSource,
ContentType,
KBN_FIELD_TYPES,
FetchOptions,
fieldFormats,
KBN_FIELD_TYPES,
} from '../../../../plugins/data/public';

export interface AggConfigOptions {
Expand Down Expand Up @@ -375,7 +375,7 @@ export class AggConfig {
return this.aggConfigs.timeRange;
}

fieldFormatter(contentType?: ContentType, defaultFormat?: any) {
fieldFormatter(contentType?: fieldFormats.ContentType, defaultFormat?: any) {
const format = this.type && this.type.getFormat(this);

if (format) {
Expand All @@ -385,12 +385,12 @@ export class AggConfig {
return this.fieldOwnFormatter(contentType, defaultFormat);
}

fieldOwnFormatter(contentType?: ContentType, defaultFormat?: any) {
const fieldFormats = npStart.plugins.data.fieldFormats;
fieldOwnFormatter(contentType?: fieldFormats.ContentType, defaultFormat?: any) {
const fieldFormatsService = npStart.plugins.data.fieldFormats;
const field = this.getField();
let format = field && field.format;
if (!format) format = defaultFormat;
if (!format) format = fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING);
if (!format) format = fieldFormatsService.getDefaultInstance(KBN_FIELD_TYPES.STRING);
return format.getConverterFor(contentType);
}

Expand Down
10 changes: 5 additions & 5 deletions src/legacy/ui/public/agg_types/agg_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { AggConfigs } from './agg_configs';
import { Adapters } from '../inspector';
import { BaseParamType } from './param_types/base';
import { AggParamType } from '../agg_types/param_types/agg';
import { KBN_FIELD_TYPES, FieldFormat, ISearchSource } from '../../../../plugins/data/public';
import { KBN_FIELD_TYPES, fieldFormats, ISearchSource } from '../../../../plugins/data/public';

export interface AggTypeConfig<
TAggConfig extends AggConfig = AggConfig,
Expand All @@ -54,16 +54,16 @@ export interface AggTypeConfig<
inspectorAdapters: Adapters,
abortSignal?: AbortSignal
) => Promise<any>;
getFormat?: (agg: TAggConfig) => FieldFormat;
getFormat?: (agg: TAggConfig) => fieldFormats.FieldFormat;
getValue?: (agg: TAggConfig, bucket: any) => any;
getKey?: (bucket: any, key: any, agg: TAggConfig) => any;
}

const getFormat = (agg: AggConfig) => {
const field = agg.getField();
const fieldFormats = npStart.plugins.data.fieldFormats;
const fieldFormatsService = npStart.plugins.data.fieldFormats;

return field ? field.format : fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING);
return field ? field.format : fieldFormatsService.getDefaultInstance(KBN_FIELD_TYPES.STRING);
};

export class AggType<
Expand Down Expand Up @@ -191,7 +191,7 @@ export class AggType<
* @param {agg} agg - the agg to pick a format for
* @return {FieldFormat}
*/
getFormat: (agg: TAggConfig) => FieldFormat;
getFormat: (agg: TAggConfig) => fieldFormats.FieldFormat;

getValue: (agg: TAggConfig, bucket: any) => any;

Expand Down
Loading

0 comments on commit 5d6dbf0

Please sign in to comment.