From 831f32485e405444232a287fef6bc69ca38af83e Mon Sep 17 00:00:00 2001 From: Davis McPhee Date: Mon, 10 Jun 2024 22:12:39 -0300 Subject: [PATCH] Add registerProfileProviders and new example profiles --- .../public/context_awareness/index.ts | 1 + .../example_data_source_profile/index.ts | 9 +++ .../example_data_source_profile/profile.tsx} | 76 +++---------------- .../example_document_profile/index.ts | 9 +++ .../example_document_profile/profile.ts | 32 ++++++++ .../example_root_pofile/index.ts | 9 +++ .../example_root_pofile/profile.tsx | 37 +++++++++ .../profile_providers/index.ts | 9 +++ .../register_profile_providers.ts | 70 +++++++++++++++++ .../register_enabled_profiles.ts | 28 ------- src/plugins/discover/public/plugin.tsx | 51 +++++-------- 11 files changed, 204 insertions(+), 127 deletions(-) create mode 100644 src/plugins/discover/public/context_awareness/profile_providers/example_data_source_profile/index.ts rename src/plugins/discover/public/context_awareness/{profiles/example_profiles.tsx => profile_providers/example_data_source_profile/profile.tsx} (50%) create mode 100644 src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/index.ts create mode 100644 src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/profile.ts create mode 100644 src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/index.ts create mode 100644 src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/profile.tsx create mode 100644 src/plugins/discover/public/context_awareness/profile_providers/index.ts create mode 100644 src/plugins/discover/public/context_awareness/profile_providers/register_profile_providers.ts delete mode 100644 src/plugins/discover/public/context_awareness/register_enabled_profiles.ts diff --git a/src/plugins/discover/public/context_awareness/index.ts b/src/plugins/discover/public/context_awareness/index.ts index 6106d9d154e4..0fd11801d739 100644 --- a/src/plugins/discover/public/context_awareness/index.ts +++ b/src/plugins/discover/public/context_awareness/index.ts @@ -8,6 +8,7 @@ export * from './types'; export * from './profiles'; +export { registerProfileProviders } from './profile_providers'; export { getMergedAccessor } from './composable_profile'; export { ProfilesManager } from './profiles_manager'; export { useProfileAccessor, useRootProfile } from './hooks'; diff --git a/src/plugins/discover/public/context_awareness/profile_providers/example_data_source_profile/index.ts b/src/plugins/discover/public/context_awareness/profile_providers/example_data_source_profile/index.ts new file mode 100644 index 000000000000..032da55d4f6d --- /dev/null +++ b/src/plugins/discover/public/context_awareness/profile_providers/example_data_source_profile/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +export { exampleDataSourceProfileProvider } from './profile'; diff --git a/src/plugins/discover/public/context_awareness/profiles/example_profiles.tsx b/src/plugins/discover/public/context_awareness/profile_providers/example_data_source_profile/profile.tsx similarity index 50% rename from src/plugins/discover/public/context_awareness/profiles/example_profiles.tsx rename to src/plugins/discover/public/context_awareness/profile_providers/example_data_source_profile/profile.tsx index 3835337b2530..b0df1c423031 100644 --- a/src/plugins/discover/public/context_awareness/profiles/example_profiles.tsx +++ b/src/plugins/discover/public/context_awareness/profile_providers/example_data_source_profile/profile.tsx @@ -7,51 +7,20 @@ */ import { EuiBadge } from '@elastic/eui'; -import { - DataTableRecord, - getMessageFieldWithFallbacks, - LogDocumentOverview, -} from '@kbn/discover-utils'; +import type { DataTableRecord } from '@kbn/discover-utils'; import { isOfAggregateQueryType } from '@kbn/es-query'; import { getIndexPatternFromESQLQuery } from '@kbn/esql-utils'; import { euiThemeVars } from '@kbn/ui-theme'; import { capitalize } from 'lodash'; import React from 'react'; -import { DataSourceType, isDataSourceType } from '../../../common/data_sources'; -import { DataSourceCategory, DataSourceProfileProvider } from './data_source_profile'; -import { DocumentProfileProvider, DocumentType } from './document_profile'; -import { RootProfileProvider, SolutionType } from './root_profile'; +import { DataSourceType, isDataSourceType } from '../../../../common/data_sources'; +import { DataSourceCategory, DataSourceProfileProvider } from '../../profiles'; -export const o11yRootProfileProvider: RootProfileProvider = { - profileId: 'o11y-root-profile', - profile: {}, - resolve: (params) => { - if (params.solutionNavId === 'oblt') { - return { - isMatch: true, - context: { - solutionType: SolutionType.Observability, - }, - }; - } - - return { isMatch: false }; - }, -}; - -export const logsDataSourceProfileProvider: DataSourceProfileProvider = { - profileId: 'logs-data-source-profile', +export const exampleDataSourceProfileProvider: DataSourceProfileProvider = { + profileId: 'example-data-source-profile', profile: { getCellRenderers: (prev) => () => ({ ...prev(), - '@timestamp': (props) => { - const timestamp = getFieldValue(props.row, '@timestamp'); - return ( - - {timestamp} - - ); - }, 'log.level': (props) => { const level = getFieldValue(props.row, 'log.level'); if (!level) { @@ -68,12 +37,6 @@ export const logsDataSourceProfileProvider: DataSourceProfileProvider = { ); }, - message: (props) => { - const { value } = getMessageFieldWithFallbacks( - props.row.flattened as unknown as LogDocumentOverview - ); - return value || (None); - }, }), }, resolve: (params) => { @@ -89,31 +52,14 @@ export const logsDataSourceProfileProvider: DataSourceProfileProvider = { indices = params.dataView.getIndexPattern().split(','); } - if (indices.every((index) => index.startsWith('logs-'))) { - return { - isMatch: true, - context: { category: DataSourceCategory.Logs }, - }; - } - - return { isMatch: false }; - }, -}; - -export const logDocumentProfileProvider: DocumentProfileProvider = { - profileId: 'log-document-profile', - profile: {}, - resolve: (params) => { - if (getFieldValue(params.record, 'data_stream.type') === 'logs') { - return { - isMatch: true, - context: { - type: DocumentType.Log, - }, - }; + if (!indices.every((index) => index.startsWith('logs-'))) { + return { isMatch: false }; } - return { isMatch: false }; + return { + isMatch: true, + context: { category: DataSourceCategory.Logs }, + }; }, }; diff --git a/src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/index.ts b/src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/index.ts new file mode 100644 index 000000000000..dab5364d8af3 --- /dev/null +++ b/src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +export { exampleDocumentProfileProvider } from './profile'; diff --git a/src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/profile.ts b/src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/profile.ts new file mode 100644 index 000000000000..303efa103e32 --- /dev/null +++ b/src/plugins/discover/public/context_awareness/profile_providers/example_document_profile/profile.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import type { DataTableRecord } from '@kbn/discover-utils'; +import { DocumentProfileProvider, DocumentType } from '../../profiles'; + +export const exampleDocumentProfileProvider: DocumentProfileProvider = { + profileId: 'example-document-profile', + profile: {}, + resolve: (params) => { + if (getFieldValue(params.record, 'data_stream.type') !== 'logs') { + return { isMatch: false }; + } + + return { + isMatch: true, + context: { + type: DocumentType.Log, + }, + }; + }, +}; + +const getFieldValue = (record: DataTableRecord, field: string) => { + const value = record.flattened[field]; + return Array.isArray(value) ? value[0] : value; +}; diff --git a/src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/index.ts b/src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/index.ts new file mode 100644 index 000000000000..53f4cdc955db --- /dev/null +++ b/src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +export { exampleRootProfileProvider } from './profile'; diff --git a/src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/profile.tsx b/src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/profile.tsx new file mode 100644 index 000000000000..311f0356e737 --- /dev/null +++ b/src/plugins/discover/public/context_awareness/profile_providers/example_root_pofile/profile.tsx @@ -0,0 +1,37 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { EuiBadge } from '@elastic/eui'; +import type { DataTableRecord } from '@kbn/discover-utils'; +import React from 'react'; +import { RootProfileProvider, SolutionType } from '../../profiles'; + +export const exampleRootProfileProvider: RootProfileProvider = { + profileId: 'example-root-profile', + profile: { + getCellRenderers: (prev) => () => ({ + ...prev(), + '@timestamp': (props) => { + const timestamp = getFieldValue(props.row, '@timestamp'); + return ( + + {timestamp} + + ); + }, + }), + }, + resolve: () => { + return { isMatch: true, context: { solutionType: SolutionType.Default } }; + }, +}; + +const getFieldValue = (record: DataTableRecord, field: string) => { + const value = record.flattened[field]; + return Array.isArray(value) ? value[0] : value; +}; diff --git a/src/plugins/discover/public/context_awareness/profile_providers/index.ts b/src/plugins/discover/public/context_awareness/profile_providers/index.ts new file mode 100644 index 000000000000..1a5979f5a37f --- /dev/null +++ b/src/plugins/discover/public/context_awareness/profile_providers/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +export { registerProfileProviders } from './register_profile_providers'; diff --git a/src/plugins/discover/public/context_awareness/profile_providers/register_profile_providers.ts b/src/plugins/discover/public/context_awareness/profile_providers/register_profile_providers.ts new file mode 100644 index 000000000000..901b757501d0 --- /dev/null +++ b/src/plugins/discover/public/context_awareness/profile_providers/register_profile_providers.ts @@ -0,0 +1,70 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import type { + DataSourceProfileService, + DocumentProfileService, + RootProfileService, +} from '../profiles'; +import type { BaseProfileProvider, BaseProfileService } from '../profile_service'; +import { exampleDataSourceProfileProvider } from './example_data_source_profile'; +import { exampleDocumentProfileProvider } from './example_document_profile'; +import { exampleRootProfileProvider } from './example_root_pofile'; + +export const registerProfileProviders = ({ + rootProfileService, + dataSourceProfileService, + documentProfileService, + enabledProfileIds, +}: { + rootProfileService: RootProfileService; + dataSourceProfileService: DataSourceProfileService; + documentProfileService: DocumentProfileService; + enabledProfileIds: string[]; +}) => { + const rootProfileProviders = [exampleRootProfileProvider]; + const dataSourceProfileProviders = [exampleDataSourceProfileProvider]; + const documentProfileProviders = [exampleDocumentProfileProvider]; + + registerEnabledProfileProviders({ + profileService: rootProfileService, + availableProviders: rootProfileProviders, + enabledProfileIds, + }); + + registerEnabledProfileProviders({ + profileService: dataSourceProfileService, + availableProviders: dataSourceProfileProviders, + enabledProfileIds, + }); + + registerEnabledProfileProviders({ + profileService: documentProfileService, + availableProviders: documentProfileProviders, + enabledProfileIds, + }); +}; + +const registerEnabledProfileProviders = < + TProvider extends BaseProfileProvider<{}>, + TService extends BaseProfileService +>({ + profileService, + availableProviders, + enabledProfileIds, +}: { + profileService: TService; + availableProviders: TProvider[]; + enabledProfileIds: string[]; +}) => { + for (const profile of availableProviders) { + if (enabledProfileIds.includes(profile.profileId)) { + profileService.registerProvider(profile); + } + } +}; diff --git a/src/plugins/discover/public/context_awareness/register_enabled_profiles.ts b/src/plugins/discover/public/context_awareness/register_enabled_profiles.ts deleted file mode 100644 index 126e4c722d73..000000000000 --- a/src/plugins/discover/public/context_awareness/register_enabled_profiles.ts +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import type { BaseProfileProvider, BaseProfileService } from './profile_service'; - -export const registerEnabledProfiles = < - TProvider extends BaseProfileProvider<{}>, - TService extends BaseProfileService ->({ - profileService, - availableProviders, - enabledProfileIds, -}: { - profileService: TService; - availableProviders: TProvider[]; - enabledProfileIds: string[]; -}) => { - for (const profile of availableProviders) { - if (enabledProfileIds.includes(profile.profileId)) { - profileService.registerProvider(profile); - } - } -}; diff --git a/src/plugins/discover/public/plugin.tsx b/src/plugins/discover/public/plugin.tsx index 7ecea86ba6ac..5d391c1f9e1c 100644 --- a/src/plugins/discover/public/plugin.tsx +++ b/src/plugins/discover/public/plugin.tsx @@ -88,12 +88,6 @@ import { ProfilesManager, RootProfileService, } from './context_awareness'; -import { - logDocumentProfileProvider, - logsDataSourceProfileProvider, - o11yRootProfileProvider, -} from './context_awareness/profiles/example_profiles'; -import { registerEnabledProfiles } from './context_awareness/register_enabled_profiles'; /** * @public @@ -243,6 +237,7 @@ export class DiscoverPlugin private locator?: DiscoverAppLocator; private contextLocator?: DiscoverContextAppLocator; private singleDocLocator?: DiscoverSingleDocLocator; + private profileProvidersRegistered = false; constructor(private readonly initializerContext: PluginInitializerContext) { const experimental = this.initializerContext.config.get().experimental; @@ -346,7 +341,7 @@ export class DiscoverPlugin history: this.historyService.getHistory(), scopedHistory: this.scopedHistory, urlTracker: this.urlTracker!, - profilesManager: this.createProfilesManager(), + profilesManager: await this.createProfilesManager(), setHeaderActionMenu: params.setHeaderActionMenu, }); @@ -429,8 +424,6 @@ export class DiscoverPlugin } start(core: CoreStart, plugins: DiscoverStartPlugins): DiscoverStart { - this.registerProfiles(); - const viewSavedSearchAction = new ViewSavedSearchAction(core.application, this.locator!); plugins.uiActions.addTriggerAction('CONTEXT_MENU_TRIGGER', viewSavedSearchAction); @@ -466,32 +459,21 @@ export class DiscoverPlugin } } - private registerProfiles() { - const rootProfileProviders = [o11yRootProfileProvider]; - const dataSourceProfileProviders = [logsDataSourceProfileProvider]; - const documentProfileProviders = [logDocumentProfileProvider]; - const enabledProfileIds = this.experimentalFeatures.enabledProfiles ?? []; - - registerEnabledProfiles({ - profileService: this.rootProfileService, - availableProviders: rootProfileProviders, - enabledProfileIds, - }); + private async createProfilesManager() { + if (!this.profileProvidersRegistered) { + const { registerProfileProviders } = await import('./context_awareness'); + const enabledProfileIds = this.experimentalFeatures.enabledProfiles ?? []; - registerEnabledProfiles({ - profileService: this.dataSourceProfileService, - availableProviders: dataSourceProfileProviders, - enabledProfileIds, - }); + registerProfileProviders({ + rootProfileService: this.rootProfileService, + dataSourceProfileService: this.dataSourceProfileService, + documentProfileService: this.documentProfileService, + enabledProfileIds, + }); - registerEnabledProfiles({ - profileService: this.documentProfileService, - availableProviders: documentProfileProviders, - enabledProfileIds, - }); - } + this.profileProvidersRegistered = true; + } - private createProfilesManager() { return new ProfilesManager( this.rootProfileService, this.dataSourceProfileService, @@ -510,7 +492,7 @@ export class DiscoverPlugin private getDiscoverServices = ( core: CoreStart, plugins: DiscoverStartPlugins, - profilesManager = this.createProfilesManager() + profilesManager: ProfilesManager ) => { return buildServices({ core, @@ -536,7 +518,8 @@ export class DiscoverPlugin const getDiscoverServicesInternal = async () => { const [coreStart, deps] = await core.getStartServices(); - return this.getDiscoverServices(coreStart, deps); + const profilesManager = await this.createProfilesManager(); + return this.getDiscoverServices(coreStart, deps, profilesManager); }; const factory = new SearchEmbeddableFactory(getStartServices, getDiscoverServicesInternal);