From 954b5b5224d297438473f90286502bf5feb139cf Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Thu, 20 Aug 2020 09:28:19 -0400 Subject: [PATCH] [Maps] Move elasticsearch_geo_utils to common (#75508) --- .../elasticsearch_geo_utils.d.ts | 6 +++- .../elasticsearch_geo_utils.js | 6 ++-- .../elasticsearch_geo_utils.test.js | 29 +++++-------------- .../public/actions/data_request_actions.ts | 2 +- .../maps/public/actions/map_actions.ts | 2 +- .../es_geo_grid_source/convert_to_geojson.js | 2 +- .../es_geo_grid_source/es_geo_grid_source.js | 2 +- .../es_geo_grid_source/geo_tile_utils.js | 2 +- .../es_pew_pew_source/es_pew_pew_source.js | 2 +- .../es_search_source/es_search_source.js | 2 +- .../classes/sources/es_source/es_source.js | 2 +- .../feature_geometry_filter_form.js | 2 +- .../map/mb/draw_control/draw_control.js | 2 +- .../connected_components/map/mb/view.js | 2 +- x-pack/plugins/maps/public/kibana_services.ts | 3 -- .../maps/public/selectors/map_selectors.ts | 2 +- 16 files changed, 29 insertions(+), 39 deletions(-) rename x-pack/plugins/maps/{public => common}/elasticsearch_geo_utils.d.ts (70%) rename x-pack/plugins/maps/{public => common}/elasticsearch_geo_utils.js (98%) rename x-pack/plugins/maps/{public => common}/elasticsearch_geo_utils.test.js (97%) diff --git a/x-pack/plugins/maps/public/elasticsearch_geo_utils.d.ts b/x-pack/plugins/maps/common/elasticsearch_geo_utils.d.ts similarity index 70% rename from x-pack/plugins/maps/public/elasticsearch_geo_utils.d.ts rename to x-pack/plugins/maps/common/elasticsearch_geo_utils.d.ts index 964afb589187f..44250360e9d00 100644 --- a/x-pack/plugins/maps/public/elasticsearch_geo_utils.d.ts +++ b/x-pack/plugins/maps/common/elasticsearch_geo_utils.d.ts @@ -4,8 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { MapExtent } from '../common/descriptor_types'; +import { MapExtent } from './descriptor_types'; export function scaleBounds(bounds: MapExtent, scaleFactor: number): MapExtent; export function turfBboxToBounds(turfBbox: unknown): MapExtent; + +export function clampToLatBounds(lat: number): number; + +export function clampToLonBounds(lon: number): number; diff --git a/x-pack/plugins/maps/public/elasticsearch_geo_utils.js b/x-pack/plugins/maps/common/elasticsearch_geo_utils.js similarity index 98% rename from x-pack/plugins/maps/public/elasticsearch_geo_utils.js rename to x-pack/plugins/maps/common/elasticsearch_geo_utils.js index b32125e6eb614..f2bf83ae18bb0 100644 --- a/x-pack/plugins/maps/public/elasticsearch_geo_utils.js +++ b/x-pack/plugins/maps/common/elasticsearch_geo_utils.js @@ -16,10 +16,12 @@ import { LON_INDEX, LAT_INDEX, } from '../common/constants'; -import { getEsSpatialRelationLabel } from '../common/i18n_getters'; -import { SPATIAL_FILTER_TYPE } from './kibana_services'; +import { getEsSpatialRelationLabel } from './i18n_getters'; +import { FILTERS } from '../../../../src/plugins/data/common'; import turfCircle from '@turf/circle'; +const SPATIAL_FILTER_TYPE = FILTERS.SPATIAL_FILTER; + function ensureGeoField(type) { const expectedTypes = [ES_GEO_FIELD_TYPE.GEO_POINT, ES_GEO_FIELD_TYPE.GEO_SHAPE]; if (!expectedTypes.includes(type)) { diff --git a/x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js b/x-pack/plugins/maps/common/elasticsearch_geo_utils.test.js similarity index 97% rename from x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js rename to x-pack/plugins/maps/common/elasticsearch_geo_utils.test.js index 6a4b4b78c829e..a8d5d650740cd 100644 --- a/x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js +++ b/x-pack/plugins/maps/common/elasticsearch_geo_utils.test.js @@ -4,14 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -jest.mock('ui/new_platform'); - -jest.mock('./kibana_services', () => { - return { - SPATIAL_FILTER_TYPE: 'spatial_filter', - }; -}); - import { hitsToGeoJson, geoPointToGeometry, @@ -22,7 +14,7 @@ import { makeESBbox, scaleBounds, } from './elasticsearch_geo_utils'; -import { indexPatterns } from '../../../../src/plugins/data/public'; +import _ from 'lodash'; const geoFieldName = 'location'; @@ -173,19 +165,14 @@ describe('hitsToGeoJson', () => { }); describe('dot in geoFieldName', () => { - const indexPatternMock = { - fields: { - getByName: (name) => { - const fields = { - ['my.location']: { - type: 'geo_point', - }, - }; - return fields[name]; - }, - }, + // This essentially should test the implmentation of index-pattern.flattenHit, rather than anything in geo_utils. + // Leaving this here for reference. + const geoFieldName = 'my.location'; + const indexPatternFlattenHit = (hit) => { + return { + [geoFieldName]: _.get(hit._source, geoFieldName), + }; }; - const indexPatternFlattenHit = indexPatterns.flattenHitWrapper(indexPatternMock); it('Should handle geoField being an object', () => { const hits = [ diff --git a/x-pack/plugins/maps/public/actions/data_request_actions.ts b/x-pack/plugins/maps/public/actions/data_request_actions.ts index a22e8d582bc5e..6f5ed680ac64f 100644 --- a/x-pack/plugins/maps/public/actions/data_request_actions.ts +++ b/x-pack/plugins/maps/public/actions/data_request_actions.ts @@ -41,7 +41,7 @@ import { ILayer } from '../classes/layers/layer'; import { IVectorLayer } from '../classes/layers/vector_layer/vector_layer'; import { DataMeta, MapExtent, MapFilters } from '../../common/descriptor_types'; import { DataRequestAbortError } from '../classes/util/data_request'; -import { scaleBounds, turfBboxToBounds } from '../elasticsearch_geo_utils'; +import { scaleBounds, turfBboxToBounds } from '../../common/elasticsearch_geo_utils'; const FIT_TO_BOUNDS_SCALE_FACTOR = 0.1; diff --git a/x-pack/plugins/maps/public/actions/map_actions.ts b/x-pack/plugins/maps/public/actions/map_actions.ts index 7ba58307e1952..f408896853155 100644 --- a/x-pack/plugins/maps/public/actions/map_actions.ts +++ b/x-pack/plugins/maps/public/actions/map_actions.ts @@ -54,7 +54,7 @@ import { MapRefreshConfig, } from '../../common/descriptor_types'; import { INITIAL_LOCATION } from '../../common/constants'; -import { scaleBounds } from '../elasticsearch_geo_utils'; +import { scaleBounds } from '../../common/elasticsearch_geo_utils'; export function setMapInitError(errorMessage: string) { return { diff --git a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/convert_to_geojson.js b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/convert_to_geojson.js index a95a8be4b24c8..35dbebdfd3c8a 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/convert_to_geojson.js +++ b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/convert_to_geojson.js @@ -8,7 +8,7 @@ import _ from 'lodash'; import { RENDER_AS } from '../../../../common/constants'; import { getTileBoundingBox } from './geo_tile_utils'; import { extractPropertiesFromBucket } from '../../util/es_agg_utils'; -import { clamp } from '../../../elasticsearch_geo_utils'; +import { clamp } from '../../../../common/elasticsearch_geo_utils'; const GRID_BUCKET_KEYS_TO_IGNORE = ['key', 'gridCentroid']; 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 a4dba71307b71..a6322ff3ba784 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 @@ -21,7 +21,7 @@ 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 { makeESBbox } from '../../../elasticsearch_geo_utils'; +import { makeESBbox } from '../../../../common/elasticsearch_geo_utils'; export const MAX_GEOTILE_LEVEL = 29; diff --git a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/geo_tile_utils.js b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/geo_tile_utils.js index 251e33b9579cb..89b24522e4275 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/geo_tile_utils.js +++ b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/geo_tile_utils.js @@ -6,7 +6,7 @@ import _ from 'lodash'; import { DECIMAL_DEGREES_PRECISION } from '../../../../common/constants'; -import { clampToLatBounds } from '../../../elasticsearch_geo_utils'; +import { clampToLatBounds } from '../../../../common/elasticsearch_geo_utils'; const ZOOM_TILE_KEY_INDEX = 0; const X_TILE_KEY_INDEX = 1; diff --git a/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js b/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js index 79eccf09b2888..92b0c717f6724 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js +++ b/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js @@ -16,7 +16,7 @@ import { getDataSourceLabel } from '../../../../common/i18n_getters'; import { convertToLines } from './convert_to_lines'; import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source'; import { registerSource } from '../source_registry'; -import { turfBboxToBounds } from '../../../elasticsearch_geo_utils'; +import { turfBboxToBounds } from '../../../../common/elasticsearch_geo_utils'; import { DataRequestAbortError } from '../../util/data_request'; const MAX_GEOTILE_LEVEL = 29; diff --git a/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js b/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js index 256becf70ffb0..6d61c4a7455b2 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js +++ b/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js @@ -9,7 +9,7 @@ import React from 'react'; import { AbstractESSource } from '../es_source'; import { getSearchService } from '../../../kibana_services'; -import { hitsToGeoJson } from '../../../elasticsearch_geo_utils'; +import { hitsToGeoJson } from '../../../../common/elasticsearch_geo_utils'; import { UpdateSourceEditor } from './update_source_editor'; import { SOURCE_TYPES, diff --git a/x-pack/plugins/maps/public/classes/sources/es_source/es_source.js b/x-pack/plugins/maps/public/classes/sources/es_source/es_source.js index 866e3c76c2a3f..8cc2aa018979b 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_source/es_source.js +++ b/x-pack/plugins/maps/public/classes/sources/es_source/es_source.js @@ -11,7 +11,7 @@ import { getTimeFilter, getSearchService, } from '../../../kibana_services'; -import { createExtentFilter } from '../../../elasticsearch_geo_utils'; +import { createExtentFilter } from '../../../../common/elasticsearch_geo_utils'; import _ from 'lodash'; import { i18n } from '@kbn/i18n'; import uuid from 'uuid/v4'; diff --git a/x-pack/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js b/x-pack/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js index e1779c1afbf47..b0ce52b4db7ab 100644 --- a/x-pack/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js +++ b/x-pack/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js @@ -10,7 +10,7 @@ import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import { URL_MAX_LENGTH } from '../../../../../../../src/core/public'; -import { createSpatialFilterWithGeometry } from '../../../elasticsearch_geo_utils'; +import { createSpatialFilterWithGeometry } from '../../../../common/elasticsearch_geo_utils'; import { GEO_JSON_TYPE } from '../../../../common/constants'; import { GeometryFilterForm } from '../../../components/geometry_filter_form'; diff --git a/x-pack/plugins/maps/public/connected_components/map/mb/draw_control/draw_control.js b/x-pack/plugins/maps/public/connected_components/map/mb/draw_control/draw_control.js index 2daa4b2c900f5..6de936fa4a8f1 100644 --- a/x-pack/plugins/maps/public/connected_components/map/mb/draw_control/draw_control.js +++ b/x-pack/plugins/maps/public/connected_components/map/mb/draw_control/draw_control.js @@ -15,7 +15,7 @@ import { createSpatialFilterWithGeometry, getBoundingBoxGeometry, roundCoordinates, -} from '../../../../elasticsearch_geo_utils'; +} from '../../../../../common/elasticsearch_geo_utils'; import { DrawTooltip } from './draw_tooltip'; const DRAW_RECTANGLE = 'draw_rectangle'; diff --git a/x-pack/plugins/maps/public/connected_components/map/mb/view.js b/x-pack/plugins/maps/public/connected_components/map/mb/view.js index d85959c3a08a4..5a38f6039ae4b 100644 --- a/x-pack/plugins/maps/public/connected_components/map/mb/view.js +++ b/x-pack/plugins/maps/public/connected_components/map/mb/view.js @@ -19,7 +19,7 @@ import sprites1 from '@elastic/maki/dist/sprite@1.png'; import sprites2 from '@elastic/maki/dist/sprite@2.png'; import { DrawControl } from './draw_control'; import { TooltipControl } from './tooltip_control'; -import { clampToLatBounds, clampToLonBounds } from '../../../elasticsearch_geo_utils'; +import { clampToLatBounds, clampToLonBounds } from '../../../../common/elasticsearch_geo_utils'; import { getInitialView } from './get_initial_view'; import { getPreserveDrawingBuffer } from '../../../kibana_services'; diff --git a/x-pack/plugins/maps/public/kibana_services.ts b/x-pack/plugins/maps/public/kibana_services.ts index f8f89ebaed102..239a2898a06fc 100644 --- a/x-pack/plugins/maps/public/kibana_services.ts +++ b/x-pack/plugins/maps/public/kibana_services.ts @@ -5,14 +5,11 @@ */ import _ from 'lodash'; -import { esFilters } from '../../../../src/plugins/data/public'; import { MapsLegacyConfigType } from '../../../../src/plugins/maps_legacy/public'; import { MapsConfigType } from '../config'; import { MapsPluginStartDependencies } from './plugin'; import { CoreStart } from '../../../../src/core/public'; -export const SPATIAL_FILTER_TYPE = esFilters.FILTERS.SPATIAL_FILTER; - let licenseId: string | undefined; export const setLicenseId = (latestLicenseId: string | undefined) => (licenseId = latestLicenseId); export const getLicenseId = () => licenseId; diff --git a/x-pack/plugins/maps/public/selectors/map_selectors.ts b/x-pack/plugins/maps/public/selectors/map_selectors.ts index 40ffda3f31c26..d48ee24027561 100644 --- a/x-pack/plugins/maps/public/selectors/map_selectors.ts +++ b/x-pack/plugins/maps/public/selectors/map_selectors.ts @@ -32,7 +32,7 @@ import { SPATIAL_FILTERS_LAYER_ID, } from '../../common/constants'; // @ts-ignore -import { extractFeaturesFromFilters } from '../elasticsearch_geo_utils'; +import { extractFeaturesFromFilters } from '../../common/elasticsearch_geo_utils'; import { MapStoreState } from '../reducers/store'; import { DataRequestDescriptor,