Skip to content

Commit f5b2453

Browse files
authored
feat: handle query interpolation multi-valued variable (#32)
This is a follow up after PR #31. This makes it possible to query with both single- and multi-valued variables. After this PR, the following query in Grafana: ```sql SELECT * FROM backend WHERE application_name IN ($application) OR duration_ms IN ($duration) OR sub = '$sub' ``` In to: ```sql SELECT * FROM backend WHERE application_name IN ('Elfskot.Api','Elfsquad.ConfiguratorApi','Elfsquad.OdataApi') OR duration_ms IN ('30', '8') OR sub = '1ff96bd8-53d8-4214-85b9-08da00dc987f' ``` The `WHERE IN (x, y, z)` with single quotes seems to work for both numbers & strings. The logic in the formatter might need to be improved for more complex data types. --------- Signed-off-by: Stan van Rooy <[email protected]> Signed-off-by: Stan van Rooy <[email protected]>
1 parent 56a7167 commit f5b2453

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/datasource.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
5959
const end = range!.to;
6060

6161
const calls = options.targets.map(target => {
62-
const query = getTemplateSrv().replace(target.queryText, options.scopedVars);
62+
const query = getTemplateSrv().replace(target.queryText, options.scopedVars, this.formatter);
6363

6464
const request = {
6565
"query": query,
@@ -90,6 +90,13 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
9090
};
9191
}
9292

93+
private formatter(value: string | string[], options: any): string {
94+
if (options.multi) {
95+
return (value as string[]).map(v => `'${v}'`).join(',');
96+
}
97+
return value as string;
98+
}
99+
93100
async metricFindQuery(query: string, options?: any): Promise<MetricFindValue[]> {
94101
const to = new Date();
95102
const from = new Date();

0 commit comments

Comments
 (0)