From c1f421cc5352bbdab58deecf4f8d5cc420c9d9ae Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Sat, 3 Oct 2020 14:23:19 -0400 Subject: [PATCH 1/2] [Maps] Register gold+ feature use (#79011) (#79334) --- x-pack/plugins/maps/common/constants.ts | 1 + .../maps/public/actions/layer_actions.test.ts | 51 ++++++++++++++++ .../maps/public/actions/layer_actions.ts | 8 ++- .../blended_vector_layer.ts | 8 +++ .../maps/public/classes/layers/layer.tsx | 6 ++ .../layers/vector_layer/vector_layer.tsx | 4 ++ .../es_geo_grid_source/es_geo_grid_source.js | 9 +++ .../es_geo_grid_source.test.ts | 30 +++++++-- .../maps/public/classes/sources/source.ts | 6 ++ .../maps/public/index_pattern_util.test.ts | 5 +- .../plugins/maps/public/index_pattern_util.ts | 3 +- x-pack/plugins/maps/public/kibana_services.ts | 9 +-- .../plugins/maps/public/licensed_features.ts | 61 +++++++++++++++++++ x-pack/plugins/maps/public/meta.test.js | 6 +- x-pack/plugins/maps/public/meta.ts | 2 +- x-pack/plugins/maps/public/plugin.ts | 17 ++---- .../maps/public/selectors/map_selectors.ts | 6 +- 17 files changed, 197 insertions(+), 35 deletions(-) create mode 100644 x-pack/plugins/maps/public/actions/layer_actions.test.ts create mode 100644 x-pack/plugins/maps/public/licensed_features.ts diff --git a/x-pack/plugins/maps/common/constants.ts b/x-pack/plugins/maps/common/constants.ts index be891b6e59608..469a4023434a8 100644 --- a/x-pack/plugins/maps/common/constants.ts +++ b/x-pack/plugins/maps/common/constants.ts @@ -5,6 +5,7 @@ */ import { i18n } from '@kbn/i18n'; import { FeatureCollection } from 'geojson'; + export const EMS_APP_NAME = 'kibana'; export const EMS_CATALOGUE_PATH = 'ems/catalogue'; diff --git a/x-pack/plugins/maps/public/actions/layer_actions.test.ts b/x-pack/plugins/maps/public/actions/layer_actions.test.ts new file mode 100644 index 0000000000000..09a22dca271d7 --- /dev/null +++ b/x-pack/plugins/maps/public/actions/layer_actions.test.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { addLayer } from './layer_actions'; +import { LayerDescriptor } from '../../common/descriptor_types'; +import { LICENSED_FEATURES } from '../licensed_features'; + +const getStoreMock = jest.fn(); +const dispatchMock = jest.fn(); + +describe('layer_actions', () => { + afterEach(() => { + jest.resetAllMocks(); + }); + + describe('addLayer', () => { + const notifyLicensedFeatureUsageMock = jest.fn(); + + beforeEach(() => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../licensed_features').notifyLicensedFeatureUsage = (feature: LICENSED_FEATURES) => { + notifyLicensedFeatureUsageMock(feature); + }; + + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../selectors/map_selectors').getMapReady = () => { + return true; + }; + + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../selectors/map_selectors').createLayerInstance = () => { + return { + getLicensedFeatures() { + return [LICENSED_FEATURES.GEO_SHAPE_AGGS_GEO_TILE]; + }, + }; + }; + }); + + it('should register feature-use', async () => { + const action = addLayer(({} as unknown) as LayerDescriptor); + await action(dispatchMock, getStoreMock); + expect(notifyLicensedFeatureUsageMock).toHaveBeenCalledWith( + LICENSED_FEATURES.GEO_SHAPE_AGGS_GEO_TILE + ); + }); + }); +}); diff --git a/x-pack/plugins/maps/public/actions/layer_actions.ts b/x-pack/plugins/maps/public/actions/layer_actions.ts index 675bb14722889..19c9adfadd45a 100644 --- a/x-pack/plugins/maps/public/actions/layer_actions.ts +++ b/x-pack/plugins/maps/public/actions/layer_actions.ts @@ -14,6 +14,7 @@ import { getSelectedLayerId, getMapReady, getMapColors, + createLayerInstance, } from '../selectors/map_selectors'; import { FLYOUT_STATE } from '../reducers/ui'; import { cancelRequest } from '../reducers/non_serializable_instances'; @@ -42,6 +43,7 @@ import { ILayer } from '../classes/layers/layer'; import { IVectorLayer } from '../classes/layers/vector_layer/vector_layer'; import { LAYER_STYLE_TYPE, LAYER_TYPE } from '../../common/constants'; import { IVectorStyle } from '../classes/styles/vector/vector_style'; +import { notifyLicensedFeatureUsage } from '../licensed_features'; export function trackCurrentLayerState(layerId: string) { return { @@ -108,7 +110,7 @@ export function cloneLayer(layerId: string) { } export function addLayer(layerDescriptor: LayerDescriptor) { - return (dispatch: Dispatch, getState: () => MapStoreState) => { + return async (dispatch: Dispatch, getState: () => MapStoreState) => { const isMapReady = getMapReady(getState()); if (!isMapReady) { dispatch({ @@ -123,6 +125,10 @@ export function addLayer(layerDescriptor: LayerDescriptor) { layer: layerDescriptor, }); dispatch(syncDataForLayerId(layerDescriptor.id)); + + const layer = createLayerInstance(layerDescriptor); + const features = await layer.getLicensedFeatures(); + features.forEach(notifyLicensedFeatureUsage); }; } diff --git a/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts b/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts index 9b6a67ac28ad0..65a76f0c54ffb 100644 --- a/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts +++ b/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts @@ -38,6 +38,7 @@ import { VectorLayerDescriptor, } from '../../../../common/descriptor_types'; import { IVectorSource } from '../../sources/vector_source'; +import { LICENSED_FEATURES } from '../../../licensed_features'; const ACTIVE_COUNT_DATA_ID = 'ACTIVE_COUNT_DATA_ID'; @@ -327,4 +328,11 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer { super._syncData(syncContext, activeSource, activeStyle); } + + async getLicensedFeatures(): Promise { + return [ + ...(await this._clusterSource.getLicensedFeatures()), + ...(await this._documentSource.getLicensedFeatures()), + ]; + } } diff --git a/x-pack/plugins/maps/public/classes/layers/layer.tsx b/x-pack/plugins/maps/public/classes/layers/layer.tsx index d6bd5180375ce..d7fd5d34a9dd0 100644 --- a/x-pack/plugins/maps/public/classes/layers/layer.tsx +++ b/x-pack/plugins/maps/public/classes/layers/layer.tsx @@ -34,6 +34,7 @@ import { Attribution, ImmutableSourceProperty, ISource, SourceEditorArgs } from import { DataRequestContext } from '../../actions'; import { IStyle } from '../styles/style'; import { getJoinAggKey } from '../../../common/get_agg_key'; +import { LICENSED_FEATURES } from '../../licensed_features'; export interface ILayer { getBounds(dataRequestContext: DataRequestContext): Promise; @@ -91,6 +92,7 @@ export interface ILayer { showJoinEditor(): boolean; getJoinsDisabledReason(): string | null; isFittable(): Promise; + getLicensedFeatures(): Promise; } export type Footnote = { icon: ReactElement; @@ -538,4 +540,8 @@ export class AbstractLayer implements ILayer { supportsLabelsOnTop(): boolean { return false; } + + async getLicensedFeatures(): Promise { + return []; + } } diff --git a/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.tsx b/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.tsx index a2532d4e7b10e..c44ebcf969f7c 100644 --- a/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.tsx +++ b/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.tsx @@ -1090,4 +1090,8 @@ export class VectorLayer extends AbstractLayer { }); return targetFeature ? targetFeature : null; } + + async getLicensedFeatures() { + return await this._source.getLicensedFeatures(); + } } diff --git a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js index 89258f04612fd..181af6b17b7dd 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js +++ b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js @@ -24,12 +24,14 @@ import { MVT_GETGRIDTILE_API_PATH, GEOTILE_GRID_AGG_NAME, GEOCENTROID_AGG_NAME, + ES_GEO_FIELD_TYPE, } from '../../../../common/constants'; import { i18n } from '@kbn/i18n'; import { getDataSourceLabel } from '../../../../common/i18n_getters'; import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source'; import { DataRequestAbortError } from '../../util/data_request'; import { registerSource } from '../source_registry'; +import { LICENSED_FEATURES } from '../../../licensed_features'; import rison from 'rison-node'; import { getHttp } from '../../../kibana_services'; @@ -399,6 +401,13 @@ export class ESGeoGridSource extends AbstractESAggSource { return [VECTOR_SHAPE_TYPE.POINT]; } + + async getLicensedFeatures() { + const geoField = await this._getGeoField(); + return geoField.type === ES_GEO_FIELD_TYPE.GEO_SHAPE + ? [LICENSED_FEATURES.GEO_SHAPE_AGGS_GEO_TILE] + : []; + } } registerSource({ diff --git a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.test.ts b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.test.ts index 06df68283c434..3b1cf3293c0d3 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.test.ts +++ b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.test.ts @@ -4,10 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { MapExtent, VectorSourceRequestMeta } from '../../../../common/descriptor_types'; - -jest.mock('../../../kibana_services'); - -import { getIndexPatternService, getSearchService, getHttp } from '../../../kibana_services'; +import { getHttp, getIndexPatternService, getSearchService } from '../../../kibana_services'; import { ESGeoGridSource } from './es_geo_grid_source'; import { ES_GEO_FIELD_TYPE, @@ -16,6 +13,9 @@ import { SOURCE_TYPES, } from '../../../../common/constants'; import { SearchSource } from 'src/plugins/data/public'; +import { LICENSED_FEATURES } from '../../../licensed_features'; + +jest.mock('../../../kibana_services'); export class MockSearchSource { setField = jest.fn(); @@ -27,6 +27,8 @@ export class MockSearchSource { describe('ESGeoGridSource', () => { const geoFieldName = 'bar'; + + let esGeoFieldType = ES_GEO_FIELD_TYPE.GEO_POINT; const mockIndexPatternService = { get() { return { @@ -34,7 +36,7 @@ describe('ESGeoGridSource', () => { getByName() { return { name: geoFieldName, - type: ES_GEO_FIELD_TYPE.GEO_POINT, + type: esGeoFieldType, }; }, }, @@ -127,6 +129,11 @@ describe('ESGeoGridSource', () => { }); }); + afterEach(() => { + esGeoFieldType = ES_GEO_FIELD_TYPE.GEO_POINT; + jest.resetAllMocks(); + }); + const extent: MapExtent = { minLon: -160, minLat: -80, @@ -271,4 +278,17 @@ describe('ESGeoGridSource', () => { ); }); }); + + describe('Gold+ usage', () => { + it('Should have none for points', async () => { + expect(await geogridSource.getLicensedFeatures()).toEqual([]); + }); + + it('Should have shape-aggs for geo_shape', async () => { + esGeoFieldType = ES_GEO_FIELD_TYPE.GEO_SHAPE; + expect(await geogridSource.getLicensedFeatures()).toEqual([ + LICENSED_FEATURES.GEO_SHAPE_AGGS_GEO_TILE, + ]); + }); + }); }); diff --git a/x-pack/plugins/maps/public/classes/sources/source.ts b/x-pack/plugins/maps/public/classes/sources/source.ts index 946381817b8fc..c4fb5178c0b56 100644 --- a/x-pack/plugins/maps/public/classes/sources/source.ts +++ b/x-pack/plugins/maps/public/classes/sources/source.ts @@ -15,6 +15,7 @@ import { IField } from '../fields/field'; import { FieldFormatter, MAX_ZOOM, MIN_ZOOM } from '../../../common/constants'; import { AbstractSourceDescriptor } from '../../../common/descriptor_types'; import { OnSourceChangeArgs } from '../../connected_components/layer_panel/view'; +import { LICENSED_FEATURES } from '../../licensed_features'; export type SourceEditorArgs = { onChange: (...args: OnSourceChangeArgs[]) => void; @@ -66,6 +67,7 @@ export interface ISource { getValueSuggestions(field: IField, query: string): Promise; getMinZoom(): number; getMaxZoom(): number; + getLicensedFeatures(): Promise; } export class AbstractSource implements ISource { @@ -188,4 +190,8 @@ export class AbstractSource implements ISource { getMaxZoom() { return MAX_ZOOM; } + + async getLicensedFeatures(): Promise { + return []; + } } diff --git a/x-pack/plugins/maps/public/index_pattern_util.test.ts b/x-pack/plugins/maps/public/index_pattern_util.test.ts index ffcc6da52677a..010c847f96eba 100644 --- a/x-pack/plugins/maps/public/index_pattern_util.test.ts +++ b/x-pack/plugins/maps/public/index_pattern_util.test.ts @@ -5,6 +5,7 @@ */ jest.mock('./kibana_services', () => ({})); +jest.mock('./licensed_features', () => ({})); import { getSourceFields, @@ -69,7 +70,7 @@ describe('Gold+ licensing', () => { describe('basic license', () => { beforeEach(() => { // eslint-disable-next-line @typescript-eslint/no-var-requires - require('./kibana_services').getIsGoldPlus = () => false; + require('./licensed_features').getIsGoldPlus = () => false; }); describe('getAggregatableGeoFieldTypes', () => { @@ -92,7 +93,7 @@ describe('Gold+ licensing', () => { describe('gold license', () => { beforeEach(() => { // eslint-disable-next-line @typescript-eslint/no-var-requires - require('./kibana_services').getIsGoldPlus = () => true; + require('./licensed_features').getIsGoldPlus = () => true; }); describe('getAggregatableGeoFieldTypes', () => { test('Should add geo_shape field', () => { diff --git a/x-pack/plugins/maps/public/index_pattern_util.ts b/x-pack/plugins/maps/public/index_pattern_util.ts index bd2a14619ac41..7af1571a0bc5b 100644 --- a/x-pack/plugins/maps/public/index_pattern_util.ts +++ b/x-pack/plugins/maps/public/index_pattern_util.ts @@ -6,9 +6,10 @@ import { IFieldType, IndexPattern } from 'src/plugins/data/public'; import { i18n } from '@kbn/i18n'; -import { getIndexPatternService, getIsGoldPlus } from './kibana_services'; +import { getIndexPatternService } from './kibana_services'; import { indexPatterns } from '../../../../src/plugins/data/public'; import { ES_GEO_FIELD_TYPE, ES_GEO_FIELD_TYPES } from '../common/constants'; +import { getIsGoldPlus } from './licensed_features'; export function getGeoTileAggNotSupportedReason(field: IFieldType): string | null { if (!field.aggregatable) { diff --git a/x-pack/plugins/maps/public/kibana_services.ts b/x-pack/plugins/maps/public/kibana_services.ts index c1dfb61e9f3b6..b520e0cb2df01 100644 --- a/x-pack/plugins/maps/public/kibana_services.ts +++ b/x-pack/plugins/maps/public/kibana_services.ts @@ -5,17 +5,10 @@ */ import _ from 'lodash'; +import { CoreStart } from 'kibana/public'; import { MapsLegacyConfig } from '../../../../src/plugins/maps_legacy/config'; import { MapsConfigType } from '../config'; import { MapsPluginStartDependencies } from './plugin'; -import { CoreStart } from '../../../../src/core/public'; - -let licenseId: string | undefined; -export const setLicenseId = (latestLicenseId: string | undefined) => (licenseId = latestLicenseId); -export const getLicenseId = () => licenseId; -let isGoldPlus: boolean = false; -export const setIsGoldPlus = (igp: boolean) => (isGoldPlus = igp); -export const getIsGoldPlus = () => isGoldPlus; let kibanaVersion: string; export const setKibanaVersion = (version: string) => (kibanaVersion = version); diff --git a/x-pack/plugins/maps/public/licensed_features.ts b/x-pack/plugins/maps/public/licensed_features.ts new file mode 100644 index 0000000000000..67fa526da0cbd --- /dev/null +++ b/x-pack/plugins/maps/public/licensed_features.ts @@ -0,0 +1,61 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { ILicense, LicenseType } from '../../licensing/common/types'; +import { LicensingPluginSetup, LicensingPluginStart } from '../../licensing/public'; +import { APP_ID } from '../common/constants'; + +export enum LICENSED_FEATURES { + GEO_SHAPE_AGGS_GEO_TILE = 'GEO_SHAPE_AGGS_GEO_TILE', +} + +export interface LicensedFeatureDetail { + name: string; + license: LicenseType; +} + +export const LICENCED_FEATURES_DETAILS: Record = { + [LICENSED_FEATURES.GEO_SHAPE_AGGS_GEO_TILE]: { + name: 'geo_tile aggregation on geo_shape field-type', + license: 'gold', + }, +}; + +let licenseId: string | undefined; +let isGoldPlus: boolean = false; + +export const getLicenseId = () => licenseId; +export const getIsGoldPlus = () => isGoldPlus; + +export function registerLicensedFeatures(licensingPlugin: LicensingPluginSetup) { + for (const licensedFeature of Object.values(LICENSED_FEATURES)) { + licensingPlugin.featureUsage.register( + LICENCED_FEATURES_DETAILS[licensedFeature].name, + LICENCED_FEATURES_DETAILS[licensedFeature].license + ); + } +} + +let licensingPluginStart: LicensingPluginStart; +export function setLicensingPluginStart(licensingPlugin: LicensingPluginStart) { + licensingPluginStart = licensingPlugin; + licensingPluginStart.license$.subscribe((license: ILicense) => { + const gold = license.check(APP_ID, 'gold'); + isGoldPlus = gold.state === 'valid'; + licenseId = license.uid; + }); +} + +export function notifyLicensedFeatureUsage(licensedFeature: LICENSED_FEATURES) { + if (!licensingPluginStart) { + // eslint-disable-next-line no-console + console.error('May not call notifyLicensedFeatureUsage before plugin start'); + return; + } + licensingPluginStart.featureUsage.notifyUsage( + LICENCED_FEATURES_DETAILS[LICENSED_FEATURES[licensedFeature]].name + ); +} diff --git a/x-pack/plugins/maps/public/meta.test.js b/x-pack/plugins/maps/public/meta.test.js index 3486bf003aee0..c414c8a2d400e 100644 --- a/x-pack/plugins/maps/public/meta.test.js +++ b/x-pack/plugins/maps/public/meta.test.js @@ -12,14 +12,14 @@ jest.mock('@elastic/ems-client'); describe('default use without proxy', () => { beforeEach(() => { require('./kibana_services').getProxyElasticMapsServiceInMaps = () => false; - require('./kibana_services').getLicenseId = () => { - return 'foobarlicenseid'; - }; require('./kibana_services').getIsEmsEnabled = () => true; require('./kibana_services').getEmsTileLayerId = () => '123'; require('./kibana_services').getEmsFileApiUrl = () => 'https://file-api'; require('./kibana_services').getEmsTileApiUrl = () => 'https://tile-api'; require('./kibana_services').getEmsLandingPageUrl = () => 'http://test.com'; + require('./licensed_features').getLicenseId = () => { + return 'foobarlicenseid'; + }; }); test('should construct EMSClient with absolute file and tile API urls', async () => { diff --git a/x-pack/plugins/maps/public/meta.ts b/x-pack/plugins/maps/public/meta.ts index 5142793bede34..4eca6c3e671b7 100644 --- a/x-pack/plugins/maps/public/meta.ts +++ b/x-pack/plugins/maps/public/meta.ts @@ -18,7 +18,6 @@ import { } from '../common/constants'; import { getHttp, - getLicenseId, getIsEmsEnabled, getRegionmapLayers, getTilemap, @@ -29,6 +28,7 @@ import { getProxyElasticMapsServiceInMaps, getKibanaVersion, } from './kibana_services'; +import { getLicenseId } from './licensed_features'; export function getKibanaRegionList(): unknown[] { return getRegionmapLayers(); diff --git a/x-pack/plugins/maps/public/plugin.ts b/x-pack/plugins/maps/public/plugin.ts index 696964f0258d4..5b79863d0dd97 100644 --- a/x-pack/plugins/maps/public/plugin.ts +++ b/x-pack/plugins/maps/public/plugin.ts @@ -18,10 +18,8 @@ import { // @ts-ignore import { MapView } from './inspector/views/map_view'; import { - setIsGoldPlus, setKibanaCommonConfig, setKibanaVersion, - setLicenseId, setMapAppConfig, setStartServices, } from './kibana_services'; @@ -42,7 +40,6 @@ import { MapEmbeddableFactory } from './embeddable/map_embeddable_factory'; import { EmbeddableSetup } from '../../../../src/plugins/embeddable/public'; import { MapsXPackConfig, MapsConfigType } from '../config'; import { getAppTitle } from '../common/i18n_getters'; -import { ILicense } from '../../licensing/common/types'; import { lazyLoadMapModules } from './lazy_load_bundle'; import { MapsStartApi } from './api'; import { createSecurityLayerDescriptors, registerLayerWizard, registerSource } from './api'; @@ -50,8 +47,9 @@ import { SharePluginSetup, SharePluginStart } from '../../../../src/plugins/shar import { EmbeddableStart } from '../../../../src/plugins/embeddable/public'; import { MapsLegacyConfig } from '../../../../src/plugins/maps_legacy/config'; import { DataPublicPluginStart } from '../../../../src/plugins/data/public'; -import { LicensingPluginStart } from '../../licensing/public'; +import { LicensingPluginSetup, LicensingPluginStart } from '../../licensing/public'; import { StartContract as FileUploadStartContract } from '../../file_upload/public'; +import { registerLicensedFeatures, setLicensingPluginStart } from './licensed_features'; export interface MapsPluginSetupDependencies { inspector: InspectorSetupContract; @@ -60,6 +58,7 @@ export interface MapsPluginSetupDependencies { embeddable: EmbeddableSetup; mapsLegacy: { config: MapsLegacyConfig }; share: SharePluginSetup; + licensing: LicensingPluginSetup; } export interface MapsPluginStartDependencies { @@ -97,6 +96,8 @@ export class MapsPlugin } public setup(core: CoreSetup, plugins: MapsPluginSetupDependencies) { + registerLicensedFeatures(plugins.licensing); + const config = this._initializerContext.config.get(); setKibanaCommonConfig(plugins.mapsLegacy.config); setMapAppConfig(config); @@ -138,13 +139,7 @@ export class MapsPlugin } public start(core: CoreStart, plugins: MapsPluginStartDependencies): MapsStartApi { - if (plugins.licensing) { - plugins.licensing.license$.subscribe((license: ILicense) => { - const gold = license.check(APP_ID, 'gold'); - setIsGoldPlus(gold.state === 'valid'); - setLicenseId(license.uid); - }); - } + setLicensingPluginStart(plugins.licensing); plugins.uiActions.addTriggerAction(VISUALIZE_GEO_FIELD_TRIGGER, visualizeGeoFieldAction); setStartServices(core, plugins); diff --git a/x-pack/plugins/maps/public/selectors/map_selectors.ts b/x-pack/plugins/maps/public/selectors/map_selectors.ts index db4371e9cd590..4b5122050eb71 100644 --- a/x-pack/plugins/maps/public/selectors/map_selectors.ts +++ b/x-pack/plugins/maps/public/selectors/map_selectors.ts @@ -52,9 +52,9 @@ import { ITMSSource } from '../classes/sources/tms_source'; import { IVectorSource } from '../classes/sources/vector_source'; import { ILayer } from '../classes/layers/layer'; -function createLayerInstance( +export function createLayerInstance( layerDescriptor: LayerDescriptor, - inspectorAdapters: Adapters + inspectorAdapters?: Adapters ): ILayer { const source: ISource = createSourceInstance(layerDescriptor.sourceDescriptor, inspectorAdapters); @@ -94,7 +94,7 @@ function createLayerInstance( } } -function createSourceInstance(sourceDescriptor: any, inspectorAdapters: Adapters): ISource { +function createSourceInstance(sourceDescriptor: any, inspectorAdapters?: Adapters): ISource { const source = getSourceByType(sourceDescriptor.type); if (!source) { throw new Error(`Unrecognized sourceType ${sourceDescriptor.type}`); From 42fc027e1482402710efab53a7c15b951b6016c7 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Sat, 3 Oct 2020 14:23:36 -0400 Subject: [PATCH 2/2] [Maps] Simplify IDynamicStyle-api (#79217) (#79333) --- .../properties/dynamic_style_property.tsx | 54 ++++++++++++++----- .../classes/styles/vector/vector_style.tsx | 40 +++++--------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index 2bc819daeea90..98b58def905eb 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -6,7 +6,8 @@ import _ from 'lodash'; import React from 'react'; -import { Feature } from 'geojson'; +import { Feature, FeatureCollection } from 'geojson'; +import { FeatureIdentifier, Map as MbMap } from 'mapbox-gl'; import { AbstractStyleProperty, IStyleProperty } from './style_property'; import { DEFAULT_SIGMA } from '../vector_style_defaults'; import { @@ -44,20 +45,14 @@ export interface IDynamicStyleProperty extends IStyleProperty { isOrdinal(): boolean; supportsFieldMeta(): boolean; getFieldMetaRequest(): Promise; - supportsMbFeatureState(): boolean; - getMbLookupFunction(): MB_LOOKUP_FUNCTION; pluckOrdinalStyleMetaFromFeatures(features: Feature[]): RangeFieldMeta | null; pluckCategoricalStyleMetaFromFeatures(features: Feature[]): CategoryFieldMeta | null; getValueSuggestions(query: string): Promise; - - // Returns the name that should be used for accessing the data from the mb-style rule - // Depending on - // - whether the field is used for labeling, icon-orientation, or other properties (color, size, ...), `feature-state` and or `get` is used - // - whether the field was run through a field-formatter, a new dynamic field is created with the formatted-value - // The combination of both will inform what field-name (e.g. the "raw" field name from the properties, the "computed field-name" for an on-the-fly created property (e.g. for feature-state or field-formatting). - // todo: There is an existing limitation to .mvt backed sources, where the field-formatters are not applied. Here, the raw-data needs to be accessed. - getMbPropertyName(): string; - getMbPropertyValue(value: RawValue): RawValue; + enrichGeoJsonAndMbFeatureState( + featureCollection: FeatureCollection, + mbMap: MbMap, + mbSourceId: string + ): boolean; } export class DynamicStyleProperty @@ -356,6 +351,12 @@ export class DynamicStyleProperty ); } + // Returns the name that should be used for accessing the data from the mb-style rule + // Depending on + // - whether the field is used for labeling, icon-orientation, or other properties (color, size, ...), `feature-state` and or `get` is used + // - whether the field was run through a field-formatter, a new dynamic field is created with the formatted-value + // The combination of both will inform what field-name (e.g. the "raw" field name from the properties, the "computed field-name" for an on-the-fly created property (e.g. for feature-state or field-formatting). + // todo: There is an existing limitation to .mvt backed sources, where the field-formatters are not applied. Here, the raw-data needs to be accessed. getMbPropertyName() { if (!this._field) { return ''; @@ -385,6 +386,35 @@ export class DynamicStyleProperty // Calling `isOrdinal` would be equivalent. return this.supportsMbFeatureState() ? getNumericalMbFeatureStateValue(rawValue) : rawValue; } + + enrichGeoJsonAndMbFeatureState( + featureCollection: FeatureCollection, + mbMap: MbMap, + mbSourceId: string + ): boolean { + const supportsFeatureState = this.supportsMbFeatureState(); + const featureIdentifier: FeatureIdentifier = { + source: mbSourceId, + id: undefined, + }; + const featureState: Record = {}; + const targetMbName = this.getMbPropertyName(); + for (let i = 0; i < featureCollection.features.length; i++) { + const feature = featureCollection.features[i]; + const rawValue = feature.properties ? feature.properties[this.getFieldName()] : undefined; + const targetMbValue = this.getMbPropertyValue(rawValue); + if (supportsFeatureState) { + featureState[targetMbName] = targetMbValue; // the same value will be potentially overridden multiple times, if the name remains identical + featureIdentifier.id = feature.id; + mbMap.setFeatureState(featureIdentifier, featureState); + } else { + if (feature.properties) { + feature.properties[targetMbName] = targetMbValue; + } + } + } + return supportsFeatureState; + } } export function getNumericalMbFeatureStateValue(value: RawValue) { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx b/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx index 5d0d9712ef988..acb158636e0b3 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx @@ -641,7 +641,7 @@ export class VectorStyle implements IVectorStyle { featureCollection: FeatureCollection, mbMap: MbMap, mbSourceId: string - ) { + ): boolean { if (!featureCollection) { return false; } @@ -651,40 +651,24 @@ export class VectorStyle implements IVectorStyle { return false; } - const tmpFeatureIdentifier: FeatureIdentifier = { - source: '', - id: undefined, - }; - const tmpFeatureState: any = {}; - - for (let i = 0; i < featureCollection.features.length; i++) { - const feature = featureCollection.features[i]; - - for (let j = 0; j < dynamicStyleProps.length; j++) { - const dynamicStyleProp = dynamicStyleProps[j]; - const targetMbName = dynamicStyleProp.getMbPropertyName(); - const rawValue = feature.properties - ? feature.properties[dynamicStyleProp.getFieldName()] - : undefined; - const targetMbValue = dynamicStyleProp.getMbPropertyValue(rawValue); - if (dynamicStyleProp.supportsMbFeatureState()) { - tmpFeatureState[targetMbName] = targetMbValue; // the same value will be potentially overridden multiple times, if the name remains identical - } else { - if (feature.properties) { - feature.properties[targetMbName] = targetMbValue; - } - } + let shouldResetAllData = false; + for (let j = 0; j < dynamicStyleProps.length; j++) { + const dynamicStyleProp = dynamicStyleProps[j]; + const usedFeatureState = dynamicStyleProp.enrichGeoJsonAndMbFeatureState( + featureCollection, + mbMap, + mbSourceId + ); + if (!usedFeatureState) { + shouldResetAllData = true; } - tmpFeatureIdentifier.source = mbSourceId; - tmpFeatureIdentifier.id = feature.id; - mbMap.setFeatureState(tmpFeatureIdentifier, tmpFeatureState); } // returns boolean indicating if styles do not support feature-state and some values are stored in geojson properties // this return-value is used in an optimization for style-updates with mapbox-gl. // `true` indicates the entire data needs to reset on the source (otherwise the style-rules will not be reapplied) // `false` indicates the data does not need to be reset on the store, because styles are re-evaluated if they use featureState - return dynamicStyleProps.some((dynamicStyleProp) => !dynamicStyleProp.supportsMbFeatureState()); + return shouldResetAllData; } arePointsSymbolizedAsCircles() {