2021-04-21 02:38:00 -05:00
|
|
|
import { map } from 'lodash';
|
2021-05-05 09:46:07 -05:00
|
|
|
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 {
|
2021-05-05 09:46:07 -05:00
|
|
|
transformMetricFindResponse(raw: FetchResponse<BackendDataSourceResponse>): MetricFindValue[] {
|
|
|
|
const frames = toDataQueryResponse(raw).data as DataFrame[];
|
2017-12-02 05:40:12 -06:00
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
if (!frames || !frames.length) {
|
2018-03-13 10:03:02 -05:00
|
|
|
return [];
|
|
|
|
}
|
2017-12-02 05:40:12 -06:00
|
|
|
|
2021-05-05 09:46:07 -05: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
|
|
|
|
2021-05-05 09:46:07 -05: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
|
|
|
}
|
|
|
|
|
2021-05-05 09:46:07 -05: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
|
|
|
}
|
|
|
|
|
2020-11-10 23:19:43 -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])) {
|
2018-03-13 10:03:02 -05:00
|
|
|
res.push({ text: rows[i][textColIndex], value: rows[i][valueColIndex] });
|
2017-12-02 05:40:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:19:43 -06:00
|
|
|
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++) {
|
2020-11-01 23:25:54 -06:00
|
|
|
res.push(rows[i][j]);
|
2017-12-02 05:40:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:25:54 -06:00
|
|
|
const unique = Array.from(new Set(res));
|
|
|
|
|
2021-04-21 02:38:00 -05:00
|
|
|
return map(unique, (value) => {
|
2018-03-13 10:03:02 -05:00
|
|
|
return { text: value };
|
2017-12-02 05:40:12 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-01 04:11:57 -05: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;
|
|
|
|
}
|
|
|
|
|
2019-07-01 04:11:57 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
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
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
if (!timeField) {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.reject({ message: 'Missing mandatory time column (with time column alias) in annotation query.' });
|
2017-12-02 05:40:12 -06:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:46:07 -05: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,
|
2021-05-05 09:46:07 -05:00
|
|
|
time: Math.floor(timeField.values.get(i)),
|
2019-12-13 10:25:36 -06:00
|
|
|
timeEnd,
|
2021-05-05 09:46:07 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|