Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing maps #56706

Merged
merged 14 commits into from
Feb 12, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
* under the License.
*/

import { geoHashBucketAgg, IBucketGeoHashGridAggConfig } from './geo_hash';
import { geoHashBucketAgg } from './geo_hash';
import { AggConfigs, IAggConfigs } from '../agg_configs';
import { BUCKET_TYPES } from './bucket_agg_types';
import { IBucketAggConfig } from './_bucket_agg_type';

jest.mock('ui/new_platform');

Expand Down Expand Up @@ -122,43 +123,30 @@ describe('Geohash Agg', () => {
describe('getRequestAggs', () => {
describe('initial aggregation creation', () => {
let aggConfigs: IAggConfigs;
let geoHashGridAgg: IBucketGeoHashGridAggConfig;
let geoHashGridAgg: IBucketAggConfig;

beforeEach(() => {
aggConfigs = getAggConfigs();
geoHashGridAgg = aggConfigs.aggs[0] as IBucketGeoHashGridAggConfig;
geoHashGridAgg = aggConfigs.aggs[0] as IBucketAggConfig;
});

it('should create filter, geohash_grid, and geo_centroid aggregations', () => {
const requestAggs = geoHashBucketAgg.getRequestAggs(
geoHashGridAgg
) as IBucketGeoHashGridAggConfig[];
const requestAggs = geoHashBucketAgg.getRequestAggs(geoHashGridAgg) as IBucketAggConfig[];

expect(requestAggs.length).toEqual(3);
expect(requestAggs[0].type.name).toEqual('filter');
expect(requestAggs[1].type.name).toEqual('geohash_grid');
expect(requestAggs[2].type.name).toEqual('geo_centroid');
});

it('should set mapCollar in vis session state', () => {
const [, geoHashAgg] = geoHashBucketAgg.getRequestAggs(
geoHashGridAgg
) as IBucketGeoHashGridAggConfig[];

expect(geoHashAgg).toHaveProperty('lastMapCollar');
expect(geoHashAgg.lastMapCollar).toHaveProperty('top_left');
expect(geoHashAgg.lastMapCollar).toHaveProperty('bottom_right');
expect(geoHashAgg.lastMapCollar).toHaveProperty('zoom');
});
});
});

describe('aggregation options', () => {
it('should only create geohash_grid and geo_centroid aggregations when isFilteredByCollar is false', () => {
const aggConfigs = getAggConfigs({ isFilteredByCollar: false });
const requestAggs = geoHashBucketAgg.getRequestAggs(
aggConfigs.aggs[0] as IBucketGeoHashGridAggConfig
) as IBucketGeoHashGridAggConfig[];
aggConfigs.aggs[0] as IBucketAggConfig
) as IBucketAggConfig[];

expect(requestAggs.length).toEqual(2);
expect(requestAggs[0].type.name).toEqual('geohash_grid');
Expand All @@ -168,8 +156,8 @@ describe('Geohash Agg', () => {
it('should only create filter and geohash_grid aggregations when useGeocentroid is false', () => {
const aggConfigs = getAggConfigs({ useGeocentroid: false });
const requestAggs = geoHashBucketAgg.getRequestAggs(
aggConfigs.aggs[0] as IBucketGeoHashGridAggConfig
) as IBucketGeoHashGridAggConfig[];
aggConfigs.aggs[0] as IBucketAggConfig
) as IBucketAggConfig[];

expect(requestAggs.length).toEqual(2);
expect(requestAggs[0].type.name).toEqual('filter');
Expand All @@ -178,12 +166,12 @@ describe('Geohash Agg', () => {
});

describe('aggregation creation after map interaction', () => {
let originalRequestAggs: IBucketGeoHashGridAggConfig[];
let originalRequestAggs: IBucketAggConfig[];

beforeEach(() => {
originalRequestAggs = geoHashBucketAgg.getRequestAggs(
getAggConfigs().aggs[0] as IBucketGeoHashGridAggConfig
) as IBucketGeoHashGridAggConfig[];
getAggConfigs().aggs[0] as IBucketAggConfig
) as IBucketAggConfig[];
});

it('should change geo_bounding_box filter aggregation and vis session state when map movement is outside map collar', () => {
Expand All @@ -193,8 +181,8 @@ describe('Geohash Agg', () => {
top_left: { lat: 10.0, lon: -10.0 },
bottom_right: { lat: 9.0, lon: -9.0 },
},
}).aggs[0] as IBucketGeoHashGridAggConfig
) as IBucketGeoHashGridAggConfig[];
}).aggs[0] as IBucketAggConfig
) as IBucketAggConfig[];

expect(originalRequestAggs[1].params).not.toEqual(geoBoxingBox.params);
});
Expand All @@ -206,20 +194,10 @@ describe('Geohash Agg', () => {
top_left: { lat: 1, lon: -1 },
bottom_right: { lat: -1, lon: 1 },
},
}).aggs[0] as IBucketGeoHashGridAggConfig
) as IBucketGeoHashGridAggConfig[];
}).aggs[0] as IBucketAggConfig
) as IBucketAggConfig[];

expect(originalRequestAggs[1].params).toEqual(geoBoxingBox.params);
});

it('should change geo_bounding_box filter aggregation and vis session state when map zoom level changes', () => {
const [, geoBoxingBox] = geoHashBucketAgg.getRequestAggs(
getAggConfigs({
mapZoom: -1,
}).aggs[0] as IBucketGeoHashGridAggConfig
) as IBucketGeoHashGridAggConfig[];

expect(originalRequestAggs[1].lastMapCollar).not.toEqual(geoBoxingBox.lastMapCollar);
});
});
});
132 changes: 23 additions & 109 deletions src/legacy/core_plugins/data/public/search/aggs/buckets/geo_hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,22 @@
*/

import { i18n } from '@kbn/i18n';
import { geohashColumns } from 'ui/vis/map/decode_geo_hash';
import chrome from 'ui/chrome';
import { BucketAggType, IBucketAggConfig } from './_bucket_agg_type';
import { KBN_FIELD_TYPES } from '../../../../../../../plugins/data/public';

import { geoContains, scaleBounds, GeoBoundingBox } from './lib/geo_utils';
import { BUCKET_TYPES } from './bucket_agg_types';
import { AggGroupNames } from '../agg_groups';

const config = chrome.getUiSettingsClient();
const defaultBoundingBox = {
top_left: { lat: 1, lon: 1 },
bottom_right: { lat: 0, lon: 0 },
};

const defaultPrecision = 2;
const maxPrecision = parseInt(config.get('visualization:tileMap:maxPrecision'), 10) || 12;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all this logic was moved inside tile_map_vis (it needs to set correct precision based on zoom level)

/**
* Map Leaflet zoom levels to geohash precision levels.
* The size of a geohash column-width on the map should be at least `minGeohashPixels` pixels wide.
*/
const zoomPrecision: any = {};
const minGeohashPixels = 16;

for (let zoom = 0; zoom <= 21; zoom += 1) {
const worldPixels = 256 * Math.pow(2, zoom);
zoomPrecision[zoom] = 1;
for (let precision = 2; precision <= maxPrecision; precision += 1) {
const columns = geohashColumns(precision);
if (worldPixels / columns >= minGeohashPixels) {
zoomPrecision[zoom] = precision;
} else {
break;
}
}
}

function getPrecision(val: string) {
let precision = parseInt(val, 10);

if (Number.isNaN(precision)) {
precision = defaultPrecision;
}

if (precision > maxPrecision) {
return maxPrecision;
}

return precision;
}

const isOutsideCollar = (bounds: GeoBoundingBox, collar: MapCollar) =>
bounds && collar && !geoContains(collar, bounds);

const geohashGridTitle = i18n.translate('data.search.aggs.buckets.geohashGridTitle', {
defaultMessage: 'Geohash',
});

interface MapCollar extends GeoBoundingBox {
zoom?: unknown;
}

export interface IBucketGeoHashGridAggConfig extends IBucketAggConfig {
lastMapCollar: MapCollar;
}

export const geoHashBucketAgg = new BucketAggType<IBucketGeoHashGridAggConfig>({
export const geoHashBucketAgg = new BucketAggType<IBucketAggConfig>({
name: BUCKET_TYPES.GEOHASH_GRID,
title: geohashGridTitle,
params: [
Expand All @@ -97,13 +50,8 @@ export const geoHashBucketAgg = new BucketAggType<IBucketGeoHashGridAggConfig>({
{
name: 'precision',
default: defaultPrecision,
deserialize: getPrecision,
write(aggConfig, output) {
const currZoom = aggConfig.params.mapZoom;
const autoPrecisionVal = zoomPrecision[currZoom];
output.params.precision = aggConfig.params.autoPrecision
? autoPrecisionVal
: getPrecision(aggConfig.params.precision);
output.params.precision = aggConfig.params.precision;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct precision is calculated in tile_map_vis

},
},
{
Expand All @@ -117,17 +65,7 @@ export const geoHashBucketAgg = new BucketAggType<IBucketGeoHashGridAggConfig>({
write: () => {},
},
{
name: 'mapZoom',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapZoom and mapCenter are no longer stored on the agg config

default: 2,
write: () => {},
},
{
name: 'mapCenter',
default: [0, 0],
write: () => {},
},
{
name: 'mapBounds',
name: 'boundingBox',
default: null,
write: () => {},
},
Expand All @@ -137,46 +75,22 @@ export const geoHashBucketAgg = new BucketAggType<IBucketGeoHashGridAggConfig>({
const params = agg.params;

if (params.isFilteredByCollar && agg.getField()) {
const { mapBounds, mapZoom } = params;
if (mapBounds) {
let mapCollar: MapCollar;

if (
mapBounds &&
(!agg.lastMapCollar ||
agg.lastMapCollar.zoom !== mapZoom ||
isOutsideCollar(mapBounds, agg.lastMapCollar))
) {
mapCollar = scaleBounds(mapBounds);
mapCollar.zoom = mapZoom;
agg.lastMapCollar = mapCollar;
} else {
mapCollar = agg.lastMapCollar;
}
const boundingBox = {
ignore_unmapped: true,
[agg.getField().name]: {
top_left: mapCollar.top_left,
bottom_right: mapCollar.bottom_right,
},
};
aggs.push(
agg.aggConfigs.createAggConfig(
{
type: 'filter',
id: 'filter_agg',
enabled: true,
params: {
geo_bounding_box: boundingBox,
},
schema: {
group: AggGroupNames.Buckets,
aggs.push(
agg.aggConfigs.createAggConfig(
{
type: 'filter',
id: 'filter_agg',
enabled: true,
params: {
geo_bounding_box: {
ignore_unmapped: true,
[agg.getField().name]: params.boundingBox || defaultBoundingBox,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if bounding box is not set we use default (which should exclude all data)

},
} as any,
{ addToAggConfigs: false }
)
);
}
},
} as any,
{ addToAggConfigs: false }
)
);
}

aggs.push(agg);
Expand All @@ -196,6 +110,6 @@ export const geoHashBucketAgg = new BucketAggType<IBucketGeoHashGridAggConfig>({
);
}

return aggs as IBucketGeoHashGridAggConfig[];
return aggs;
},
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ export const createInputControlVisController = (deps: InputControlVisDependencie
}

async render(visData: any, visParams: VisParams, status: any) {
if (status.params || (visParams.useTimeFilter && status.time)) {
this.visParams = visParams;
this.controls = [];
this.controls = await this.initControls();
const [{ i18n }] = await deps.core.getStartServices();
this.I18nContext = i18n.Context;
this.drawVis();
}
this.visParams = visParams;
this.controls = [];
this.controls = await this.initControls();
const [{ i18n }] = await deps.core.getStartServices();
this.I18nContext = i18n.Context;
this.drawVis();
}

destroy() {
Expand Down
Loading