From 736ed90ca3a3b9b4d8f2f8ea36f6f1f474aa59f7 Mon Sep 17 00:00:00 2001 From: Tim Sullivan Date: Wed, 5 Feb 2020 11:33:21 -0700 Subject: [PATCH] [Reporting/FieldFormats] expose `setFieldFormats` and call from ReportingPlugin.start (#56563) (#56875) * [Reporting] New Platform Migration Part of #53898 * fix CI * fix typescript Co-authored-by: Alexey Antonov Co-authored-by: Elastic Machine Co-authored-by: Alexey Antonov Co-authored-by: Elastic Machine --- .../core => common}/create_getter_setter.ts | 0 src/plugins/kibana_utils/common/index.ts | 1 + .../public/core/create_kibana_utils_core.ts | 2 +- src/plugins/kibana_utils/public/core/index.ts | 1 - .../public/core/saved_objects_client.ts | 2 +- src/plugins/kibana_utils/public/core/state.ts | 2 +- src/plugins/kibana_utils/public/index.ts | 2 +- src/plugins/kibana_utils/server/index.ts | 20 +++ .../public/np_ready/services.ts | 2 +- .../csv/server/execute_job.test.js | 14 +- .../export_types/csv/server/execute_job.ts | 3 +- x-pack/legacy/plugins/reporting/index.ts | 39 +---- .../legacy/plugins/reporting/server/index.ts | 12 ++ .../legacy/plugins/reporting/server/legacy.ts | 49 +++++++ .../legacy/plugins/reporting/server/plugin.ts | 134 ++++++++---------- .../plugins/reporting/server/services.ts | 12 ++ 16 files changed, 174 insertions(+), 121 deletions(-) rename src/plugins/kibana_utils/{public/core => common}/create_getter_setter.ts (100%) create mode 100644 src/plugins/kibana_utils/server/index.ts create mode 100644 x-pack/legacy/plugins/reporting/server/index.ts create mode 100644 x-pack/legacy/plugins/reporting/server/legacy.ts create mode 100644 x-pack/legacy/plugins/reporting/server/services.ts diff --git a/src/plugins/kibana_utils/public/core/create_getter_setter.ts b/src/plugins/kibana_utils/common/create_getter_setter.ts similarity index 100% rename from src/plugins/kibana_utils/public/core/create_getter_setter.ts rename to src/plugins/kibana_utils/common/create_getter_setter.ts diff --git a/src/plugins/kibana_utils/common/index.ts b/src/plugins/kibana_utils/common/index.ts index d4aeb2c0fe4ad..444c10194a8e3 100644 --- a/src/plugins/kibana_utils/common/index.ts +++ b/src/plugins/kibana_utils/common/index.ts @@ -20,4 +20,5 @@ export * from './defer'; export * from './of'; export * from './state_containers'; +export { createGetterSetter, Get, Set } from './create_getter_setter'; export { distinctUntilChangedWithInitialValue } from './distinct_until_changed_with_initial_value'; diff --git a/src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts b/src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts index 84ecffa1da634..c528c68f29edb 100644 --- a/src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts +++ b/src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts @@ -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'; diff --git a/src/plugins/kibana_utils/public/core/index.ts b/src/plugins/kibana_utils/public/core/index.ts index 7e8dff7191fe8..3f08d591300a2 100644 --- a/src/plugins/kibana_utils/public/core/index.ts +++ b/src/plugins/kibana_utils/public/core/index.ts @@ -17,5 +17,4 @@ * under the License. */ -export * from './create_getter_setter'; export * from './create_kibana_utils_core'; diff --git a/src/plugins/kibana_utils/public/core/saved_objects_client.ts b/src/plugins/kibana_utils/public/core/saved_objects_client.ts index 40407fea5d189..5262755a6a3ab 100644 --- a/src/plugins/kibana_utils/public/core/saved_objects_client.ts +++ b/src/plugins/kibana_utils/public/core/saved_objects_client.ts @@ -18,7 +18,7 @@ */ import { CoreStart } from '../../../../core/public'; -import { Get } from './create_getter_setter'; +import { Get } from '../../common'; type CoreSavedObjectClient = CoreStart['savedObjects']['client']; diff --git a/src/plugins/kibana_utils/public/core/state.ts b/src/plugins/kibana_utils/public/core/state.ts index 8ac6e4e0e58e6..52c4d166d737e 100644 --- a/src/plugins/kibana_utils/public/core/state.ts +++ b/src/plugins/kibana_utils/public/core/state.ts @@ -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'); diff --git a/src/plugins/kibana_utils/public/index.ts b/src/plugins/kibana_utils/public/index.ts index 78828ad9c4b2d..5b6d304e14c2e 100644 --- a/src/plugins/kibana_utils/public/index.ts +++ b/src/plugins/kibana_utils/public/index.ts @@ -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'; diff --git a/src/plugins/kibana_utils/server/index.ts b/src/plugins/kibana_utils/server/index.ts new file mode 100644 index 0000000000000..f8b79a1b8b339 --- /dev/null +++ b/src/plugins/kibana_utils/server/index.ts @@ -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'; diff --git a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/public/np_ready/services.ts b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/public/np_ready/services.ts index 657d8d5150c3a..a700727d87299 100644 --- a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/public/np_ready/services.ts +++ b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/public/np_ready/services.ts @@ -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('Expressions'); diff --git a/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.test.js b/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.test.js index 5042389f2bf6b..537b366acb7ac 100644 --- a/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.test.js +++ b/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.test.js @@ -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, diff --git a/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.ts b/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.ts index 280bbf13fa992..9f94a755cf655 100644 --- a/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.ts +++ b/x-pack/legacy/plugins/reporting/export_types/csv/server/execute_job.ts @@ -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 { - const fieldFormats = await server.fieldFormatServiceFactory(uiConfig); + const fieldFormats = await getFieldFormats().fieldFormatServiceFactory(uiConfig); return fieldFormatMapFactory(indexPatternSavedObject, fieldFormats); })(), (async () => { diff --git a/x-pack/legacy/plugins/reporting/index.ts b/x-pack/legacy/plugins/reporting/index.ts index 966e4ff209ad6..cbafc4b1ecc4b 100644 --- a/x-pack/legacy/plugins/reporting/index.ts +++ b/x-pack/legacy/plugins/reporting/index.ts @@ -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 { 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) { - 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) { diff --git a/x-pack/legacy/plugins/reporting/server/index.ts b/x-pack/legacy/plugins/reporting/server/index.ts new file mode 100644 index 0000000000000..438a3fd595a10 --- /dev/null +++ b/x-pack/legacy/plugins/reporting/server/index.ts @@ -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); +}; diff --git a/x-pack/legacy/plugins/reporting/server/legacy.ts b/x-pack/legacy/plugins/reporting/server/legacy.ts new file mode 100644 index 0000000000000..c80aef06cf270 --- /dev/null +++ b/x-pack/legacy/plugins/reporting/server/legacy.ts @@ -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 { 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, { + 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, + }) + ); +}; diff --git a/x-pack/legacy/plugins/reporting/server/plugin.ts b/x-pack/legacy/plugins/reporting/server/plugin.ts index e618d23e8ed1f..ef7b01f8e9c15 100644 --- a/x-pack/legacy/plugins/reporting/server/plugin.ts +++ b/x-pack/legacy/plugins/reporting/server/plugin.ts @@ -9,8 +9,8 @@ import { CoreSetup, CoreStart, ElasticsearchServiceSetup, - LoggerFactory, Plugin, + PluginInitializerContext, } from 'src/core/server'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { PluginStart as DataPluginStart } from '../../../../../src/plugins/data/server'; @@ -24,95 +24,77 @@ import { ReportingPluginSpecOptions } from '../types.d'; import { createBrowserDriverFactory } from './browsers'; import { checkLicenseFactory, getExportTypesRegistry, LevelLogger, runValidations } from './lib'; import { registerRoutes } from './routes'; +import { setFieldFormats } from './services'; import { registerReportingUsageCollector } from './usage'; -export interface ReportingInitializerContext { - logger: LoggerFactory; -} - -// For now there is no exposed functionality to other plugins -export type ReportingSetup = object; -export type ReportingStart = object; - export interface ReportingSetupDeps { elasticsearch: ElasticsearchServiceSetup; usageCollection: UsageCollectionSetup; security: SecurityPluginSetup; + __LEGACY: LegacySetup; +} + +export interface ReportingStartDeps { + data: DataPluginStart; } -export type ReportingStartDeps = object; export interface LegacySetup { config: Legacy.Server['config']; info: Legacy.Server['info']; - plugins: { xpack_main: XPackMainPlugin & { status?: any } }; + plugins: { + elasticsearch: Legacy.Server['plugins']['elasticsearch']; + xpack_main: XPackMainPlugin & { + status?: any; + }; + reporting: ReportingPluginSpecOptions; + }; route: Legacy.Server['route']; savedObjects: Legacy.Server['savedObjects']; uiSettingsServiceFactory: Legacy.Server['uiSettingsServiceFactory']; - fieldFormatServiceFactory: DataPluginStart['fieldFormats']['fieldFormatServiceFactory']; } -export type ReportingPlugin = Plugin< - ReportingSetup, - ReportingStart, - ReportingSetupDeps, - ReportingStartDeps ->; - -/* We need a factory that returns an instance of the class because the class - * implementation itself restricts against having Legacy dependencies passed - * into `setup`. The factory parameters take the legacy dependencies, and the - * `setup` method gets it from enclosure */ -export function reportingPluginFactory( - initializerContext: ReportingInitializerContext, - __LEGACY: LegacySetup, - legacyPlugin: ReportingPluginSpecOptions -) { - return new (class ReportingPlugin implements ReportingPlugin { - private initializerContext: ReportingInitializerContext; - - constructor(context: ReportingInitializerContext) { - this.initializerContext = context; - } - - public async setup(core: CoreSetup, plugins: ReportingSetupDeps): Promise { - const exportTypesRegistry = getExportTypesRegistry(); - const { usageCollection, elasticsearch } = plugins; - - let isCollectorReady = false; - // Register a function with server to manage the collection of usage stats - registerReportingUsageCollector( - usageCollection, - __LEGACY, - () => isCollectorReady, - exportTypesRegistry - ); - - const logger = new LevelLogger(this.initializerContext.logger.get('reporting')); - const browserDriverFactory = await createBrowserDriverFactory(__LEGACY, logger); - - logConfiguration(__LEGACY, logger); - runValidations(__LEGACY, elasticsearch, logger, browserDriverFactory); - - const { xpack_main: xpackMainPlugin } = __LEGACY.plugins; - mirrorPluginStatus(xpackMainPlugin, legacyPlugin); - const checkLicense = checkLicenseFactory(exportTypesRegistry); - (xpackMainPlugin as any).status.once('green', () => { - // Register a function that is called whenever the xpack info changes, - // to re-compute the license check results for this plugin - xpackMainPlugin.info.feature(PLUGIN_ID).registerLicenseCheckResultsGenerator(checkLicense); - }); - - // Post initialization of the above code, the collector is now ready to fetch its data - isCollectorReady = true; - - // Reporting routes - registerRoutes(__LEGACY, plugins, exportTypesRegistry, browserDriverFactory, logger); - - return {}; - } - - public start(core: CoreStart, plugins: ReportingStartDeps): ReportingStart { - return {}; - } - })(initializerContext); +export class ReportingPlugin implements Plugin { + constructor(private context: PluginInitializerContext) {} + + public async setup(core: CoreSetup, plugins: ReportingSetupDeps) { + const { elasticsearch, usageCollection, __LEGACY } = plugins; + const exportTypesRegistry = getExportTypesRegistry(); + + let isCollectorReady = false; + + // Register a function with server to manage the collection of usage stats + registerReportingUsageCollector( + usageCollection, + __LEGACY, + () => isCollectorReady, + exportTypesRegistry + ); + + const logger = new LevelLogger(this.context.logger.get('reporting')); + const browserDriverFactory = await createBrowserDriverFactory(__LEGACY, logger); + + logConfiguration(__LEGACY, logger); + runValidations(__LEGACY, elasticsearch, logger, browserDriverFactory); + + const { xpack_main: xpackMainPlugin, reporting } = __LEGACY.plugins; + mirrorPluginStatus(xpackMainPlugin, reporting); + + const checkLicense = checkLicenseFactory(exportTypesRegistry); + + (xpackMainPlugin as any).status.once('green', () => { + // Register a function that is called whenever the xpack info changes, + // to re-compute the license check results for this plugin + xpackMainPlugin.info.feature(PLUGIN_ID).registerLicenseCheckResultsGenerator(checkLicense); + }); + + // Post initialization of the above code, the collector is now ready to fetch its data + isCollectorReady = true; + + // Reporting routes + registerRoutes(__LEGACY, plugins, exportTypesRegistry, browserDriverFactory, logger); + } + + public start(core: CoreStart, plugins: ReportingStartDeps) { + setFieldFormats(plugins.data.fieldFormats); + } } diff --git a/x-pack/legacy/plugins/reporting/server/services.ts b/x-pack/legacy/plugins/reporting/server/services.ts new file mode 100644 index 0000000000000..7d15d2e1af1ae --- /dev/null +++ b/x-pack/legacy/plugins/reporting/server/services.ts @@ -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 { createGetterSetter } from '../../../../../src/plugins/kibana_utils/server'; +import { PluginStart as DataPluginStart } from '../../../../../src/plugins/data/server'; + +export const [getFieldFormats, setFieldFormats] = createGetterSetter< + DataPluginStart['fieldFormats'] +>('FieldFormats');