Commit c92cd73b by Torkel Ödegaard Committed by GitHub

Merge pull request #15355 from grafana/15284_fix_time_range

WIP: Datasources with custom time range handling should always take dashboard timezone into consideration
parents 13f21fff b9c36e53
...@@ -31,8 +31,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -31,8 +31,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
this.query = function(options) { this.query = function(options) {
const graphOptions = { const graphOptions = {
from: this.translateTime(options.rangeRaw.from, false), from: this.translateTime(options.rangeRaw.from, false, options.timezone),
until: this.translateTime(options.rangeRaw.to, true), until: this.translateTime(options.rangeRaw.to, true, options.timezone),
targets: options.targets, targets: options.targets,
format: options.format, format: options.format,
cacheTimeout: options.cacheTimeout || this.cacheTimeout, cacheTimeout: options.cacheTimeout || this.cacheTimeout,
...@@ -165,9 +165,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -165,9 +165,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
method: 'GET', method: 'GET',
url: url:
'/events/get_data?from=' + '/events/get_data?from=' +
this.translateTime(options.range.from, false) + this.translateTime(options.range.from, false, options.timezone) +
'&until=' + '&until=' +
this.translateTime(options.range.to, true) + this.translateTime(options.range.to, true, options.timezone) +
tags, tags,
}); });
} catch (err) { } catch (err) {
...@@ -179,7 +179,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -179,7 +179,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
return templateSrv.variableExists(target.target); return templateSrv.variableExists(target.target);
}; };
this.translateTime = (date, roundUp) => { this.translateTime = (date, roundUp, timezone) => {
if (_.isString(date)) { if (_.isString(date)) {
if (date === 'now') { if (date === 'now') {
return 'now'; return 'now';
...@@ -189,7 +189,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -189,7 +189,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
date = date.replace('M', 'mon'); date = date.replace('M', 'mon');
return date; return date;
} }
date = dateMath.parse(date, roundUp); date = dateMath.parse(date, roundUp, timezone);
} }
// graphite' s from filter is exclusive // graphite' s from filter is exclusive
...@@ -255,8 +255,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -255,8 +255,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}; };
if (options.range) { if (options.range) {
httpOptions.params.from = this.translateTime(options.range.from, false); httpOptions.params.from = this.translateTime(options.range.from, false, options.timezone);
httpOptions.params.until = this.translateTime(options.range.to, true); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then(results => {
...@@ -280,8 +280,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -280,8 +280,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}; };
if (options.range) { if (options.range) {
httpOptions.params.from = this.translateTime(options.range.from, false); httpOptions.params.from = this.translateTime(options.range.from, false, options.timezone);
httpOptions.params.until = this.translateTime(options.range.to, true); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then(results => {
...@@ -305,8 +305,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -305,8 +305,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}; };
if (options.range) { if (options.range) {
httpOptions.params.from = this.translateTime(options.range.from, false); httpOptions.params.from = this.translateTime(options.range.from, false, options.timezone);
httpOptions.params.until = this.translateTime(options.range.to, true); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then(results => {
...@@ -343,8 +343,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -343,8 +343,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
httpOptions.params.limit = options.limit; httpOptions.params.limit = options.limit;
} }
if (options.range) { if (options.range) {
httpOptions.params.from = this.translateTime(options.range.from, false); httpOptions.params.from = this.translateTime(options.range.from, false, options.timezone);
httpOptions.params.until = this.translateTime(options.range.to, true); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then(results => {
...@@ -379,8 +379,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -379,8 +379,8 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
httpOptions.params.limit = options.limit; httpOptions.params.limit = options.limit;
} }
if (options.range) { if (options.range) {
httpOptions.params.from = this.translateTime(options.range.from, false); httpOptions.params.from = this.translateTime(options.range.from, false, options.timezone);
httpOptions.params.until = this.translateTime(options.range.to, true); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then(results => {
......
...@@ -127,7 +127,7 @@ export default class InfluxDatasource { ...@@ -127,7 +127,7 @@ export default class InfluxDatasource {
}); });
} }
const timeFilter = this.getTimeFilter({ rangeRaw: options.rangeRaw }); const timeFilter = this.getTimeFilter({ rangeRaw: options.rangeRaw, timezone: options.timezone });
let query = options.annotation.query.replace('$timeFilter', timeFilter); let query = options.annotation.query.replace('$timeFilter', timeFilter);
query = this.templateSrv.replace(query, null, 'regex'); query = this.templateSrv.replace(query, null, 'regex');
...@@ -184,7 +184,7 @@ export default class InfluxDatasource { ...@@ -184,7 +184,7 @@ export default class InfluxDatasource {
} }
if (options && options.range) { if (options && options.range) {
const timeFilter = this.getTimeFilter({ rangeRaw: options.range }); const timeFilter = this.getTimeFilter({ rangeRaw: options.range, timezone: options.timezone });
query = query.replace('$timeFilter', timeFilter); query = query.replace('$timeFilter', timeFilter);
} }
...@@ -291,8 +291,8 @@ export default class InfluxDatasource { ...@@ -291,8 +291,8 @@ export default class InfluxDatasource {
} }
getTimeFilter(options) { getTimeFilter(options) {
const from = this.getInfluxTime(options.rangeRaw.from, false); const from = this.getInfluxTime(options.rangeRaw.from, false, options.timezone);
const until = this.getInfluxTime(options.rangeRaw.to, true); const until = this.getInfluxTime(options.rangeRaw.to, true, options.timezone);
const fromIsAbsolute = from[from.length - 1] === 'ms'; const fromIsAbsolute = from[from.length - 1] === 'ms';
if (until === 'now()' && !fromIsAbsolute) { if (until === 'now()' && !fromIsAbsolute) {
...@@ -302,7 +302,7 @@ export default class InfluxDatasource { ...@@ -302,7 +302,7 @@ export default class InfluxDatasource {
return 'time >= ' + from + ' and time <= ' + until; return 'time >= ' + from + ' and time <= ' + until;
} }
getInfluxTime(date, roundUp) { getInfluxTime(date, roundUp, timezone) {
if (_.isString(date)) { if (_.isString(date)) {
if (date === 'now') { if (date === 'now') {
return 'now()'; return 'now()';
...@@ -314,7 +314,7 @@ export default class InfluxDatasource { ...@@ -314,7 +314,7 @@ export default class InfluxDatasource {
const unit = parts[2]; const unit = parts[2];
return 'now() - ' + amount + unit; return 'now() - ' + amount + unit;
} }
date = dateMath.parse(date, roundUp); date = dateMath.parse(date, roundUp, timezone);
} }
return date.valueOf() + 'ms'; return date.valueOf() + 'ms';
......
...@@ -33,8 +33,8 @@ export default class OpenTsDatasource { ...@@ -33,8 +33,8 @@ export default class OpenTsDatasource {
// Called once per panel (graph) // Called once per panel (graph)
query(options) { query(options) {
const start = this.convertToTSDBTime(options.rangeRaw.from, false); const start = this.convertToTSDBTime(options.rangeRaw.from, false, options.timezone);
const end = this.convertToTSDBTime(options.rangeRaw.to, true); const end = this.convertToTSDBTime(options.rangeRaw.to, true, options.timezone);
const qs = []; const qs = [];
_.each(options.targets, target => { _.each(options.targets, target => {
...@@ -86,8 +86,8 @@ export default class OpenTsDatasource { ...@@ -86,8 +86,8 @@ export default class OpenTsDatasource {
} }
annotationQuery(options) { annotationQuery(options) {
const start = this.convertToTSDBTime(options.rangeRaw.from, false); const start = this.convertToTSDBTime(options.rangeRaw.from, false, options.timezone);
const end = this.convertToTSDBTime(options.rangeRaw.to, true); const end = this.convertToTSDBTime(options.rangeRaw.to, true, options.timezone);
const qs = []; const qs = [];
const eventList = []; const eventList = [];
...@@ -484,12 +484,12 @@ export default class OpenTsDatasource { ...@@ -484,12 +484,12 @@ export default class OpenTsDatasource {
}); });
} }
convertToTSDBTime(date, roundUp) { convertToTSDBTime(date, roundUp, timezone) {
if (date === 'now') { if (date === 'now') {
return null; return null;
} }
date = dateMath.parse(date, roundUp); date = dateMath.parse(date, roundUp, timezone);
return date.valueOf(); return date.valueOf();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment