From e104e9b2c251d8c7f068fc8c109e49900e6eca0b Mon Sep 17 00:00:00 2001 From: Aleksei Magusev Date: Mon, 29 Jan 2018 16:46:51 +0100 Subject: [PATCH 1/2] Conditionally select a field to return in ResponseParser for InfluxDB This patch also fixes "value[1] || value[0]" to not ignore zeros. --- .../datasource/influxdb/response_parser.ts | 22 +++++- .../influxdb/specs/response_parser.jest.ts | 71 +++++++++++++------ 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/response_parser.ts b/public/app/plugins/datasource/influxdb/response_parser.ts index 78ce67e7a37..ea7403f1909 100644 --- a/public/app/plugins/datasource/influxdb/response_parser.ts +++ b/public/app/plugins/datasource/influxdb/response_parser.ts @@ -11,14 +11,30 @@ export default class ResponseParser { 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 = {}; _.each(influxResults.series, serie => { _.each(serie.values, value => { if (_.isArray(value)) { - if (influxdb11format) { - addUnique(res, value[1] || value[0]); + // 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]); } diff --git a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts index 8ddc0fcdaf1..2928b9728d3 100644 --- a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts +++ b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts @@ -85,30 +85,36 @@ 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', () => { var query = 'SHOW FIELD KEYS FROM "cpu"'; - describe('response from 0.10.0', () => { - var response = { - results: [ - { - series: [ - { - name: 'measurements', - columns: ['name'], - values: [['cpu'], ['derivative'], ['logins.count'], ['logs'], ['payment.ended'], ['payment.started']], - }, - ], - }, - ], - }; - var result = parser.parse(query, response); - it('should get two responses', () => { - expect(_.size(result)).toBe(6); - }); - }); - - describe('response from 0.11.0', () => { + describe('response from pre-1.0', () => { var response = { results: [ { @@ -129,5 +135,28 @@ describe('influxdb response parser', () => { expect(_.size(result)).toBe(1); }); }); + + describe('response from 1.0', () => { + var response = { + results: [ + { + series: [ + { + name: 'cpu', + columns: ['fieldKey', 'fieldType'], + values: [['time', 'float']], + }, + ], + }, + ], + }; + + var result = parser.parse(query, response); + + it('should return first column', () => { + expect(_.size(result)).toBe(1); + expect(result[0].text).toBe('time'); + }); + }); }); }); From b7482ae8b784b9c5364229be0c86b8ec61074826 Mon Sep 17 00:00:00 2001 From: Aleksei Magusev Date: Thu, 1 Feb 2018 14:55:03 +0100 Subject: [PATCH 2/2] Fix ResponseParser for InfluxDB to return only string values --- public/app/plugins/datasource/influxdb/response_parser.ts | 2 +- .../datasource/influxdb/specs/response_parser.jest.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/response_parser.ts b/public/app/plugins/datasource/influxdb/response_parser.ts index ea7403f1909..f1a46001d95 100644 --- a/public/app/plugins/datasource/influxdb/response_parser.ts +++ b/public/app/plugins/datasource/influxdb/response_parser.ts @@ -45,7 +45,7 @@ export default class ResponseParser { }); return _.map(res, value => { - return { text: value }; + return { text: value.toString() }; }); } } diff --git a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts index 2928b9728d3..525508b2c1d 100644 --- a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts +++ b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts @@ -105,9 +105,9 @@ describe('influxdb response parser', () => { 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); + expect(result[0].text).toBe('0'); + expect(result[1].text).toBe('15'); + expect(result[2].text).toBe('20.2'); }); });