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

Expose NP FieldFormats service to server side #55419

Merged
merged 27 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
08c845d
Expose NP FieldFormats service to server side
alexwizp Jan 21, 2020
7752b6e
fix CI
alexwizp Jan 22, 2020
02df835
Merge branch 'master' into 54571
elasticmachine Jan 22, 2020
e86b069
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 22, 2020
0be4619
fix PR comments
alexwizp Jan 22, 2020
b763a66
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 23, 2020
9bb6300
fix PR comments
alexwizp Jan 23, 2020
cd12136
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 23, 2020
0b1985f
fix CI
alexwizp Jan 23, 2020
73ee1e6
getFieldFormatsRegistry -> getFieldFormatRegistry
alexwizp Jan 23, 2020
8162165
Merge branch 'master' into 54571
alexwizp Jan 23, 2020
380a921
fix CI
alexwizp Jan 23, 2020
9f0cf97
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 23, 2020
223738e
memoize - add resolve cache function
alexwizp Jan 23, 2020
81414fb
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 23, 2020
1aa0097
fix Jest
alexwizp Jan 23, 2020
3d4adc3
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 23, 2020
b25d81a
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 24, 2020
32388e4
move IFieldFormatMetaParams to types.ts
alexwizp Jan 24, 2020
d76ceb3
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 27, 2020
369b1c0
FieldFormatRegistry -> FieldFormatsRegistry
alexwizp Jan 27, 2020
91e6d12
update src/core/MIGRATION.md
alexwizp Jan 27, 2020
74d5735
update public contract
alexwizp Jan 27, 2020
b205ba6
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 27, 2020
523d4b5
Merge branch 'master' into 54571
elasticmachine Jan 27, 2020
7ff4c2d
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 27, 2020
5072086
Merge remote-tracking branch 'upstream/master' into 54571
alexwizp Jan 27, 2020
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
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/**/*',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

modified to add support for:
x-pack/legacy/plugins/reporting/export_types/csv/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
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 @@ -26,7 +26,6 @@ import { importApi } from './server/routes/api/import';
import { exportApi } from './server/routes/api/export';
import { homeApi } from './server/routes/api/home';
import { managementApi } from './server/routes/api/management';
import { registerFieldFormats } from './server/field_formats/register';
import { registerTutorials } from './server/tutorials/register';
import * as systemApi from './server/lib/system_api';
import mappings from './mappings.json';
Expand Down Expand Up @@ -334,7 +333,6 @@ export default function(kibana) {
exportApi(server);
homeApi(server);
managementApi(server);
registerFieldFormats(server);
registerTutorials(server);
registerCspCollector(usageCollection, server);
server.expose('systemApi', systemApi);
Expand Down
54 changes: 0 additions & 54 deletions src/legacy/core_plugins/kibana/server/field_formats/register.js

This file was deleted.

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

This file was deleted.

2 changes: 0 additions & 2 deletions src/legacy/ui/ui_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import { fieldFormatsMixin } from './field_formats';
import { tutorialsMixin } from './tutorials_mixin';
import { uiAppsMixin } from './ui_apps';
import { uiBundlesMixin } from './ui_bundles';
Expand All @@ -28,7 +27,6 @@ export async function uiMixin(kbnServer) {
await kbnServer.mixin(uiAppsMixin);
await kbnServer.mixin(uiBundlesMixin);
await kbnServer.mixin(uiSettingsMixin);
await kbnServer.mixin(fieldFormatsMixin);
await kbnServer.mixin(tutorialsMixin);
await kbnServer.mixin(uiRenderMixin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { FieldFormatsService } from './field_formats_service';
import { FieldFormats } from './field_formats';
import { NumberFormat } from '../../../../plugins/data/public';

const getConfig = (key: string) => {
Expand All @@ -33,22 +33,22 @@ const getConfig = (key: string) => {
};

describe('FieldFormatsService', () => {
let fieldFormatsService: FieldFormatsService;
let fieldFormats: FieldFormats;

beforeEach(() => {
const fieldFormatClasses = [NumberFormat];

fieldFormatsService = new FieldFormatsService(fieldFormatClasses, getConfig);
fieldFormats = new FieldFormats(fieldFormatClasses, getConfig);
});

test('FieldFormats are accessible via getType method', () => {
const Type = fieldFormatsService.getType('number');
const Type = fieldFormats.getType('number');

expect(Type.id).toBe('number');
});

test('getDefaultInstance returns default FieldFormat instance for fieldType', () => {
const instance = fieldFormatsService.getDefaultInstance('number');
const instance = fieldFormats.getDefaultInstance('number');

expect(instance.type.id).toBe('number');
expect(instance.convert('0.33333')).toBe('0.333');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ interface FieldFormatConfig {
params?: Record<string, any>;
}

export class FieldFormatsService {
getConfig: any;
_fieldFormats: Dictionary<IFieldFormatType>;
export class FieldFormats {
private readonly _fieldFormats: Dictionary<IFieldFormatType>;

constructor(fieldFormatClasses: IFieldFormatType[], getConfig: Function) {
constructor(fieldFormatClasses: IFieldFormatType[], private getConfig: Function) {
this._fieldFormats = indexBy(fieldFormatClasses, 'id');
this.getConfig = getConfig;
}

/**
Expand All @@ -43,6 +41,7 @@ export class FieldFormatsService {
*/
getDefaultConfig(fieldType: string): FieldFormatConfig {
const defaultMap = this.getConfig('format:defaultTypeMap');

return defaultMap[fieldType] || defaultMap._default_;
}

Expand Down
87 changes: 87 additions & 0 deletions src/plugins/data/server/field_formats/field_formats_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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 { has } from 'lodash';
import { FieldFormats } from './field_formats';
import { IFieldFormatType } from '../../common/field_formats';
import { IUiSettingsClient } from '../../../../core/server';

import {
UrlFormat,
StringFormat,
NumberFormat,
BytesFormat,
TruncateFormat,
RelativeDateFormat,
PercentFormat,
IpFormat,
DurationFormat,
DateNanosFormat,
DateFormat,
ColorFormat,
BoolFormat,
SourceFormat,
StaticLookupFormat,
} from '../../common/field_formats';

export class FieldFormatsService {
lizozom marked this conversation as resolved.
Show resolved Hide resolved
private readonly fieldFormatClasses: IFieldFormatType[] = [
UrlFormat,
StringFormat,
NumberFormat,
BytesFormat,
TruncateFormat,
RelativeDateFormat,
PercentFormat,
IpFormat,
DurationFormat,
DateNanosFormat,
DateFormat,
ColorFormat,
BoolFormat,
SourceFormat,
StaticLookupFormat,
];

public setup() {
return {
registerFieldFormat: (customFieldFormat: IFieldFormatType) =>
this.fieldFormatClasses.push(customFieldFormat),
};
}

public start() {
return {
fieldFormatServiceFactory: async (uiSettings: IUiSettingsClient) => {
Copy link
Contributor

@lizozom lizozom Jan 26, 2020

Choose a reason for hiding this comment

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

This function needs refactoring -

(a) we can get uiSettings from the function input
(b) we can initialize fieldFormatRegistry during setup and then only return the registry itself in start
(c) both the setup and start phases should return this.fieldFormatRegistry

Changing this would make the interfaces of the FE\BE plugins the same.

const uiConfigs = await uiSettings.getAll();
const registeredUiSettings = uiSettings.getRegistered();

Object.keys(registeredUiSettings).forEach(key => {
if (has(uiConfigs, key) && registeredUiSettings[key].type === 'json') {
uiConfigs[key] = JSON.parse(uiConfigs[key]);
}
});

return new FieldFormats(this.fieldFormatClasses, (key: string) => uiConfigs[key]);
},
};
}
}

export type FieldFormatsServiceSetup = ReturnType<FieldFormatsService['setup']>;
export type FieldFormatsServiceStart = ReturnType<FieldFormatsService['start']>;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@
* under the License.
*/

export { fieldFormatsMixin } from './mixin/field_formats_mixin';
export {
FieldFormatsService,
FieldFormatsServiceSetup,
FieldFormatsServiceStart,
} from './field_formats_service';
export { FieldFormats } from './field_formats';
10 changes: 8 additions & 2 deletions src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/

import { PluginInitializerContext } from '../../../core/server';
import { DataServerPlugin, DataPluginSetup } from './plugin';
import { DataServerPlugin, DataPluginSetup, DataPluginStart } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
return new DataServerPlugin(initializerContext);
}

export { FieldFormatsService, FieldFormats } from './field_formats';

/**
* Types to be shared externally
* @public
Expand Down Expand Up @@ -93,4 +95,8 @@ export {
getKbnTypeNames,
} from '../common';

export { DataServerPlugin as Plugin, DataPluginSetup as PluginSetup };
export {
DataServerPlugin as Plugin,
DataPluginSetup as PluginSetup,
DataPluginStart as PluginStart,
};
22 changes: 20 additions & 2 deletions src/plugins/data/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,32 @@ import { ScriptsService } from './scripts';
import { KqlTelemetryService } from './kql_telemetry';
import { UsageCollectionSetup } from '../../usage_collection/server';
import { AutocompleteService } from './autocomplete';
import {
FieldFormatsService,
FieldFormatsServiceSetup,
FieldFormatsServiceStart,
} from './field_formats';

export interface DataPluginSetup {
search: ISearchSetup;
fieldFormats: FieldFormatsServiceSetup;
}

export interface DataPluginStart {
fieldFormats: FieldFormatsServiceStart;
}

export interface DataPluginSetupDependencies {
usageCollection?: UsageCollectionSetup;
}
export class DataServerPlugin implements Plugin<DataPluginSetup> {

export class DataServerPlugin implements Plugin<DataPluginSetup, DataPluginStart> {
private readonly searchService: SearchService;
private readonly scriptsService: ScriptsService;
private readonly kqlTelemetryService: KqlTelemetryService;
private readonly autocompleteService = new AutocompleteService();
private readonly indexPatterns = new IndexPatternsService();
private readonly fieldFormats = new FieldFormatsService();

constructor(initializerContext: PluginInitializerContext) {
this.searchService = new SearchService(initializerContext);
Expand All @@ -53,11 +65,17 @@ export class DataServerPlugin implements Plugin<DataPluginSetup> {
this.kqlTelemetryService.setup(core, { usageCollection });

return {
fieldFormats: this.fieldFormats.setup(),
search: this.searchService.setup(core),
};
}

public start(core: CoreStart) {}
public start(core: CoreStart) {
return {
fieldFormats: this.fieldFormats.start(),
};
}

public stop() {}
}

Expand Down
Loading