mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Graphite: Graphite query builder can now handle functions that multiple series as arguments! #117
This commit is contained in:
84
src/test/specs/graphiteDatasource-specs.js
Normal file
84
src/test/specs/graphiteDatasource-specs.js
Normal file
@@ -0,0 +1,84 @@
|
||||
define([
|
||||
'./helpers',
|
||||
'services/graphite/graphiteDatasource'
|
||||
], function(helpers) {
|
||||
'use strict';
|
||||
|
||||
describe('graphiteDatasource', function() {
|
||||
var ctx = new helpers.ServiceTestContext();
|
||||
|
||||
beforeEach(module('grafana.services'));
|
||||
beforeEach(ctx.providePhase());
|
||||
beforeEach(ctx.createService('GraphiteDatasource'));
|
||||
beforeEach(function() {
|
||||
ctx.ds = new ctx.service({ url: [''] });
|
||||
});
|
||||
|
||||
describe('When querying influxdb with one target using query editor target spec', function() {
|
||||
var query = {
|
||||
range: { from: 'now-1h', to: 'now' },
|
||||
targets: [{ target: 'prod1.count' }, {target: 'prod2.count'}],
|
||||
maxDataPoints: 500
|
||||
};
|
||||
|
||||
var response = [{ target: 'prod1.count', points: [[10, 1], [12,1]], }];
|
||||
var results;
|
||||
var request;
|
||||
|
||||
beforeEach(function() {
|
||||
|
||||
ctx.$httpBackend.expectPOST('/render', function(body) { request = body; return true; })
|
||||
.respond(response);
|
||||
|
||||
ctx.ds.query(query).then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('should generate the correct query', function() {
|
||||
ctx.$httpBackend.verifyNoOutstandingExpectation();
|
||||
});
|
||||
|
||||
it('should query correctly', function() {
|
||||
var params = request.split('&');
|
||||
expect(params).to.contain('target=prod1.count');
|
||||
expect(params).to.contain('target=prod2.count');
|
||||
expect(params).to.contain('from=-1h');
|
||||
expect(params).to.contain('until=now');
|
||||
});
|
||||
|
||||
it('should return series list', function() {
|
||||
expect(results.data.length).to.be(1);
|
||||
expect(results.data[0].target).to.be('prod1.count');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('building graphite params', function() {
|
||||
|
||||
it('should uri escape targets', function() {
|
||||
var results = ctx.ds.buildGraphiteParams({
|
||||
targets: [{target: 'prod1.{test,test2}'}, {target: 'prod2.count'}]
|
||||
});
|
||||
expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
|
||||
});
|
||||
|
||||
it('should replace target placeholder', function() {
|
||||
var results = ctx.ds.buildGraphiteParams({
|
||||
targets: [{target: 'series1'}, {target: 'series2'}, {target: 'asPercent(#A,#B)'}]
|
||||
});
|
||||
expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
|
||||
});
|
||||
|
||||
it('should ignore empty targets', function() {
|
||||
var results = ctx.ds.buildGraphiteParams({
|
||||
targets: [{target: 'series1'}, {target: ''}]
|
||||
});
|
||||
expect(results.length).to.be(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -64,6 +64,59 @@ define([
|
||||
});
|
||||
});
|
||||
|
||||
describe('when adding function before any metric segment', function() {
|
||||
beforeEach(function() {
|
||||
ctx.scope.target.target = '';
|
||||
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: true}]));
|
||||
ctx.scope.init();
|
||||
ctx.scope.$digest();
|
||||
|
||||
ctx.scope.$parent = { get_data: sinon.spy() };
|
||||
ctx.scope.addFunction(gfunc.getFuncDef('asPercent'));
|
||||
});
|
||||
|
||||
it('should add function and remove select metric link', function() {
|
||||
expect(ctx.scope.segments.length).to.be(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when initalizing target without metric expression and only function', function() {
|
||||
beforeEach(function() {
|
||||
ctx.scope.target.target = 'asPercent(#A, #B)';
|
||||
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
||||
ctx.scope.init();
|
||||
ctx.scope.$digest();
|
||||
ctx.scope.$parent = { get_data: sinon.spy() };
|
||||
});
|
||||
|
||||
it('should not add select metric segment', function() {
|
||||
expect(ctx.scope.segments.length).to.be(0);
|
||||
});
|
||||
|
||||
it('should add both series refs as params', function() {
|
||||
expect(ctx.scope.functions[0].params.length).to.be(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('when initalizing target without metric expression and function with series-ref', function() {
|
||||
beforeEach(function() {
|
||||
ctx.scope.target.target = 'asPercent(metric.node.count, #A)';
|
||||
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
||||
ctx.scope.init();
|
||||
ctx.scope.$digest();
|
||||
ctx.scope.$parent = { get_data: sinon.spy() };
|
||||
});
|
||||
|
||||
it('should add segments', function() {
|
||||
expect(ctx.scope.segments.length).to.be(3);
|
||||
});
|
||||
|
||||
it('should have correct func params', function() {
|
||||
expect(ctx.scope.functions[0].params.length).to.be(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('targetChanged', function() {
|
||||
beforeEach(function() {
|
||||
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}]));
|
||||
|
||||
@@ -156,6 +156,16 @@ define([
|
||||
expect(rootNode.segments[1].value).to.be('test');
|
||||
});
|
||||
|
||||
it('series parameters', function() {
|
||||
var parser = new Parser('asPercent(#A, #B)');
|
||||
var rootNode = parser.getAst();
|
||||
expect(rootNode.type).to.be('function');
|
||||
expect(rootNode.params[0].type).to.be('series-ref');
|
||||
expect(rootNode.params[0].value).to.be('#A');
|
||||
expect(rootNode.params[1].value).to.be('#B');
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -121,6 +121,7 @@ require([
|
||||
'specs/timeSeries-specs',
|
||||
'specs/row-ctrl-specs',
|
||||
'specs/graphiteTargetCtrl-specs',
|
||||
'specs/graphiteDatasource-specs',
|
||||
'specs/influxSeries-specs',
|
||||
'specs/influxQueryBuilder-specs',
|
||||
'specs/influxdb-datasource-specs',
|
||||
|
||||
Reference in New Issue
Block a user