forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(discover-shared): add DiscoverFeaturesService
- Loading branch information
Marco Antonio Ghiani
committed
Apr 9, 2024
1 parent
3088891
commit 98645ae
Showing
9 changed files
with
227 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,97 @@ | ||
# Discover Shared | ||
|
||
A stateful layer to register shared features and provide an access point to discover without a direct dependency | ||
A stateful layer to register shared features and provide an access point to discover without a direct dependency. | ||
|
||
## Register new features | ||
|
||
The plugin exposes a service to register features that can be opinionatedly used in Discover on both the setup and start lifecycle hooks. | ||
|
||
Although this allows for greater flexibility, its purpose is not to customize Discover as a default choice but to be used as a solution to prevent cyclic dependency between plugins that interact with Discover. | ||
|
||
To register a new feature, let's take a more practical case. | ||
|
||
> _We want to introduce the LogsAIAssistant in the Discover flyout. Porting all the logic of the Observability AI Assistant into Discover is not an option, and we don't want Discover to directly depend on the AI Assistant codebase._ | ||
We can solve this case with some steps: | ||
|
||
### Define a feature registration contract | ||
|
||
First of all, we need to define an interface to which the plugin registering the AI Assistant and Discover can adhere. | ||
|
||
The `DiscoverFeaturesService` already defines a union of available features and uses them to strictly type the exposed registry from the discover_shared plugin, so we can update it with the new feature: | ||
|
||
```tsx | ||
// src/plugins/discover_shared/public/services/discover_features/types.ts | ||
|
||
export interface SecurityAIAssistantFeature { | ||
id: 'security-ai-assistant'; | ||
render: (/* Update with deps required for this integration */) => React.ReactNode; | ||
// Add any prop required for the feature | ||
} | ||
|
||
export interface ObservabilityLogsAIAssistantFeature { | ||
id: 'observability-logs-ai-assistant'; | ||
render: (deps: {doc: DocumentOverview}) => React.ReactNode; | ||
// Add any prop required for the feature | ||
} | ||
|
||
// This should be a union of all the available client features. | ||
export type DiscoverFeature = SecurityAIAssistantFeature | ObservabilityLogsAIAssistantFeature; | ||
``` | ||
|
||
### Discover consumes the registered feature | ||
|
||
Once we have an interface for the feature, Discover can now retrieve it and use its content if is registered by any app in Kibana. | ||
|
||
```tsx | ||
// Somewhere in the unified doc viewer | ||
|
||
function LogsOverviewAIAssistant ({ doc }) { | ||
const { discoverShared } = getUnifiedDocViewerServices(); | ||
|
||
const discoverShared = discoverShared.features.registry.getById('observability-logs-ai-assistant') | ||
|
||
if (logsAIAssistantFeature) { | ||
return logsAIAssistantFeature.render({ doc }) | ||
} | ||
} | ||
``` | ||
|
||
### Register the feature | ||
|
||
Having an interface for the feature and Discover consuming its definition, we are left with the registration part. | ||
|
||
For our example, we'll go to the logs app that owns the LogsAIAssistant codebase and register the feature: | ||
|
||
```tsx | ||
// x-pack/plugins/observability_solution/logs_shared/public/plugin.ts | ||
|
||
export class LogsSharedPlugin implements LogsSharedClientPluginClass { | ||
// The rest of the plugin implementation is hidden for a cleaner example | ||
|
||
public start(core: CoreStart, plugins: LogsSharedClientStartDeps) { | ||
const { observabilityAIAssistant } = plugins; | ||
|
||
const LogAIAssistant = createLogAIAssistant({ observabilityAIAssistant }); | ||
|
||
// Strict typing on the registry will let you know which features you can register | ||
plugins.discoverShared.features.registry.register({ | ||
id: 'observability-logs-ai-assistant', | ||
render: ({doc}) => <LogAIAssistant doc={doc}/> | ||
}) | ||
|
||
return { | ||
LogAIAssistant, | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
At this point, the feature should work correctly when registered and we have not created any direct dependency between the Discover and LogsShared apps. | ||
|
||
## Subscribe to feature changes | ||
|
||
If you need to subscribe and listen for changes on the features registry, it exposes 2 methods for interacting with it: | ||
|
||
- `discoverShared.features.registry.subscribe(callback)`: takes a callback and provides a unique argument, the Map of the registered features. | ||
- `discoverShared.features.registry.observe()`: returns the features registry as an observable, allowing for more flexible operations. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import './index.scss'; | ||
/* | ||
* 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 { DiscoverSharedPlugin } from './plugin'; | ||
|
||
// This exports static code and TypeScript types, | ||
// as well as, Kibana Platform `plugin()` initializer. | ||
export function plugin() { | ||
return new DiscoverSharedPlugin(); | ||
} | ||
|
||
export type { DiscoverSharedPluginSetup, DiscoverSharedPluginStart } from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...ugins/discover_shared/public/services/discover_features/discover_features_service.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { FeaturesRegistry } from '@kbn/discover-utils'; | ||
|
||
export const createDiscoverFeaturesServiceStartMock = () => ({ | ||
registry: new FeaturesRegistry(), | ||
}); |
26 changes: 26 additions & 0 deletions
26
src/plugins/discover_shared/public/services/discover_features/discover_features_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 { FeaturesRegistry } from '@kbn/discover-utils'; | ||
import { DiscoverFeature } from './types'; | ||
|
||
export class DiscoverFeaturesService { | ||
private registry: FeaturesRegistry<DiscoverFeature> = new FeaturesRegistry(); | ||
|
||
public setup() { | ||
return { | ||
registry: this.registry, | ||
}; | ||
} | ||
|
||
public start() { | ||
return { | ||
registry: this.registry, | ||
}; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/plugins/discover_shared/public/services/discover_features/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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 * from './discover_features_service'; | ||
export * from './types'; |
39 changes: 39 additions & 0 deletions
39
src/plugins/discover_shared/public/services/discover_features/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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 { FeaturesRegistry } from '@kbn/discover-utils'; | ||
|
||
/** | ||
* Features types | ||
* Here goes the contract definition for the client features that can be registered | ||
* and that will be consumed by Discover. | ||
*/ | ||
|
||
/** | ||
* Allow to register an AIAssistant scoped to investigate log entries. | ||
* It will be opinionatedly used as an additional tool to investigate a log document and | ||
* will be shown on the logs-overview preset tab of the UnifiedDocViewer. | ||
*/ | ||
export interface ObservabilityLogsAIAssistantFeature { | ||
id: 'observability-logs-ai-assistant'; | ||
render: (/* TODO: Update with deps required for this integration */) => React.ReactNode; | ||
} | ||
|
||
// This should be a union of all the available client features. | ||
export type DiscoverFeature = ObservabilityLogsAIAssistantFeature; | ||
|
||
/** | ||
* Service types | ||
*/ | ||
export interface DiscoverFeaturesServiceSetup { | ||
registry: FeaturesRegistry<DiscoverFeature>; | ||
} | ||
|
||
export interface DiscoverFeaturesServiceStart { | ||
registry: FeaturesRegistry<DiscoverFeature>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters