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

[Reporting/FieldFormats] expose setFieldFormats and call from ReportingPlugin.start #56563

Merged
merged 7 commits into from
Feb 3, 2020
Merged
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/common/index.ts
Original file line number Diff line number Diff line change
@@ -19,4 +19,5 @@

export * from './defer';
export * from './of';
export { createGetterSetter, Get, Set } from './create_getter_setter';
export { distinctUntilChangedWithInitialValue } from './distinct_until_changed_with_initial_value';
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter, Get, Set } from './create_getter_setter';
import { createGetterSetter, Get, Set } from '../../common';
import { CoreStart } from '../../../../core/public';
import { KUSavedObjectClient, createSavedObjectsClient } from './saved_objects_client';

1 change: 0 additions & 1 deletion src/plugins/kibana_utils/public/core/index.ts
Original file line number Diff line number Diff line change
@@ -17,5 +17,4 @@
* under the License.
*/

export * from './create_getter_setter';
export * from './create_kibana_utils_core';
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
*/

import { CoreStart } from '../../../../core/public';
import { Get } from './create_getter_setter';
import { Get } from '../../common';

type CoreSavedObjectClient = CoreStart['savedObjects']['client'];

2 changes: 1 addition & 1 deletion src/plugins/kibana_utils/public/core/state.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from './create_getter_setter';
import { createGetterSetter } from '../../common';
import { CoreStart } from '../../../../core/public';

export const [getCoreStart, setCoreStart] = createGetterSetter<CoreStart>('CoreStart');
2 changes: 1 addition & 1 deletion src/plugins/kibana_utils/public/index.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
* under the License.
*/

export { defer, Defer, of } from '../common';
export { defer, Defer, of, createGetterSetter, Get, Set } from '../common';
export * from './core';
export * from './errors';
export * from './field_mapping';
20 changes: 20 additions & 0 deletions src/plugins/kibana_utils/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export { Get, Set, createGetterSetter } from '../common';
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from '../../../../../../src/plugins/kibana_utils/public/core';
import { createGetterSetter } from '../../../../../../src/plugins/kibana_utils/public';
import { ExpressionsStart } from './types';

export const [getExpressions, setExpressions] = createGetterSetter<ExpressionsStart>('Expressions');
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import { CancellationToken } from '../../../common/cancellation_token';
import { fieldFormats } from '../../../../../../../src/plugins/data/server';
import { LevelLogger } from '../../../server/lib/level_logger';
import { executeJobFactory } from './execute_job';
import { setFieldFormats } from '../../../server/services';

const delay = ms => new Promise(resolve => setTimeout(() => resolve(), ms));

@@ -73,7 +74,7 @@ describe('CSV Execute Job', function() {
uiSettingsGetStub.withArgs('csv:separator').returns(',');
uiSettingsGetStub.withArgs('csv:quoteValues').returns(true);

mockServer = {
setFieldFormats({
fieldFormatServiceFactory: function() {
const uiConfigMock = {};
uiConfigMock['format:defaultTypeMap'] = {
@@ -86,6 +87,17 @@ describe('CSV Execute Job', function() {

return fieldFormatsRegistry;
},
});

mockServer = {
expose: function() {},
plugins: {
elasticsearch: {
getCluster: function() {
return clusterStub;
},
},
},
config: function() {
return {
get: configGetStub,
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import { ESQueueWorkerExecuteFn, ExecuteJobFactory, Logger, ServerFacade } from
import { JobDocPayloadDiscoverCsv } from '../types';
import { fieldFormatMapFactory } from './lib/field_format_map';
import { createGenerateCsv } from './lib/generate_csv';
import { getFieldFormats } from '../../../server/services';

export const executeJobFactory: ExecuteJobFactory<ESQueueWorkerExecuteFn<
JobDocPayloadDiscoverCsv
@@ -94,7 +95,7 @@ export const executeJobFactory: ExecuteJobFactory<ESQueueWorkerExecuteFn<

const [formatsMap, uiSettings] = await Promise.all([
(async () => {
const fieldFormats = await server.fieldFormatServiceFactory(uiConfig);
const fieldFormats = await getFieldFormats().fieldFormatServiceFactory(uiConfig);
return fieldFormatMapFactory(indexPatternSavedObject, fieldFormats);
})(),
(async () => {
39 changes: 2 additions & 37 deletions x-pack/legacy/plugins/reporting/index.ts
Original file line number Diff line number Diff line change
@@ -6,23 +6,16 @@

import { i18n } from '@kbn/i18n';
import { Legacy } from 'kibana';
import { IUiSettingsClient } from 'kibana/server';
import { resolve } from 'path';
import { PluginStart as DataPluginStart } from '../../../../src/plugins/data/server';
import { PluginSetupContract as SecurityPluginSetup } from '../../../plugins/security/server';
import { PLUGIN_ID, UI_SETTINGS_CUSTOM_PDF_LOGO } from './common/constants';
import { config as reportingConfig } from './config';
import { LegacySetup, ReportingPlugin, reportingPluginFactory } from './server/plugin';
import { legacyInit } from './server/legacy';
import { ReportingConfigOptions, ReportingPluginSpecOptions } from './types.d';

const kbToBase64Length = (kb: number) => {
return Math.floor((kb * 1024 * 8) / 6);
};

interface ReportingDeps {
data: DataPluginStart;
}

export const reporting = (kibana: any) => {
return new kibana.Plugin({
id: PLUGIN_ID,
@@ -68,35 +61,7 @@ export const reporting = (kibana: any) => {
},

async init(server: Legacy.Server) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this has to be async anymore

const coreSetup = server.newPlatform.setup.core;

const fieldFormatServiceFactory = async (uiSettings: IUiSettingsClient) => {
const [, plugins] = await coreSetup.getStartServices();
const { fieldFormats } = (plugins as ReportingDeps).data;

return fieldFormats.fieldFormatServiceFactory(uiSettings);
};

const __LEGACY: LegacySetup = {
config: server.config,
info: server.info,
route: server.route.bind(server),
plugins: { xpack_main: server.plugins.xpack_main },
savedObjects: server.savedObjects,
fieldFormatServiceFactory,
uiSettingsServiceFactory: server.uiSettingsServiceFactory,
};

const plugin: ReportingPlugin = reportingPluginFactory(
server.newPlatform.coreContext,
__LEGACY,
this
);
await plugin.setup(coreSetup, {
elasticsearch: coreSetup.elasticsearch,
security: server.newPlatform.setup.plugins.security as SecurityPluginSetup,
usageCollection: server.newPlatform.setup.plugins.usageCollection,
});
return legacyInit(server, this);
},

deprecations({ unused }: any) {
12 changes: 12 additions & 0 deletions x-pack/legacy/plugins/reporting/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { PluginInitializerContext } from 'src/core/server';
import { ReportingPlugin as Plugin } from './plugin';

export const plugin = (context: PluginInitializerContext) => {
return new Plugin(context);
};
49 changes: 49 additions & 0 deletions x-pack/legacy/plugins/reporting/server/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Legacy } from 'kibana';
import { PluginInitializerContext } from 'src/core/server';
import { PluginSetupContract as SecurityPluginSetup } from '../../../../plugins/security/server';
import { ReportingPluginSpecOptions } from '../types';
import { plugin } from './index';
import { LegacySetup, ReportingStartDeps } from './plugin';

const buildLegacyDependencies = (
server: Legacy.Server,
reportingPlugin: ReportingPluginSpecOptions
): LegacySetup => ({
config: server.config,
info: server.info,
route: server.route.bind(server),
plugins: {
elasticsearch: server.plugins.elasticsearch,
xpack_main: server.plugins.xpack_main,
reporting: reportingPlugin,
},
savedObjects: server.savedObjects,
uiSettingsServiceFactory: server.uiSettingsServiceFactory,
});

export const legacyInit = async (
server: Legacy.Server,
reportingPlugin: ReportingPluginSpecOptions
) => {
const coreSetup = server.newPlatform.setup.core;
const pluginInstance = plugin(server.newPlatform.coreContext as PluginInitializerContext);

await pluginInstance.setup(coreSetup, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have to await the result of setup?
The next stage awaits on the results of getStartServices anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

I do need a way to make sure that my plugin's start method would not be called before setup.

Soon in the plugin class there will be a private field referenced in setup and start, so I'd have a race condition if start was using the data before it is initialized in setup.

I don't have my mind settled yet on how to co-ordinate dependencies in setup and start, but I plan to have it outlined in an upcoming PR to provide more plugin dependencies to modules that aren't called from routes handlers. :)

elasticsearch: coreSetup.elasticsearch,
security: server.newPlatform.setup.plugins.security as SecurityPluginSetup,
usageCollection: server.newPlatform.setup.plugins.usageCollection,
__LEGACY: buildLegacyDependencies(server, reportingPlugin),
});

// Schedule to call the "start" hook only after start dependencies are ready
coreSetup.getStartServices().then(([core, plugins]) =>
pluginInstance.start(core, {
data: (plugins as ReportingStartDeps).data,
})
);
};
Loading