diff --git a/src/legacy/ui/public/agg_types/__tests__/buckets/_date_range.js b/src/legacy/ui/public/agg_types/__tests__/buckets/_date_range.js new file mode 100644 index 0000000000000..10ac4f3befeb7 --- /dev/null +++ b/src/legacy/ui/public/agg_types/__tests__/buckets/_date_range.js @@ -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(); + }); + }); +}); diff --git a/src/legacy/ui/public/agg_types/buckets/date_range.js b/src/legacy/ui/public/agg_types/buckets/date_range.js index 3f74791bb3a94..e975a33035d8c 100644 --- a/src/legacy/ui/public/agg_types/buckets/date_range.js +++ b/src/legacy/ui/public/agg_types/buckets/date_range.js @@ -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'; @@ -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', { @@ -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; + } }] });