2017-12-20 12:33:33 +01:00
|
|
|
import _ from 'lodash';
|
2016-03-22 20:23:27 +01:00
|
|
|
|
|
|
|
|
export default class ResponseParser {
|
2016-03-23 11:09:57 +01:00
|
|
|
parse(query, results) {
|
2017-12-19 16:06:54 +01:00
|
|
|
if (!results || results.results.length === 0) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2016-03-22 20:23:27 +01:00
|
|
|
|
2018-08-29 14:27:29 +02:00
|
|
|
const influxResults = results.results[0];
|
2016-03-22 22:43:55 +01:00
|
|
|
if (!influxResults.series) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 14:27:29 +02:00
|
|
|
const normalizedQuery = query.toLowerCase();
|
|
|
|
|
const isValueFirst =
|
2018-01-29 16:46:51 +01:00
|
|
|
normalizedQuery.indexOf('show field keys') >= 0 || normalizedQuery.indexOf('show retention policies') >= 0;
|
2018-05-21 15:47:29 +02:00
|
|
|
|
2018-08-29 14:27:29 +02:00
|
|
|
const res = {};
|
2016-04-18 18:46:50 +02:00
|
|
|
_.each(influxResults.series, serie => {
|
|
|
|
|
_.each(serie.values, value => {
|
2016-04-18 16:17:03 +02:00
|
|
|
if (_.isArray(value)) {
|
2018-01-29 16:46:51 +01:00
|
|
|
// 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]);
|
2016-04-18 16:17:03 +02:00
|
|
|
} else {
|
2016-04-18 18:46:50 +02:00
|
|
|
addUnique(res, value[0]);
|
2016-04-18 16:17:03 +02:00
|
|
|
}
|
2016-03-22 20:23:27 +01:00
|
|
|
} else {
|
2016-04-18 18:46:50 +02:00
|
|
|
addUnique(res, value);
|
2016-03-22 20:23:27 +01:00
|
|
|
}
|
2016-04-18 16:17:03 +02:00
|
|
|
});
|
2016-03-22 22:43:55 +01:00
|
|
|
});
|
2016-04-18 16:17:03 +02:00
|
|
|
|
2016-04-18 18:46:50 +02:00
|
|
|
return _.map(res, value => {
|
2018-02-01 14:55:03 +01:00
|
|
|
return { text: value.toString() };
|
2016-04-18 18:46:50 +02:00
|
|
|
});
|
2016-04-18 16:17:03 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addUnique(arr, value) {
|
2016-04-18 18:46:50 +02:00
|
|
|
arr[value] = value;
|
2016-03-22 20:23:27 +01:00
|
|
|
}
|