Skip to content

Commit f526561

Browse files
committed
pass raw values and format afterwards
1 parent 7bcc763 commit f526561

10 files changed

Lines changed: 105 additions & 51 deletions

File tree

src/legacy/ui/public/agg_types/__tests__/buckets/_date_range.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { set } from 'lodash';
2020
import expect from '@kbn/expect';
2121
import sinon from 'sinon';
2222
import ngMock from 'ng_mock';
23+
import { aggTypes } from '../..';
2324
import AggParamWriterProvider from '../agg_param_writer';
2425
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
2526
import chrome from '../../../chrome';
@@ -38,6 +39,15 @@ describe('date_range params', function () {
3839
timeField = indexPattern.timeFieldName;
3940
paramWriter = new AggParamWriter({ aggType: 'date_range' });
4041
}));
42+
43+
describe('getKey', () => {
44+
const dateRange = aggTypes.buckets.find(agg => agg.name === 'date_range');
45+
it('should return object', () => {
46+
const bucket = { from: 'from-date', to: 'to-date', key: 'from-dateto-date' };
47+
expect(dateRange.getKey(bucket)).to.equal({ from: 'from-date', to: 'to-date' });
48+
});
49+
});
50+
4151
describe('time_zone', () => {
4252
beforeEach(() => {
4353
sinon.stub(config, 'get');

src/legacy/ui/public/agg_types/__tests__/buckets/create_filter/date_range.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ describe('AggConfig Filters', function () {
5353
});
5454

5555
const aggConfig = vis.aggs.byName('date_range')[0];
56-
const filter = createFilterDateRange(aggConfig, 'February 1st, 2015 to February 7th, 2015');
56+
const from = new Date('1 Feb 2015');
57+
const to = new Date('7 Feb 2015');
58+
const filter = createFilterDateRange(aggConfig, { from: from.valueOf(), to: to.valueOf() });
5759
expect(filter).to.have.property('range');
5860
expect(filter).to.have.property('meta');
5961
expect(filter.meta).to.have.property('index', indexPattern.id);
6062
expect(filter.range).to.have.property('@timestamp');
61-
expect(filter.range['@timestamp']).to.have.property('gte', moment(new Date('1 Feb 2015')).toISOString());
62-
expect(filter.range['@timestamp']).to.have.property('lt', moment(new Date('7 Feb 2015')).toISOString());
63+
expect(filter.range['@timestamp']).to.have.property('gte', moment(from).toISOString());
64+
expect(filter.range['@timestamp']).to.have.property('lt', moment(to).toISOString());
6365
});
6466
});
6567
});

src/legacy/ui/public/agg_types/__tests__/buckets/create_filter/ip_range.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('AggConfig Filters', function () {
5555
});
5656

5757
const aggConfig = vis.aggs.byName('ip_range')[0];
58-
const filter = createFilterIpRange(aggConfig, '0.0.0.0 to 1.1.1.1');
58+
const filter = createFilterIpRange(aggConfig, { type: 'fromTo', from: '0.0.0.0', to: '1.1.1.1' });
5959
expect(filter).to.have.property('range');
6060
expect(filter).to.have.property('meta');
6161
expect(filter.meta).to.have.property('index', indexPattern.id);
@@ -85,7 +85,7 @@ describe('AggConfig Filters', function () {
8585
});
8686

8787
const aggConfig = vis.aggs.byName('ip_range')[0];
88-
const filter = createFilterIpRange(aggConfig, '67.129.65.201/27');
88+
const filter = createFilterIpRange(aggConfig, { type: 'mask', mask: '67.129.65.201/27' });
8989
expect(filter).to.have.property('range');
9090
expect(filter).to.have.property('meta');
9191
expect(filter.meta).to.have.property('index', indexPattern.id);

src/legacy/ui/public/agg_types/buckets/create_filter/date_range.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@
1717
* under the License.
1818
*/
1919

20-
import chrome from '../../../chrome';
21-
import { dateRange } from '../../../utils/date_range';
2220
import { buildRangeFilter } from '@kbn/es-query';
21+
import moment from 'moment';
2322

24-
const config = chrome.getUiSettingsClient();
25-
26-
export function createFilterDateRange(agg, key) {
27-
const range = dateRange.parse(key, config.get('dateFormat'));
28-
23+
export function createFilterDateRange(agg, { from, to }) {
2924
const filter = {};
30-
if (range.from) filter.gte = range.from.toISOString();
31-
if (range.to) filter.lt = range.to.toISOString();
32-
if (range.to && range.from) filter.format = 'strict_date_optional_time';
25+
if (from) filter.gte = moment(from).toISOString();
26+
if (to) filter.lt = moment(to).toISOString();
27+
if (to && from) filter.format = 'strict_date_optional_time';
3328

3429
return buildRangeFilter(agg.params.field, filter, agg.getIndexPattern());
3530
}

src/legacy/ui/public/agg_types/buckets/create_filter/ip_range.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ import { buildRangeFilter } from '@kbn/es-query';
2222

2323
export function createFilterIpRange(aggConfig, key) {
2424
let range;
25-
if (aggConfig.params.ipRangeType === 'mask') {
26-
range = new CidrMask(key).getRange();
25+
if (key.type === 'mask') {
26+
range = new CidrMask(key.mask).getRange();
2727
} else {
28-
const [from, to] = key.split(/\s+to\s+/);
2928
range = {
30-
from: from === '-Infinity' ? -Infinity : from,
31-
to: to === 'Infinity' ? Infinity : to
29+
from: key.from ? key.from : -Infinity,
30+
to: key.to ? key.to : Infinity
3231
};
3332
}
3433

src/legacy/ui/public/agg_types/buckets/date_range.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { BucketAggType } from './_bucket_agg_type';
2424
import { createFilterDateRange } from './create_filter/date_range';
2525
import { fieldFormats } from '../../registry/field_formats';
2626
import { DateRangesParamEditor } from '../../vis/editors/default/controls/date_ranges';
27+
import { FieldFormat } from '../../../../../plugins/data/common/field_formats';
2728
import { i18n } from '@kbn/i18n';
2829

2930
const config = chrome.getUiSettingsClient();
@@ -36,12 +37,15 @@ export const dateRangeBucketAgg = new BucketAggType({
3637
defaultMessage: 'Date Range',
3738
}),
3839
createFilter: createFilterDateRange,
39-
getKey: function (bucket, key, agg) {
40-
const formatter = agg.fieldOwnFormatter('text', fieldFormats.getDefaultInstance('date'));
41-
return dateRange.toString(bucket, formatter);
40+
getKey: function ({ from, to }) {
41+
return { from, to };
4242
},
43-
getFormat: function () {
44-
return fieldFormats.getDefaultInstance('string');
43+
getFormat: function (agg) {
44+
const formatter = agg.fieldOwnFormatter('text', fieldFormats.getDefaultInstance('date'));
45+
const DateRangeFormat = FieldFormat.from(function (range) {
46+
return dateRange.toString(range, formatter);
47+
});
48+
return new DateRangeFormat();
4549
},
4650
makeLabel: function (aggConfig) {
4751
return aggConfig.getFieldDisplayName() + ' date ranges';

src/legacy/ui/public/agg_types/buckets/ip_range.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import { BucketAggType } from './_bucket_agg_type';
2222
import { createFilterIpRange } from './create_filter/ip_range';
2323
import { IpRangeTypeParamEditor } from '../../vis/editors/default/controls/ip_range_type';
2424
import { IpRangesParamEditor } from '../../vis/editors/default/controls/ip_ranges';
25+
import { fieldFormats } from '../../registry/field_formats';
26+
import { FieldFormat } from '../../../../../plugins/data/common/field_formats';
27+
import { ipRange } from '../../utils/ip_range';
2528
import { i18n } from '@kbn/i18n';
2629

2730
export const ipRangeBucketAgg = new BucketAggType({
@@ -30,11 +33,18 @@ export const ipRangeBucketAgg = new BucketAggType({
3033
defaultMessage: 'IPv4 Range',
3134
}),
3235
createFilter: createFilterIpRange,
33-
getKey: function (bucket, key) {
34-
if (key) return key;
35-
const from = _.get(bucket, 'from', '-Infinity');
36-
const to = _.get(bucket, 'to', 'Infinity');
37-
return `${from} to ${to}`;
36+
getKey: function (bucket, key, agg) {
37+
if (agg.params.ipRangeType === 'mask') {
38+
return { type: 'mask', mask: key };
39+
}
40+
return { type: 'range', from: bucket.from, to: bucket.to };
41+
},
42+
getFormat: function (agg) {
43+
const formatter = agg.fieldOwnFormatter('text', fieldFormats.getDefaultInstance('ip'));
44+
const IpRangeFormat = FieldFormat.from(function (range) {
45+
return ipRange.toString(range, formatter);
46+
});
47+
return new IpRangeFormat();
3848
},
3949
makeLabel: function (aggConfig) {
4050
return i18n.translate('common.ui.aggTypes.buckets.ipRangeLabel', {

src/legacy/ui/public/utils/date_range.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,14 @@
1717
* under the License.
1818
*/
1919

20-
import moment from 'moment';
21-
2220
export const dateRange = {
23-
toString: function (range, format) {
24-
if (!range.from) {
25-
return 'Before ' + format(range.to);
26-
} else if (!range.to) {
27-
return 'After ' + format(range.from);
21+
toString: function ({ from, to }, format) {
22+
if (!from) {
23+
return 'Before ' + format(to);
24+
} else if (!to) {
25+
return 'After ' + format(from);
2826
} else {
29-
return format(range.from) + ' to ' + format(range.to);
27+
return format(from) + ' to ' + format(to);
3028
}
3129
},
32-
parse: function (rangeString, format) {
33-
let chunks = rangeString.split(' to ');
34-
if (chunks.length === 2) return { from: moment(chunks[0], format), to: moment(chunks[1], format) };
35-
36-
chunks = rangeString.split('Before ');
37-
if (chunks.length === 2) return { to: moment(chunks[1], format) };
38-
39-
chunks = rangeString.split('After ');
40-
if (chunks.length === 2) return { from: moment(chunks[1], format) };
41-
42-
throw new Error('Error attempting to parse date range: ' + rangeString);
43-
}
4430
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export const ipRange = {
21+
toString: function (range, format) {
22+
if (range.type === 'mask') {
23+
return format(range.mask);
24+
}
25+
const from = range.from ? format(range.from) : '-Infinity';
26+
const to = range.to ? format(range.to) : 'Infinity';
27+
return `${from} to ${to}`;
28+
},
29+
};

src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import { tabifyGetColumns } from '../../../agg_response/tabify/_get_columns';
2828
import chrome from '../../../chrome';
2929
// @ts-ignore
3030
import { fieldFormats } from '../../../registry/field_formats';
31+
// @ts-ignore
32+
import { dateRange } from '../../../utils/date_range';
33+
// @ts-ignore
34+
import { ipRange } from '../../../utils/ip_range';
3135

3236
interface TermsFieldFormatParams {
3337
otherBucketLabel: string;
@@ -58,7 +62,8 @@ const getFieldFormat = (id: string | undefined, params: object = {}) => {
5862
export const createFormat = (agg: AggConfig): SerializedFieldFormat => {
5963
const format: SerializedFieldFormat = agg.params.field ? agg.params.field.format.toJSON() : {};
6064
const formats: Record<string, () => SerializedFieldFormat> = {
61-
date_range: () => ({ id: 'string' }),
65+
date_range: () => ({ id: 'date_range', params: format }),
66+
ip_range: () => ({ id: 'ip_range', params: format }),
6267
percentile_ranks: () => ({ id: 'percent' }),
6368
count: () => ({ id: 'number' }),
6469
cardinality: () => ({ id: 'number' }),
@@ -109,6 +114,20 @@ export const getFormat: FormatFactory = (mapping = {}) => {
109114
});
110115
});
111116
return new RangeFormat();
117+
} else if (id === 'date_range') {
118+
const nestedFormatter = mapping.params as SerializedFieldFormat;
119+
const DateRangeFormat = FieldFormat.from((range: any) => {
120+
const format = getFieldFormat(nestedFormatter.id, nestedFormatter.params);
121+
return dateRange.toString(range, format.convert.bind(format));
122+
});
123+
return new DateRangeFormat();
124+
} else if (id === 'ip_range') {
125+
const nestedFormatter = mapping.params as SerializedFieldFormat;
126+
const IpRangeFormat = FieldFormat.from((range: any) => {
127+
const format = getFieldFormat(nestedFormatter.id, nestedFormatter.params);
128+
return ipRange.toString(range, format.convert.bind(format));
129+
});
130+
return new IpRangeFormat();
112131
} else if (isTermsFieldFormat(mapping) && mapping.params) {
113132
const params = mapping.params;
114133
return {

0 commit comments

Comments
 (0)