Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -77,88 +78,35 @@ describe('Geohash Agg', () => {
it('should select precision parameter', () => {
expect(precisionParam.name).toEqual('precision');
});

describe('precision parameter write', () => {
const zoomToGeoHashPrecision: Record<string, any> = {
0: 1,
1: 2,
2: 2,
3: 2,
4: 3,
5: 3,
6: 4,
7: 4,
8: 4,
9: 5,
10: 5,
11: 6,
12: 6,
13: 6,
14: 7,
15: 7,
16: 8,
17: 8,
18: 8,
19: 9,
20: 9,
21: 10,
};

Object.keys(zoomToGeoHashPrecision).forEach((zoomLevel: string) => {
it(`zoom level ${zoomLevel} should correspond to correct geohash-precision`, () => {
const aggConfigs = getAggConfigs({
autoPrecision: true,
mapZoom: zoomLevel,
});

const { [BUCKET_TYPES.GEOHASH_GRID]: params } = aggConfigs.aggs[0].toDsl();

expect(params.precision).toEqual(zoomToGeoHashPrecision[zoomLevel]);
});
});
});
});

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 +116,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,48 +126,43 @@ 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({
boundingBox: {
top_left: { lat: 1, lon: -1 },
bottom_right: { lat: -1, lon: 1 },
},
}).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', () => {
const [, geoBoxingBox] = geoHashBucketAgg.getRequestAggs(
getAggConfigs({
mapBounds: {
boundingBox: {
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);
});

it('should not change geo_bounding_box filter aggregation and vis session state when map movement is within map collar', () => {
const [, geoBoxingBox] = geoHashBucketAgg.getRequestAggs(
getAggConfigs({
mapBounds: {
boundingBox: {
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;
/**
* 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;
},
},
{
Expand All @@ -117,17 +65,7 @@ export const geoHashBucketAgg = new BucketAggType<IBucketGeoHashGridAggConfig>({
write: () => {},
},
{
name: 'mapZoom',
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,
},
} 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;
},
});
Loading