Postgres/MySQL/MSSQL: Fix so that numeric/non-string values are returned from query variables (#35411)

Fixes a problem with query variables where SQL query returning for example only 
numeric values didn't populate the query variables with values.

Fixes #35391
This commit is contained in:
Marcus Efraimsson
2021-06-09 14:18:54 +02:00
committed by GitHub
parent dc7a641978
commit bf7301e485
6 changed files with 148 additions and 185 deletions

View File

@ -1,6 +1,5 @@
import { AnnotationEvent, DataFrame, FieldType, MetricFindValue } from '@grafana/data';
import { AnnotationEvent, DataFrame, MetricFindValue } from '@grafana/data';
import { BackendDataSourceResponse, FetchResponse, toDataQueryResponse } from '@grafana/runtime';
import { map } from 'lodash';
export default class ResponseParser {
transformMetricFindResponse(raw: FetchResponse<BackendDataSourceResponse>): MetricFindValue[] {
@ -21,16 +20,13 @@ export default class ResponseParser {
values.push({ text: '' + textField.values.get(i), value: '' + valueField.values.get(i) });
}
} else {
const textFields = frame.fields.filter((f) => f.type === FieldType.string);
if (textFields) {
values.push(
...textFields
.flatMap((f) => f.values.toArray())
.map((v) => ({
text: '' + v,
}))
);
}
values.push(
...frame.fields
.flatMap((f) => f.values.toArray())
.map((v) => ({
text: v,
}))
);
}
return Array.from(new Set(values.map((v) => v.text))).map((text) => ({
@ -39,56 +35,6 @@ export default class ResponseParser {
}));
}
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number) {
const res = [];
for (let i = 0; i < rows.length; i++) {
if (!this.containsKey(res, rows[i][textColIndex])) {
res.push({
text: rows[i][textColIndex],
value: rows[i][valueColIndex],
});
}
}
return res;
}
transformToSimpleList(rows: any[][]) {
const res = [];
for (let i = 0; i < rows.length; i++) {
for (let j = 0; j < rows[i].length; j++) {
res.push(rows[i][j]);
}
}
const unique = Array.from(new Set(res));
return map(unique, (value) => {
return { text: value };
});
}
findColIndex(columns: any[], colName: string) {
for (let i = 0; i < columns.length; i++) {
if (columns[i].text === colName) {
return i;
}
}
return -1;
}
containsKey(res: any, key: any) {
for (let i = 0; i < res.length; i++) {
if (res[i].text === key) {
return true;
}
}
return false;
}
async transformAnnotationResponse(options: any, data: BackendDataSourceResponse): Promise<AnnotationEvent[]> {
const frames = toDataQueryResponse({ data: data }).data as DataFrame[];
const frame = frames[0];