2014-03-22 07:45:02 -05:00
|
|
|
define([
|
2014-12-23 14:01:50 -06:00
|
|
|
'helpers',
|
2015-02-28 01:25:13 -06:00
|
|
|
'plugins/datasource/graphite/gfunc',
|
|
|
|
'plugins/datasource/graphite/queryCtrl'
|
2014-08-07 06:44:09 -05:00
|
|
|
], function(helpers, gfunc) {
|
2014-04-06 03:47:14 -05:00
|
|
|
'use strict';
|
2014-03-22 07:45:02 -05:00
|
|
|
|
2014-12-30 13:49:04 -06:00
|
|
|
describe('GraphiteQueryCtrl', function() {
|
2014-08-07 03:42:05 -05:00
|
|
|
var ctx = new helpers.ControllerTestContext();
|
2014-03-22 07:45:02 -05:00
|
|
|
|
2014-08-07 03:42:05 -05:00
|
|
|
beforeEach(module('grafana.controllers'));
|
|
|
|
beforeEach(ctx.providePhase());
|
2014-12-30 13:49:04 -06:00
|
|
|
beforeEach(ctx.createControllerPhase('GraphiteQueryCtrl'));
|
2014-03-22 07:45:02 -05:00
|
|
|
|
2014-08-07 06:44:09 -05:00
|
|
|
beforeEach(function() {
|
|
|
|
ctx.scope.target = {
|
|
|
|
target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)'
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.scope.datasource = ctx.datasource;
|
|
|
|
ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
|
|
|
|
});
|
|
|
|
|
2014-03-22 07:45:02 -05:00
|
|
|
describe('init', function() {
|
|
|
|
beforeEach(function() {
|
2014-08-07 03:42:05 -05:00
|
|
|
ctx.scope.init();
|
2014-08-07 06:44:09 -05:00
|
|
|
ctx.scope.$digest();
|
2014-08-07 03:42:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate metric key exists', function() {
|
2014-08-27 09:29:48 -05:00
|
|
|
expect(ctx.scope.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*');
|
2014-08-07 03:42:05 -05:00
|
|
|
});
|
|
|
|
|
2014-08-07 06:44:09 -05:00
|
|
|
it('should delete last segment if no metrics are found', function() {
|
|
|
|
expect(ctx.scope.segments[2].value).to.be('select metric');
|
|
|
|
});
|
|
|
|
|
2014-08-07 03:42:05 -05:00
|
|
|
it('should parse expression and build function model', function() {
|
|
|
|
expect(ctx.scope.functions.length).to.be(2);
|
2014-03-22 07:45:02 -05:00
|
|
|
});
|
2014-08-07 06:44:09 -05:00
|
|
|
});
|
2014-08-07 03:42:05 -05:00
|
|
|
|
2014-08-07 06:44:09 -05:00
|
|
|
describe('when adding function', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
ctx.scope.target.target = 'test.prod.*.count';
|
|
|
|
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}]));
|
|
|
|
ctx.scope.init();
|
|
|
|
ctx.scope.$digest();
|
|
|
|
|
|
|
|
ctx.scope.$parent = { get_data: sinon.spy() };
|
|
|
|
ctx.scope.addFunction(gfunc.getFuncDef('aliasByNode'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add function with correct node number', function() {
|
|
|
|
expect(ctx.scope.functions[0].params[0]).to.be(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update target', function() {
|
2014-09-03 01:53:08 -05:00
|
|
|
expect(ctx.scope.target.target).to.be('aliasByNode(test.prod.*.count, 2)');
|
2014-08-07 06:44:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call get_data', function() {
|
|
|
|
expect(ctx.scope.$parent.get_data.called).to.be(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-02 13:59:54 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-09-11 10:19:39 -05:00
|
|
|
describe('when initializing a target with single param func using variable', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
ctx.scope.target.target = 'movingAverage(prod.count, $var)';
|
|
|
|
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
|
|
|
ctx.scope.init();
|
|
|
|
ctx.scope.$digest();
|
|
|
|
ctx.scope.$parent = { get_data: sinon.spy() };
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add 2 segments', function() {
|
|
|
|
expect(ctx.scope.segments.length).to.be(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add function param', function() {
|
|
|
|
expect(ctx.scope.functions[0].params.length).to.be(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-09-02 13:59:54 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-15 08:15:30 -06:00
|
|
|
describe('when getting altSegments and metricFindQuery retuns empty array', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
ctx.scope.target.target = 'test.count';
|
|
|
|
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
|
|
|
|
ctx.scope.init();
|
|
|
|
ctx.scope.getAltSegments(1);
|
|
|
|
ctx.scope.$digest();
|
|
|
|
ctx.scope.$parent = { get_data: sinon.spy() };
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have no segments', function() {
|
|
|
|
expect(ctx.scope.altSegments.length).to.be(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-08-07 06:44:09 -05:00
|
|
|
describe('targetChanged', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}]));
|
|
|
|
ctx.scope.init();
|
|
|
|
ctx.scope.$digest();
|
|
|
|
|
|
|
|
ctx.scope.$parent = { get_data: sinon.spy() };
|
|
|
|
ctx.scope.target.target = '';
|
|
|
|
ctx.scope.targetChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should rebuld target after expression model', function() {
|
2014-09-03 01:53:08 -05:00
|
|
|
expect(ctx.scope.target.target).to.be('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
|
2014-08-07 06:44:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call get_data', function() {
|
|
|
|
expect(ctx.scope.$parent.get_data.called).to.be(true);
|
|
|
|
});
|
2014-03-22 07:45:02 -05:00
|
|
|
});
|
2014-08-07 06:44:09 -05:00
|
|
|
|
|
|
|
|
2014-03-23 10:04:21 -05:00
|
|
|
});
|
2014-03-22 07:45:02 -05:00
|
|
|
});
|