diff --git a/src/plugins/index_pattern_management/public/plugin.ts b/src/plugins/index_pattern_management/public/plugin.ts index 610b3541620b00..d254691a0270da 100644 --- a/src/plugins/index_pattern_management/public/plugin.ts +++ b/src/plugins/index_pattern_management/public/plugin.ts @@ -80,17 +80,15 @@ export class IndexPatternManagementPlugin return mountManagementSection(core.getStartServices, params); }, }); + } - return this.indexPatternManagementService.setup({ + public start(core: CoreStart, plugins: IndexPatternManagementStartDependencies) { + return this.indexPatternManagementService.start({ httpClient: core.http, uiSettings: core.uiSettings, }); } - public start(core: CoreStart, plugins: IndexPatternManagementStartDependencies) { - return this.indexPatternManagementService.start(); - } - public stop() { this.indexPatternManagementService.stop(); } diff --git a/src/plugins/index_pattern_management/public/service/creation/manager.ts b/src/plugins/index_pattern_management/public/service/creation/manager.ts index c139b10ebb1fe6..f1fdb68418a652 100644 --- a/src/plugins/index_pattern_management/public/service/creation/manager.ts +++ b/src/plugins/index_pattern_management/public/service/creation/manager.ts @@ -6,31 +6,36 @@ * Side Public License, v 1. */ -import { HttpSetup } from '../../../../../core/public'; +import { memoize } from 'lodash'; +import { HttpStart, CoreStart } from '../../../../../core/public'; import { IndexPatternCreationConfig, UrlHandler, IndexPatternCreationOption } from './config'; +import { CONFIG_ROLLUPS } from '../../constants'; +// @ts-ignore +import { RollupIndexPatternCreationConfig } from './rollup_creation_config'; -export class IndexPatternCreationManager { - private configs: IndexPatternCreationConfig[] = []; +interface IndexPatternCreationManagerStart { + httpClient: HttpStart; + uiSettings: CoreStart['uiSettings']; +} - setup(httpClient: HttpSetup) { - return { - addCreationConfig: (Config: typeof IndexPatternCreationConfig) => { - const config = new Config({ httpClient }); +export class IndexPatternCreationManager { + start({ httpClient, uiSettings }: IndexPatternCreationManagerStart) { + const getConfigs = memoize(() => { + const configs: IndexPatternCreationConfig[] = []; + configs.push(new IndexPatternCreationConfig({ httpClient })); - if (this.configs.findIndex((c) => c.key === config.key) !== -1) { - throw new Error(`${config.key} exists in IndexPatternCreationManager.`); - } + if (uiSettings.get(CONFIG_ROLLUPS, false)) { + configs.push(new RollupIndexPatternCreationConfig({ httpClient })); + } - this.configs.push(config); - }, - }; - } + return configs; + }); - start() { const getType = (key: string | undefined): IndexPatternCreationConfig => { + const configs = getConfigs(); if (key) { - const index = this.configs.findIndex((config) => config.key === key); - const config = this.configs[index]; + const index = configs.findIndex((config) => config.key === key); + const config = configs[index]; if (config) { return config; @@ -48,7 +53,7 @@ export class IndexPatternCreationManager { const options: IndexPatternCreationOption[] = []; await Promise.all( - this.configs.map(async (config) => { + getConfigs().map(async (config) => { const option = config.getIndexPatternCreationOption ? await config.getIndexPatternCreationOption(urlHandler) : null; diff --git a/src/plugins/index_pattern_management/public/service/index_pattern_management_service.ts b/src/plugins/index_pattern_management/public/service/index_pattern_management_service.ts index f1fa2b76b28882..25a36faa1c3e37 100644 --- a/src/plugins/index_pattern_management/public/service/index_pattern_management_service.ts +++ b/src/plugins/index_pattern_management/public/service/index_pattern_management_service.ts @@ -6,22 +6,13 @@ * Side Public License, v 1. */ -import { HttpSetup, CoreSetup } from '../../../../core/public'; -import { - IndexPatternCreationManager, - IndexPatternCreationConfig, - RollupIndexPatternCreationConfig, -} from './creation'; -import { - IndexPatternListManager, - IndexPatternListConfig, - RollupIndexPatternListConfig, -} from './list'; +import { HttpStart, CoreStart } from '../../../../core/public'; +import { IndexPatternCreationManager } from './creation'; +import { IndexPatternListManager } from './list'; -import { CONFIG_ROLLUPS } from '../constants'; -interface SetupDependencies { - httpClient: HttpSetup; - uiSettings: CoreSetup['uiSettings']; +interface StartDependencies { + httpClient: HttpStart; + uiSettings: CoreStart['uiSettings']; } /** @@ -38,23 +29,12 @@ export class IndexPatternManagementService { this.indexPatternListConfig = new IndexPatternListManager(); } - public setup({ httpClient, uiSettings }: SetupDependencies) { - const creationManagerSetup = this.indexPatternCreationManager.setup(httpClient); - creationManagerSetup.addCreationConfig(IndexPatternCreationConfig); + public setup() {} - const indexPatternListConfigSetup = this.indexPatternListConfig.setup(); - indexPatternListConfigSetup.addListConfig(IndexPatternListConfig); - - if (uiSettings.get(CONFIG_ROLLUPS, false)) { - creationManagerSetup.addCreationConfig(RollupIndexPatternCreationConfig); - indexPatternListConfigSetup.addListConfig(RollupIndexPatternListConfig); - } - } - - public start() { + public start({ httpClient, uiSettings }: StartDependencies) { return { - creation: this.indexPatternCreationManager.start(), - list: this.indexPatternListConfig.start(), + creation: this.indexPatternCreationManager.start({ httpClient, uiSettings }), + list: this.indexPatternListConfig.start({ uiSettings }), }; } diff --git a/src/plugins/index_pattern_management/public/service/list/manager.ts b/src/plugins/index_pattern_management/public/service/list/manager.ts index 22877f78d46fcd..2164d770069785 100644 --- a/src/plugins/index_pattern_management/public/service/list/manager.ts +++ b/src/plugins/index_pattern_management/public/service/list/manager.ts @@ -8,31 +8,35 @@ import { IIndexPattern, IFieldType } from 'src/plugins/data/public'; import { SimpleSavedObject } from 'src/core/public'; +import { memoize } from 'lodash'; +import { CoreStart } from '../../../../../core/public'; import { IndexPatternListConfig, IndexPatternTag } from './config'; +import { CONFIG_ROLLUPS } from '../../constants'; +// @ts-ignore +import { RollupIndexPatternListConfig } from './rollup_list_config'; -export class IndexPatternListManager { - private configs: IndexPatternListConfig[] = []; +interface IndexPatternListManagerStart { + uiSettings: CoreStart['uiSettings']; +} - setup() { - return { - addListConfig: (Config: typeof IndexPatternListConfig) => { - const config = new Config(); +export class IndexPatternListManager { + start({ uiSettings }: IndexPatternListManagerStart) { + const getConfigs = memoize(() => { + const configs: IndexPatternListConfig[] = []; + configs.push(new IndexPatternListConfig()); - if (this.configs.findIndex((c) => c.key === config.key) !== -1) { - throw new Error(`${config.key} exists in IndexPatternListManager.`); - } - this.configs.push(config); - }, - }; - } + if (uiSettings.get(CONFIG_ROLLUPS, false)) { + configs.push(new RollupIndexPatternListConfig()); + } - start() { + return configs; + }); return { getIndexPatternTags: ( indexPattern: IIndexPattern | SimpleSavedObject, isDefault: boolean ) => - this.configs.reduce( + getConfigs().reduce( (tags: IndexPatternTag[], config) => config.getIndexPatternTags ? tags.concat(config.getIndexPatternTags(indexPattern, isDefault)) @@ -41,14 +45,14 @@ export class IndexPatternListManager { ), getFieldInfo: (indexPattern: IIndexPattern, field: IFieldType): string[] => - this.configs.reduce( + getConfigs().reduce( (info: string[], config) => config.getFieldInfo ? info.concat(config.getFieldInfo(indexPattern, field)) : info, [] ), areScriptedFieldsEnabled: (indexPattern: IIndexPattern): boolean => - this.configs.every((config) => + getConfigs().every((config) => config.areScriptedFieldsEnabled ? config.areScriptedFieldsEnabled(indexPattern) : true ), };