From 8b3047ba1e00779efedaae0eb6345b44cd8b90d5 Mon Sep 17 00:00:00 2001 From: Andreas Christou Date: Wed, 19 Feb 2025 15:37:35 +0000 Subject: [PATCH] InfluxDB: Improve handling of template variables contained in regular expressions (InfluxQL) (#100762) * Improve handling of template vars in regex * Review * Minor update --- .../datasource/influxdb/datasource.test.ts | 12 ++++++++++ .../plugins/datasource/influxdb/datasource.ts | 23 +++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/datasource.test.ts b/public/app/plugins/datasource/influxdb/datasource.test.ts index b2cdd4522e3..284717ac701 100644 --- a/public/app/plugins/datasource/influxdb/datasource.test.ts +++ b/public/app/plugins/datasource/influxdb/datasource.test.ts @@ -363,6 +363,18 @@ describe('interpolateQueryExpr', () => { expect(result).toBe(expectation); }); + it('should **not** return the escaped value if the value **is not** wrapped in regex and the query is more complex (e.g. text is contained between two / but not a regex', () => { + const value = 'testmatch'; + const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); + const result = ds.interpolateQueryExpr( + value, + variableMock, + `select value where ("tag"::tag =~ /value/) AND where other = $tempVar $timeFilter GROUP BY time($__interval) tz('Europe/London')` + ); + const expectation = `testmatch`; + expect(result).toBe(expectation); + }); + it('should return floating point number as it is', () => { const variableMock = queryBuilder() .withId('tempVar') diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index acfa33d6ced..b6f55d1a3ef 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -351,17 +351,30 @@ export default class InfluxDatasource extends DataSourceWithBackend escapeRegex(v)).join('|')})`; + return typeof value === 'string' ? escapeRegex(value) : `(${value.map((v) => escapeRegex(v)).join('|')})`; } return value;