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

* Handle optional options parameter

* unit tests
pull/77096/head^2
ismail simsek 2 years ago committed by GitHub
parent ba9c22f51b
commit 283f279a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 51
      public/app/plugins/datasource/influxdb/datasource.test.ts
  2. 4
      public/app/plugins/datasource/influxdb/datasource.ts

@ -161,6 +161,9 @@ describe('InfluxDataSource Frontend Mode', () => {
const mockTemplateService = new TemplateSrv(); const mockTemplateService = new TemplateSrv();
mockTemplateService.getAdhocFilters = jest.fn((_: string) => adhocFilters); mockTemplateService.getAdhocFilters = jest.fn((_: string) => adhocFilters);
let ds = getMockInfluxDS(getMockDSInstanceSettings(), mockTemplateService); let ds = getMockInfluxDS(getMockDSInstanceSettings(), mockTemplateService);
// const fetchMock = jest.fn().mockReturnValue(fetchResult);
it('query should contain the ad-hoc variable', () => { it('query should contain the ad-hoc variable', () => {
ds.query(mockInfluxQueryRequest()); ds.query(mockInfluxQueryRequest());
const expected = encodeURIComponent( const expected = encodeURIComponent(
@ -168,6 +171,54 @@ describe('InfluxDataSource Frontend Mode', () => {
); );
expect(fetchMock.mock.calls[0][0].data).toBe(`q=${expected}`); 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', () => { describe('datasource contract', () => {

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

Loading…
Cancel
Save