From 3b4be7d6e30539a1b27dc5fb4ee4b35baffb0090 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:11:18 +0200 Subject: [PATCH] [v10.2.x] InfluxDB: Fix adhoc filter calls by properly checking optional parameter in metricFindQuery (#77145) InfluxDB: Fix adhoc filter calls by properly checking optional parameter in metricFindQuery (#77113) * Handle optional options parameter * unit tests (cherry picked from commit 283f279a1740b8471a31c6e0443df139133c114e) Co-authored-by: ismail simsek --- .../datasource/influxdb/datasource.test.ts | 51 +++++++++++++++++++ .../plugins/datasource/influxdb/datasource.ts | 4 +- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/datasource.test.ts b/public/app/plugins/datasource/influxdb/datasource.test.ts index 7f5831a6ad0..6462fbd09b2 100644 --- a/public/app/plugins/datasource/influxdb/datasource.test.ts +++ b/public/app/plugins/datasource/influxdb/datasource.test.ts @@ -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', () => { diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index 246ecdc9ec7..22e501696d8 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -313,7 +313,7 @@ export default class InfluxDatasource extends DataSourceWithBackend { @@ -324,7 +324,7 @@ export default class InfluxDatasource extends DataSourceWithBackend { return this.responseParser.parse(query, resp);