mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
tests(influxdb): adds tests for influxdb response
This commit is contained in:
parent
2d59112c1b
commit
53312852e9
@ -6,6 +6,7 @@ import _ from 'lodash';
|
||||
import * as dateMath from 'app/core/utils/datemath';
|
||||
import InfluxSeries from './influx_series';
|
||||
import InfluxQuery from './influx_query';
|
||||
import ResponseParser from './response_parser';
|
||||
|
||||
/** @ngInject */
|
||||
export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) {
|
||||
@ -22,6 +23,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
|
||||
this.interval = (instanceSettings.jsonData || {}).timeInterval;
|
||||
this.supportAnnotations = true;
|
||||
this.supportMetrics = true;
|
||||
this.responseParser = new ResponseParser();
|
||||
|
||||
this.query = function(options) {
|
||||
var timeFilter = getTimeFilter(options);
|
||||
@ -101,7 +103,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
|
||||
});
|
||||
};
|
||||
|
||||
this.metricFindQuery = function (query) {
|
||||
this.metricFindQuery = function (query, queryType) {
|
||||
var interpolated;
|
||||
try {
|
||||
interpolated = templateSrv.replace(query, null, 'regex');
|
||||
@ -109,23 +111,8 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
|
||||
return $q.reject(err);
|
||||
}
|
||||
|
||||
return this._seriesQuery(interpolated).then(function (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 };
|
||||
}
|
||||
});
|
||||
});
|
||||
return this._seriesQuery(interpolated)
|
||||
.then(_.curry(this.responseParser.parse)(queryType));
|
||||
};
|
||||
|
||||
this._seriesQuery = function(query) {
|
||||
|
23
public/app/plugins/datasource/influxdb/response_parser.ts
Normal file
23
public/app/plugins/datasource/influxdb/response_parser.ts
Normal file
@ -0,0 +1,23 @@
|
||||
///<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 };
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user