Skip to content

Commit

Permalink
[data.search.aggs]: Clean up TimeBuckets implementation (#62123)
Browse files Browse the repository at this point in the history
* Created tests for time_buckets. Clean up code. Removed getConfig method.

* Fixed comments

* Fixed comments (2)

* Fixes

* Removed __cached__

* Fixes for comments

* some refactoring

* Fixed comment about config

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
VladLasitsa and elasticmachine committed Apr 9, 2020
1 parent 7b0e9d0 commit 7ec6357
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 153 deletions.
29 changes: 15 additions & 14 deletions src/plugins/data/public/search/aggs/buckets/date_histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,23 @@ const updateTimeBuckets = (
timefilter: TimefilterContract,
customBuckets?: IBucketDateHistogramAggConfig['buckets']
) => {
const bounds = agg.params.timeRange ? timefilter.calculateBounds(agg.params.timeRange) : null;
const bounds =
agg.params.timeRange && agg.fieldIsTimeField()
? timefilter.calculateBounds(agg.params.timeRange)
: undefined;
const buckets = customBuckets || agg.buckets;
buckets.setBounds(agg.fieldIsTimeField() && bounds);
buckets.setBounds(bounds);
buckets.setInterval(agg.params.interval);
};

// TODO: Need to incorporate these properly into TimeBuckets
interface ITimeBuckets {
setBounds: Function;
getScaledDateFormat: TimeBuckets['getScaledDateFormat'];
setInterval: Function;
getInterval: Function;
}

export interface DateHistogramBucketAggDependencies {
uiSettings: IUiSettingsClient;
query: QuerySetup;
getInternalStartServices: GetInternalStartServicesFn;
}

export interface IBucketDateHistogramAggConfig extends IBucketAggConfig {
buckets: ITimeBuckets;
buckets: TimeBuckets;
}

export function isDateHistogramBucketAggConfig(agg: any): agg is IBucketDateHistogramAggConfig {
Expand Down Expand Up @@ -113,7 +108,12 @@ export const getDateHistogramBucketAgg = ({
if (buckets) return buckets;

const { timefilter } = query.timefilter;
buckets = new TimeBuckets({ uiSettings });
buckets = new TimeBuckets({
'histogram:maxBars': uiSettings.get('histogram:maxBars'),
'histogram:barTarget': uiSettings.get('histogram:barTarget'),
dateFormat: uiSettings.get('dateFormat'),
'dateFormat:scaled': uiSettings.get('dateFormat:scaled'),
});
updateTimeBuckets(this, timefilter, buckets);

return buckets;
Expand Down Expand Up @@ -206,7 +206,8 @@ export const getDateHistogramBucketAgg = ({
...dateHistogramInterval(interval.expression),
};

const scaleMetrics = scaleMetricValues && interval.scaled && interval.scale < 1;
const scaleMetrics =
scaleMetricValues && interval.scaled && interval.scale && interval.scale < 1;
if (scaleMetrics && aggs) {
const metrics = aggs.aggs.filter(a => isMetricAggType(a.type));
const all = every(metrics, (a: IBucketAggConfig) => {
Expand All @@ -218,7 +219,7 @@ export const getDateHistogramBucketAgg = ({
});
if (all) {
output.metricScale = interval.scale;
output.metricScaleText = interval.preScaled.description;
output.metricScaleText = interval.preScaled?.description || '';
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* 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 moment from 'moment';

import { TimeBuckets, TimeBucketsConfig } from './time_buckets';

describe('TimeBuckets', () => {
const timeBucketConfig: TimeBucketsConfig = {
'histogram:maxBars': 4,
'histogram:barTarget': 3,
dateFormat: 'YYYY-MM-DD',
'dateFormat:scaled': [
['', 'HH:mm:ss.SSS'],
['PT1S', 'HH:mm:ss'],
['PT1M', 'HH:mm'],
['PT1H', 'YYYY-MM-DD HH:mm'],
['P1DT', 'YYYY-MM-DD'],
['P1YT', 'YYYY'],
],
};

test('setBounds/getBounds - bounds is correct', () => {
const timeBuckets = new TimeBuckets(timeBucketConfig);
const bounds = {
min: moment('2020-03-25'),
max: moment('2020-03-31'),
};
timeBuckets.setBounds(bounds);
const timeBucketsBounds = timeBuckets.getBounds();

expect(timeBucketsBounds).toEqual(bounds);
});

test('setBounds/getBounds - bounds is undefined', () => {
const timeBuckets = new TimeBuckets(timeBucketConfig);
const bounds = {
min: moment('2020-03-25'),
max: moment('2020-03-31'),
};
timeBuckets.setBounds(bounds);
let timeBucketsBounds = timeBuckets.getBounds();

expect(timeBucketsBounds).toEqual(bounds);

timeBuckets.setBounds();
timeBucketsBounds = timeBuckets.getBounds();

expect(timeBucketsBounds).toBeUndefined();
});

test('setInterval/getInterval - intreval is a string', () => {
const timeBuckets = new TimeBuckets(timeBucketConfig);
timeBuckets.setInterval('20m');
const interval = timeBuckets.getInterval();

expect(interval.description).toEqual('20 minutes');
expect(interval.esValue).toEqual(20);
expect(interval.esUnit).toEqual('m');
expect(interval.expression).toEqual('20m');
});

test('setInterval/getInterval - intreval is a string and bounds is defined', () => {
const timeBuckets = new TimeBuckets(timeBucketConfig);
const bounds = {
min: moment('2020-03-25'),
max: moment('2020-03-31'),
};
timeBuckets.setBounds(bounds);
timeBuckets.setInterval('20m');
const interval = timeBuckets.getInterval();

expect(interval.description).toEqual('day');
expect(interval.esValue).toEqual(1);
expect(interval.esUnit).toEqual('d');
expect(interval.expression).toEqual('1d');
expect(interval.scaled).toBeTruthy();
expect(interval.scale).toEqual(0.013888888888888888);

if (interval.preScaled) {
expect(interval.preScaled.description).toEqual('20 minutes');
expect(interval.preScaled.esValue).toEqual(20);
expect(interval.preScaled.esUnit).toEqual('m');
expect(interval.preScaled.expression).toEqual('20m');
}
});

test('setInterval/getInterval - intreval is a "auto"', () => {
const timeBuckets = new TimeBuckets(timeBucketConfig);
timeBuckets.setInterval('auto');
const interval = timeBuckets.getInterval();

expect(interval.description).toEqual('0 milliseconds');
expect(interval.esValue).toEqual(0);
expect(interval.esUnit).toEqual('ms');
expect(interval.expression).toEqual('0ms');
});

test('getScaledDateFormat', () => {
const timeBuckets = new TimeBuckets(timeBucketConfig);
timeBuckets.setInterval('20m');
timeBuckets.getScaledDateFormat();
const format = timeBuckets.getScaledDateFormat();
expect(format).toEqual('HH:mm');
});
});
Loading

0 comments on commit 7ec6357

Please sign in to comment.