From e07ea0398f56bbff8dec97bbf70aa51e17b17870 Mon Sep 17 00:00:00 2001 From: Guillaume Thomas Date: Tue, 1 Jun 2021 16:02:23 +0200 Subject: [PATCH] Fix handling of InUse timezone for date_histogram (#44) Co-authored-by: Guillaume Thomas --- .../agg_types/buckets/date_histogram.js | 30 +++++++++++++++++-- src/ui/public/vis/lib/timezone.js | 19 +++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/ui/public/agg_types/buckets/date_histogram.js b/src/ui/public/agg_types/buckets/date_histogram.js index 16e336514d..0c206f9640 100644 --- a/src/ui/public/agg_types/buckets/date_histogram.js +++ b/src/ui/public/agg_types/buckets/date_histogram.js @@ -32,6 +32,22 @@ import { timefilter } from '../../timefilter'; import dropPartialTemplate from '../controls/drop_partials.html'; import { i18n } from '@kbn/i18n'; +function getCookie(cname) { + const name = cname + '='; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + for(let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) === ' ') { + c = c.substring(1); + } + if (c.indexOf(name) === 0) { + return c.substring(name.length, c.length); + } + } + return ''; +} + const config = chrome.getUiSettingsClient(); const detectedTimezone = tzDetect.determine().name(); const tzOffset = moment().format('Z'); @@ -52,6 +68,7 @@ function setBounds(agg, force) { } + export const dateHistogramBucketAgg = new BucketAggType({ name: 'date_histogram', title: i18n.translate('common.ui.aggTypes.buckets.dateHistogramTitle', { @@ -157,9 +174,16 @@ export const dateHistogramBucketAgg = new BucketAggType({ }, { name: 'time_zone', - default: () => { - const isDefaultTimezone = config.isDefault('dateFormat:tz'); - return isDefaultTimezone ? detectedTimezone || tzOffset : config.get('dateFormat:tz'); + write: (agg, output) => { + if (config.isDefault('dateFormat:tz')) { + output.params.time_zone = getCookie('timezone'); + } + else if (config.get('dateFormat:tz') === 'Browser') { + output.params.time_zone = detectedTimezone || tzOffset; + } + else { + output.params.time_zone = config.get('dateFormat:tz'); + } }, }, { diff --git a/src/ui/public/vis/lib/timezone.js b/src/ui/public/vis/lib/timezone.js index 473be70aa7..678ad4cae0 100644 --- a/src/ui/public/vis/lib/timezone.js +++ b/src/ui/public/vis/lib/timezone.js @@ -20,10 +20,27 @@ const tzDetect = require('jstimezonedetect').jstz; import moment from 'moment'; +function getCookie(cname) { + const name = cname + '='; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + for(let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) === ' ') { + c = c.substring(1); + } + if (c.indexOf(name) === 0) { + return c.substring(name.length, c.length); + } + } + return ''; +} + export function timezoneProvider(config) { return function () { - if (config.isDefault('dateFormat:tz')) { + return getCookie('timezone'); + } else if (config.get('dateFormat:tz') === 'Browser') { const detectedTimezone = tzDetect.determine().name(); if (detectedTimezone) return detectedTimezone; else return moment().format('Z');