[v10.2.x] InfluxDB: Fix handle multiple frames in metricFindQuery and runMetadataQuery (#77160)

InfluxDB: Fix handle multiple frames in metricFindQuery and runMetadataQuery (#77154)

Handle multiple frames in metricFindQuery and runMetadataQuery

(cherry picked from commit 51e49eaa20)

Co-authored-by: ismail simsek <ismailsimsek09@gmail.com>
This commit is contained in:
grafana-delivery-bot[bot]
2023-10-25 19:42:03 +03:00
committed by GitHub
co-authored by ismail simsek
parent 3b4be7d6e3
commit 5fd5268782
2 changed files with 73 additions and 12 deletions
@@ -296,12 +296,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
super.query({
targets: [target],
} as DataQueryRequest)
).then((rsp) => {
if (rsp.data?.length) {
return frameToMetricFindValue(rsp.data[0]);
}
return [];
});
).then(this.toMetricFindValue);
}
async metricFindQuery(query: string, options?: any): Promise<MetricFindValue[]> {
@@ -316,12 +311,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
...(options ?? {}), // includes 'range'
targets: [target],
})
).then((rsp) => {
if (rsp.data?.length) {
return frameToMetricFindValue(rsp.data[0]);
}
return [];
});
).then(this.toMetricFindValue);
}
const interpolated = this.templateSrv.replace(query, options?.scopedVars, this.interpolateQueryExpr);
@@ -331,6 +321,14 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
});
}
toMetricFindValue(rsp: DataQueryResponse): MetricFindValue[] {
const data = rsp.data ?? [];
// Create MetricFindValue object for all frames
const values = data.map((d) => frameToMetricFindValue(d)).flat();
// Filter out duplicate elements
return values.filter((elm, idx, self) => idx === self.findIndex((t) => t.text === elm.text));
}
// By implementing getTagKeys and getTagValues we add ad-hoc filters functionality
// Used in public/app/features/variables/adhoc/picker/AdHocFilterKey.tsx::fetchFilterKeys
getTagKeys(options?: DataSourceGetTagKeysOptions) {
@@ -283,4 +283,67 @@ describe('InfluxDataSource Backend Mode', () => {
expect(qData).toBe(qe);
});
});
describe('metric find query', () => {
let ds = getMockInfluxDS(getMockDSInstanceSettings());
it('handles multiple frames', async () => {
const fetchMockImpl = () => {
return of(mockMetricFindQueryResponse);
};
fetchMock.mockImplementation(fetchMockImpl);
const values = await ds.getTagValues({ key: 'test_id', filters: [] });
expect(fetchMock).toHaveBeenCalled();
expect(values.length).toBe(5);
expect(values[0].text).toBe('test-t2-1');
});
});
});
const mockMetricFindQueryResponse = {
data: {
results: {
metricFindQuery: {
status: 200,
frames: [
{
schema: {
name: 'NoneNone',
refId: 'metricFindQuery',
fields: [
{
name: 'Value',
type: 'string',
typeInfo: {
frame: 'string',
},
},
],
},
data: {
values: [['test-t2-1', 'test-t2-10']],
},
},
{
schema: {
name: 'some-other',
refId: 'metricFindQuery',
fields: [
{
name: 'Value',
type: 'string',
typeInfo: {
frame: 'string',
},
},
],
},
data: {
values: [['test-t2-1', 'test-t2-10', 'test-t2-2', 'test-t2-3', 'test-t2-4']],
},
},
],
},
},
},
};