From c6280f264a1df33f0c9a28364321f53e900a51f7 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Mon, 7 Aug 2017 16:13:07 -0700 Subject: [PATCH 1/4] Fixes #12113 - Add timezone to date histogram aggregations --- .../metrics/public/kbn_vis_types/request_handler.js | 13 ++++++++++++- .../annotations/date_histogram.js | 2 ++ .../request_processors/series/date_histogram.js | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js index 839b79c56fcf1..9b6f335f72273 100644 --- a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js +++ b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js @@ -1,5 +1,7 @@ import { validateInterval } from '../lib/validate_interval'; import { dashboardContextProvider } from 'plugins/kibana/dashboard/dashboard_context'; +import { jstz as tzDetect } from 'jstimezonedetect'; +import moment from 'moment'; const MetricsRequestHandlerProvider = function (Private, Notifier, config, timefilter, $http) { const dashboardContext = Private(dashboardContextProvider); @@ -9,11 +11,20 @@ const MetricsRequestHandlerProvider = function (Private, Notifier, config, timef name: 'metrics', handler: function (vis /*, appState, uiState, queryFilter*/) { + let timezone = config.get('dateFormat:tz', 'Browser'); + if (timezone === 'Browser') { + timezone = tzDetect.determine().name(); + if (!timezone) { + timezone = moment().format('Z'); + } + + } + return new Promise((resolve) => { const panel = vis.params; if (panel && panel.id) { const params = { - timerange: timefilter.getBounds(), + timerange: { timezone, ...timefilter.getBounds() }, filters: [dashboardContext()], panels: [panel] }; diff --git a/src/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js b/src/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js index 08d51dc05ca7a..f64ab9ad5fbab 100644 --- a/src/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js +++ b/src/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js @@ -6,10 +6,12 @@ export default function dateHistogram(req, panel, annotation) { const timeField = annotation.time_field; const { bucketSize, intervalString } = getBucketSize(req, 'auto'); const { from, to } = getTimerange(req); + const { timezone:time_zone } = req.payload.timerange; _.set(doc, `aggs.${annotation.id}.date_histogram`, { field: timeField, interval: intervalString, min_doc_count: 0, + time_zone, extended_bounds: { min: from.valueOf(), max: to.valueOf() - (bucketSize * 1000) diff --git a/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js b/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js index 2dcac4ce0942f..fcbb07a6f2736 100644 --- a/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js +++ b/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js @@ -7,11 +7,13 @@ export default function dateHistogram(req, panel, series) { const { timeField, interval } = getIntervalAndTimefield(panel, series); const { intervalString } = getBucketSize(req, interval); const { from, to } = offsetTime(req, series.offset_time); + const { timezone:time_zone } = req.payload.timerange; set(doc, `aggs.${series.id}.aggs.timeseries.date_histogram`, { field: timeField, interval: intervalString, min_doc_count: 0, + time_zone, extended_bounds: { min: from.valueOf(), max: to.valueOf() From 243bc34ff955c5fc72bede99c32c687922e009a0 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Mon, 7 Aug 2017 16:17:07 -0700 Subject: [PATCH 2/4] Fixing tests --- .../request_processors/series/__tests__/date_histogram.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js b/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js index f14f649d4b715..cb0046f6e7ed2 100644 --- a/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js +++ b/src/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js @@ -11,6 +11,7 @@ describe('dateHistogram(req, panel, series)', () => { req = { payload: { timerange: { + timezone: 'UTC', min: '2017-01-01T00:00:00Z', max: '2017-01-01T01:00:00Z' } @@ -42,6 +43,7 @@ describe('dateHistogram(req, panel, series)', () => { field: '@timestamp', interval: '10s', min_doc_count: 0, + time_zone: 'UTC', extended_bounds: { min: 1483228800000, max: 1483232400000 @@ -67,6 +69,7 @@ describe('dateHistogram(req, panel, series)', () => { field: '@timestamp', interval: '10s', min_doc_count: 0, + time_zone: 'UTC', extended_bounds: { min: 1483225200000, max: 1483228800000 @@ -95,6 +98,7 @@ describe('dateHistogram(req, panel, series)', () => { field: 'timestamp', interval: '20s', min_doc_count: 0, + time_zone: 'UTC', extended_bounds: { min: 1483228800000, max: 1483232400000 From 431c2d77a4c8d94c695aa0a9dd7d9cad64c0fe00 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Mon, 7 Aug 2017 17:03:54 -0700 Subject: [PATCH 3/4] Fixing tests --- .../metrics/server/lib/vis_data/__tests__/build_request_body.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core_plugins/metrics/server/lib/vis_data/__tests__/build_request_body.js b/src/core_plugins/metrics/server/lib/vis_data/__tests__/build_request_body.js index 93b6802b509ac..4d8d93e96bba5 100644 --- a/src/core_plugins/metrics/server/lib/vis_data/__tests__/build_request_body.js +++ b/src/core_plugins/metrics/server/lib/vis_data/__tests__/build_request_body.js @@ -49,6 +49,7 @@ const body = JSON.parse(` } ], "timerange": { + "timezone": "UTC", "max": "2017-01-26T20:52:35.881Z", "min": "2017-01-26T20:37:35.881Z" } @@ -104,6 +105,7 @@ describe('buildRequestBody(req)', () => { 'field': '@timestamp', 'interval': '10s', 'min_doc_count': 0, + 'time_zone': 'UTC', 'extended_bounds': { 'min': 1485463055881, 'max': 1485463955881 From d825e36c15410cc11dc8b1f73e24c1e0cbc5a720 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Thu, 10 Aug 2017 16:45:55 -0700 Subject: [PATCH 4/4] Moving to universal timezone function --- .../metrics/public/kbn_vis_types/request_handler.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js index 9b6f335f72273..522e73fb15322 100644 --- a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js +++ b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js @@ -1,7 +1,6 @@ import { validateInterval } from '../lib/validate_interval'; import { dashboardContextProvider } from 'plugins/kibana/dashboard/dashboard_context'; -import { jstz as tzDetect } from 'jstimezonedetect'; -import moment from 'moment'; +import { timezoneProvider } from 'ui/vis/lib/timezone'; const MetricsRequestHandlerProvider = function (Private, Notifier, config, timefilter, $http) { const dashboardContext = Private(dashboardContextProvider); @@ -10,15 +9,7 @@ const MetricsRequestHandlerProvider = function (Private, Notifier, config, timef return { name: 'metrics', handler: function (vis /*, appState, uiState, queryFilter*/) { - - let timezone = config.get('dateFormat:tz', 'Browser'); - if (timezone === 'Browser') { - timezone = tzDetect.determine().name(); - if (!timezone) { - timezone = moment().format('Z'); - } - - } + const timezone = Private(timezoneProvider)(); return new Promise((resolve) => { const panel = vis.params;