Skip to content

Commit a19b6db

Browse files
jirkafajfrJirka Fajfr
andauthored
Adding $__isoFrom() and $__isoTo() macros (#115)
Co-authored-by: Jirka Fajfr <jirka.fajfr@enverus.com>
1 parent 37b14b5 commit a19b6db

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/datasource.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { replaceMacros } from './datasource';
2+
import { TimeRange, dateTime } from '@grafana/data';
3+
4+
const sampleTimestampFrom = '2021-05-17T20:48:09.000Z'; // -> 1621284489
5+
const sampleTimestmapTo = '2021-05-17T20:50:23.000Z'; // -> 1621284623
6+
7+
const range: TimeRange = {
8+
from: dateTime(sampleTimestampFrom),
9+
to: dateTime(sampleTimestmapTo),
10+
raw: {
11+
from: sampleTimestampFrom,
12+
to: sampleTimestmapTo,
13+
},
14+
};
15+
16+
test('range gets converted into ISO8601 notation', () => {
17+
expect(replaceMacros('$__isoFrom()', range)).toStrictEqual(sampleTimestampFrom);
18+
expect(replaceMacros('$__isoTo()', range)).toStrictEqual(sampleTimestmapTo);
19+
});
20+
21+
test('range gets converted into unix epoch notation', () => {
22+
expect(replaceMacros('$__unixEpochFrom()', range)).toStrictEqual('1621284489');
23+
expect(replaceMacros('$__unixEpochTo()', range)).toStrictEqual('1621284623');
24+
});

src/datasource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,13 @@ const replace = (scopedVars?: any, range?: TimeRange) => (str: string): string =
209209
};
210210

211211
// replaceMacros substitutes all available macros with their current value.
212-
const replaceMacros = (str: string, range?: TimeRange) => {
212+
export const replaceMacros = (str: string, range?: TimeRange) => {
213213
return range
214214
? str
215215
.replace(/\$__unixEpochFrom\(\)/g, range.from.unix().toString())
216216
.replace(/\$__unixEpochTo\(\)/g, range.to.unix().toString())
217+
.replace(/\$__isoFrom\(\)/g, range.from.toISOString())
218+
.replace(/\$__isoTo\(\)/g, range.to.toISOString())
217219
: str;
218220
};
219221

website/docs/macros.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ title: Macros
55

66
Use macros to add dashboard context to your queries. Macros are available in HTTP params and JSONPath expressions.
77

8-
| Macro | Description |
9-
|----------------------|---------------------------------------------------------------------------|
10-
| `$__unixEpochFrom()` | Start of the dashboard time interval as a Unix timestamp, i.e. 1494410783 |
11-
| `$__unixEpochTo()` | End of the dashboard time interval as a Unix timestamp, i.e. 1494410783 |
8+
| Macro | Description |
9+
| -------------------- | ------------------------------------------------------------------------------------------ |
10+
| `$__unixEpochFrom()` | Start of the dashboard time interval as a Unix timestamp, i.e. 1494410783 |
11+
| `$__unixEpochTo()` | End of the dashboard time interval as a Unix timestamp, i.e. 1494410783 |
12+
| `$__isoFrom()` | Start of the dashboard time interval as a ISO8601 timestamp, i.e. 2021-05-17T20:48:09.000Z |
13+
| `$__isoTo()` | End of the dashboard time interval as a ISO8601 timestamp, i.e. 2021-05-17T20:48:09.000Z |

0 commit comments

Comments
 (0)