Skip to content

Commit

Permalink
[maps] fix geo line source not loaded unless maps application is open…
Browse files Browse the repository at this point in the history
…ed (elastic#159432)

closes elastic#159408

PR consolidates source registry into a single file to ensure that all
sources are registered when only map embeddable is loaded. To prevent
regression, unit test added to ensure that all SOURCE_TYPES enum values
are contained in registry.

---------

Co-authored-by: kibanamachine <[email protected]>
(cherry picked from commit cb7c5b3)
  • Loading branch information
nreese committed Jun 12, 2023
1 parent d252e30 commit 9041115
Show file tree
Hide file tree
Showing 25 changed files with 217 additions and 86 deletions.
7 changes: 7 additions & 0 deletions x-pack/plugins/maps/public/actions/layer_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ jest.mock('../kibana_services', () => {
getMapsCapabilities() {
return { save: true };
},
getEMSSettings() {
return {
isEMSUrlSet() {
return false;
},
};
},
};
});

Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/maps/public/actions/tooltip_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
* 2.0.
*/

jest.mock('../kibana_services', () => ({
getEMSSettings() {
return {
isEMSUrlSet() {
return false;
},
};
},
}));

import { TooltipState } from '../../common/descriptor_types';
import { openOnClickTooltip } from './tooltip_actions';
import { MapStoreState } from '../reducers/store';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import { VectorStyle } from '../../../styles/vector/vector_style';
import { GeoJsonVectorLayer, MvtVectorLayer } from '../../vector_layer';
import { EMSFileSource } from '../../../sources/ems_file_source';
// @ts-ignore
import { ESSearchSource } from '../../../sources/es_search_source';
import { getDefaultDynamicProperties } from '../../../styles/vector/vector_style_defaults';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { AbstractSourceDescriptor } from '../../../common/descriptor_types';
import { ISource } from './source';
import { getSourceByType } from './source_registry';
import { setupSources } from './setup_sources';

setupSources();

export function createSourceInstance(sourceDescriptor: AbstractSourceDescriptor | null): ISource {
if (sourceDescriptor === null) {
throw new Error('Source-descriptor should be initialized');
}
const source = getSourceByType(sourceDescriptor.type);
if (!source) {
throw new Error(`Unrecognized sourceType ${sourceDescriptor.type}`);
}
return new source.ConstructorFunction(sourceDescriptor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { getEmsFileLayers } from '../../../util';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { UpdateSourceEditor } from './update_source_editor';
import { EMSFileField } from '../../fields/ems_file_field';
import { registerSource } from '../source_registry';
import { IField } from '../../fields/field';
import { EMSFileSourceDescriptor } from '../../../../common/descriptor_types';
import { ITooltipProperty } from '../../tooltips/tooltip_property';
Expand Down Expand Up @@ -218,8 +217,3 @@ export class EMSFileSource extends AbstractVectorSource implements IEmsFileSourc
return emsSettings.isEMSUrlSet() ? [LICENSED_FEATURES.ON_PREM_EMS] : [];
}
}

registerSource({
ConstructorFunction: EMSFileSource,
type: SOURCE_TYPES.EMS_FILE,
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { SOURCE_TYPES } from '../../../../common/constants';
import { EMSTMSSourceDescriptor } from '../../../../common/descriptor_types';
import { getEmsTileLayerId, getIsDarkMode, getEMSSettings } from '../../../kibana_services';
import { registerSource } from '../source_registry';
import { getEmsUnavailableMessage } from '../../../components/ems_unavailable_message';
import { LICENSED_FEATURES } from '../../../licensed_features';

Expand Down Expand Up @@ -169,8 +168,3 @@ export class EMSTMSSource extends AbstractSource implements ITMSSource {
return emsSettings.isEMSUrlSet() ? [LICENSED_FEATURES.ON_PREM_EMS] : [];
}
}

registerSource({
ConstructorFunction: EMSTMSSource,
type: SOURCE_TYPES.EMS_TMS,
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { getDataSourceLabel, getDataViewLabel } from '../../../../common/i18n_ge
import { buildGeoGridFilter } from '../../../../common/elasticsearch_util';
import { AbstractESAggSource } from '../es_agg_source';
import { DataRequestAbortError } from '../../util/data_request';
import { registerSource } from '../source_registry';
import { LICENSED_FEATURES } from '../../../licensed_features';

import { getHttp } from '../../../kibana_services';
Expand Down Expand Up @@ -627,8 +626,3 @@ export class ESGeoGridSource extends AbstractESAggSource implements IMvtVectorSo
];
}
}

registerSource({
ConstructorFunction: ESGeoGridSource,
type: SOURCE_TYPES.ES_GEO_GRID,
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { getDataSourceLabel, getDataViewLabel } from '../../../../common/i18n_getters';
import { AbstractESAggSource } from '../es_agg_source';
import { DataRequest } from '../../util/data_request';
import { registerSource } from '../source_registry';
import { convertToGeoJson } from './convert_to_geojson';
import { ESDocField } from '../../fields/es_doc_field';
import { UpdateSourceEditor } from './update_source_editor';
Expand Down Expand Up @@ -404,8 +403,3 @@ export class ESGeoLineSource extends AbstractESAggSource {
return [LICENSED_FEATURES.GEO_LINE_AGG];
}
}

registerSource({
ConstructorFunction: ESGeoLineSource,
type: SOURCE_TYPES.ES_GEO_LINE,
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { SOURCE_TYPES, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
import { getDataSourceLabel, getDataViewLabel } from '../../../../common/i18n_getters';
import { convertToLines } from './convert_to_lines';
import { AbstractESAggSource } from '../es_agg_source';
import { registerSource } from '../source_registry';
import { turfBboxToBounds } from '../../../../common/elasticsearch_util';
import { DataRequestAbortError } from '../../util/data_request';
import { mergeExecutionContext } from '../execution_context_utils';
Expand Down Expand Up @@ -295,8 +294,3 @@ export class ESPewPewSource extends AbstractESAggSource {
return true;
}
}

registerSource({
ConstructorFunction: ESPewPewSource,
type: SOURCE_TYPES.ES_PEW_PEW,
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import { getSourceFields } from '../../../index_pattern_util';
import { loadIndexSettings } from './util/load_index_settings';
import { DEFAULT_FILTER_BY_MAP_BOUNDS } from './constants';
import { ESDocField } from '../../fields/es_doc_field';
import { registerSource } from '../source_registry';
import {
DataRequestMeta,
ESSearchSourceDescriptor,
Expand Down Expand Up @@ -1009,8 +1008,3 @@ export class ESSearchSource extends AbstractESSource implements IMvtVectorSource
: [];
}
}

registerSource({
ConstructorFunction: ESSearchSource,
type: SOURCE_TYPES.ES_SEARCH,
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
GeojsonFileSourceDescriptor,
MapExtent,
} from '../../../../common/descriptor_types';
import { registerSource } from '../source_registry';
import { IField } from '../../fields/field';
import { getFeatureCollectionBounds } from '../../util/get_feature_collection_bounds';
import { InlineField } from '../../fields/inline_field';
Expand Down Expand Up @@ -135,8 +134,3 @@ export class GeoJsonFileSource extends AbstractVectorSource {
return (this._descriptor as GeojsonFileSourceDescriptor).__featureCollection;
}
}

registerSource({
ConstructorFunction: GeoJsonFileSource,
type: SOURCE_TYPES.GEOJSON_FILE,
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { i18n } from '@kbn/i18n';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import _ from 'lodash';
import { SOURCE_TYPES } from '../../../../common/constants';
import { registerSource } from '../source_registry';
import { extractAttributions } from './extract_attributions';

export const sourceTitle = i18n.translate('xpack.maps.source.kbnTMSTitle', {
Expand Down Expand Up @@ -87,8 +86,3 @@ export class KibanaTilemapSource extends AbstractSource {
}
}
}

registerSource({
ConstructorFunction: KibanaTilemapSource,
type: SOURCE_TYPES.KIBANA_TILEMAP,
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
SOURCE_TYPES,
VECTOR_SHAPE_TYPE,
} from '../../../../common/constants';
import { registerSource } from '../source_registry';
import { getDataSourceLabel, getUrlLabel } from '../../../../common/i18n_getters';
import {
MapExtent,
Expand Down Expand Up @@ -250,8 +249,3 @@ export class MVTSingleLayerVectorSource extends AbstractSource implements IMvtVe
return [];
}
}

registerSource({
ConstructorFunction: MVTSingleLayerVectorSource,
type: SOURCE_TYPES.MVT_SINGLE_LAYER,
});
45 changes: 45 additions & 0 deletions x-pack/plugins/maps/public/classes/sources/setup_sources.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

jest.mock('../../kibana_services', () => {
return {
getEMSSettings() {
return {
isEMSUrlSet() {
return false;
},
};
},
};
});

import { setupSources } from './setup_sources';
import { SOURCE_TYPES } from '../../../common/constants';
import { getSourceByType } from './source_registry';

const EXPECTED_UNREGISTERED_SOURCE_TYPES = [
SOURCE_TYPES.ES_ML_ANOMALIES, // registered in ML plugin
// join sources are not contained in source registry
SOURCE_TYPES.ES_DISTANCE_SOURCE,
SOURCE_TYPES.ES_TERM_SOURCE,
SOURCE_TYPES.TABLE_SOURCE,
];

test('should register all Elastic Maps sources', () => {
setupSources();

Object.values(SOURCE_TYPES)
.filter((sourceType) => {
return !EXPECTED_UNREGISTERED_SOURCE_TYPES.includes(sourceType);
})
.forEach((sourceType) => {
const entry = getSourceByType(sourceType);
if (!entry) {
throw new Error(`Required source type "${sourceType}" not registered.`);
}
});
});
85 changes: 85 additions & 0 deletions x-pack/plugins/maps/public/classes/sources/setup_sources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { SOURCE_TYPES } from '../../../common/constants';
import { registerSource } from './source_registry';
import { EMSFileSource } from './ems_file_source';
import { EMSTMSSource } from './ems_tms_source';
import { ESGeoGridSource } from './es_geo_grid_source';
import { ESGeoLineSource } from './es_geo_line_source';
import { ESPewPewSource } from './es_pew_pew_source';
import { ESSearchSource } from './es_search_source';
import { GeoJsonFileSource } from './geojson_file_source';
import { KibanaTilemapSource } from './kibana_tilemap_source';
import { MVTSingleLayerVectorSource } from './mvt_single_layer_vector_source';
import { WMSSource } from './wms_source';
import { XYZTMSSource } from './xyz_tms_source';

let registered = false;

export function setupSources() {
if (registered) {
return;
}

registerSource({
ConstructorFunction: EMSFileSource,
type: SOURCE_TYPES.EMS_FILE,
});

registerSource({
ConstructorFunction: EMSTMSSource,
type: SOURCE_TYPES.EMS_TMS,
});

registerSource({
ConstructorFunction: ESGeoGridSource,
type: SOURCE_TYPES.ES_GEO_GRID,
});

registerSource({
ConstructorFunction: ESGeoLineSource,
type: SOURCE_TYPES.ES_GEO_LINE,
});

registerSource({
ConstructorFunction: ESPewPewSource,
type: SOURCE_TYPES.ES_PEW_PEW,
});

registerSource({
ConstructorFunction: ESSearchSource,
type: SOURCE_TYPES.ES_SEARCH,
});

registerSource({
ConstructorFunction: GeoJsonFileSource,
type: SOURCE_TYPES.GEOJSON_FILE,
});

registerSource({
ConstructorFunction: KibanaTilemapSource,
type: SOURCE_TYPES.KIBANA_TILEMAP,
});

registerSource({
ConstructorFunction: MVTSingleLayerVectorSource,
type: SOURCE_TYPES.MVT_SINGLE_LAYER,
});

registerSource({
ConstructorFunction: WMSSource,
type: SOURCE_TYPES.WMS,
});

registerSource({
ConstructorFunction: XYZTMSSource,
type: SOURCE_TYPES.EMS_XYZ,
});

registered = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { getDataSourceLabel, getUrlLabel } from '../../../../common/i18n_getters
// @ts-ignore
import { WmsClient } from './wms_client';
import { SOURCE_TYPES } from '../../../../common/constants';
import { registerSource } from '../source_registry';
import { IRasterSource, RasterTileSourceData } from '../raster_source';
import { WMSSourceDescriptor } from '../../../../common/descriptor_types';
export const sourceTitle = i18n.translate('xpack.maps.source.wmsTitle', {
Expand Down Expand Up @@ -80,8 +79,3 @@ export class WMSSource extends AbstractSource implements IRasterSource {
return client.getUrlTemplate(this._descriptor.layers, this._descriptor.styles || '');
}
}

registerSource({
ConstructorFunction: WMSSource,
type: SOURCE_TYPES.WMS,
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ReactElement } from 'react';
import { RasterTileSource } from 'maplibre-gl';
import { getDataSourceLabel, getUrlLabel } from '../../../../common/i18n_getters';
import { SOURCE_TYPES } from '../../../../common/constants';
import { registerSource } from '../source_registry';
import {
XYZTMSSourceDescriptor,
DataRequestMeta,
Expand Down Expand Up @@ -89,8 +88,3 @@ export class XYZTMSSource extends AbstractSource implements IRasterSource {
return canSkip;
}
}

registerSource({
ConstructorFunction: XYZTMSSource,
type: SOURCE_TYPES.EMS_XYZ,
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ jest.mock('../../kibana_services', () => {
getCore() {
return {};
},
getEMSSettings() {
return {
isEMSUrlSet() {
return false;
},
};
},
};
});

Expand Down
Loading

0 comments on commit 9041115

Please sign in to comment.