Skip to content

Commit

Permalink
updating based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Nov 4, 2020
1 parent a8444a2 commit c90abc2
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 220 deletions.
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,
};
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@
* under the License.
*/

export const semverGte = (semver1: string, semver2: string) => {
const regex = /^([0-9]+)\.([0-9]+)\.([0-9]+)$/;
const matches1 = regex.exec(semver1) as RegExpMatchArray;
const matches2 = regex.exec(semver2) as RegExpMatchArray;

const [, major1, minor1, patch1] = matches1;
const [, major2, minor2, patch2] = matches2;

return (
major1 > major2 ||
(major1 === major2 && (minor1 > minor2 || (minor1 === minor2 && patch1 >= patch2)))
);
};
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;
};
};
41 changes: 41 additions & 0 deletions src/plugins/embeddable/common/lib/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 { telemetryBaseEmbeddableInput } from './migrate_base_input';

export const getTelemetryFunction = (embeddables: CommonEmbeddableStartContract) => {
return (state: EmbeddableStateWithType, telemetryData: Record<string, any> = {}) => {
const enhancements: Record<string, any> = state.enhancements || {};
const factory = embeddables.getEmbeddableFactory(state.type);

let outputTelemetryData = telemetryBaseEmbeddableInput(state, telemetryData);
if (factory) {
outputTelemetryData = factory.telemetry(state, outputTelemetryData);
}
Object.keys(enhancements).map((key) => {
if (!enhancements[key]) return;
outputTelemetryData = embeddables
.getEnhancement(key)
.telemetry(enhancements[key], outputTelemetryData);
});

return outputTelemetryData;
};
};
5 changes: 5 additions & 0 deletions src/plugins/embeddable/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ export type EmbeddableInput = {
};

export type EmbeddableStateWithType = EmbeddableInput & { type: string };

export interface CommonEmbeddableStartContract {
getEmbeddableFactory: (embeddableFactoryId: string) => any;
getEnhancement: (enhancementId: string) => any;
}
119 changes: 15 additions & 104 deletions src/plugins/embeddable/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
Plugin,
ScopedHistory,
PublicAppInfo,
SavedObjectReference,
} from '../../../core/public';
import {
EmbeddableFactoryRegistry,
Expand All @@ -51,16 +50,16 @@ import {
} from './lib';
import { EmbeddableFactoryDefinition } from './lib/embeddables/embeddable_factory_definition';
import { EmbeddableStateTransfer } from './lib/state_transfer';
import {
baseEmbeddableMigrations,
extractBaseEmbeddableInput,
injectBaseEmbeddableInput,
telemetryBaseEmbeddableInput,
} from '../common/lib/migrate_base_input';
import { PersistableStateService, SerializableState } from '../../kibana_utils/common';
import { ATTRIBUTE_SERVICE_KEY, AttributeService } from './lib/attribute_service';
import { AttributeServiceOptions } from './lib/attribute_service/attribute_service';
import { EmbeddableStateWithType } from '../common/types';
import {
getExtractFunction,
getInjectFunction,
getMigrateFunction,
getTelemetryFunction,
} from '../common/lib';

export interface EmbeddableSetupDependencies {
data: DataPublicPluginSetup;
Expand Down Expand Up @@ -190,6 +189,11 @@ export class EmbeddablePublicPlugin implements Plugin<EmbeddableSetup, Embeddabl
/>
);

const commonContract = {
getEmbeddableFactory: this.getEmbeddableFactory,
getEnhancement: this.getEnhancement,
};

return {
getEmbeddableFactory: this.getEmbeddableFactory,
getEmbeddableFactories: this.getEmbeddableFactories,
Expand All @@ -209,10 +213,10 @@ export class EmbeddablePublicPlugin implements Plugin<EmbeddableSetup, Embeddabl
},
EmbeddablePanel: getEmbeddablePanelHoc(),
getEmbeddablePanel: getEmbeddablePanelHoc,
telemetry: this.telemetry,
extract: this.extract,
inject: this.inject,
migrate: this.migrate,
telemetry: getTelemetryFunction(commonContract),
extract: getExtractFunction(commonContract),
inject: getInjectFunction(commonContract),
migrate: getMigrateFunction(commonContract),
};
}

Expand All @@ -222,99 +226,6 @@ export class EmbeddablePublicPlugin implements Plugin<EmbeddableSetup, Embeddabl
}
}

private telemetry = (state: EmbeddableStateWithType, telemetryData: Record<string, any> = {}) => {
const enhancements: Record<string, any> = state.enhancements || {};
const factory = this.getEmbeddableFactory(state.id);

let telemetry = telemetryBaseEmbeddableInput(state, telemetryData);
if (factory) {
telemetry = factory.telemetry(state, telemetry);
}
Object.keys(enhancements).map((key) => {
if (!enhancements[key]) return;
telemetry = this.getEnhancement(key).telemetry(enhancements[key], telemetry);
});

return telemetry;
};

private extract = (state: EmbeddableStateWithType) => {
const enhancements = state.enhancements || {};
const factory = this.getEmbeddableFactory(state.id);

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 = this.getEnhancement(key).extract(
enhancements[key] as SerializableState
);
refs.push(...enhancementResult.references);
updatedInput.enhancements![key] = enhancementResult.state;
});

return {
state: updatedInput,
references: refs,
};
};

private inject = (state: EmbeddableStateWithType, references: SavedObjectReference[]) => {
const enhancements = state.enhancements || {};
const factory = this.getEmbeddableFactory(state.id);

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] = this.getEnhancement(key).inject(
enhancements[key] as SerializableState,
references
);
});

return updatedInput;
};

private migrate = (state: SerializableState, version: string) => {
const enhancements: SerializableState = (state.enhancements as SerializableState) || {};
const factory = this.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;
const migrations = this.getEnhancement(key).migrations;
if (!migrations[version]) return;
(updatedInput.enhancements as SerializableState)[key] = migrations[version](
enhancements[key] as SerializableState
);
});

return updatedInput;
};

private registerEnhancement = (enhancement: EnhancementRegistryDefinition) => {
if (this.enhancements.has(enhancement.id)) {
throw new Error(`enhancement with id ${enhancement.id} already exists in the registry`);
Expand Down
Loading

0 comments on commit c90abc2

Please sign in to comment.