Skip to content

Commit

Permalink
[Maps] fix fit to data on heatmap not working (#92697) (#93112)
Browse files Browse the repository at this point in the history
* [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>
  • Loading branch information
nreese and kibanamachine authored Mar 1, 2021
1 parent fe92408 commit 3df7f57
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -46,6 +46,12 @@ export class HeatmapLayer extends AbstractLayer {
}
}

destroy() {
if (this.getSource()) {
this.getSource().destroy();
}
}

getSource(): ESGeoGridSource {
return super.getSource() as ESGeoGridSource;
}
Expand Down Expand Up @@ -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<boolean> {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* 2.0.
*/

export { addGeoJsonMbSource, syncVectorSource } from './utils';
export { addGeoJsonMbSource, getVectorSourceBounds, syncVectorSource } from './utils';
export { IVectorLayer, VectorLayer, VectorLayerArguments } from './vector_layer';
44 changes: 43 additions & 1 deletion x-pack/plugins/maps/public/classes/layers/vector_layer/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<MapExtent | null> {
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 3df7f57

Please sign in to comment.