Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 5ab9de9

Browse files
author
Scott Lepper
committed
add macros for referencing time window
1 parent 62a83dd commit 5ab9de9

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ From the dashboard, add a panel and select SQL Proxy from the Query dropdown. Y
3131

3232
Enter your SQL and click away to run the SQL command and get results into the panel.
3333

34+
## Time macros
35+
36+
To reference the dashboard time frame, you can supply macros (variables) within your sql:
37+
38+
* $__timeFrom - the start time in milliseconds
39+
* $__timeTo - the end time in milliseconds
40+
* $__timeFrom(format) - the start time converted to a date string with the supplied format
41+
* $__timeTo(format) - the end time converted to a date string with the supplied format
42+
43+
"format" example:
44+
* SELECT NAME FROM FOO where "TIME" > '$__timeFrom(yyyy-MM-dd hh:mm:ss)' and "TIME" < '$__timeTo(yyyy-MM-dd)'
45+
3446
## How to contribute
3547

3648
This plugin is using Typescript.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"@types/react": "16.8.16 ",
2020
"@types/request-promise-native": "^1.0.17"
2121
},
22-
"screenshots": [
23-
],
22+
"screenshots": [],
2423
"dependencies": {
2524
"codemirror": "^5.51.0",
25+
"date-fns": "^2.16.1",
2626
"react-codemirror2": "^6.0.0"
2727
}
2828
}

src/SqlProxyDatasource.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212

1313
import { SqlQuery } from './types';
1414
import { getBackendSrv } from '@grafana/runtime';
15+
import { format } from 'date-fns';
1516

1617
export class DataSource extends DataSourceApi<SqlQuery, DataSourceJsonData> {
1718
/** @ngInject */
@@ -70,13 +71,49 @@ export class DataSource extends DataSourceApi<SqlQuery, DataSourceJsonData> {
7071
targets: visibleTargets.map(target => {
7172
const query: SqlQuery = {
7273
...target,
73-
sql: this.templateSrv.replace(target.sql),
74+
sql: this.applyMacros(this.templateSrv.replace(target.sql), options),
7475
};
7576
return query;
7677
}),
7778
};
7879
}
7980

81+
applyMacros(sql: string, options: DataQueryRequest<SqlQuery>) {
82+
if (sql.includes('$__timeFrom(')) {
83+
sql = this.applyMacroFunction('$__timeFrom(', sql, options);
84+
}
85+
if (sql.includes('$__timeTo(')) {
86+
sql = this.applyMacroFunction('$__timeTo(', sql, options);
87+
}
88+
if (sql.includes('$__timeFrom')) {
89+
sql = sql.replace(/\$__timeFrom/g, options.startTime.toString());
90+
}
91+
if (sql.includes('$__timeTo')) {
92+
sql = sql.replace(/\$__timeTo/g, options.endTime?.toString() ?? '');
93+
}
94+
return sql;
95+
}
96+
97+
applyMacroFunction(macro: string, sql: string, options: DataQueryRequest<SqlQuery>) {
98+
if (sql.includes(macro)) {
99+
let time;
100+
if (macro === '$__timeFrom(') {
101+
time = new Date(options.startTime);
102+
} else {
103+
time = new Date(options.endTime!);
104+
}
105+
106+
const start = sql.indexOf(macro) + macro.length;
107+
const end = sql.indexOf(')', start);
108+
const fmt = sql.substring(start, end);
109+
const dateStr = format(time, fmt);
110+
const toReplace = sql.substring(start - macro.length, end + 1);
111+
sql = sql.replace(toReplace, dateStr);
112+
return this.applyMacroFunction(macro, sql, options);
113+
}
114+
return sql;
115+
}
116+
80117
metricFindQuery(query: any) {
81118
console.log('query', query);
82119
return Promise.resolve([]);

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4253,6 +4253,11 @@ date-fns@^1.23.0:
42534253
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c"
42544254
integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==
42554255

4256+
date-fns@^2.16.1:
4257+
version "2.16.1"
4258+
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.16.1.tgz#05775792c3f3331da812af253e1a935851d3834b"
4259+
integrity sha512-sAJVKx/FqrLYHAQeN7VpJrPhagZc9R4ImZIWYRFZaaohR3KzmuK88touwsSwSVT8Qcbd4zoDsnGfX4GFB4imyQ==
4260+
42564261
debug@=3.1.0:
42574262
version "3.1.0"
42584263
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"

0 commit comments

Comments
 (0)