-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbac5ef
commit 6209fb2
Showing
12 changed files
with
227 additions
and
17 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
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
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,67 @@ | ||
import { getFormatAndTimeShift, relativeTimeStampParse, shiftTime } from './UnixTimeStamp'; | ||
|
||
describe('getFormatAndTimeShift', () => { | ||
it('getFormatAndTimeShift', () => { | ||
expect(getFormatAndTimeShift([]).format).toBe('ms'); | ||
expect(getFormatAndTimeShift([]).timeShift).toBe(''); | ||
expect(getFormatAndTimeShift(['s']).format).toBe('s'); | ||
expect(getFormatAndTimeShift([]).timeShift).toBe(''); | ||
expect(getFormatAndTimeShift(['ms']).format).toBe('ms'); | ||
expect(getFormatAndTimeShift([]).timeShift).toBe(''); | ||
expect(getFormatAndTimeShift(['10d', 's']).format).toBe('s'); | ||
expect(getFormatAndTimeShift(['10d', 's']).timeShift).toBe('10d'); | ||
// expect(getFormatAndTimeShift(['10d']).format).toBe('ms'); // error | ||
expect(getFormatAndTimeShift(['10d']).timeShift).toBe('10d'); | ||
}); | ||
}); | ||
|
||
describe('relativeTimeStampParse', () => { | ||
it('relativeTimeStampParse test', () => { | ||
expect(relativeTimeStampParse('30d').value).toBe(30); | ||
expect(relativeTimeStampParse('30d').key).toBe('d'); | ||
expect(relativeTimeStampParse('-12M').value).toBe(-12); | ||
expect(relativeTimeStampParse('-12M').key).toBe('M'); | ||
expect(relativeTimeStampParse('h').value).toBe(1); | ||
expect(relativeTimeStampParse('h').key).toBe('h'); | ||
expect(relativeTimeStampParse('-h').value).toBe(-1); | ||
expect(relativeTimeStampParse('-h').key).toBe('h'); | ||
}); | ||
}); | ||
|
||
const currentDate = new Date('2021-07-31T00:00:00.000Z').getTime(); | ||
describe('shiftTime', () => { | ||
it('shiftTime', () => { | ||
expect(new Date(shiftTime(currentDate, '')).toDateString()).toBe(new Date(currentDate).toDateString()); | ||
expect(new Date(shiftTime(currentDate, '0d')).toDateString()).toBe(new Date(currentDate).toDateString()); | ||
expect(new Date(shiftTime(currentDate, '-1d')).toDateString()).toBe(new Date('2021-Jul-30').toDateString()); | ||
expect(new Date(shiftTime(currentDate, '1d')).toDateString()).toBe(new Date('2021-Aug-1').toDateString()); | ||
expect(new Date(shiftTime(currentDate, 'd')).toDateString()).toBe(new Date('2021-Aug-1').toDateString()); | ||
expect(new Date(shiftTime(currentDate, '12d')).toDateString()).toBe(new Date('2021-Aug-12').toDateString()); | ||
expect(new Date(shiftTime(currentDate, '40d')).toDateString()).toBe(new Date('2021-Sep-9').toDateString()); | ||
expect(new Date(shiftTime(currentDate, '-40d')).toDateString()).toBe(new Date('2021-Jun-21').toDateString()); | ||
expect(new Date(shiftTime(currentDate, '-1h')).toISOString()).toBe( | ||
new Date('2021-07-30T23:00:00.000Z').toISOString() | ||
); | ||
expect(new Date(shiftTime(currentDate, '1h')).toISOString()).toBe( | ||
new Date('2021-07-31T01:00:00.000Z').toISOString() | ||
); | ||
expect(new Date(shiftTime(currentDate, '-1m')).toISOString()).toBe( | ||
new Date('2021-07-30T23:59:00.000Z').toISOString() | ||
); | ||
expect(new Date(shiftTime(currentDate, '1m')).toISOString()).toBe( | ||
new Date('2021-07-31T00:01:00.000Z').toISOString() | ||
); | ||
expect(new Date(shiftTime(currentDate, '-1s')).toISOString()).toBe( | ||
new Date('2021-07-30T23:59:59.000Z').toISOString() | ||
); | ||
expect(new Date(shiftTime(currentDate, '1s')).toISOString()).toBe( | ||
new Date('2021-07-31T00:00:01.000Z').toISOString() | ||
); | ||
expect(new Date(shiftTime(currentDate, '-1ms')).toISOString()).toBe( | ||
new Date('2021-07-30T23:59:59.999Z').toISOString() | ||
); | ||
expect(new Date(shiftTime(currentDate, '1ms')).toISOString()).toBe( | ||
new Date('2021-07-31T00:00:00.001Z').toISOString() | ||
); | ||
}); | ||
}); |
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,84 @@ | ||
import { SelectableValue } from '@grafana/data'; | ||
import { replaceTokenFromVariable } from './utils'; | ||
|
||
// UnixTimeStamp(30d) --> (current timestamp +30d) in ms | ||
// UnixTimeStamp(-30d) --> (current timestamp -30d) in ms | ||
// UnixTimeStamp(30h,s) --> (current timestamp +30h) in s | ||
// UnixTimeStamp(-30d,ms) --> (current timestamp -30d) in ms | ||
// UnixTimeStamp(s) --> current timestamp in s | ||
// UnixTimeStamp() --> current timestamp in ms | ||
|
||
export const relativeTimeStampParse = (input: string): { value: number; key: string } => { | ||
let numberParts = input.match(/[\-\d]+/g) || ['1']; | ||
let textParts = input.match(/[A-Za-z]+/g) || ['h']; | ||
return { | ||
value: numberParts[0] === '-' ? -1 : +numberParts[0], | ||
key: textParts[0], | ||
}; | ||
}; | ||
|
||
export const shiftTime = (value: number, timeShift: string): number => { | ||
if (timeShift) { | ||
let relativeTimeStamp = relativeTimeStampParse(timeShift); | ||
switch (relativeTimeStamp.key) { | ||
case 'd': | ||
value = value + relativeTimeStamp.value * (HOURS * MINUTES * SECONDS * MILLISECONDS); | ||
break; | ||
case 'h': | ||
value = value + relativeTimeStamp.value * (MINUTES * SECONDS * MILLISECONDS); | ||
break; | ||
case 'm': | ||
value = value + relativeTimeStamp.value * (SECONDS * MILLISECONDS); | ||
break; | ||
case 's': | ||
value = value + relativeTimeStamp.value * MILLISECONDS; | ||
break; | ||
case 'ms': | ||
value = value + relativeTimeStamp.value; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
return value; | ||
}; | ||
|
||
export const getFormatAndTimeShift = (args: string[]): { format: string; timeShift: string } => { | ||
return { | ||
format: args.length === 1 ? args[0] || 'ms' : args[1] || 'ms', | ||
timeShift: ['s', 'ms', 'seconds', 'milliseconds'].includes(args[0]) ? '' : args[0] || '', | ||
}; | ||
}; | ||
|
||
export const getCurrentTime = () => new Date().getTime(); | ||
|
||
const HOURS = 24; | ||
const MINUTES = 60; | ||
const SECONDS = 60; | ||
const MILLISECONDS = 1000; | ||
|
||
export const UnixTimeStampVariable = (query: string): Array<SelectableValue<string>> => { | ||
let value: number = getCurrentTime(); | ||
let args = replaceTokenFromVariable(query, 'UnixTimeStamp').split(','); | ||
let format = getFormatAndTimeShift(args).format; | ||
let timeShift = getFormatAndTimeShift(args).timeShift; | ||
if (args[0] && !['s', 'ms', 'seconds', 'milliseconds'].includes(timeShift)) { | ||
value = shiftTime(value, timeShift); | ||
} | ||
switch (format) { | ||
case 'seconds': | ||
case 's': | ||
value = Math.round(value / 1000); | ||
break; | ||
case 'milliseconds': | ||
case 'ms': | ||
default: | ||
value = value; | ||
} | ||
return [ | ||
{ | ||
value: query, | ||
text: value, | ||
}, | ||
]; | ||
}; |
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
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
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
6209fb2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: