Skip to content

Commit

Permalink
Fixed comment about config
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Apr 7, 2020
1 parent 304f08d commit 7c6cf77
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,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
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,26 @@
* under the License.
*/
import moment from 'moment';
import { coreMock } from '../../../../../../../../../src/core/public/mocks';

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

describe('TimeBuckets', () => {
const { uiSettings } = coreMock.createSetup();
uiSettings.get.mockImplementation((key: string) => {
switch (key) {
case 'histogram:maxBars':
return 4;
case 'histogram:barTarget':
return 2;
case 'dateFormat:scaled':
return [
['', 'HH:mm:ss.SSS'],
['PT1S', 'HH:mm:ss'],
['PT1M', 'HH:mm'],
['PT1H', 'YYYY-MM-DD HH:mm'],
['P1DT', 'YYYY-MM-DD'],
['P1YT', 'YYYY'],
];
}
});
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({ uiSettings });
const timeBuckets = new TimeBuckets(timeBucketConfig);
const bounds = {
min: moment('2020-03-25'),
max: moment('2020-03-31'),
Expand All @@ -54,7 +48,7 @@ describe('TimeBuckets', () => {
});

test('setBounds/getBounds - bounds is undefined', () => {
const timeBuckets = new TimeBuckets({ uiSettings });
const timeBuckets = new TimeBuckets(timeBucketConfig);
const bounds = {
min: moment('2020-03-25'),
max: moment('2020-03-31'),
Expand All @@ -71,7 +65,7 @@ describe('TimeBuckets', () => {
});

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

Expand All @@ -82,7 +76,7 @@ describe('TimeBuckets', () => {
});

test('setInterval/getInterval - intreval is a string and bounds is defined', () => {
const timeBuckets = new TimeBuckets({ uiSettings });
const timeBuckets = new TimeBuckets(timeBucketConfig);
const bounds = {
min: moment('2020-03-25'),
max: moment('2020-03-31'),
Expand All @@ -107,7 +101,7 @@ describe('TimeBuckets', () => {
});

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

Expand All @@ -118,7 +112,7 @@ describe('TimeBuckets', () => {
});

test('getScaledDateFormat', () => {
const timeBuckets = new TimeBuckets({ uiSettings });
const timeBuckets = new TimeBuckets(timeBucketConfig);
timeBuckets.setInterval('20m');
timeBuckets.getScaledDateFormat();
const format = timeBuckets.getScaledDateFormat();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import { isString, isObject as isObjectLodash, isPlainObject, sortBy } from 'lodash';
import moment, { Moment } from 'moment';

import { IUiSettingsClient } from 'src/core/public';
import { parseInterval } from '../../../../../../common';
import { TimeRangeBounds } from '../../../../../query';
import { calcAutoIntervalLessThan, calcAutoIntervalNear } from './calc_auto_interval';
Expand Down Expand Up @@ -49,8 +48,11 @@ function isValidMoment(m: any): boolean {
return m && 'isValid' in m && m.isValid();
}

interface TimeBucketsConfig {
uiSettings: IUiSettingsClient;
export interface TimeBucketsConfig {
'histogram:maxBars': number;
'histogram:barTarget': number;
dateFormat: string;
'dateFormat:scaled': string[][];
}

/**
Expand All @@ -61,7 +63,7 @@ interface TimeBucketsConfig {
* @param {[type]} display [description]
*/
export class TimeBuckets {
private _uiSettings: IUiSettingsClient;
private _timeBucketConfig: TimeBucketsConfig;
private _lb: TimeRangeBounds['min'];
private _ub: TimeRangeBounds['max'];
private _originalInterval: string | null = null;
Expand All @@ -70,8 +72,8 @@ export class TimeBuckets {
// because other parts of Kibana arbitrarily add properties
[key: string]: any;

constructor({ uiSettings }: TimeBucketsConfig) {
this._uiSettings = uiSettings;
constructor(timeBucketConfig: TimeBucketsConfig) {
this._timeBucketConfig = timeBucketConfig;
}

/**
Expand Down Expand Up @@ -248,7 +250,7 @@ export class TimeBuckets {
const readInterval = () => {
const interval = this._i;
if (moment.isDuration(interval)) return interval;
return calcAutoIntervalNear(this._uiSettings.get('histogram:barTarget'), Number(duration));
return calcAutoIntervalNear(this._timeBucketConfig['histogram:barTarget'], Number(duration));
};

const parsedInterval = readInterval();
Expand All @@ -259,7 +261,7 @@ export class TimeBuckets {
return interval;
}

const maxLength: number = this._uiSettings.get('histogram:maxBars');
const maxLength: number = this._timeBucketConfig['histogram:maxBars'];
const approxLen = Number(duration) / Number(interval);

let scaled;
Expand Down Expand Up @@ -316,7 +318,7 @@ export class TimeBuckets {
*/
getScaledDateFormat() {
const interval = this.getInterval();
const rules = this._uiSettings.get('dateFormat:scaled');
const rules = this._timeBucketConfig['dateFormat:scaled'];

for (let i = rules.length - 1; i >= 0; i--) {
const rule = rules[i];
Expand All @@ -325,6 +327,6 @@ export class TimeBuckets {
}
}

return this._uiSettings.get('dateFormat');
return this._timeBucketConfig.dateFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export function getCalculateAutoTimeExpression(uiSettings: IUiSettingsClient) {
return;
}

const buckets = new TimeBuckets({ uiSettings });
const 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'),
});

buckets.setInterval('auto');
buckets.setBounds({
Expand Down

0 comments on commit 7c6cf77

Please sign in to comment.