query_ctrl_specs.ts 11 KB

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