query_ctrl_specs.ts 11 KB

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