Commit 6871f4c1 by Daniel Lee Committed by GitHub

Merge pull request #12018 from forzafootball/am/fix-query-variable-influxdb

Conditionally select a field to return in ResponseParser for InfluxDB
parents e17944db b7482ae8
...@@ -11,14 +11,30 @@ export default class ResponseParser { ...@@ -11,14 +11,30 @@ export default class ResponseParser {
return []; return [];
} }
var influxdb11format = query.toLowerCase().indexOf('show tag values') >= 0; var normalizedQuery = query.toLowerCase();
var isValueFirst =
normalizedQuery.indexOf('show field keys') >= 0 || normalizedQuery.indexOf('show retention policies') >= 0;
var res = {}; var res = {};
_.each(influxResults.series, serie => { _.each(influxResults.series, serie => {
_.each(serie.values, value => { _.each(serie.values, value => {
if (_.isArray(value)) { if (_.isArray(value)) {
if (influxdb11format) { // In general, there are 2 possible shapes for the returned value.
addUnique(res, value[1] || value[0]); // 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 { } else {
addUnique(res, value[0]); addUnique(res, value[0]);
} }
...@@ -29,7 +45,7 @@ export default class ResponseParser { ...@@ -29,7 +45,7 @@ export default class ResponseParser {
}); });
return _.map(res, value => { return _.map(res, value => {
return { text: value }; return { text: value.toString() };
}); });
} }
} }
......
...@@ -85,17 +85,44 @@ describe('influxdb response parser', () => { ...@@ -85,17 +85,44 @@ describe('influxdb response parser', () => {
}); });
}); });
describe('SELECT response', () => {
var query = 'SELECT "usage_iowait" FROM "cpu" LIMIT 10';
var response = {
results: [
{
series: [
{
name: 'cpu',
columns: ['time', 'usage_iowait'],
values: [[1488465190006040638, 0.0], [1488465190006040638, 15.0], [1488465190006040638, 20.2]],
},
],
},
],
};
var result = parser.parse(query, response);
it('should return second column', () => {
expect(_.size(result)).toBe(3);
expect(result[0].text).toBe('0');
expect(result[1].text).toBe('15');
expect(result[2].text).toBe('20.2');
});
});
describe('SHOW FIELD response', () => { describe('SHOW FIELD response', () => {
var query = 'SHOW FIELD KEYS FROM "cpu"'; var query = 'SHOW FIELD KEYS FROM "cpu"';
describe('response from 0.10.0', () => {
describe('response from pre-1.0', () => {
var response = { var response = {
results: [ results: [
{ {
series: [ series: [
{ {
name: 'measurements', name: 'cpu',
columns: ['name'], columns: ['fieldKey'],
values: [['cpu'], ['derivative'], ['logins.count'], ['logs'], ['payment.ended'], ['payment.started']], values: [['value']],
}, },
], ],
}, },
...@@ -103,20 +130,21 @@ describe('influxdb response parser', () => { ...@@ -103,20 +130,21 @@ describe('influxdb response parser', () => {
}; };
var result = parser.parse(query, response); var result = parser.parse(query, response);
it('should get two responses', () => { it('should get two responses', () => {
expect(_.size(result)).toBe(6); expect(_.size(result)).toBe(1);
}); });
}); });
describe('response from 0.11.0', () => { describe('response from 1.0', () => {
var response = { var response = {
results: [ results: [
{ {
series: [ series: [
{ {
name: 'cpu', name: 'cpu',
columns: ['fieldKey'], columns: ['fieldKey', 'fieldType'],
values: [['value']], values: [['time', 'float']],
}, },
], ],
}, },
...@@ -125,8 +153,9 @@ describe('influxdb response parser', () => { ...@@ -125,8 +153,9 @@ describe('influxdb response parser', () => {
var result = parser.parse(query, response); var result = parser.parse(query, response);
it('should get two responses', () => { it('should return first column', () => {
expect(_.size(result)).toBe(1); expect(_.size(result)).toBe(1);
expect(result[0].text).toBe('time');
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment