Skip to content

Commit

Permalink
Embeddables/migrations (elastic#82296)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Nov 5, 2020
1 parent cf82180 commit 2b3787a
Show file tree
Hide file tree
Showing 37 changed files with 364 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ EmbeddableFactories create and initialize an embeddable instance
<b>Signature:</b>

```typescript
export interface EmbeddableFactory<TEmbeddableInput extends EmbeddableInput = EmbeddableInput, TEmbeddableOutput extends EmbeddableOutput = EmbeddableOutput, TEmbeddable extends IEmbeddable<TEmbeddableInput, TEmbeddableOutput> = IEmbeddable<TEmbeddableInput, TEmbeddableOutput>, TSavedObjectAttributes extends SavedObjectAttributes = SavedObjectAttributes> extends PersistableState<EmbeddableInput>
export interface EmbeddableFactory<TEmbeddableInput extends EmbeddableInput = EmbeddableInput, TEmbeddableOutput extends EmbeddableOutput = EmbeddableOutput, TEmbeddable extends IEmbeddable<TEmbeddableInput, TEmbeddableOutput> = IEmbeddable<TEmbeddableInput, TEmbeddableOutput>, TSavedObjectAttributes extends SavedObjectAttributes = SavedObjectAttributes> extends PersistableState<EmbeddableStateWithType>
```
## Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
export declare type EmbeddableFactoryDefinition<I extends EmbeddableInput = EmbeddableInput, O extends EmbeddableOutput = EmbeddableOutput, E extends IEmbeddable<I, O> = IEmbeddable<I, O>, T extends SavedObjectAttributes = SavedObjectAttributes> = Pick<EmbeddableFactory<I, O, E, T>, 'create' | 'type' | 'isEditable' | 'getDisplayName'> & Partial<Pick<EmbeddableFactory<I, O, E, T>, 'createFromSavedObject' | 'isContainerType' | 'getExplicitInput' | 'savedObjectMetaData' | 'canCreateNew' | 'getDefaultInput' | 'telemetry' | 'extract' | 'inject'>>;
export declare type EmbeddableFactoryDefinition<I extends EmbeddableInput = EmbeddableInput, O extends EmbeddableOutput = EmbeddableOutput, E extends IEmbeddable<I, O> = IEmbeddable<I, O>, T extends SavedObjectAttributes = SavedObjectAttributes> = Pick<EmbeddableFactory<I, O, E, T>, 'create' | 'type' | 'isEditable' | 'getDisplayName'> & Partial<Pick<EmbeddableFactory<I, O, E, T>, 'createFromSavedObject' | 'isContainerType' | 'getExplicitInput' | 'savedObjectMetaData' | 'canCreateNew' | 'getDefaultInput' | 'telemetry' | 'extract' | 'inject' | 'migrations'>>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
export interface EmbeddableStart extends PersistableState<EmbeddableInput>
export interface EmbeddableStart extends PersistableStateService<EmbeddableStateWithType>
```
## Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
export interface EmbeddableRegistryDefinition<P extends EmbeddableInput = EmbeddableInput> extends PersistableStateDefinition<P>
export interface EmbeddableRegistryDefinition<P extends EmbeddableStateWithType = EmbeddableStateWithType> extends PersistableStateDefinition<P>
```
## Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
export declare class Executor<Context extends Record<string, unknown> = Record<string, unknown>> implements PersistableState<ExpressionAstExpression>
export declare class Executor<Context extends Record<string, unknown> = Record<string, unknown>> implements PersistableStateService<ExpressionAstExpression>
```
## Constructors
Expand Down Expand Up @@ -40,7 +40,6 @@ export declare class Executor<Context extends Record<string, unknown> = Record<s
| [getTypes()](./kibana-plugin-plugins-expressions-public.executor.gettypes.md) | | |
| [inject(ast, references)](./kibana-plugin-plugins-expressions-public.executor.inject.md) | | |
| [migrate(ast, version)](./kibana-plugin-plugins-expressions-public.executor.migrate.md) | | |
| [migrateToLatest(ast, version)](./kibana-plugin-plugins-expressions-public.executor.migratetolatest.md) | | |
| [registerFunction(functionDefinition)](./kibana-plugin-plugins-expressions-public.executor.registerfunction.md) | | |
| [registerType(typeDefinition)](./kibana-plugin-plugins-expressions-public.executor.registertype.md) | | |
| [run(ast, input, params)](./kibana-plugin-plugins-expressions-public.executor.run.md) | | Execute expression and return result. |
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ so that JSDoc appears in developers IDE when they use those `plugins.expressions
<b>Signature:</b>

```typescript
export declare class ExpressionsService implements PersistableState<ExpressionAstExpression>
export declare class ExpressionsService implements PersistableStateService<ExpressionAstExpression>
```
## Constructors
Expand All @@ -39,8 +39,7 @@ export declare class ExpressionsService implements PersistableState<ExpressionAs
| [getType](./kibana-plugin-plugins-expressions-public.expressionsservice.gettype.md) | | <code>ExpressionsServiceStart['getType']</code> | |
| [getTypes](./kibana-plugin-plugins-expressions-public.expressionsservice.gettypes.md) | | <code>() =&gt; ReturnType&lt;Executor['getTypes']&gt;</code> | Returns POJO map of all registered expression types, where keys are names of the types and values are <code>ExpressionType</code> instances. |
| [inject](./kibana-plugin-plugins-expressions-public.expressionsservice.inject.md) | | <code>(state: ExpressionAstExpression, references: SavedObjectReference[]) =&gt; ExpressionAstExpression</code> | Injects saved object references into expression AST |
| [migrate](./kibana-plugin-plugins-expressions-public.expressionsservice.migrate.md) | | <code>(state: SerializableState, version: string) =&gt; ExpressionAstExpression</code> | Injects saved object references into expression AST |
| [migrateToLatest](./kibana-plugin-plugins-expressions-public.expressionsservice.migratetolatest.md) | | <code>(state: unknown, version: string) =&gt; ExpressionAstExpression</code> | Injects saved object references into expression AST |
| [migrate](./kibana-plugin-plugins-expressions-public.expressionsservice.migrate.md) | | <code>(state: SerializableState, version: string) =&gt; ExpressionAstExpression</code> | Runs the migration (if it exists) for specified version. This will run a single migration step (ie from 7.10.0 to 7.10.1) |
| [registerFunction](./kibana-plugin-plugins-expressions-public.expressionsservice.registerfunction.md) | | <code>(functionDefinition: AnyExpressionFunctionDefinition &#124; (() =&gt; AnyExpressionFunctionDefinition)) =&gt; void</code> | Register an expression function, which will be possible to execute as part of the expression pipeline.<!-- -->Below we register a function which simply sleeps for given number of milliseconds to delay the execution and outputs its input as-is.
```ts
expressions.registerFunction({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## ExpressionsService.migrate property

Injects saved object references into expression AST
Runs the migration (if it exists) for specified version. This will run a single migration step (ie from 7.10.0 to 7.10.1)

<b>Signature:</b>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
export declare class Executor<Context extends Record<string, unknown> = Record<string, unknown>> implements PersistableState<ExpressionAstExpression>
export declare class Executor<Context extends Record<string, unknown> = Record<string, unknown>> implements PersistableStateService<ExpressionAstExpression>
```
## Constructors
Expand Down Expand Up @@ -40,7 +40,6 @@ export declare class Executor<Context extends Record<string, unknown> = Record<s
| [getTypes()](./kibana-plugin-plugins-expressions-server.executor.gettypes.md) | | |
| [inject(ast, references)](./kibana-plugin-plugins-expressions-server.executor.inject.md) | | |
| [migrate(ast, version)](./kibana-plugin-plugins-expressions-server.executor.migrate.md) | | |
| [migrateToLatest(ast, version)](./kibana-plugin-plugins-expressions-server.executor.migratetolatest.md) | | |
| [registerFunction(functionDefinition)](./kibana-plugin-plugins-expressions-server.executor.registerfunction.md) | | |
| [registerType(typeDefinition)](./kibana-plugin-plugins-expressions-server.executor.registertype.md) | | |
| [run(ast, input, params)](./kibana-plugin-plugins-expressions-server.executor.run.md) | | Execute expression and return result. |
Expand Down

This file was deleted.

54 changes: 54 additions & 0 deletions src/plugins/embeddable/common/lib/extract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.
*/

import { CommonEmbeddableStartContract, EmbeddableStateWithType } from '../types';
import { extractBaseEmbeddableInput } from './migrate_base_input';
import { SerializableState } from '../../../kibana_utils/common/persistable_state';

export const getExtractFunction = (embeddables: CommonEmbeddableStartContract) => {
return (state: EmbeddableStateWithType) => {
const enhancements = state.enhancements || {};
const factory = embeddables.getEmbeddableFactory(state.type);

const baseResponse = extractBaseEmbeddableInput(state);
let updatedInput = baseResponse.state;
const refs = baseResponse.references;

if (factory) {
const factoryResponse = factory.extract(state);
updatedInput = factoryResponse.state;
refs.push(...factoryResponse.references);
}

updatedInput.enhancements = {};
Object.keys(enhancements).forEach((key) => {
if (!enhancements[key]) return;
const enhancementResult = embeddables
.getEnhancement(key)
.extract(enhancements[key] as SerializableState);
refs.push(...enhancementResult.references);
updatedInput.enhancements![key] = enhancementResult.state;
});

return {
state: updatedInput,
references: refs,
};
};
};
24 changes: 24 additions & 0 deletions src/plugins/embeddable/common/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 * from './extract';
export * from './inject';
export * from './migrate';
export * from './migrate_base_input';
export * from './telemetry';
46 changes: 46 additions & 0 deletions src/plugins/embeddable/common/lib/inject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/

import { CommonEmbeddableStartContract, EmbeddableStateWithType } from '../types';
import { SavedObjectReference } from '../../../../core/types';
import { injectBaseEmbeddableInput } from './migrate_base_input';
import { SerializableState } from '../../../kibana_utils/common/persistable_state';

export const getInjectFunction = (embeddables: CommonEmbeddableStartContract) => {
return (state: EmbeddableStateWithType, references: SavedObjectReference[]) => {
const enhancements = state.enhancements || {};
const factory = embeddables.getEmbeddableFactory(state.type);

let updatedInput = injectBaseEmbeddableInput(state, references);

if (factory) {
updatedInput = factory.inject(updatedInput, references);
}

updatedInput.enhancements = {};
Object.keys(enhancements).forEach((key) => {
if (!enhancements[key]) return;
updatedInput.enhancements![key] = embeddables
.getEnhancement(key)
.inject(enhancements[key] as SerializableState, references);
});

return updatedInput;
};
};
47 changes: 47 additions & 0 deletions src/plugins/embeddable/common/lib/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.
*/

import { CommonEmbeddableStartContract } from '../types';
import { baseEmbeddableMigrations } from './migrate_base_input';
import { SerializableState } from '../../../kibana_utils/common/persistable_state';

export const getMigrateFunction = (embeddables: CommonEmbeddableStartContract) => {
return (state: SerializableState, version: string) => {
const enhancements = (state.enhancements as SerializableState) || {};
const factory = embeddables.getEmbeddableFactory(state.type as string);

let updatedInput = baseEmbeddableMigrations[version]
? baseEmbeddableMigrations[version](state)
: state;

if (factory && factory.migrations[version]) {
updatedInput = factory.migrations[version](updatedInput);
}

updatedInput.enhancements = {};
Object.keys(enhancements).forEach((key) => {
if (!enhancements[key]) return;
(updatedInput.enhancements! as Record<string, any>)[key] = embeddables
.getEnhancement(key)
.migrations[version](enhancements[key] as SerializableState);
});

return updatedInput;
};
};
11 changes: 7 additions & 4 deletions src/plugins/embeddable/common/lib/migrate_base_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@
*/

import { SavedObjectReference } from '../../../../core/types';
import { EmbeddableInput } from '../types';
import { EmbeddableStateWithType } from '../types';
import { MigrateFunctionsObject } from '../../../kibana_utils/common';

export const telemetryBaseEmbeddableInput = (
state: EmbeddableInput,
state: EmbeddableStateWithType,
telemetryData: Record<string, any>
) => {
return telemetryData;
};

export const extractBaseEmbeddableInput = (state: EmbeddableInput) => {
export const extractBaseEmbeddableInput = (state: EmbeddableStateWithType) => {
return { state, references: [] as SavedObjectReference[] };
};

export const injectBaseEmbeddableInput = (
state: EmbeddableInput,
state: EmbeddableStateWithType,
references: SavedObjectReference[]
) => {
return state;
};

export const baseEmbeddableMigrations: MigrateFunctionsObject = {};
Loading

0 comments on commit 2b3787a

Please sign in to comment.