17278 prometheus step align utc (#17477)

* Update datasource.ts

* Update datasource.test.ts

* utcOffset reverse

from moment docs, utcOffset "function returns the real offset from UTC, not the reverse offset"

* add utcOffset() to DateTime interface

method returns the UTC offset as a number of minutes

* Fixed test
This commit is contained in:
fxmiii
2019-06-26 01:55:36 -04:00
committed by Torkel Ödegaard
parent 654f1f2eb6
commit 19185bd0af
3 changed files with 31 additions and 9 deletions

View File

@@ -367,7 +367,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
// Align query interval with step to allow query caching and to ensure
// that about-same-time query results look the same.
const adjusted = alignRange(start, end, query.step);
const adjusted = alignRange(start, end, query.step, this.timeSrv.timeRange().to.utcOffset() * 60);
query.start = adjusted.start;
query.end = adjusted.end;
this._addTracingHeaders(query, options);
@@ -694,10 +694,16 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
* @param start Timestamp marking the beginning of the range.
* @param end Timestamp marking the end of the range.
* @param step Interval to align start and end with.
* @param utcOffsetSec Number of seconds current timezone is offset from UTC
*/
export function alignRange(start: number, end: number, step: number): { end: number; start: number } {
const alignedEnd = Math.floor(end / step) * step;
const alignedStart = Math.floor(start / step) * step;
export function alignRange(
start: number,
end: number,
step: number,
utcOffsetSec: number
): { end: number; start: number } {
const alignedEnd = Math.floor((end + utcOffsetSec) / step) * step - utcOffsetSec;
const alignedStart = Math.floor((start + utcOffsetSec) / step) * step - utcOffsetSec;
return {
end: alignedEnd,
start: alignedStart,