Files
grafana/public/app/plugins/datasource/influxdb/response_parser.ts
Joey Tawadrous 10232c7857 InfluxDB: backend migration (run query in explore) (#43352)
* InfluxDB backend migration

* Multiple queries and more

* Added types

* Updated preferredVisualisationType

* Updated model parser test to include limit,slimit,orderByTime

* Added test for building query with limit, slimit

* Added test for building query with limit, slimit, orderByTime and puts them in the correct order

* Add test: Influxdb response parser should parse two responses with different refIDs

* Moved methods to responds parser

* Add test to ensure ExecutedQueryString is populated

* Move functions out of response parser class

* Test for getSelectedParams

* Merge cases

* Change to const

* Test get table columns correctly

* Removed unecessary fields

* Test get table rows correctly

* Removed getSeries function

* Added test for preferredVisualisationType

* Added test for executedQueryString

* Modified response parser

* Removed test

* Improvements

* Tests

* Review changes

* Feature flag rename and code gen
2022-02-09 18:26:16 +00:00

161 lines
4.9 KiB
TypeScript

import { DataFrame, FieldType, QueryResultMeta } from '@grafana/data';
import TableModel from 'app/core/table_model';
import { each, groupBy, isArray } from 'lodash';
import { InfluxQuery } from './types';
export default class ResponseParser {
parse(query: string, results: { results: any }) {
if (!results?.results || results.results.length === 0) {
return [];
}
const influxResults = results.results[0];
if (!influxResults.series) {
return [];
}
const normalizedQuery = query.toLowerCase();
const isValueFirst =
normalizedQuery.indexOf('show field keys') >= 0 || normalizedQuery.indexOf('show retention policies') >= 0;
const res = new Set<string>();
each(influxResults.series, (serie) => {
each(serie.values, (value) => {
if (isArray(value)) {
// In general, there are 2 possible shapes for the returned value.
// The first one is a two-element array,
// where the first element is somewhat a metadata value:
// the tag name for SHOW TAG VALUES queries,
// the time field for SELECT queries, etc.
// The second shape is an one-element array,
// that is containing an immediate value.
// For example, SHOW FIELD KEYS queries return such shape.
// Note, pre-0.11 versions return
// the second shape for SHOW TAG VALUES queries
// (while the newer versions—first).
if (isValueFirst) {
addUnique(res, value[0]);
} else if (value[1] !== undefined) {
addUnique(res, value[1]);
} else {
addUnique(res, value[0]);
}
} else {
addUnique(res, value);
}
});
});
// NOTE: it is important to keep the order of items in the parsed output
// the same as it was in the influxdb-response.
// we use a `Set` to collect the unique-results, and `Set` iteration
// order is insertion-order, so this should be ok.
return Array.from(res).map((v) => ({ text: v }));
}
getTable(dfs: DataFrame[], target: InfluxQuery, meta: QueryResultMeta): TableModel {
let table = new TableModel();
if (dfs.length > 0) {
table.meta = {
...meta,
executedQueryString: dfs[0].meta?.executedQueryString,
};
table.refId = target.refId;
table = getTableCols(dfs, table, target);
// if group by tag(s) added
if (dfs[0].fields[1].labels) {
let dfsByLabels: any = groupBy(dfs, (df: DataFrame) =>
df.fields[1].labels ? Object.values(df.fields[1].labels!) : null
);
const labels = Object.keys(dfsByLabels);
dfsByLabels = Object.values(dfsByLabels);
for (let i = 0; i < dfsByLabels.length; i++) {
table = getTableRows(dfsByLabels[i], table, [...labels[i].split(',')]);
}
} else {
table = getTableRows(dfs, table, []);
}
}
return table;
}
}
function getTableCols(dfs: DataFrame[], table: TableModel, target: InfluxQuery): TableModel {
const selectedParams = getSelectedParams(target);
dfs[0].fields.forEach((field) => {
// Time col
if (field.name === 'time') {
table.columns.push({ text: 'Time', type: FieldType.time });
}
// Group by (label) column(s)
else if (field.name === 'value') {
if (field.labels) {
Object.keys(field.labels).forEach((key) => {
table.columns.push({ text: key });
});
}
}
});
// Select (metric) column(s)
for (let i = 0; i < selectedParams.length; i++) {
table.columns.push({ text: selectedParams[i] });
}
return table;
}
function getTableRows(dfs: DataFrame[], table: TableModel, labels: string[]): TableModel {
const values = dfs[0].fields[0].values.toArray();
for (let i = 0; i < values.length; i++) {
const time = values[i];
const metrics = dfs.map((df: DataFrame) => {
return df.fields[1].values.toArray()[i];
});
table.rows.push([time, ...labels, ...metrics]);
}
return table;
}
export function getSelectedParams(target: InfluxQuery): string[] {
let allParams: string[] = [];
target.select?.forEach((select) => {
const selector = select.filter((x) => x.type !== 'field');
if (selector.length > 0) {
allParams.push(selector[0].type);
} else {
if (select[0] && select[0].params && select[0].params[0]) {
allParams.push(select[0].params[0].toString());
}
}
});
let uniqueParams: string[] = [];
allParams.forEach((param) => {
uniqueParams.push(incrementName(param, param, uniqueParams, 0));
});
return uniqueParams;
}
function incrementName(name: string, nameIncremenet: string, params: string[], index: number): string {
if (params.indexOf(nameIncremenet) > -1) {
index++;
return incrementName(name, name + '_' + index, params, index);
}
return nameIncremenet;
}
function addUnique(s: Set<string>, value: string | number) {
s.add(value.toString());
}