From c51565ba72df40d685dd1820c1dcb83a37fe609d Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Wed, 19 Jan 2022 19:35:46 +0100 Subject: [PATCH] handle ignore daylight time correctly and fix shift problem --- .../components/vis_types/timeseries/vis.js | 1 + .../visualizations/views/timeseries/index.js | 5 +++- .../response_processors/series/time_shift.js | 30 +++++++++++++++++-- .../vis_data/series/handle_response_body.ts | 3 +- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/plugins/vis_types/timeseries/public/application/components/vis_types/timeseries/vis.js b/src/plugins/vis_types/timeseries/public/application/components/vis_types/timeseries/vis.js index b177ef632e210..2790130c553b5 100644 --- a/src/plugins/vis_types/timeseries/public/application/components/vis_types/timeseries/vis.js +++ b/src/plugins/vis_types/timeseries/public/application/components/vis_types/timeseries/vis.js @@ -266,6 +266,7 @@ class TimeseriesVisualization extends Component { legend={Boolean(model.show_legend)} legendPosition={model.legend_position} truncateLegend={Boolean(model.truncate_legend)} + ignoreDaylightTime={Boolean(model.ignore_daylight_time)} maxLegendLines={model.max_lines_legend} tooltipMode={model.tooltip_mode} xAxisFormatter={this.xAxisFormatter(interval)} diff --git a/src/plugins/vis_types/timeseries/public/application/visualizations/views/timeseries/index.js b/src/plugins/vis_types/timeseries/public/application/visualizations/views/timeseries/index.js index 9dfddd3457d44..c8e845ce6b54c 100644 --- a/src/plugins/vis_types/timeseries/public/application/visualizations/views/timeseries/index.js +++ b/src/plugins/vis_types/timeseries/public/application/visualizations/views/timeseries/index.js @@ -77,6 +77,7 @@ export const TimeSeries = ({ interval, isLastBucketDropped, useLegacyTimeAxis, + ignoreDaylightTime, }) => { // If the color isn't configured by the user, use the color mapping service // to assign a color from the Kibana palette. Colors will be shared across the @@ -152,7 +153,9 @@ export const TimeSeries = ({ const shouldUseNewTimeAxis = series.every( ({ stack, bars, lines }) => (bars?.show && stack !== STACKED_OPTIONS.NONE) || lines?.show - ) && !useLegacyTimeAxis; + ) && + !useLegacyTimeAxis && + !ignoreDaylightTime; return ( diff --git a/src/plugins/vis_types/timeseries/server/lib/vis_data/response_processors/series/time_shift.js b/src/plugins/vis_types/timeseries/server/lib/vis_data/response_processors/series/time_shift.js index 109e552ce89a1..429050fab36cc 100644 --- a/src/plugins/vis_types/timeseries/server/lib/vis_data/response_processors/series/time_shift.js +++ b/src/plugins/vis_types/timeseries/server/lib/vis_data/response_processors/series/time_shift.js @@ -7,9 +7,18 @@ */ import { startsWith } from 'lodash'; -import moment from 'moment'; +import moment from 'moment-timezone'; -export function timeShift(resp, panel, series) { +export function timeShift( + resp, + panel, + series, + meta, + extractFields, + fieldFormatService, + cachedIndexPatternFetcher, + timezone +) { return (next) => (results) => { if (/^([+-]?[\d]+)([shmdwMy]|ms)$/.test(series.offset_time)) { const matches = series.offset_time.match(/^([+-]?[\d]+)([shmdwMy]|ms)$/); @@ -18,14 +27,29 @@ export function timeShift(resp, panel, series) { const offsetValue = matches[1]; const offsetUnit = matches[2]; + let defaultTimezone; + if (!panel.ignore_daylight_time) { + // the datemath plugin always parses dates by using the current default moment time zone. + // to use the configured time zone, we are switching just for the bounds calculation. + defaultTimezone = moment().zoneName(); + moment.tz.setDefault(timezone); + } + results.forEach((item) => { if (startsWith(item.id, series.id)) { item.data = item.data.map((row) => [ - moment.utc(row[0]).add(offsetValue, offsetUnit).valueOf(), + (panel.ignore_daylight_time ? moment.utc : moment)(row[0]) + .add(offsetValue, offsetUnit) + .valueOf(), row[1], ]); } }); + + if (!panel.ignore_daylight_time) { + // reset default moment timezone + moment.tz.setDefault(defaultTimezone); + } } } diff --git a/src/plugins/vis_types/timeseries/server/lib/vis_data/series/handle_response_body.ts b/src/plugins/vis_types/timeseries/server/lib/vis_data/series/handle_response_body.ts index 415844abeedaf..5244bca66a5b3 100644 --- a/src/plugins/vis_types/timeseries/server/lib/vis_data/series/handle_response_body.ts +++ b/src/plugins/vis_types/timeseries/server/lib/vis_data/series/handle_response_body.ts @@ -59,7 +59,8 @@ export function handleResponseBody( meta, extractFields, fieldFormatService, - services.cachedIndexPatternFetcher + services.cachedIndexPatternFetcher, + req.body.timerange.timezone ); return await processor([]);