From 8c7281236699873b8125a38a4326f4a6553133b5 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 1 Mar 2021 15:59:28 -0700 Subject: [PATCH] [Maps] fix fit to data on heatmap not working (#92697) (#93113) * [Maps] fix fit to data on heatmap not working * tslint Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../layers/heatmap_layer/heatmap_layer.ts | 33 +++++++++++- .../classes/layers/vector_layer/index.ts | 2 +- .../classes/layers/vector_layer/utils.tsx | 44 +++++++++++++++- .../layers/vector_layer/vector_layer.tsx | 52 ++++--------------- 4 files changed, 86 insertions(+), 45 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/layers/heatmap_layer/heatmap_layer.ts b/x-pack/plugins/maps/public/classes/layers/heatmap_layer/heatmap_layer.ts index 8eebd7c57afd7f..96c7fcedaf3d9c 100644 --- a/x-pack/plugins/maps/public/classes/layers/heatmap_layer/heatmap_layer.ts +++ b/x-pack/plugins/maps/public/classes/layers/heatmap_layer/heatmap_layer.ts @@ -12,7 +12,7 @@ import { HeatmapStyle } from '../../styles/heatmap/heatmap_style'; import { EMPTY_FEATURE_COLLECTION, LAYER_TYPE } from '../../../../common/constants'; import { HeatmapLayerDescriptor, MapQuery } from '../../../../common/descriptor_types'; import { ESGeoGridSource } from '../../sources/es_geo_grid_source'; -import { addGeoJsonMbSource, syncVectorSource } from '../vector_layer'; +import { addGeoJsonMbSource, getVectorSourceBounds, syncVectorSource } from '../vector_layer'; import { DataRequestContext } from '../../../actions'; import { DataRequestAbortError } from '../../util/data_request'; @@ -46,6 +46,12 @@ export class HeatmapLayer extends AbstractLayer { } } + destroy() { + if (this.getSource()) { + this.getSource().destroy(); + } + } + getSource(): ESGeoGridSource { return super.getSource() as ESGeoGridSource; } @@ -179,4 +185,29 @@ export class HeatmapLayer extends AbstractLayer { const metricFields = this.getSource().getMetricFields(); return this.getCurrentStyle().renderLegendDetails(metricFields[0]); } + + async getBounds(syncContext: DataRequestContext) { + return await getVectorSourceBounds({ + layerId: this.getId(), + syncContext, + source: this.getSource(), + sourceQuery: this.getQuery() as MapQuery, + }); + } + + async isFilteredByGlobalTime(): Promise { + return this.getSource().getApplyGlobalTime() && (await this.getSource().isTimeAware()); + } + + getIndexPatternIds() { + return this.getSource().getIndexPatternIds(); + } + + getQueryableIndexPatternIds() { + return this.getSource().getQueryableIndexPatternIds(); + } + + async getLicensedFeatures() { + return await this.getSource().getLicensedFeatures(); + } } diff --git a/x-pack/plugins/maps/public/classes/layers/vector_layer/index.ts b/x-pack/plugins/maps/public/classes/layers/vector_layer/index.ts index 4b509ba5dff000..b6777f8a5e4544 100644 --- a/x-pack/plugins/maps/public/classes/layers/vector_layer/index.ts +++ b/x-pack/plugins/maps/public/classes/layers/vector_layer/index.ts @@ -5,5 +5,5 @@ * 2.0. */ -export { addGeoJsonMbSource, syncVectorSource } from './utils'; +export { addGeoJsonMbSource, getVectorSourceBounds, syncVectorSource } from './utils'; export { IVectorLayer, VectorLayer, VectorLayerArguments } from './vector_layer'; diff --git a/x-pack/plugins/maps/public/classes/layers/vector_layer/utils.tsx b/x-pack/plugins/maps/public/classes/layers/vector_layer/utils.tsx index a3754b20de8185..91bdd74c158f9d 100644 --- a/x-pack/plugins/maps/public/classes/layers/vector_layer/utils.tsx +++ b/x-pack/plugins/maps/public/classes/layers/vector_layer/utils.tsx @@ -9,10 +9,11 @@ import { FeatureCollection } from 'geojson'; import { Map as MbMap } from 'mapbox-gl'; import { EMPTY_FEATURE_COLLECTION, + SOURCE_BOUNDS_DATA_REQUEST_ID, SOURCE_DATA_REQUEST_ID, VECTOR_SHAPE_TYPE, } from '../../../../common/constants'; -import { VectorSourceRequestMeta } from '../../../../common/descriptor_types'; +import { MapExtent, MapQuery, VectorSourceRequestMeta } from '../../../../common/descriptor_types'; import { DataRequestContext } from '../../../actions'; import { IVectorSource } from '../../sources/vector_source'; import { DataRequestAbortError } from '../../util/data_request'; @@ -112,3 +113,44 @@ export async function syncVectorSource({ throw error; } } + +export async function getVectorSourceBounds({ + layerId, + syncContext, + source, + sourceQuery, +}: { + layerId: string; + syncContext: DataRequestContext; + source: IVectorSource; + sourceQuery: MapQuery | null; +}): Promise { + const { startLoading, stopLoading, registerCancelCallback, dataFilters } = syncContext; + + const requestToken = Symbol(`${SOURCE_BOUNDS_DATA_REQUEST_ID}-${layerId}`); + + // Do not pass all searchFilters to source.getBoundsForFilters(). + // For example, do not want to filter bounds request by extent and buffer. + const boundsFilters = { + sourceQuery: sourceQuery ? sourceQuery : undefined, + query: dataFilters.query, + timeFilters: dataFilters.timeFilters, + filters: dataFilters.filters, + applyGlobalQuery: source.getApplyGlobalQuery(), + applyGlobalTime: source.getApplyGlobalTime(), + }; + + let bounds = null; + try { + startLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, boundsFilters); + bounds = await source.getBoundsForFilters( + boundsFilters, + registerCancelCallback.bind(null, requestToken) + ); + } finally { + // Use stopLoading callback instead of onLoadError callback. + // Function is loading bounds and not feature data. + stopLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, bounds ? bounds : {}); + } + return bounds; +} 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 2373ed3ba2062f..b21bff99226717 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 @@ -17,7 +17,6 @@ import { FEATURE_ID_PROPERTY_NAME, SOURCE_META_DATA_REQUEST_ID, SOURCE_FORMATTERS_DATA_REQUEST_ID, - SOURCE_BOUNDS_DATA_REQUEST_ID, FEATURE_VISIBLE_PROPERTY_NAME, EMPTY_FEATURE_COLLECTION, KBN_TOO_MANY_FEATURES_PROPERTY, @@ -60,7 +59,7 @@ import { IDynamicStyleProperty } from '../../styles/vector/properties/dynamic_st import { IESSource } from '../../sources/es_source'; import { PropertiesMap } from '../../../../common/elasticsearch_util'; import { ITermJoinSource } from '../../sources/term_join_source'; -import { addGeoJsonMbSource, syncVectorSource } from './utils'; +import { addGeoJsonMbSource, getVectorSourceBounds, syncVectorSource } from './utils'; interface SourceResult { refreshed: boolean; @@ -241,47 +240,16 @@ export class VectorLayer extends AbstractLayer implements IVectorLayer { return this.getCurrentStyle().renderLegendDetails(); } - async getBounds({ - startLoading, - stopLoading, - registerCancelCallback, - dataFilters, - }: DataRequestContext) { + async getBounds(syncContext: DataRequestContext) { const isStaticLayer = !this.getSource().isBoundsAware(); - if (isStaticLayer || this.hasJoins()) { - return getFeatureCollectionBounds(this._getSourceFeatureCollection(), this.hasJoins()); - } - - const requestToken = Symbol(`${SOURCE_BOUNDS_DATA_REQUEST_ID}-${this.getId()}`); - const searchFilters: VectorSourceRequestMeta = this._getSearchFilters( - dataFilters, - this.getSource(), - this.getCurrentStyle() - ); - // Do not pass all searchFilters to source.getBoundsForFilters(). - // For example, do not want to filter bounds request by extent and buffer. - const boundsFilters = { - sourceQuery: searchFilters.sourceQuery, - query: searchFilters.query, - timeFilters: searchFilters.timeFilters, - filters: searchFilters.filters, - applyGlobalQuery: searchFilters.applyGlobalQuery, - applyGlobalTime: searchFilters.applyGlobalTime, - }; - - let bounds = null; - try { - startLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, boundsFilters); - bounds = await this.getSource().getBoundsForFilters( - boundsFilters, - registerCancelCallback.bind(null, requestToken) - ); - } finally { - // Use stopLoading callback instead of onLoadError callback. - // Function is loading bounds and not feature data. - stopLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, bounds ? bounds : {}, boundsFilters); - } - return bounds; + return isStaticLayer || this.hasJoins() + ? getFeatureCollectionBounds(this._getSourceFeatureCollection(), this.hasJoins()) + : getVectorSourceBounds({ + layerId: this.getId(), + syncContext, + source: this.getSource(), + sourceQuery: this.getQuery() as MapQuery, + }); } async getLeftJoinFields() {