grafana/public/app/plugins/datasource/mssql/response_parser.ts

123 lines
3.6 KiB
TypeScript
Raw Normal View History

import { map } from 'lodash';
import { AnnotationEvent, DataFrame, FieldType, MetricFindValue } from '@grafana/data';
import { BackendDataSourceResponse, toDataQueryResponse, FetchResponse } from '@grafana/runtime';
2017-12-02 05:40:12 -06:00
export default class ResponseParser {
transformMetricFindResponse(raw: FetchResponse<BackendDataSourceResponse>): MetricFindValue[] {
const frames = toDataQueryResponse(raw).data as DataFrame[];
2017-12-02 05:40:12 -06:00
if (!frames || !frames.length) {
return [];
}
2017-12-02 05:40:12 -06:00
const frame = frames[0];
const values: MetricFindValue[] = [];
const textField = frame.fields.find((f) => f.name === '__text');
const valueField = frame.fields.find((f) => f.name === '__value');
2017-12-02 05:40:12 -06:00
if (textField && valueField) {
for (let i = 0; i < textField.values.length; i++) {
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,
}))
);
}
2017-12-02 05:40:12 -06:00
}
return Array.from(new Set(values.map((v) => v.text))).map((text) => ({
text,
value: values.find((v) => v.text === text)?.value,
}));
2017-12-02 05:40:12 -06:00
}
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number): MetricFindValue[] {
2017-12-02 05:40:12 -06:00
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] });
2017-12-02 05:40:12 -06:00
}
}
return res;
}
transformToSimpleList(rows: any): MetricFindValue[] {
2017-12-02 05:40:12 -06:00
const res = [];
for (let i = 0; i < rows.length; i++) {
for (let j = 0; j < rows[i].length; j++) {
res.push(rows[i][j]);
2017-12-02 05:40:12 -06:00
}
}
const unique = Array.from(new Set(res));
return map(unique, (value) => {
return { text: value };
2017-12-02 05:40:12 -06:00
});
}
findColIndex(columns: any[], colName: string) {
2017-12-02 05:40:12 -06:00
for (let i = 0; i < columns.length; i++) {
if (columns[i].text === colName) {
return i;
}
}
return -1;
}
containsKey(res: any[], key: any) {
2017-12-02 05:40:12 -06:00
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];
const timeField = frame.fields.find((f) => f.name === 'time');
2017-12-02 05:40:12 -06:00
if (!timeField) {
return Promise.reject({ message: 'Missing mandatory time column (with time column alias) in annotation query.' });
2017-12-02 05:40:12 -06:00
}
const timeEndField = frame.fields.find((f) => f.name === 'timeend');
const textField = frame.fields.find((f) => f.name === 'text');
const tagsField = frame.fields.find((f) => f.name === 'tags');
const list: AnnotationEvent[] = [];
for (let i = 0; i < frame.length; i++) {
const timeEnd = timeEndField && timeEndField.values.get(i) ? Math.floor(timeEndField.values.get(i)) : undefined;
2017-12-02 05:40:12 -06:00
list.push({
annotation: options.annotation,
time: Math.floor(timeField.values.get(i)),
timeEnd,
text: textField && textField.values.get(i) ? textField.values.get(i) : '',
tags:
tagsField && tagsField.values.get(i)
? tagsField.values
.get(i)
.trim()
.split(/\s*,\s*/)
: [],
2017-12-02 05:40:12 -06:00
});
}
return list;
}
}