Skip to content

Commit d02294c

Browse files
[Maps] fix fit to data on heatmap not working (#92697)
* [Maps] fix fit to data on heatmap not working * tslint Co-authored-by: Kibana Machine <[email protected]>
1 parent 1a3bbbf commit d02294c

File tree

4 files changed

+86
-45
lines changed

4 files changed

+86
-45
lines changed

x-pack/plugins/maps/public/classes/layers/heatmap_layer/heatmap_layer.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { HeatmapStyle } from '../../styles/heatmap/heatmap_style';
1212
import { EMPTY_FEATURE_COLLECTION, LAYER_TYPE } from '../../../../common/constants';
1313
import { HeatmapLayerDescriptor, MapQuery } from '../../../../common/descriptor_types';
1414
import { ESGeoGridSource } from '../../sources/es_geo_grid_source';
15-
import { addGeoJsonMbSource, syncVectorSource } from '../vector_layer';
15+
import { addGeoJsonMbSource, getVectorSourceBounds, syncVectorSource } from '../vector_layer';
1616
import { DataRequestContext } from '../../../actions';
1717
import { DataRequestAbortError } from '../../util/data_request';
1818

@@ -46,6 +46,12 @@ export class HeatmapLayer extends AbstractLayer {
4646
}
4747
}
4848

49+
destroy() {
50+
if (this.getSource()) {
51+
this.getSource().destroy();
52+
}
53+
}
54+
4955
getSource(): ESGeoGridSource {
5056
return super.getSource() as ESGeoGridSource;
5157
}
@@ -179,4 +185,29 @@ export class HeatmapLayer extends AbstractLayer {
179185
const metricFields = this.getSource().getMetricFields();
180186
return this.getCurrentStyle().renderLegendDetails(metricFields[0]);
181187
}
188+
189+
async getBounds(syncContext: DataRequestContext) {
190+
return await getVectorSourceBounds({
191+
layerId: this.getId(),
192+
syncContext,
193+
source: this.getSource(),
194+
sourceQuery: this.getQuery() as MapQuery,
195+
});
196+
}
197+
198+
async isFilteredByGlobalTime(): Promise<boolean> {
199+
return this.getSource().getApplyGlobalTime() && (await this.getSource().isTimeAware());
200+
}
201+
202+
getIndexPatternIds() {
203+
return this.getSource().getIndexPatternIds();
204+
}
205+
206+
getQueryableIndexPatternIds() {
207+
return this.getSource().getQueryableIndexPatternIds();
208+
}
209+
210+
async getLicensedFeatures() {
211+
return await this.getSource().getLicensedFeatures();
212+
}
182213
}

x-pack/plugins/maps/public/classes/layers/vector_layer/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
* 2.0.
66
*/
77

8-
export { addGeoJsonMbSource, syncVectorSource } from './utils';
8+
export { addGeoJsonMbSource, getVectorSourceBounds, syncVectorSource } from './utils';
99
export { IVectorLayer, VectorLayer, VectorLayerArguments } from './vector_layer';

x-pack/plugins/maps/public/classes/layers/vector_layer/utils.tsx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import { FeatureCollection } from 'geojson';
99
import { Map as MbMap } from 'mapbox-gl';
1010
import {
1111
EMPTY_FEATURE_COLLECTION,
12+
SOURCE_BOUNDS_DATA_REQUEST_ID,
1213
SOURCE_DATA_REQUEST_ID,
1314
VECTOR_SHAPE_TYPE,
1415
} from '../../../../common/constants';
15-
import { VectorSourceRequestMeta } from '../../../../common/descriptor_types';
16+
import { MapExtent, MapQuery, VectorSourceRequestMeta } from '../../../../common/descriptor_types';
1617
import { DataRequestContext } from '../../../actions';
1718
import { IVectorSource } from '../../sources/vector_source';
1819
import { DataRequestAbortError } from '../../util/data_request';
@@ -112,3 +113,44 @@ export async function syncVectorSource({
112113
throw error;
113114
}
114115
}
116+
117+
export async function getVectorSourceBounds({
118+
layerId,
119+
syncContext,
120+
source,
121+
sourceQuery,
122+
}: {
123+
layerId: string;
124+
syncContext: DataRequestContext;
125+
source: IVectorSource;
126+
sourceQuery: MapQuery | null;
127+
}): Promise<MapExtent | null> {
128+
const { startLoading, stopLoading, registerCancelCallback, dataFilters } = syncContext;
129+
130+
const requestToken = Symbol(`${SOURCE_BOUNDS_DATA_REQUEST_ID}-${layerId}`);
131+
132+
// Do not pass all searchFilters to source.getBoundsForFilters().
133+
// For example, do not want to filter bounds request by extent and buffer.
134+
const boundsFilters = {
135+
sourceQuery: sourceQuery ? sourceQuery : undefined,
136+
query: dataFilters.query,
137+
timeFilters: dataFilters.timeFilters,
138+
filters: dataFilters.filters,
139+
applyGlobalQuery: source.getApplyGlobalQuery(),
140+
applyGlobalTime: source.getApplyGlobalTime(),
141+
};
142+
143+
let bounds = null;
144+
try {
145+
startLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, boundsFilters);
146+
bounds = await source.getBoundsForFilters(
147+
boundsFilters,
148+
registerCancelCallback.bind(null, requestToken)
149+
);
150+
} finally {
151+
// Use stopLoading callback instead of onLoadError callback.
152+
// Function is loading bounds and not feature data.
153+
stopLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, bounds ? bounds : {});
154+
}
155+
return bounds;
156+
}

x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.tsx

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
FEATURE_ID_PROPERTY_NAME,
1818
SOURCE_META_DATA_REQUEST_ID,
1919
SOURCE_FORMATTERS_DATA_REQUEST_ID,
20-
SOURCE_BOUNDS_DATA_REQUEST_ID,
2120
FEATURE_VISIBLE_PROPERTY_NAME,
2221
EMPTY_FEATURE_COLLECTION,
2322
KBN_TOO_MANY_FEATURES_PROPERTY,
@@ -60,7 +59,7 @@ import { IDynamicStyleProperty } from '../../styles/vector/properties/dynamic_st
6059
import { IESSource } from '../../sources/es_source';
6160
import { PropertiesMap } from '../../../../common/elasticsearch_util';
6261
import { ITermJoinSource } from '../../sources/term_join_source';
63-
import { addGeoJsonMbSource, syncVectorSource } from './utils';
62+
import { addGeoJsonMbSource, getVectorSourceBounds, syncVectorSource } from './utils';
6463

6564
interface SourceResult {
6665
refreshed: boolean;
@@ -241,47 +240,16 @@ export class VectorLayer extends AbstractLayer implements IVectorLayer {
241240
return this.getCurrentStyle().renderLegendDetails();
242241
}
243242

244-
async getBounds({
245-
startLoading,
246-
stopLoading,
247-
registerCancelCallback,
248-
dataFilters,
249-
}: DataRequestContext) {
243+
async getBounds(syncContext: DataRequestContext) {
250244
const isStaticLayer = !this.getSource().isBoundsAware();
251-
if (isStaticLayer || this.hasJoins()) {
252-
return getFeatureCollectionBounds(this._getSourceFeatureCollection(), this.hasJoins());
253-
}
254-
255-
const requestToken = Symbol(`${SOURCE_BOUNDS_DATA_REQUEST_ID}-${this.getId()}`);
256-
const searchFilters: VectorSourceRequestMeta = this._getSearchFilters(
257-
dataFilters,
258-
this.getSource(),
259-
this.getCurrentStyle()
260-
);
261-
// Do not pass all searchFilters to source.getBoundsForFilters().
262-
// For example, do not want to filter bounds request by extent and buffer.
263-
const boundsFilters = {
264-
sourceQuery: searchFilters.sourceQuery,
265-
query: searchFilters.query,
266-
timeFilters: searchFilters.timeFilters,
267-
filters: searchFilters.filters,
268-
applyGlobalQuery: searchFilters.applyGlobalQuery,
269-
applyGlobalTime: searchFilters.applyGlobalTime,
270-
};
271-
272-
let bounds = null;
273-
try {
274-
startLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, boundsFilters);
275-
bounds = await this.getSource().getBoundsForFilters(
276-
boundsFilters,
277-
registerCancelCallback.bind(null, requestToken)
278-
);
279-
} finally {
280-
// Use stopLoading callback instead of onLoadError callback.
281-
// Function is loading bounds and not feature data.
282-
stopLoading(SOURCE_BOUNDS_DATA_REQUEST_ID, requestToken, bounds ? bounds : {}, boundsFilters);
283-
}
284-
return bounds;
245+
return isStaticLayer || this.hasJoins()
246+
? getFeatureCollectionBounds(this._getSourceFeatureCollection(), this.hasJoins())
247+
: getVectorSourceBounds({
248+
layerId: this.getId(),
249+
syncContext,
250+
source: this.getSource(),
251+
sourceQuery: this.getQuery() as MapQuery,
252+
});
285253
}
286254

287255
async getLeftJoinFields() {

0 commit comments

Comments
 (0)