diff --git a/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.test.ts b/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.test.ts index 04e64233ce196..8128f1a18a66a 100644 --- a/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.test.ts +++ b/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.test.ts @@ -69,6 +69,7 @@ describe('TimeBuckets', () => { 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'); @@ -77,6 +78,23 @@ describe('TimeBuckets', () => { expect(interval.expression).toEqual('20m'); }); + test('getInterval - should scale interval', () => { + const timeBuckets = new TimeBuckets(timeBucketConfig); + const bounds = { + min: moment('2020-03-25'), + max: moment('2020-03-31'), + }; + timeBuckets.setBounds(bounds); + timeBuckets.setInterval('1m'); + + const interval = timeBuckets.getInterval(); + + expect(interval.description).toEqual('day'); + expect(interval.esValue).toEqual(1); + expect(interval.esUnit).toEqual('d'); + expect(interval.expression).toEqual('1d'); + }); + test('setInterval/getInterval - intreval is a string and bounds is defined', () => { const timeBuckets = new TimeBuckets(timeBucketConfig); const bounds = { diff --git a/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts b/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts index d054df0c9274e..f11f89317aea6 100644 --- a/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts +++ b/src/plugins/data/common/search/aggs/buckets/lib/time_buckets/time_buckets.ts @@ -263,18 +263,16 @@ export class TimeBuckets { } const maxLength: number = this._timeBucketConfig['histogram:maxBars']; - const approxLen = Number(duration) / Number(interval); + const minInterval = calcAutoIntervalLessThan(maxLength, Number(duration)); let scaled; - if (approxLen > maxLength) { - scaled = calcAutoIntervalLessThan(maxLength, Number(duration)); + if (interval < minInterval) { + scaled = minInterval; } else { return interval; } - if (+scaled === +interval) return interval; - interval = decorateInterval(interval); return Object.assign(scaled, { preScaled: interval,