[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 <alexwizp@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Alexey Antonov <alexwizp@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Tim Sullivan 2020-02-05 11:33:21 -07:00 committed by GitHub
parent d5a1546eda
commit 736ed90ca3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 163 additions and 110 deletions

View file

@ -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';

View file

@ -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';

View file

@ -17,5 +17,4 @@
* under the License.
*/
export * from './create_getter_setter';
export * from './create_kibana_utils_core';

View file

@ -18,7 +18,7 @@
*/
import { CoreStart } from '../../../../core/public';
import { Get } from './create_getter_setter';
import { Get } from '../../common';
type CoreSavedObjectClient = CoreStart['savedObjects']['client'];

View file

@ -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');

View file

@ -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';

View file

@ -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';

View file

@ -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');

View file

@ -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,

View file

@ -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 () => {

View file

@ -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) {

View file

@ -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);
};

View file

@ -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,
})
);
};

View file

@ -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
>;
export class ReportingPlugin implements Plugin<void, void, ReportingSetupDeps, ReportingStartDeps> {
constructor(private context: PluginInitializerContext) {}
/* 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;
public async setup(core: CoreSetup, plugins: ReportingSetupDeps) {
const { elasticsearch, usageCollection, __LEGACY } = plugins;
const exportTypesRegistry = getExportTypesRegistry();
constructor(context: ReportingInitializerContext) {
this.initializerContext = context;
}
let isCollectorReady = false;
public async setup(core: CoreSetup, plugins: ReportingSetupDeps): Promise<ReportingSetup> {
const exportTypesRegistry = getExportTypesRegistry();
const { usageCollection, elasticsearch } = plugins;
// Register a function with server to manage the collection of usage stats
registerReportingUsageCollector(
usageCollection,
__LEGACY,
() => isCollectorReady,
exportTypesRegistry
);
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);
const logger = new LevelLogger(this.initializerContext.logger.get('reporting'));
const browserDriverFactory = await createBrowserDriverFactory(__LEGACY, logger);
logConfiguration(__LEGACY, logger);
runValidations(__LEGACY, elasticsearch, logger, browserDriverFactory);
logConfiguration(__LEGACY, logger);
runValidations(__LEGACY, elasticsearch, logger, browserDriverFactory);
const { xpack_main: xpackMainPlugin, reporting } = __LEGACY.plugins;
mirrorPluginStatus(xpackMainPlugin, reporting);
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);
});
const checkLicense = checkLicenseFactory(exportTypesRegistry);
// Post initialization of the above code, the collector is now ready to fetch its data
isCollectorReady = true;
(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);
});
// Reporting routes
registerRoutes(__LEGACY, plugins, exportTypesRegistry, browserDriverFactory, logger);
// Post initialization of the above code, the collector is now ready to fetch its data
isCollectorReady = true;
return {};
}
// Reporting routes
registerRoutes(__LEGACY, plugins, exportTypesRegistry, browserDriverFactory, logger);
}
public start(core: CoreStart, plugins: ReportingStartDeps): ReportingStart {
return {};
}
})(initializerContext);
public start(core: CoreStart, plugins: ReportingStartDeps) {
setFieldFormats(plugins.data.fieldFormats);
}
}

View file

@ -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');