From 3e0b92d6feca7da9603ca2038099fbdc6e6c40e6 Mon Sep 17 00:00:00 2001 From: Chris Burkhart Date: Mon, 24 Jul 2017 09:55:57 -0700 Subject: [PATCH 1/2] Enable datasources to be able to round off to a UTC day properly --- public/app/core/utils/datemath.ts | 8 ++++++-- public/app/features/dashboard/time_srv.ts | 5 +++-- public/app/features/panel/metrics_panel_ctrl.ts | 3 +++ public/test/core/utils/datemath_specs.ts | 8 ++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/public/app/core/utils/datemath.ts b/public/app/core/utils/datemath.ts index 2aa793016ba..f5608443f49 100644 --- a/public/app/core/utils/datemath.ts +++ b/public/app/core/utils/datemath.ts @@ -5,7 +5,7 @@ import moment from 'moment'; var units = ['y', 'M', 'w', 'd', 'h', 'm', 's']; -export function parse(text, roundUp?) { +export function parse(text, roundUp?, timezone?) { if (!text) { return undefined; } if (moment.isMoment(text)) { return text; } if (_.isDate(text)) { return moment(text); } @@ -16,7 +16,11 @@ export function parse(text, roundUp?) { var parseString; if (text.substring(0, 3) === 'now') { - time = moment(); + if (timezone === 'utc') { + time = moment.utc(); + } else { + time = moment(); + } mathString = text.substring('now'.length); } else { index = text.indexOf('||'); diff --git a/public/app/features/dashboard/time_srv.ts b/public/app/features/dashboard/time_srv.ts index abde4152b63..ef5dead40a7 100644 --- a/public/app/features/dashboard/time_srv.ts +++ b/public/app/features/dashboard/time_srv.ts @@ -199,10 +199,11 @@ class TimeSrv { from: moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from, to: moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to, }; + var timezone = this.dashboard && this.dashboard.getTimezone ? this.dashboard.getTimezone() : 'local'; return { - from: dateMath.parse(raw.from, false), - to: dateMath.parse(raw.to, true), + from: dateMath.parse(raw.from, false, timezone), + to: dateMath.parse(raw.to, true, timezone), raw: raw }; } diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index 0e94df437a8..1ae24066bb9 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -221,7 +221,10 @@ class MetricsPanelCtrl extends PanelCtrl { "__interval_ms": {text: this.intervalMs, value: this.intervalMs}, }); + var timezone = this.dashboard.getTimezone ? this.dashboard.getTimezone() : 'local'; + var metricsQuery = { + timezone: timezone, panelId: this.panel.id, range: this.range, rangeRaw: this.range.raw, diff --git a/public/test/core/utils/datemath_specs.ts b/public/test/core/utils/datemath_specs.ts index c6096485f65..b919653c2c8 100644 --- a/public/test/core/utils/datemath_specs.ts +++ b/public/test/core/utils/datemath_specs.ts @@ -46,6 +46,14 @@ describe("DateMath", () => { expect(startOfDay).to.be(expected.getTime()); }); + it("now/d on a utc dashboard should be start of the current day in UTC time", () => { + var today = new Date(); + var expected = new Date(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0)); + + var startOfDay = dateMath.parse('now/d', false, 'utc').valueOf(); + expect(startOfDay).to.be(expected.getTime()); + }); + describe('subtraction', () => { var now; var anchored; From a02cac21268b2fc1184b549f58fe0b75bbe089d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 2 Oct 2017 16:45:25 +0200 Subject: [PATCH 2/2] refactoring: minor refactoring of PR #8916 --- CHANGELOG.md | 7 ++++--- public/app/features/dashboard/time_srv.ts | 3 ++- public/app/features/panel/metrics_panel_ctrl.ts | 4 +--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2deafc59207..23633bfbc1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * **Prometheus**: Add support for instant queries [#5765](https://github.com/grafana/grafana/issues/5765), thx [@mtanda](https://github.com/mtanda) * **Cloudwatch**: Add support for alerting using the cloudwatch datasource [#8050](https://github.com/grafana/grafana/pull/8050), thx [@mtanda](https://github.com/mtanda) * **Pagerduty**: Include triggering series in pagerduty notification [#8479](https://github.com/grafana/grafana/issues/8479), thx [@rickymoorhouse](https://github.com/rickymoorhouse) +* **Timezone**: Time ranges like Today & Yesterday now work correctly when timezone setting is set to UTC [#8916](https://github.com/grafana/grafana/issues/8916), thx [@ctide](https://github.com/ctide) ## Minor * **SMTP**: Make it possible to set specific EHLO for smtp client. [#9319](https://github.com/grafana/grafana/issues/9319) @@ -31,7 +32,7 @@ # 4.5.2 (2017-09-22) -## Fixes +## Fixes * **Graphite**: Fix for issues with jsonData & graphiteVersion null errors [#9258](https://github.com/grafana/grafana/issues/9258) * **Graphite**: Fix for Grafana internal metrics to Graphite sending NaN values [#9279](https://github.com/grafana/grafana/issues/9279) * **HTTP API**: Fix for HEAD method requests [#9307](https://github.com/grafana/grafana/issues/9307) @@ -44,7 +45,7 @@ * **MySQL**: Fixed issue with query editor not showing [#9247](https://github.com/grafana/grafana/issues/9247) ## Breaking changes -* **Metrics**: The metric structure for internal metrics about Grafana published to graphite has changed. This might break dashboards for internal metrics. +* **Metrics**: The metric structure for internal metrics about Grafana published to graphite has changed. This might break dashboards for internal metrics. # 4.5.0 (2017-09-14) @@ -73,7 +74,7 @@ ### Breaking change * **InfluxDB/Elasticsearch**: The panel & data source option named "Group by time interval" is now named "Min time interval" and does now always define a lower limit for the auto group by time. Without having to use `>` prefix (that prefix still works). This should in theory have close to zero actual impact on existing dashboards. It does mean that if you used this setting to define a hard group by time interval of, say "1d", if you zoomed to a time range wide enough the time range could increase above the "1d" range as the setting is now always considered a lower limit. -* **Elasticsearch**: Elasticsearch metric queries without date histogram now return table formated data making table panel much easier to use for this use case. Should not break/change existing dashboards with stock panels but external panel plugins can be affected. +* **Elasticsearch**: Elasticsearch metric queries without date histogram now return table formated data making table panel much easier to use for this use case. Should not break/change existing dashboards with stock panels but external panel plugins can be affected. ## Changes diff --git a/public/app/features/dashboard/time_srv.ts b/public/app/features/dashboard/time_srv.ts index eb4bddb9e80..dfed156afae 100644 --- a/public/app/features/dashboard/time_srv.ts +++ b/public/app/features/dashboard/time_srv.ts @@ -195,7 +195,8 @@ class TimeSrv { from: moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from, to: moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to, }; - var timezone = this.dashboard && this.dashboard.getTimezone ? this.dashboard.getTimezone() : 'local'; + + var timezone = this.dashboard && this.dashboard.getTimezone(); return { from: dateMath.parse(raw.from, false, timezone), diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index 1d055c11da0..38d9c631b54 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -217,10 +217,8 @@ class MetricsPanelCtrl extends PanelCtrl { "__interval_ms": {text: this.intervalMs, value: this.intervalMs}, }); - var timezone = this.dashboard.getTimezone ? this.dashboard.getTimezone() : 'local'; - var metricsQuery = { - timezone: timezone, + timezone: this.dashboard.getTimezone(), panelId: this.panel.id, range: this.range, rangeRaw: this.range.raw,