query_ctrl_specs.ts 11 KB

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