Skip to content

Commit

Permalink
[Maps] Add suggest EMS layer utility (elastic#94969)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Apr 1, 2021
1 parent 95f3773 commit 3e79172
Show file tree
Hide file tree
Showing 37 changed files with 506 additions and 94 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/maps/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,7 @@ export type FieldFormatter = (value: RawValue) => string | number;
export const INDEX_META_DATA_CREATED_BY = 'maps-drawing-data-ingest';

export const MAX_DRAWING_SIZE_BYTES = 10485760; // 10MB

export const emsWorldLayerId = 'world_countries';
export const emsRegionLayerId = 'administrative_regions_lvl2';
export const emsUsaZipLayerId = 'usa_zip_codes';
16 changes: 16 additions & 0 deletions x-pack/plugins/maps/public/api/ems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 { EMSTermJoinConfig, SampleValuesConfig } from '../ems_autosuggest';
import { lazyLoadMapModules } from '../lazy_load_bundle';

export async function suggestEMSTermJoinConfig(
sampleValuesConfig: SampleValuesConfig
): Promise<EMSTermJoinConfig | null> {
const mapModules = await lazyLoadMapModules();
return await mapModules.suggestEMSTermJoinConfig(sampleValuesConfig);
}
1 change: 1 addition & 0 deletions x-pack/plugins/maps/public/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
export { MapsStartApi } from './start_api';
export { createLayerDescriptors } from './create_layer_descriptors';
export { registerLayerWizard, registerSource } from './register';
export { suggestEMSTermJoinConfig } from './ems';
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/public/api/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/

import { SourceRegistryEntry } from '../classes/sources/source_registry';
import { LayerWizard } from '../classes/layers/layer_wizard_registry';
import type { SourceRegistryEntry } from '../classes/sources/source_registry';
import type { LayerWizard } from '../classes/layers/layer_wizard_registry';
import { lazyLoadMapModules } from '../lazy_load_bundle';

export async function registerLayerWizard(layerWizard: LayerWizard): Promise<void> {
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/maps/public/api/start_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* 2.0.
*/

import { LayerDescriptor } from '../../common/descriptor_types';
import { SourceRegistryEntry } from '../classes/sources/source_registry';
import { LayerWizard } from '../classes/layers/layer_wizard_registry';
import type { LayerDescriptor } from '../../common/descriptor_types';
import type { SourceRegistryEntry } from '../classes/sources/source_registry';
import type { LayerWizard } from '../classes/layers/layer_wizard_registry';
import type { CreateLayerDescriptorParams } from '../classes/sources/es_search_source';
import type { SampleValuesConfig, EMSTermJoinConfig } from '../ems_autosuggest';

export interface MapsStartApi {
createLayerDescriptors: {
Expand All @@ -23,4 +24,5 @@ export interface MapsStartApi {
};
registerLayerWizard(layerWizard: LayerWizard): Promise<void>;
registerSource(entry: SourceRegistryEntry): Promise<void>;
suggestEMSTermJoinConfig(config: SampleValuesConfig): Promise<EMSTermJoinConfig | null>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { EMSFileSelect } from '../../../components/ems_file_select';
import { GeoIndexPatternSelect } from '../../../components/geo_index_pattern_select';
import { SingleFieldSelect } from '../../../components/single_field_select';
import { getGeoFields, getSourceFields, getTermsFields } from '../../../index_pattern_util';
import { getEmsFileLayers } from '../../../meta';
import { getEmsFileLayers } from '../../../util';
import { getIndexPatternSelectComponent, getIndexPatternService } from '../../../kibana_services';
import {
createEmsChoroplethLayerDescriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

jest.mock('../../meta', () => {
jest.mock('../../util', () => {
return {};
});
jest.mock('../../kibana_services', () => {
Expand Down Expand Up @@ -33,7 +33,7 @@ import { createBasemapLayerDescriptor } from './create_basemap_layer_descriptor'
describe('kibana.yml configured with map.tilemap.url', () => {
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../../meta').getKibanaTileMap = () => {
require('../../util').getKibanaTileMap = () => {
return {
url: 'myTileUrl',
};
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('kibana.yml configured with map.tilemap.url', () => {
describe('EMS is enabled', () => {
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../../meta').getKibanaTileMap = () => {
require('../../util').getKibanaTileMap = () => {
return null;
};
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('EMS is enabled', () => {
describe('EMS is not enabled', () => {
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../../meta').getKibanaTileMap = () => {
require('../../util').getKibanaTileMap = () => {
return null;
};
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import _ from 'lodash';
import { LayerDescriptor } from '../../../common/descriptor_types';
import { getKibanaTileMap } from '../../meta';
import { getKibanaTileMap } from '../../util';
import { getEMSSettings } from '../../kibana_services';
// @ts-expect-error
import { KibanaTilemapSource } from '../sources/kibana_tilemap_source';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { emsWorldLayerId } from '../../../../../common';

jest.mock('../../../../kibana_services', () => {
return {
getIsDarkMode() {
Expand Down Expand Up @@ -71,7 +73,7 @@ describe('createLayerDescriptor', () => {
maxZoom: 24,
minZoom: 0,
sourceDescriptor: {
id: 'world_countries',
id: emsWorldLayerId,
tooltipProperties: ['name', 'iso2'],
type: 'EMS_FILE',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import {
AGG_TYPE,
COLOR_MAP_TYPE,
emsWorldLayerId,
FIELD_ORIGIN,
GRID_RESOLUTION,
RENDER_AS,
Expand Down Expand Up @@ -182,7 +183,7 @@ export function createLayerDescriptor({
},
],
sourceDescriptor: EMSFileSource.createDescriptor({
id: 'world_countries',
id: emsWorldLayerId,
tooltipProperties: ['name', 'iso2'],
}),
style: VectorStyle.createDescriptor({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { TileLayer } from '../tile_layer/tile_layer';
import _ from 'lodash';
import { SOURCE_DATA_REQUEST_ID, LAYER_TYPE, LAYER_STYLE_TYPE } from '../../../../common/constants';
import { isRetina } from '../../../meta';
import { isRetina } from '../../../util';
import {
addSpriteSheetToMapFromImageData,
loadSpriteSheetImageData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
VECTOR_SHAPE_TYPE,
FORMAT_TYPE,
} from '../../../../common/constants';
import { getEmsFileLayers } from '../../../meta';
import { fetchGeoJson, getEmsFileLayers } from '../../../util';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { UpdateSourceEditor } from './update_source_editor';
import { EMSFileField } from '../../fields/ems_file_field';
Expand Down Expand Up @@ -123,12 +123,11 @@ export class EMSFileSource extends AbstractVectorSource implements IEmsFileSourc

async getGeoJsonWithMeta(): Promise<GeoJsonWithMeta> {
const emsFileLayer = await this.getEMSFileLayer();
// @ts-ignore
const featureCollection = await AbstractVectorSource.getGeoJson({
format: emsFileLayer.getDefaultFormatType() as FORMAT_TYPE,
featureCollectionPath: 'data',
fetchUrl: emsFileLayer.getDefaultFormatUrl(),
});
const featureCollection = await fetchGeoJson(
emsFileLayer.getDefaultFormatUrl(),
emsFileLayer.getDefaultFormatType() as FORMAT_TYPE,
'data'
);

const emsIdField = emsFileLayer.getFields().find((field) => {
return field.type === 'id';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, { Component, Fragment } from 'react';
import { EuiTitle, EuiPanel, EuiSpacer } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { TooltipSelector } from '../../../components/tooltip_selector';
import { getEmsFileLayers } from '../../../meta';
import { getEmsFileLayers } from '../../../util';
import { IEmsFileSource } from './ems_file_source';
import { IField } from '../../fields/field';
import { OnSourceChangeArgs } from '../../../connected_components/layer_panel/view';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import { AbstractTMSSource } from '../tms_source';
import { getEmsTmsServices } from '../../../meta';
import { getEmsTmsServices } from '../../../util';
import { UpdateSourceEditor } from './update_source_editor';
import { i18n } from '@kbn/i18n';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

jest.mock('../../../meta', () => {
jest.mock('../../../util', () => {
return {
getEmsTmsServices: () => {
class MockTMSService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, { ChangeEvent, Component } from 'react';
import { EuiSelect, EuiSelectOption, EuiFormRow } from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import { getEmsTmsServices } from '../../../meta';
import { getEmsTmsServices } from '../../../util';
import { getEmsUnavailableMessage } from '../../../components/ems_unavailable_message';

export const AUTO_SELECT = 'auto_select';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { EuiSelect, EuiFormRow, EuiPanel } from '@elastic/eui';
import { getKibanaRegionList } from '../../../meta';
import { getKibanaRegionList } from '../../../util';
import { i18n } from '@kbn/i18n';

export function CreateSourceEditor({ onSourceConfigChange }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { KibanaRegionmapSource, sourceTitle } from './kibana_regionmap_source';
import { VectorLayer } from '../../layers/vector_layer';
// @ts-ignore
import { CreateSourceEditor } from './create_source_editor';
import { getKibanaRegionList } from '../../../meta';
import { getKibanaRegionList } from '../../../util';
import { LAYER_WIZARD_CATEGORY } from '../../../../common/constants';

export const kibanaRegionMapLayerWizardConfig: LayerWizard = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';
import { AbstractVectorSource, GeoJsonWithMeta } from '../vector_source';
import { getKibanaRegionList } from '../../../meta';
import { fetchGeoJson, getKibanaRegionList } from '../../../util';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { FIELD_ORIGIN, FORMAT_TYPE, SOURCE_TYPES } from '../../../../common/constants';
import { KibanaRegionField } from '../../fields/kibana_region_field';
Expand Down Expand Up @@ -79,11 +79,12 @@ export class KibanaRegionmapSource extends AbstractVectorSource {

async getGeoJsonWithMeta(): Promise<GeoJsonWithMeta> {
const vectorFileMeta = await this.getVectorFileMeta();
const featureCollection = await AbstractVectorSource.getGeoJson({
format: vectorFileMeta.format.type as FORMAT_TYPE,
featureCollectionPath: vectorFileMeta.meta.feature_collection_path,
fetchUrl: vectorFileMeta.url,
});
const featureCollection = await fetchGeoJson(
vectorFileMeta.url,
vectorFileMeta.format.type as FORMAT_TYPE,
vectorFileMeta.meta.feature_collection_path
);

return {
data: featureCollection,
meta: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { EuiFieldText, EuiFormRow, EuiPanel } from '@elastic/eui';

import { getKibanaTileMap } from '../../../meta';
import { getKibanaTileMap } from '../../../util';
import { i18n } from '@kbn/i18n';

export function CreateSourceEditor({ onSourceConfigChange }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CreateSourceEditor } from './create_source_editor';
// @ts-ignore
import { KibanaTilemapSource, sourceTitle } from './kibana_tilemap_source';
import { TileLayer } from '../../layers/tile_layer/tile_layer';
import { getKibanaTileMap } from '../../../meta';
import { getKibanaTileMap } from '../../../util';
import { LAYER_WIZARD_CATEGORY } from '../../../../common/constants';

export const kibanaBasemapLayerWizardConfig: LayerWizard = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { AbstractTMSSource } from '../tms_source';
import { getKibanaTileMap } from '../../../meta';
import { getKibanaTileMap } from '../../../util';
import { i18n } from '@kbn/i18n';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import _ from 'lodash';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
* 2.0.
*/

// @ts-expect-error
import * as topojson from 'topojson-client';
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { FeatureCollection, GeoJsonProperties } from 'geojson';
import { Filter, TimeRange } from 'src/plugins/data/public';
import { FORMAT_TYPE, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
import { VECTOR_SHAPE_TYPE } from '../../../../common/constants';
import { TooltipProperty, ITooltipProperty } from '../../tooltips/tooltip_property';
import { AbstractSource, ISource } from '../source';
import { IField } from '../../fields/field';
Expand Down Expand Up @@ -85,48 +81,6 @@ export interface ITiledSingleLayerVectorSource extends IVectorSource {
}

export class AbstractVectorSource extends AbstractSource implements IVectorSource {
static async getGeoJson({
format,
featureCollectionPath,
fetchUrl,
}: {
format: FORMAT_TYPE;
featureCollectionPath: string;
fetchUrl: string;
}) {
let fetchedJson;
try {
const response = await fetch(fetchUrl);
if (!response.ok) {
throw new Error('Request failed');
}
fetchedJson = await response.json();
} catch (e) {
throw new Error(
i18n.translate('xpack.maps.source.vetorSource.requestFailedErrorMessage', {
defaultMessage: `Unable to fetch vector shapes from url: {fetchUrl}`,
values: { fetchUrl },
})
);
}

if (format === FORMAT_TYPE.GEOJSON) {
return fetchedJson;
}

if (format === FORMAT_TYPE.TOPOJSON) {
const features = _.get(fetchedJson, `objects.${featureCollectionPath}`);
return topojson.feature(fetchedJson, features);
}

throw new Error(
i18n.translate('xpack.maps.source.vetorSource.formatErrorMessage', {
defaultMessage: `Unable to fetch vector shapes from url: {format}`,
values: { format },
})
);
}

getFieldNames(): string[] {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/maps/public/components/ems_file_select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EuiComboBox, EuiComboBoxOptionOption, EuiFormRow, EuiSelect } from '@el

import { i18n } from '@kbn/i18n';
import { FileLayer } from '@elastic/ems-client';
import { getEmsFileLayers } from '../meta';
import { getEmsFileLayers } from '../util';
import { getEmsUnavailableMessage } from './ems_unavailable_message';

interface Props {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
RawValue,
ZOOM_PRECISION,
} from '../../../common/constants';
import { getGlyphUrl, isRetina } from '../../meta';
import { getGlyphUrl, isRetina } from '../../util';
import { syncLayerOrder } from './sort_layers';
// @ts-expect-error
import { removeOrphanedSourcesAndLayers, addSpritesheetToMap } from './utils';
Expand Down
Loading

0 comments on commit 3e79172

Please sign in to comment.