query_ctrl.test.ts 10 KB

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