query_ctrl_specs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import 'app/core/services/segment_srv';
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  3. import gfunc from '../gfunc';
  4. import helpers from 'test/specs/helpers';
  5. import {GraphiteQueryCtrl} from '../query_ctrl';
  6. describe('GraphiteQueryCtrl', function() {
  7. var ctx = new helpers.ControllerTestContext();
  8. beforeEach(angularMocks.module('grafana.core'));
  9. beforeEach(angularMocks.module('grafana.controllers'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(angularMocks.module(function($compileProvider) {
  12. $compileProvider.preAssignBindingsEnabled(true);
  13. }));
  14. beforeEach(ctx.providePhase());
  15. beforeEach(angularMocks.inject(($rootScope, $controller, $q) => {
  16. ctx.$q = $q;
  17. ctx.scope = $rootScope.$new();
  18. ctx.target = {target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)'};
  19. ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
  20. ctx.panelCtrl = {panel: {}};
  21. ctx.panelCtrl = {
  22. panel: {
  23. targets: [ctx.target]
  24. }
  25. };
  26. ctx.panelCtrl.refresh = sinon.spy();
  27. ctx.ctrl = $controller(GraphiteQueryCtrl, {$scope: ctx.scope}, {
  28. panelCtrl: ctx.panelCtrl,
  29. datasource: ctx.datasource,
  30. target: ctx.target
  31. });
  32. ctx.scope.$digest();
  33. }));
  34. describe('init', function() {
  35. it('should validate metric key exists', function() {
  36. expect(ctx.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*');
  37. });
  38. it('should delete last segment if no metrics are found', function() {
  39. expect(ctx.ctrl.segments[2].value).to.be('select metric');
  40. });
  41. it('should parse expression and build function model', function() {
  42. expect(ctx.ctrl.queryModel.functions.length).to.be(2);
  43. });
  44. });
  45. describe('when adding function', function() {
  46. beforeEach(function() {
  47. ctx.ctrl.target.target = 'test.prod.*.count';
  48. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  49. ctx.ctrl.parseTarget();
  50. ctx.ctrl.addFunction(gfunc.getFuncDef('aliasByNode'));
  51. });
  52. it('should add function with correct node number', function() {
  53. expect(ctx.ctrl.queryModel.functions[0].params[0]).to.be(2);
  54. });
  55. it('should update target', function() {
  56. expect(ctx.ctrl.target.target).to.be('aliasByNode(test.prod.*.count, 2)');
  57. });
  58. it('should call refresh', function() {
  59. expect(ctx.panelCtrl.refresh.called).to.be(true);
  60. });
  61. });
  62. describe('when adding function before any metric segment', function() {
  63. beforeEach(function() {
  64. ctx.ctrl.target.target = '';
  65. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: true}]));
  66. ctx.ctrl.parseTarget();
  67. ctx.ctrl.addFunction(gfunc.getFuncDef('asPercent'));
  68. });
  69. it('should add function and remove select metric link', function() {
  70. expect(ctx.ctrl.segments.length).to.be(0);
  71. });
  72. });
  73. describe('when initalizing target without metric expression and only function', function() {
  74. beforeEach(function() {
  75. ctx.ctrl.target.target = 'asPercent(#A, #B)';
  76. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  77. ctx.ctrl.parseTarget();
  78. ctx.scope.$digest();
  79. });
  80. it('should not add select metric segment', function() {
  81. expect(ctx.ctrl.segments.length).to.be(1);
  82. });
  83. it('should add second series ref as param', function() {
  84. expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1);
  85. });
  86. });
  87. describe('when initializing a target with single param func using variable', function() {
  88. beforeEach(function() {
  89. ctx.ctrl.target.target = 'movingAverage(prod.count, $var)';
  90. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  91. ctx.ctrl.parseTarget();
  92. });
  93. it('should add 2 segments', function() {
  94. expect(ctx.ctrl.segments.length).to.be(2);
  95. });
  96. it('should add function param', function() {
  97. expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1);
  98. });
  99. });
  100. describe('when initalizing target without metric expression and function with series-ref', function() {
  101. beforeEach(function() {
  102. ctx.ctrl.target.target = 'asPercent(metric.node.count, #A)';
  103. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  104. ctx.ctrl.parseTarget();
  105. });
  106. it('should add segments', function() {
  107. expect(ctx.ctrl.segments.length).to.be(3);
  108. });
  109. it('should have correct func params', function() {
  110. expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1);
  111. });
  112. });
  113. describe('when getting altSegments and metricFindQuery retuns empty array', function() {
  114. beforeEach(function() {
  115. ctx.ctrl.target.target = 'test.count';
  116. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  117. ctx.ctrl.parseTarget();
  118. ctx.ctrl.getAltSegments(1).then(function(results) {
  119. ctx.altSegments = results;
  120. });
  121. ctx.scope.$digest();
  122. });
  123. it('should have no segments', function() {
  124. expect(ctx.altSegments.length).to.be(0);
  125. });
  126. });
  127. describe('targetChanged', function() {
  128. beforeEach(function() {
  129. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  130. ctx.ctrl.parseTarget();
  131. ctx.ctrl.target.target = '';
  132. ctx.ctrl.targetChanged();
  133. });
  134. it('should rebuld target after expression model', function() {
  135. expect(ctx.ctrl.target.target).to.be('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
  136. });
  137. it('should call panelCtrl.refresh', function() {
  138. expect(ctx.panelCtrl.refresh.called).to.be(true);
  139. });
  140. });
  141. describe('when updating targets with nested query', function() {
  142. beforeEach(function() {
  143. ctx.ctrl.target.target = 'scaleToSeconds(#A, 60)';
  144. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  145. ctx.ctrl.parseTarget();
  146. ctx.ctrl.panelCtrl.panel.targets = [ {
  147. target: 'nested.query.count',
  148. refId: 'A'
  149. }];
  150. ctx.ctrl.updateModelTarget();
  151. });
  152. it('target should remain the same', function() {
  153. expect(ctx.ctrl.target.target).to.be('scaleToSeconds(#A, 60)');
  154. });
  155. it('targetFull should include nexted queries', function() {
  156. expect(ctx.ctrl.target.targetFull).to.be('scaleToSeconds(nested.query.count, 60)');
  157. });
  158. });
  159. describe('when updating target used in other query', function() {
  160. beforeEach(function() {
  161. ctx.ctrl.target.target = 'metrics.a.count';
  162. ctx.ctrl.target.refId = 'A';
  163. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  164. ctx.ctrl.parseTarget();
  165. ctx.ctrl.panelCtrl.panel.targets = [
  166. ctx.ctrl.target, {target: 'sumSeries(#A)', refId: 'B'}
  167. ];
  168. ctx.ctrl.updateModelTarget();
  169. });
  170. it('targetFull of other query should update', function() {
  171. expect(ctx.ctrl.panel.targets[1].targetFull).to.be('sumSeries(metrics.a.count)');
  172. });
  173. });
  174. describe('when adding seriesByTag function', function() {
  175. beforeEach(function() {
  176. ctx.ctrl.target.target = '';
  177. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  178. ctx.ctrl.parseTarget();
  179. ctx.ctrl.addFunction(gfunc.getFuncDef('seriesByTag'));
  180. });
  181. it('should update functions', function() {
  182. expect(ctx.ctrl.queryModel.getSeriesByTagFuncIndex()).to.be(0);
  183. });
  184. it('should update seriesByTagUsed flag', function() {
  185. expect(ctx.ctrl.queryModel.seriesByTagUsed).to.be(true);
  186. });
  187. it('should update target', function() {
  188. expect(ctx.ctrl.target.target).to.be('seriesByTag()');
  189. });
  190. it('should call refresh', function() {
  191. expect(ctx.panelCtrl.refresh.called).to.be(true);
  192. });
  193. });
  194. describe('when parsing seriesByTag function', function() {
  195. beforeEach(function() {
  196. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  197. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  198. ctx.ctrl.parseTarget();
  199. });
  200. it('should add tags', function() {
  201. const expected = [
  202. {key: 'tag1', operator: '=', value: 'value1'},
  203. {key: 'tag2', operator: '!=~', value: 'value2'}
  204. ];
  205. expect(ctx.ctrl.queryModel.tags).to.eql(expected);
  206. });
  207. it('should add plus button', function() {
  208. expect(ctx.ctrl.addTagSegments.length).to.be(1);
  209. });
  210. });
  211. describe('when tag added', function() {
  212. beforeEach(function() {
  213. ctx.ctrl.target.target = "seriesByTag()";
  214. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  215. ctx.ctrl.parseTarget();
  216. ctx.ctrl.addNewTag({value: 'tag1'});
  217. });
  218. it('should update tags with default value', function() {
  219. const expected = [
  220. {key: 'tag1', operator: '=', value: 'select tag value'}
  221. ];
  222. expect(ctx.ctrl.queryModel.tags).to.eql(expected);
  223. });
  224. it('should update target', function() {
  225. const expected = "seriesByTag('tag1=select tag value')";
  226. expect(ctx.ctrl.target.target).to.eql(expected);
  227. });
  228. });
  229. describe('when tag changed', function() {
  230. beforeEach(function() {
  231. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  232. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  233. ctx.ctrl.parseTarget();
  234. ctx.ctrl.tagChanged({key: 'tag1', operator: '=', value: 'new_value'}, 0);
  235. });
  236. it('should update tags', function() {
  237. const expected = [
  238. {key: 'tag1', operator: '=', value: 'new_value'},
  239. {key: 'tag2', operator: '!=~', value: 'value2'}
  240. ];
  241. expect(ctx.ctrl.queryModel.tags).to.eql(expected);
  242. });
  243. it('should update target', function() {
  244. const expected = "seriesByTag('tag1=new_value', 'tag2!=~value2')";
  245. expect(ctx.ctrl.target.target).to.eql(expected);
  246. });
  247. });
  248. describe('when tag removed', function() {
  249. beforeEach(function() {
  250. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  251. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  252. ctx.ctrl.parseTarget();
  253. ctx.ctrl.removeTag(0);
  254. });
  255. it('should update tags', function() {
  256. const expected = [
  257. {key: 'tag2', operator: '!=~', value: 'value2'}
  258. ];
  259. expect(ctx.ctrl.queryModel.tags).to.eql(expected);
  260. });
  261. it('should update target', function() {
  262. const expected = "seriesByTag('tag2!=~value2')";
  263. expect(ctx.ctrl.target.target).to.eql(expected);
  264. });
  265. });
  266. });