Skip to content

Commit

Permalink
Adds time zone to date_range bucket aggregation (#37576)
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaHeiligers authored May 31, 2019
1 parent f4155a6 commit bc8beac
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
70 changes: 70 additions & 0 deletions src/legacy/ui/public/agg_types/__tests__/buckets/_date_range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 { set } from 'lodash';
import expect from '@kbn/expect';
import sinon from 'sinon';
import ngMock from 'ng_mock';
import AggParamWriterProvider from '../agg_param_writer';
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
import chrome from '../../../chrome';

const config = chrome.getUiSettingsClient();

describe('date_range params', function () {
let paramWriter;
let timeField;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
const AggParamWriter = Private(AggParamWriterProvider);
const indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider);

timeField = indexPattern.timeFieldName;
paramWriter = new AggParamWriter({ aggType: 'date_range' });
}));
describe('time_zone', () => {
beforeEach(() => {
sinon.stub(config, 'get');
sinon.stub(config, 'isDefault');
});

it('should use the specified time_zone', () => {
const output = paramWriter.write({ time_zone: 'Europe/Kiev' });
expect(output.params).to.have.property('time_zone', 'Europe/Kiev');
});

it('should use the Kibana time_zone if no parameter specified', () => {
config.isDefault.withArgs('dateFormat:tz').returns(false);
config.get.withArgs('dateFormat:tz').returns('Europe/Riga');
const output = paramWriter.write({});
expect(output.params).to.have.property('time_zone', 'Europe/Riga');
});

it('should use the fixed time_zone from the index pattern typeMeta', () => {
set(paramWriter.indexPattern, ['typeMeta', 'aggs', 'date_range', timeField, 'time_zone'], 'Europe/Rome');
const output = paramWriter.write({ field: timeField });
expect(output.params).to.have.property('time_zone', 'Europe/Rome');
});

afterEach(() => {
config.get.restore();
config.isDefault.restore();
});
});
});
24 changes: 23 additions & 1 deletion src/legacy/ui/public/agg_types/buckets/date_range.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
* specific language governing permissions and limitations
* under the License.
*/

import { get } from 'lodash';
import chrome from '../../chrome';
import moment from 'moment-timezone';
import { dateRange } from '../../utils/date_range';
import '../directives/validate_date_math';
import '../directives/documentation_href';
Expand All @@ -26,6 +28,10 @@ import { fieldFormats } from '../../registry/field_formats';
import dateRangesTemplate from '../controls/date_ranges.html';
import { i18n } from '@kbn/i18n';

const config = chrome.getUiSettingsClient();
const detectedTimezone = moment.tz.guess();
const tzOffset = moment().format('Z');

export const dateRangeBucketAgg = new BucketAggType({
name: 'date_range',
title: i18n.translate('common.ui.aggTypes.buckets.dateRangeTitle', {
Expand Down Expand Up @@ -56,5 +62,21 @@ export const dateRangeBucketAgg = new BucketAggType({
to: 'now'
}],
editor: dateRangesTemplate
}, {
name: 'time_zone',
default: undefined,
// Implimentation method is the same as that of date_histogram
serialize: () => undefined,
write: (agg, output) => {
let tz = agg.params.time_zone;
if (!tz && agg.params.field) {
tz = get(agg.getIndexPattern(), ['typeMeta', 'aggs', 'date_range', agg.params.field.name, 'time_zone']);
}
if (!tz) {
const isDefaultTimezone = config.isDefault('dateFormat:tz');
tz = isDefaultTimezone ? detectedTimezone || tzOffset : config.get('dateFormat:tz');
}
output.params.time_zone = tz;
}
}]
});

0 comments on commit bc8beac

Please sign in to comment.