From ba48f40d2181eae2eace4eedfb562dc5be267a4d Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 23 Mar 2016 11:09:57 +0100 Subject: [PATCH] feat(influxdb): bases parsing upon query --- .../plugins/datasource/influxdb/datasource.ts | 2 +- .../datasource/influxdb/response_parser.ts | 6 +- .../influxdb/specs/response_parser_specs.ts | 66 +++++++++++++++++-- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index 7052201b422..fd5f4b0bb77 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -112,7 +112,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) } return this._seriesQuery(interpolated) - .then(_.curry(this.responseParser.parse)(queryType)); + .then(_.curry(this.responseParser.parse)(query)); }; this._seriesQuery = function(query) { diff --git a/public/app/plugins/datasource/influxdb/response_parser.ts b/public/app/plugins/datasource/influxdb/response_parser.ts index ff99ee24284..ae6f2cb75a9 100644 --- a/public/app/plugins/datasource/influxdb/response_parser.ts +++ b/public/app/plugins/datasource/influxdb/response_parser.ts @@ -4,7 +4,7 @@ import _ from 'lodash'; export default class ResponseParser { - parse(queryType, results) { + parse(query, results) { if (!results || results.results.length === 0) { return []; } var influxResults = results.results[0]; @@ -13,9 +13,9 @@ export default class ResponseParser { } var series = influxResults.series[0]; - return _.map(series.values, function(value) { + return _.map(series.values, (value) => { if (_.isArray(value)) { - if (queryType === 'SHOW_TAGS') { + if (query.indexOf('SHOW TAG VALUES') >= 0) { return { text: (value[1] || value[0]) }; } else { return { text: value[0] }; diff --git a/public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts b/public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts index 506d828ab7e..d83b3ab4fa2 100644 --- a/public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts +++ b/public/app/plugins/datasource/influxdb/specs/response_parser_specs.ts @@ -4,7 +4,56 @@ import ResponseParser from '../response_parser'; describe("influxdb response parser", () => { this.parser = new ResponseParser(); - describe("SHOW_TAGS response", () => { + describe("SHOW TAG response", () => { + var query = 'SHOW TAG KEYS FROM "cpu"'; + describe("response from 0.10.0", () => { + var response = { + "results": [ + { + "series": [ + { + "name": "cpu", + "columns": ["tagKey"], + "values": [ ["datacenter"], ["hostname"], ["source"] ] + } + ] + } + ] + }; + + var result = this.parser.parse(query, response); + + it("expects three results", () => { + expect(_.size(result)).to.be(3); + }); + }); + + describe("response from 0.11.0", () => { + var response = { + "results": [ + { + "series": [ + { + "name": "cpu", + "columns": ["tagKey"], + "values": [ ["datacenter"], ["hostname"], ["source"] ] + } + ] + } + ] + }; + + var result = this.parser.parse(query, response); + + it("expects three results", () => { + expect(_.size(result)).to.be(3); + }); + }); + }); + + describe("SHOW TAG VALUES response", () => { + var query = 'SHOW TAG VALUES FROM "cpu" WITH KEY = "hostname"'; + describe("response from 0.10.0", () => { var response = { "results": [ @@ -20,7 +69,7 @@ describe("influxdb response parser", () => { ] }; - var result = this.parser.parse('SHOW_TAGS', response); + var result = this.parser.parse(query, response); it("should get two responses", () => { expect(_.size(result)).to.be(2); @@ -44,7 +93,7 @@ describe("influxdb response parser", () => { ] }; - var result = this.parser.parse('SHOW_TAGS', response); + var result = this.parser.parse(query, response); it("should get two responses", () => { expect(_.size(result)).to.be(2); @@ -52,9 +101,14 @@ describe("influxdb response parser", () => { expect(result[1].text).to.be('api'); }); }); + + + + }); - describe("SHOW_FIELDS response", () => { + describe("SHOW FIELD response", () => { + var query = 'SHOW FIELD KEYS FROM "cpu"'; describe("response from 0.10.0", () => { var response = { "results": [ @@ -72,7 +126,7 @@ describe("influxdb response parser", () => { ] }; - var result = this.parser.parse('SHOW_FIELDS', response); + var result = this.parser.parse(query, response); it("should get two responses", () => { expect(_.size(result)).to.be(6); }); @@ -93,7 +147,7 @@ describe("influxdb response parser", () => { ] }; - var result = this.parser.parse('SHOW_FIELDS', response); + var result = this.parser.parse(query, response); it("should get two responses", () => { expect(_.size(result)).to.be(1);