InfluxDB: Fix interpolating field keys in influxql (#86401)

* interpolate field keys

* use scopedVars
This commit is contained in:
ismail simsek 2024-04-19 01:14:29 +02:00 committed by GitHub
parent b311612cf2
commit 842c8dd206
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 1 deletions

View File

@ -254,7 +254,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
return selects.map((select) => {
return {
...select,
params: select.params?.map((param) => this.templateSrv.replace(param.toString(), undefined)),
params: select.params?.map((param) => this.templateSrv.replace(param.toString(), scopedVars)),
};
});
});

View File

@ -221,6 +221,26 @@ describe('InfluxDataSource Backend Mode', () => {
const variablesMock = [
queryBuilder().withId('var1').withName('var1').withCurrent('var1').build(),
queryBuilder().withId('path').withName('path').withCurrent('/etc/hosts').build(),
queryBuilder()
.withId('field_var')
.withName('field_var')
.withMulti(true)
.withOptions(
{
text: `field_1`,
value: `field_1`,
},
{
text: `field_2`,
value: `field_2`,
},
{
text: `field_3`,
value: `field_3`,
}
)
.withCurrent(['field_1', 'field_3'])
.build(),
];
const mockTemplateService = new TemplateSrv({
getVariables: () => variablesMock,
@ -328,6 +348,34 @@ describe('InfluxDataSource Backend Mode', () => {
const expected = `/etc/hosts`;
expect(res.tags?.[0].value).toEqual(expected);
});
it('should interpolate field keys with given scopedVars', () => {
const query: InfluxQuery = {
refId: 'A',
tags: [
{
key: 'key',
operator: '=',
value: 'value',
},
],
select: [
[
{
type: 'field',
params: ['$field_var'],
},
{
type: 'mean',
params: [],
},
],
],
};
const res = ds.applyVariables(query, { field_var: { text: 'field_3', value: 'field_3' } });
const expected = `field_3`;
expect(res.select?.[0][0].params?.[0]).toEqual(expected);
});
});
describe('metric find query', () => {