Skip to content

Commit

Permalink
[ui/public/utils] Move items into agg_types (#52744)
Browse files Browse the repository at this point in the history
Closes #51855
  • Loading branch information
alexwizp authored Dec 11, 2019
1 parent 19fec54 commit f2b4891
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { CidrMask } from '../../../utils/cidr_mask';
import { CidrMask } from '../lib/cidr_mask';
import { IBucketAggConfig } from '../_bucket_agg_type';
import { IpRangeKey } from '../ip_range';
import { esFilters } from '../../../../../../plugins/data/public';
Expand Down
17 changes: 14 additions & 3 deletions src/legacy/ui/public/agg_types/buckets/date_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import { BucketAggType, IBucketAggConfig } from './_bucket_agg_type';
import { createFilterDateRange } from './create_filter/date_range';
import { DateRangesParamEditor } from '../../vis/editors/default/controls/date_ranges';

// @ts-ignore
import { dateRange } from '../../utils/date_range';
import {
KBN_FIELD_TYPES,
TEXT_CONTEXT_TYPE,
Expand Down Expand Up @@ -57,7 +55,7 @@ export const dateRangeBucketAgg = new BucketAggType({
fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.DATE)
);
const DateRangeFormat = FieldFormat.from(function(range: DateRangeKey) {
return dateRange.toString(range, formatter);
return convertDateRangeToString(range, formatter);
});
return new DateRangeFormat();
},
Expand Down Expand Up @@ -114,3 +112,16 @@ export const dateRangeBucketAgg = new BucketAggType({
},
],
});

export const convertDateRangeToString = (
{ from, to }: DateRangeKey,
format: (val: any) => string
) => {
if (!from) {
return 'Before ' + format(to);
} else if (!to) {
return 'After ' + format(from);
} else {
return format(from) + ' to ' + format(to);
}
};
19 changes: 10 additions & 9 deletions src/legacy/ui/public/agg_types/buckets/geo_hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import { PrecisionParamEditor } from '../../vis/editors/default/controls/precisi
import { AggGroupNames } from '../../vis/editors/default/agg_groups';
import { KBN_FIELD_TYPES } from '../../../../../plugins/data/public';

// @ts-ignore
import { geoContains, scaleBounds } from '../../utils/geo_utils';
import { geoContains, scaleBounds, GeoBoundingBox } from './lib/geo_utils';
import { BUCKET_TYPES } from './bucket_agg_types';

const config = chrome.getUiSettingsClient();
Expand Down Expand Up @@ -70,15 +69,15 @@ function getPrecision(val: string) {
return precision;
}

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

const geohashGridTitle = i18n.translate('common.ui.aggTypes.buckets.geohashGridTitle', {
defaultMessage: 'Geohash',
});

interface MapCollar {
zoom: unknown;
interface MapCollar extends GeoBoundingBox {
zoom?: unknown;
}

export interface IBucketGeoHashGridAggConfig extends IBucketAggConfig {
Expand Down Expand Up @@ -148,11 +147,13 @@ export const geoHashBucketAgg = new BucketAggType<IBucketGeoHashGridAggConfig>({
if (params.isFilteredByCollar && agg.getField()) {
const { mapBounds, mapZoom } = params;
if (mapBounds) {
let mapCollar;
let mapCollar: MapCollar;

if (
!agg.lastMapCollar ||
agg.lastMapCollar.zoom !== mapZoom ||
isOutsideCollar(mapBounds, agg.lastMapCollar)
mapBounds &&
(!agg.lastMapCollar ||
agg.lastMapCollar.zoom !== mapZoom ||
isOutsideCollar(mapBounds, agg.lastMapCollar))
) {
mapCollar = scaleBounds(mapBounds);
mapCollar.zoom = mapZoom;
Expand Down
13 changes: 11 additions & 2 deletions src/legacy/ui/public/agg_types/buckets/ip_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { npStart } from 'ui/new_platform';
import { BucketAggType, IBucketAggConfig } from './_bucket_agg_type';
import { IpRangeTypeParamEditor } from '../../vis/editors/default/controls/ip_range_type';
import { IpRangesParamEditor } from '../../vis/editors/default/controls/ip_ranges';
import { ipRange } from '../../utils/ip_range';
import { BUCKET_TYPES } from './bucket_agg_types';

// @ts-ignore
Expand Down Expand Up @@ -59,7 +58,7 @@ export const ipRangeBucketAgg = new BucketAggType({
fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.IP)
);
const IpRangeFormat = FieldFormat.from(function(range: IpRangeKey) {
return ipRange.toString(range, formatter);
return convertIPRangeToString(range, formatter);
});
return new IpRangeFormat();
},
Expand Down Expand Up @@ -106,3 +105,13 @@ export const ipRangeBucketAgg = new BucketAggType({
},
],
});

export const convertIPRangeToString = (range: IpRangeKey, format: (val: any) => string) => {
if (range.type === 'mask') {
return format(range.mask);
}
const from = range.from ? format(range.from) : '-Infinity';
const to = range.to ? format(range.to) : 'Infinity';

return `${from} to ${to}`;
};
75 changes: 75 additions & 0 deletions src/legacy/ui/public/agg_types/buckets/lib/cidr_mask.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { CidrMask } from './cidr_mask';

describe('CidrMask', () => {
test('should throw errors with invalid CIDR masks', () => {
expect(
() =>
// @ts-ignore
new CidrMask()
).toThrowError();

expect(() => new CidrMask('')).toThrowError();
expect(() => new CidrMask('hello, world')).toThrowError();
expect(() => new CidrMask('0.0.0.0')).toThrowError();
expect(() => new CidrMask('0.0.0.0/0')).toThrowError();
expect(() => new CidrMask('0.0.0.0/33')).toThrowError();
expect(() => new CidrMask('256.0.0.0/32')).toThrowError();
expect(() => new CidrMask('0.0.0.0/32/32')).toThrowError();
expect(() => new CidrMask('1.2.3/1')).toThrowError();
expect(() => new CidrMask('0.0.0.0/123d')).toThrowError();
});

test('should correctly grab IP address and prefix length', () => {
let mask = new CidrMask('0.0.0.0/1');
expect(mask.initialAddress.toString()).toBe('0.0.0.0');
expect(mask.prefixLength).toBe(1);

mask = new CidrMask('128.0.0.1/31');
expect(mask.initialAddress.toString()).toBe('128.0.0.1');
expect(mask.prefixLength).toBe(31);
});

test('should calculate a range of IP addresses', () => {
let mask = new CidrMask('0.0.0.0/1');
let range = mask.getRange();
expect(range.from.toString()).toBe('0.0.0.0');
expect(range.to.toString()).toBe('127.255.255.255');

mask = new CidrMask('1.2.3.4/2');
range = mask.getRange();
expect(range.from.toString()).toBe('0.0.0.0');
expect(range.to.toString()).toBe('63.255.255.255');

mask = new CidrMask('67.129.65.201/27');
range = mask.getRange();
expect(range.from.toString()).toBe('67.129.65.192');
expect(range.to.toString()).toBe('67.129.65.223');
});

test('toString()', () => {
let mask = new CidrMask('.../1');
expect(mask.toString()).toBe('0.0.0.0/1');

mask = new CidrMask('128.0.0.1/31');
expect(mask.toString()).toBe('128.0.0.1/31');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* under the License.
*/

import { Ipv4Address } from '../../../../plugins/kibana_utils/public';
import { Ipv4Address } from '../../../../../../plugins/kibana_utils/public';

const NUM_BITS = 32;

function throwError(mask: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,57 @@

import _ from 'lodash';

export function geoContains(collar, bounds) {
//test if bounds top_left is outside collar
if(bounds.top_left.lat > collar.top_left.lat || bounds.top_left.lon < collar.top_left.lon) {
interface GeoBoundingBoxCoordinate {
lat: number;
lon: number;
}

export interface GeoBoundingBox {
top_left: GeoBoundingBoxCoordinate;
bottom_right: GeoBoundingBoxCoordinate;
}

export function geoContains(collar: GeoBoundingBox, bounds: GeoBoundingBox) {
// test if bounds top_left is outside collar
if (bounds.top_left.lat > collar.top_left.lat || bounds.top_left.lon < collar.top_left.lon) {
return false;
}

//test if bounds bottom_right is outside collar
if(bounds.bottom_right.lat < collar.bottom_right.lat || bounds.bottom_right.lon > collar.bottom_right.lon) {
// test if bounds bottom_right is outside collar
if (
bounds.bottom_right.lat < collar.bottom_right.lat ||
bounds.bottom_right.lon > collar.bottom_right.lon
) {
return false;
}

//both corners are inside collar so collar contains bounds
// both corners are inside collar so collar contains bounds
return true;
}

export function scaleBounds(bounds) {
if (!bounds) return;

const scale = .5; // scale bounds by 50%
export function scaleBounds(bounds: GeoBoundingBox): GeoBoundingBox {
const scale = 0.5; // scale bounds by 50%

const topLeft = bounds.top_left;
const bottomRight = bounds.bottom_right;
let latDiff = _.round(Math.abs(topLeft.lat - bottomRight.lat), 5);
const lonDiff = _.round(Math.abs(bottomRight.lon - topLeft.lon), 5);
//map height can be zero when vis is first created
if(latDiff === 0) latDiff = lonDiff;
// map height can be zero when vis is first created
if (latDiff === 0) latDiff = lonDiff;

const latDelta = latDiff * scale;
let topLeftLat = _.round(topLeft.lat, 5) + latDelta;
if(topLeftLat > 90) topLeftLat = 90;
if (topLeftLat > 90) topLeftLat = 90;
let bottomRightLat = _.round(bottomRight.lat, 5) - latDelta;
if(bottomRightLat < -90) bottomRightLat = -90;
if (bottomRightLat < -90) bottomRightLat = -90;
const lonDelta = lonDiff * scale;
let topLeftLon = _.round(topLeft.lon, 5) - lonDelta;
if(topLeftLon < -180) topLeftLon = -180;
if (topLeftLon < -180) topLeftLon = -180;
let bottomRightLon = _.round(bottomRight.lon, 5) + lonDelta;
if(bottomRightLon > 180) bottomRightLon = 180;
if (bottomRightLon > 180) bottomRightLon = 180;

return {
'top_left': { lat: topLeftLat, lon: topLeftLon },
'bottom_right': { lat: bottomRightLat, lon: bottomRightLon }
top_left: { lat: topLeftLat, lon: topLeftLon },
bottom_right: { lat: bottomRightLat, lon: bottomRightLon },
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
* under the License.
*/

import _ from 'lodash';
import { ordinalSuffix } from '../ordinal_suffix';
import expect from '@kbn/expect';
import { forOwn } from 'lodash';
import { ordinalSuffix } from './ordinal_suffix';

describe('ordinal suffix util', function () {
describe('ordinal suffix util', () => {
const checks = {
1: 'st',
2: 'nd',
Expand Down Expand Up @@ -52,19 +51,19 @@ describe('ordinal suffix util', function () {
27: 'th',
28: 'th',
29: 'th',
30: 'th'
30: 'th',
};

_.forOwn(checks, function (expected, num) {
forOwn(checks, (expected, num: any) => {
const int = parseInt(num, 10);
const float = int + Math.random();

it('knowns ' + int, function () {
expect(ordinalSuffix(num)).to.be(num + '' + expected);
it('knowns ' + int, () => {
expect(ordinalSuffix(num)).toBe(num + '' + expected);
});

it('knows ' + float, function () {
expect(ordinalSuffix(num)).to.be(num + '' + expected);
it('knows ' + float, () => {
expect(ordinalSuffix(num)).toBe(num + '' + expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/

// adopted from http://stackoverflow.com/questions/3109978/php-display-number-with-ordinal-suffix
export function ordinalSuffix(num) {
export function ordinalSuffix(num: any): string {
return num + '' + suffix(num);
}

function suffix(num) {
function suffix(num: any): string {
const int = Math.floor(parseFloat(num));

const hunth = int % 100;
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/ui/public/agg_types/metrics/percentiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { getPercentileValue } from './percentiles_get_value';
import { PercentilesEditor } from '../../vis/editors/default/controls/percentiles';

// @ts-ignore
import { ordinalSuffix } from '../../utils/ordinal_suffix';
import { ordinalSuffix } from './lib/ordinal_suffix';

export type IPercentileAggConfig = IResponseAggConfig;

Expand Down
Loading

0 comments on commit f2b4891

Please sign in to comment.