Skip to content

Commit

Permalink
Use NP registry instead of ui/registry/field_formats (elastic#48108)
Browse files Browse the repository at this point in the history
* Create Field Format Provider

* Pass Field Format as a start dependency

* Register utils in the new field formats

* Fix types issue

* Make FieldFormats instance as a service

* Exclude importing of register.js

* Remove uiExports/fieldFormats imports

* Migrate some dependencies to new Field Format

* Migrate field format to new instance

* Fix type errors

* Fix Jest tests

* Move from getFieldFormat to npSetup

* Fix types errors

* Fix reviews

* Fix types errors

* Fix types

* fix CI

* Bind getConfig to FieldFormatRegisty

* Mock FieldFormat in FieldItem

* Fix DataPanel tests

* Fix DataPanel tests

* Fix Jest tests

* Create a fieldFormats mock for mocha tests

* fix CI

* fix CI

* Mock fieldFormatsRegistry in all visualizations

* Fix 'should create scripted field' functional test

* Change FieldFormat.from return type

* Fix reviews

* Fix mocha test

* Fix mocha tests

* Encapsulate FieldFormatRegistry

* Pretty field_item.tsx

* Fix reviews

* Fix PR comments

* Fix CI

* Replace any by a specific type

* Fix mocha tests

* Remove config from url converter

* Expand Field Format ID type for creating custom converters

* Fix a jest test

* Update migration doc

* Add quote and semicolon

* Fix types errors which doesn't relate to the aim of PR
  • Loading branch information
Artyom Gospodarsky authored and Liza K committed Nov 20, 2019
1 parent 3a4268f commit b4c2448
Show file tree
Hide file tree
Showing 92 changed files with 927 additions and 752 deletions.
1 change: 0 additions & 1 deletion docs/developer/plugin/development-uiexports.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ An aggregate list of available UiExport types:
| Type | Purpose
| hacks | Any module that should be included in every application
| visTypes | Modules that register providers with the `ui/registry/vis_types` registry.
| fieldFormats | Modules that register providers with the `ui/registry/field_formats` registry.
| inspectorViews | Modules that register custom inspector views via the `viewRegistry` in `ui/inspector`.
| chromeNavControls | Modules that register providers with the `ui/registry/chrome_nav_controls` registry.
| navbarExtensions | Modules that register providers with the `ui/registry/navbar_extensions` registry.
Expand Down
3 changes: 2 additions & 1 deletion src/fixtures/stubbed_logstash_index_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import StubIndexPattern from 'test_utils/stub_index_pattern';
import stubbedLogstashFields from 'fixtures/logstash_fields';

import { getKbnFieldType } from '../plugins/data/common';
import { mockUiSettings } from '../legacy/ui/public/new_platform/new_platform.karma_mock';

export default function stubbedLogstashIndexPatternService() {
const mockLogstashFields = stubbedLogstashFields();
Expand All @@ -40,7 +41,7 @@ export default function stubbedLogstashIndexPatternService() {
};
});

const indexPattern = new StubIndexPattern('logstash-*', cfg => cfg, 'time', fields);
const indexPattern = new StubIndexPattern('logstash-*', cfg => cfg, 'time', fields, mockUiSettings);
indexPattern.id = 'logstash-*';
indexPattern.isTimeNanosBased = () => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
* under the License.
*/

// @ts-ignore
import { fieldFormats } from 'ui/registry/field_formats';
import { i18n } from '@kbn/i18n';
// @ts-ignore
import { ObjDefine } from './obj_define';
// @ts-ignore
import { shortenDottedString } from '../../../../../core_plugins/kibana/common/utils/shorten_dotted_string';
import { IndexPattern } from '../index_patterns';
import { getNotifications } from '../services';
import { getNotifications, getFieldFormats } from '../services';

import {
FieldFormat,
Expand Down Expand Up @@ -104,6 +102,8 @@ export class Field implements FieldType {

let format = spec.format;
if (!format || !(format instanceof FieldFormat)) {
const fieldFormats = getFieldFormats();

format =
indexPattern.fieldFormatMap[spec.name] ||
fieldFormats.getDefaultInstance(spec.type, spec.esTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ import mockLogStashFields from '../../../../../../fixtures/logstash_fields';

import { stubbedSavedObjectIndexPattern } from '../../../../../../fixtures/stubbed_saved_object_index_pattern';
import { Field } from '../index_patterns_service';
import { setNotifications } from '../services';
import { setNotifications, setFieldFormats } from '../services';

// Temporary disable eslint, will be removed after moving to new platform folder
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { notificationServiceMock } from '../../../../../../core/public/notifications/notifications_service.mock';
import { FieldFormatRegisty } from '../../../../../../plugins/data/public';

jest.mock('ui/registry/field_formats', () => ({
fieldFormats: {
getDefaultInstance: jest.fn(),
},
}));
jest.mock('ui/new_platform');

jest.mock('../../../../../../plugins/kibana_utils/public', () => {
const originalModule = jest.requireActual('../../../../../../plugins/kibana_utils/public');
Expand Down Expand Up @@ -142,6 +139,9 @@ describe('IndexPattern', () => {
// create an indexPattern instance for each test
beforeEach(() => {
setNotifications(notifications);
setFieldFormats(({
getDefaultInstance: jest.fn(),
} as unknown) as FieldFormatRegisty);

return create(indexPatternId).then((pattern: IndexPattern) => {
indexPattern = pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import { FormattedMessage } from '@kbn/i18n/react';
import { EuiButton, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import React from 'react';
import chrome from 'ui/chrome';
// @ts-ignore
import { fieldFormats } from 'ui/registry/field_formats';
import { SavedObjectsClientContract } from 'src/core/public';

import {
DuplicateField,
SavedObjectNotFound,
Expand All @@ -35,15 +34,20 @@ import {
} from '../../../../../../plugins/kibana_utils/public';
import { toMountPoint } from '../../../../../../plugins/kibana_react/public';

import {
ES_FIELD_TYPES,
KBN_FIELD_TYPES,
IIndexPattern,
} from '../../../../../../plugins/data/public';

import { findIndexPatternByTitle, getRoutes } from '../utils';
import { IndexPatternMissingIndices } from '../errors';
import { Field, FieldList, FieldListInterface, FieldType } from '../fields';
import { createFieldsFetcher } from './_fields_fetcher';
import { formatHitProvider } from './format_hit';
import { flattenHitWrapper } from './flatten_hit';
import { IIndexPatternsApiClient } from './index_patterns_api_client';
import { ES_FIELD_TYPES, IIndexPattern } from '../../../../../../plugins/data/public';
import { getNotifications } from '../services';
import { getNotifications, getFieldFormats } from '../services';

const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3;
const type = 'index-pattern';
Expand Down Expand Up @@ -120,7 +124,10 @@ export class IndexPattern implements IIndexPattern {
this.fields = new FieldList(this, [], this.shortDotsEnable);
this.fieldsFetcher = createFieldsFetcher(this, apiClient, this.getConfig('metaFields'));
this.flattenHit = flattenHitWrapper(this, this.getConfig('metaFields'));
this.formatHit = formatHitProvider(this, fieldFormats.getDefaultInstance('string'));
this.formatHit = formatHitProvider(
this,
getFieldFormats().getDefaultInstance(KBN_FIELD_TYPES.STRING)
);
this.formatField = this.formatHit.formatField;
}

Expand All @@ -131,12 +138,14 @@ export class IndexPattern implements IIndexPattern {
}

private deserializeFieldFormatMap(mapping: any) {
const FieldFormat = fieldFormats.getType(mapping.id);
const FieldFormat = getFieldFormats().getType(mapping.id);

return FieldFormat && new FieldFormat(mapping.params, this.getConfig);
}

private initFields(input?: any) {
const newValue = input || this.fields;

this.fields = new FieldList(this, newValue, this.shortDotsEnable);
}

Expand Down Expand Up @@ -535,6 +544,7 @@ export class IndexPattern implements IIndexPattern {
const { toasts } = getNotifications();

toasts.addDanger(message);

throw err;
}

Expand Down Expand Up @@ -576,6 +586,7 @@ export class IndexPattern implements IIndexPattern {

if (err instanceof IndexPatternMissingIndices) {
toasts.addDanger((err as any).message);

return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ jest.mock('../errors', () => ({
IndexPatternMissingIndices: jest.fn(),
}));

jest.mock('ui/registry/field_formats', () => ({
fieldFormats: {
getDefaultInstance: jest.fn(),
},
}));

jest.mock('./index_pattern', () => {
class IndexPattern {
init = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import {
UiSettingsClientContract,
HttpServiceBase,
} from 'src/core/public';
// @ts-ignore
import { fieldFormats } from 'ui/registry/field_formats';

import { createIndexPatternCache } from './_pattern_cache';
import { IndexPattern } from './index_pattern';
Expand All @@ -34,8 +32,6 @@ import { IndexPatternsApiClient, GetFieldsOptions } from './index_patterns_api_c
const indexPatternCache = createIndexPatternCache();

export class IndexPatterns {
fieldFormats: fieldFormats;

private config: UiSettingsClientContract;
private savedObjectsClient: SavedObjectsClientContract;
private savedObjectsCache?: Array<SimpleSavedObject<Record<string, any>>> | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import {
HttpServiceBase,
NotificationsStart,
} from 'src/core/public';
import { FieldFormatsStart } from '../../../../../plugins/data/public';
import { Field, FieldList, FieldListInterface, FieldType } from './fields';
import { createIndexPatternSelect } from './components';
import { setNotifications } from './services';
import { setNotifications, setFieldFormats } from './services';

import {
createFlattenHitWrapper,
Expand All @@ -40,6 +41,7 @@ export interface IndexPatternDependencies {
savedObjectsClient: SavedObjectsClientContract;
http: HttpServiceBase;
notifications: NotificationsStart;
fieldFormats: FieldFormatsStart;
}

/**
Expand All @@ -64,8 +66,15 @@ export class IndexPatternsService {
return this.setupApi;
}

public start({ uiSettings, savedObjectsClient, http, notifications }: IndexPatternDependencies) {
public start({
uiSettings,
savedObjectsClient,
http,
notifications,
fieldFormats,
}: IndexPatternDependencies) {
setNotifications(notifications);
setFieldFormats(fieldFormats);

return {
...this.setupApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

import { NotificationsStart } from 'src/core/public';
import { createGetterSetter } from '../../../../../plugins/kibana_utils/public';
import { FieldFormatsStart } from '../../../../../plugins/data/public';

export const [getNotifications, setNotifications] = createGetterSetter<NotificationsStart>(
'Notifications'
);

export const [getFieldFormats, setFieldFormats] = createGetterSetter<FieldFormatsStart>(
'FieldFormats'
);
1 change: 1 addition & 0 deletions src/legacy/core_plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class DataPlugin implements Plugin<DataSetup, DataStart, {}, DataPluginSt
savedObjectsClient: savedObjects.client,
http,
notifications,
fieldFormats: data.fieldFormats,
});

initLegacyModule(indexPatternsService.indexPatterns);
Expand Down
1 change: 0 additions & 1 deletion src/legacy/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export default function (kibana) {
hacks: [
'plugins/kibana/dev_tools',
],
fieldFormats: ['plugins/kibana/field_formats/register'],
savedObjectTypes: [
'plugins/kibana/visualize/saved_visualizations/saved_visualization_register',
'plugins/kibana/discover/saved_searches/saved_search_register',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ import { TimeRange, TimefilterContract } from 'src/plugins/data/public';
import { ViewMode } from 'src/plugins/embeddable/public';
import { InputTimeRange } from 'ui/timefilter';

jest.mock('ui/registry/field_formats', () => ({
fieldFormats: {
getDefaultInstance: jest.fn(),
},
jest.mock('ui/state_management/state', () => ({
State: {},
}));

describe('DashboardState', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ describe('discover field chooser directives', function () {
$scope.computeDetails(field);
expect(field.details.buckets).to.not.be(undefined);
expect(field.details.buckets[0].value).to.be(40.141592);
expect(field.details.buckets[0].display).to.be('40.142');
});


Expand Down
Loading

0 comments on commit b4c2448

Please sign in to comment.