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

Handle multiple frames in metricFindQuery and runMetadataQuery
This commit is contained in:
ismail simsek 2023-10-25 18:27:25 +02:00 committed by GitHub
parent 333c858bc6
commit 51e49eaa20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 12 deletions

View File

@ -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) {

View File

@ -286,4 +286,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']],
},
},
],
},
},
},
};