Commit 53312852 by bergquist

tests(influxdb): adds tests for influxdb response

parent 2d59112c
...@@ -6,6 +6,7 @@ import _ from 'lodash'; ...@@ -6,6 +6,7 @@ import _ from 'lodash';
import * as dateMath from 'app/core/utils/datemath'; import * as dateMath from 'app/core/utils/datemath';
import InfluxSeries from './influx_series'; import InfluxSeries from './influx_series';
import InfluxQuery from './influx_query'; import InfluxQuery from './influx_query';
import ResponseParser from './response_parser';
/** @ngInject */ /** @ngInject */
export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) { export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) {
...@@ -22,6 +23,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -22,6 +23,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
this.interval = (instanceSettings.jsonData || {}).timeInterval; this.interval = (instanceSettings.jsonData || {}).timeInterval;
this.supportAnnotations = true; this.supportAnnotations = true;
this.supportMetrics = true; this.supportMetrics = true;
this.responseParser = new ResponseParser();
this.query = function(options) { this.query = function(options) {
var timeFilter = getTimeFilter(options); var timeFilter = getTimeFilter(options);
...@@ -101,7 +103,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -101,7 +103,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
}); });
}; };
this.metricFindQuery = function (query) { this.metricFindQuery = function (query, queryType) {
var interpolated; var interpolated;
try { try {
interpolated = templateSrv.replace(query, null, 'regex'); interpolated = templateSrv.replace(query, null, 'regex');
...@@ -109,23 +111,8 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -109,23 +111,8 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
return $q.reject(err); return $q.reject(err);
} }
return this._seriesQuery(interpolated).then(function (results) { return this._seriesQuery(interpolated)
if (!results || results.results.length === 0) { return []; } .then(_.curry(this.responseParser.parse)(queryType));
var influxResults = results.results[0];
if (!influxResults.series) {
return [];
}
var series = influxResults.series[0];
return _.map(series.values, function(value) {
if (_.isArray(value)) {
return { text: value[0] };
} else {
return { text: value };
}
});
});
}; };
this._seriesQuery = function(query) { this._seriesQuery = function(query) {
......
///<reference path="../../../headers/common.d.ts" />
import _ from 'lodash';
export default class ResponseParser {
parse(queryType, results) {
if (!results || results.results.length === 0) { return []; }
var influxResults = results.results[0];
if (!influxResults.series) {
return [];
}
var series = influxResults.series[0];
return _.map(series.values, function(value) {
if (_.isArray(value)) {
return { text: value[0] };
} else {
return { text: value };
}
});
}
}
import _ from 'lodash';
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
import ResponseParser from '../response_parser';
describe.only("influxdb response parser", () => {
describe("SHOW_TAGS response", () => {
this.parser = new ResponseParser();
describe("response from 0.10.0", () => {
var response = {
"results": [
{
"series": [
{
"name": "hostnameTagValues",
"columns": ["hostname"],
"values": [ ["server1"], ["server2"] ]
}
]
}
]
};
var result = this.parser.parse('SHOW_TAGS', response);
it("should get two responses", () => {
expect(_.size(result)).to.be(2);
expect(result[0].text).to.be("server1");
expect(result[1].text).to.be("server2");
});
});
});
describe("SHOW_FIELDS response", () => {
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 = this.parser.parse('SHOW_FIELDS', response);
it("should get two responses", () => {
expect(_.size(result)).to.be(6);
});
});
});
});
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