InfluxDB: Fix adhoc filter calls by properly checking optional parameter in metricFindQuery (#77113)

* Handle optional options parameter

* unit tests
This commit is contained in:
ismail simsek 2023-10-25 15:56:10 +02:00 committed by GitHub
parent ba9c22f51b
commit 283f279a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 2 deletions

View File

@ -161,6 +161,9 @@ describe('InfluxDataSource Frontend Mode', () => {
const mockTemplateService = new TemplateSrv();
mockTemplateService.getAdhocFilters = jest.fn((_: string) => adhocFilters);
let ds = getMockInfluxDS(getMockDSInstanceSettings(), mockTemplateService);
// const fetchMock = jest.fn().mockReturnValue(fetchResult);
it('query should contain the ad-hoc variable', () => {
ds.query(mockInfluxQueryRequest());
const expected = encodeURIComponent(
@ -168,6 +171,54 @@ describe('InfluxDataSource Frontend Mode', () => {
);
expect(fetchMock.mock.calls[0][0].data).toBe(`q=${expected}`);
});
it('should make the fetch call for adhoc filter keys', () => {
fetchMock.mockReturnValue(
of({
results: [
{
statement_id: 0,
series: [
{
name: 'cpu',
columns: ['tagKey'],
values: [['datacenter'], ['geohash'], ['source']],
},
],
},
],
})
);
ds.getTagKeys();
expect(fetchMock).toHaveBeenCalled();
const fetchReq = fetchMock.mock.calls[0][0];
expect(fetchReq).not.toBeNull();
expect(fetchReq.data).toMatch(encodeURIComponent(`SHOW TAG KEYS`));
});
it('should make the fetch call for adhoc filter values', () => {
fetchMock.mockReturnValue(
of({
results: [
{
statement_id: 0,
series: [
{
name: 'mykey',
columns: ['key', 'value'],
values: [['mykey', 'value']],
},
],
},
],
})
);
ds.getTagValues({ key: 'mykey', filters: [] });
expect(fetchMock).toHaveBeenCalled();
const fetchReq = fetchMock.mock.calls[0][0];
expect(fetchReq).not.toBeNull();
expect(fetchReq.data).toMatch(encodeURIComponent(`SHOW TAG VALUES WITH KEY = "mykey"`));
});
});
describe('datasource contract', () => {

View File

@ -313,7 +313,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
};
return lastValueFrom(
super.query({
...options, // includes 'range'
...(options ?? {}), // includes 'range'
targets: [target],
})
).then((rsp) => {
@ -324,7 +324,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
});
}
const interpolated = this.templateSrv.replace(query, options.scopedVars, this.interpolateQueryExpr);
const interpolated = this.templateSrv.replace(query, options?.scopedVars, this.interpolateQueryExpr);
return lastValueFrom(this._seriesQuery(interpolated, options)).then((resp) => {
return this.responseParser.parse(query, resp);