Skip to content

Commit 4416ab1

Browse files
feature: Adds support for variable interpolation
1 parent 2172fb5 commit 4416ab1

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,24 @@ one by selecting `+ New Dashboard` from the Dashboard menu in the left panel.
2424
Once within the panel editor, select your Haystack data source in the Data Sources menu. Next, select the type of
2525
Haystack query that should be performed. The supported queries are:
2626

27-
- Eval: Evaluate a free-form Axon expression. Grafana variables may be injected into the query, with the supported
28-
variables listed below. *Note: Not all Haystack servers support this functionality*
27+
- Eval: Evaluate a free-form Axon expression. *Note: Not all Haystack servers support this functionality*
2928
- HisRead: Display the history of a single point over the selected time range.
3029
- Read: Display the records matching a filter. Since this is not timeseries data, it can only be viewed in Grafana's
3130
"Table" view.
3231

3332
#### Variables
3433

35-
Some queries support injecting values from the Grafana UI. The following variables are supported:
34+
Grafana template variables can be injected into queries using the ordinary syntax, e.g. `$varName`.
35+
36+
We also support injecting a few special variables from the time-range selector into the Eval and Read requests:
3637

3738
- `$__timeRange_start`: DateTime start of the selected Grafana time range
3839
- `$__timeRange_end`: DateTime end of the selected Grafana time range
3940
- `$__maxDataPoints`: Number representing the pixel width of Grafana's display panel.
4041
- `$__interval`: Number representing Grafana's recommended data interval. This is the duration of the time range,
4142
divided by the number of pixels, delivered in units of minutes.
4243

43-
To use them, simply use the value in the input string. Below is an example of using the variables in an Eval query:
44+
To use them, simply enter the value in the input string. Below is an example of using the variables in an Eval query:
4445

4546
```
4647
> [{ts: $__timeRange_start, v0: 0}, {ts: $__timeRange_end, v0: 10}].toGrid

src/datasource.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { DataSourceInstanceSettings, CoreApp } from '@grafana/data';
2-
import { DataSourceWithBackend } from '@grafana/runtime';
1+
import { DataSourceInstanceSettings, CoreApp, ScopedVars, DataQueryRequest, DataQueryResponse } from '@grafana/data';
2+
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
3+
import { Observable } from 'rxjs';
34

45
import { HaystackQuery, HaystackDataSourceOptions, DEFAULT_QUERY } from './types';
56

@@ -8,6 +9,15 @@ export class DataSource extends DataSourceWithBackend<HaystackQuery, HaystackDat
89
super(instanceSettings);
910
}
1011

12+
applyTemplateVariables(query: HaystackQuery, scopedVars: ScopedVars): Record<string, any> {
13+
return {
14+
...query,
15+
eval: getTemplateSrv().replace(query.eval, scopedVars),
16+
hisRead: getTemplateSrv().replace(query.hisRead, scopedVars),
17+
read: getTemplateSrv().replace(query.read, scopedVars),
18+
};
19+
}
20+
1121
getDefaultQuery(_: CoreApp): Partial<HaystackQuery> {
1222
return DEFAULT_QUERY
1323
}

0 commit comments

Comments
 (0)