From f5b24530e4425489115007e880bca5468e229632 Mon Sep 17 00:00:00 2001 From: Stan van Rooy Date: Thu, 31 Aug 2023 08:32:48 +0200 Subject: [PATCH] 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 Signed-off-by: Stan van Rooy --- src/datasource.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/datasource.ts b/src/datasource.ts index ccf436c..2072fc4 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -59,7 +59,7 @@ export class DataSource extends DataSourceApi { const end = range!.to; const calls = options.targets.map(target => { - const query = getTemplateSrv().replace(target.queryText, options.scopedVars); + const query = getTemplateSrv().replace(target.queryText, options.scopedVars, this.formatter); const request = { "query": query, @@ -90,6 +90,13 @@ export class DataSource extends DataSourceApi { }; } + private formatter(value: string | string[], options: any): string { + if (options.multi) { + return (value as string[]).map(v => `'${v}'`).join(','); + } + return value as string; + } + async metricFindQuery(query: string, options?: any): Promise { const to = new Date(); const from = new Date();