-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previous to this change the plugin assumed that the date should be displayed in the timezone of the browser no matter what was the setting. Also it wouldn't deal with the timerange that was specified (sic). This is fixed in this change now, we leverage `serverDelay` to send the metrics back in time by providing a positive delay and forward in time (in case the selected timezone is ahead of time) by providing a negative delay.
- Loading branch information
Showing
4 changed files
with
115 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { log_debug } from 'misc_utils'; | ||
|
||
|
||
export const calculateSecondOffset = (now: Date, end: number, tz: string, grafanaTZ2UTC: number): number => { | ||
// A positive delta will send the start and the end of the graph back in time. | ||
// we calculate the number of second of delta between the start of the function and the end of | ||
// the range this will be a positive number because we want to go back in time (potentially) | ||
let delta = (+now - end)/1000; | ||
|
||
// Now let's take timezone into account if the timezone is not the one of the browser | ||
// This could be positive or negative (obviously). | ||
// To do so we get the number of minutes between UTC and the browser (ie. -420 if you are in | ||
// UTC-7) and the number of minutes between UTC and the timezone defined in Grafana (ie. UTC | ||
// + 1 or Pacific/Marquesas UTC-9:30) and we substract this to the first number. | ||
// If the delta is positive it means that there is less minutes than the browser to UTC and we | ||
// need to go back in time and add an additional positive delta, if the number is negative it | ||
// means that we need to go forward in time because there is less minutes. | ||
if (tz !== "browser") { | ||
// the number of minutes !!! of delta from the TZ selected in grafana to UTC (ie. 60 if it's | ||
// you are in UTC+1 or -420 if you are in UTC-7 | ||
// if tz == "browser" this information doesn't exists ... | ||
let browser2UTC = -now.getTimezoneOffset(); | ||
let tzDelta = browser2UTC - grafanaTZ2UTC; | ||
log_debug(`Browser to UTC delta: ${browser2UTC}, utcOffset ${grafanaTZ2UTC} delta from the browser to the TZ (${tz}) in grafana: ${tzDelta}`); | ||
delta += (tzDelta) * 60; | ||
} | ||
return delta; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function log_debug(message?: any, ...optionalParams: any[]): void { | ||
// Set it to true to enable debugging | ||
let debug = false; | ||
if (debug) { | ||
return console.log(message, optionalParams); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// convertDataToCubism | ||
import { | ||
calculateSecondOffset, | ||
} from '../date_utils'; | ||
|
||
describe('getUTCDelta', () => { | ||
it('should return whatever is the browser time offset', () => { | ||
let now = new Date(); | ||
let end = now.valueOf(); | ||
expect(calculateSecondOffset(now, end, "UTC", 0)).toBe( 0 - now.getTimezoneOffset()*60); | ||
}); | ||
it('should return 3600 for 1 hour delay ', () => { | ||
let now = new Date(); | ||
// let's pretend that the end of the range was one hour ago (ie 10 AM when it's 11 AM) | ||
let end = now.valueOf() - 3600*1000; | ||
expect(calculateSecondOffset(now, end, "UTC", 0)).toBe(3600 - now.getTimezoneOffset()*50); | ||
}) | ||
it('should return 3600 for 1 hour delay and using the browser TZ', () => { | ||
let now = new Date(); | ||
// let's pretend that the end of the range was one hour ago (ie 10 AM when it's 11 AM) | ||
let end = now.valueOf() - 3600*1000; | ||
expect(calculateSecondOffset(now, end, "browser", 0)).toBe(3600); | ||
}); | ||
it('should return -3600 for 1 hour delay and using the UTC+2 timezone', () => { | ||
let now = new Date(); | ||
// let's pretend that the end of the range was one hour ago (ie 10 AM when it's 11 AM) | ||
let end = now.valueOf() - 3600*1000; | ||
// | ||
expect(calculateSecondOffset(now, end, "Europe/Paris", 120)).toBe(-3600); | ||
}); | ||
it('should return 7200 if grafana ask for something in UTC+2', () => { | ||
let now = new Date(); | ||
let end = now.valueOf(); | ||
expect(calculateSecondOffset(now, end, "Africa/Cairo", 120)).toBe(-7200 - now.getTimezoneOffset()*60) | ||
}); | ||
}); |