query_ctrl.test.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 a target with single param func using variable', () => {
  73. beforeEach(() => {
  74. ctx.ctrl.target.target = 'movingAverage(prod.count, $var)';
  75. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
  76. ctx.ctrl.parseTarget();
  77. });
  78. it('should add 2 segments', () => {
  79. expect(ctx.ctrl.segments.length).toBe(2);
  80. });
  81. it('should add function param', () => {
  82. expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
  83. });
  84. });
  85. describe('when initializing target without metric expression and function with series-ref', () => {
  86. beforeEach(() => {
  87. ctx.ctrl.target.target = 'asPercent(metric.node.count, #A)';
  88. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
  89. ctx.ctrl.parseTarget();
  90. });
  91. it('should add segments', () => {
  92. expect(ctx.ctrl.segments.length).toBe(3);
  93. });
  94. it('should have correct func params', () => {
  95. expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
  96. });
  97. });
  98. describe('when getting altSegments and metricFindQuery returns empty array', () => {
  99. beforeEach(() => {
  100. ctx.ctrl.target.target = 'test.count';
  101. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
  102. ctx.ctrl.parseTarget();
  103. ctx.ctrl.getAltSegments(1).then(results => {
  104. ctx.altSegments = results;
  105. });
  106. });
  107. it('should have no segments', () => {
  108. expect(ctx.altSegments.length).toBe(0);
  109. });
  110. });
  111. describe('targetChanged', () => {
  112. beforeEach(() => {
  113. ctx.ctrl.target.target = 'aliasByNode(scaleToSeconds(test.prod.*, 1), 2)';
  114. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  115. ctx.ctrl.parseTarget();
  116. ctx.ctrl.target.target = '';
  117. ctx.ctrl.targetChanged();
  118. });
  119. it('should rebuild target after expression model', () => {
  120. expect(ctx.ctrl.target.target).toBe('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
  121. });
  122. it('should call panelCtrl.refresh', () => {
  123. expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
  124. });
  125. });
  126. describe('when updating targets with nested query', () => {
  127. beforeEach(() => {
  128. ctx.ctrl.target.target = 'scaleToSeconds(#A, 60)';
  129. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  130. ctx.ctrl.parseTarget();
  131. });
  132. it('should add function params', () => {
  133. expect(ctx.ctrl.queryModel.segments.length).toBe(1);
  134. expect(ctx.ctrl.queryModel.segments[0].value).toBe('#A');
  135. expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
  136. expect(ctx.ctrl.queryModel.functions[0].params[0]).toBe(60);
  137. });
  138. it('target should remain the same', () => {
  139. expect(ctx.ctrl.target.target).toBe('scaleToSeconds(#A, 60)');
  140. });
  141. it('targetFull should include nested queries', () => {
  142. ctx.ctrl.panelCtrl.panel.targets = [
  143. {
  144. target: 'nested.query.count',
  145. refId: 'A',
  146. },
  147. ];
  148. ctx.ctrl.updateModelTarget();
  149. expect(ctx.ctrl.target.target).toBe('scaleToSeconds(#A, 60)');
  150. expect(ctx.ctrl.target.targetFull).toBe('scaleToSeconds(nested.query.count, 60)');
  151. });
  152. });
  153. describe('when updating target used in other query', () => {
  154. beforeEach(() => {
  155. ctx.ctrl.target.target = 'metrics.a.count';
  156. ctx.ctrl.target.refId = 'A';
  157. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  158. ctx.ctrl.parseTarget();
  159. ctx.ctrl.panelCtrl.panel.targets = [ctx.ctrl.target, { target: 'sumSeries(#A)', refId: 'B' }];
  160. ctx.ctrl.updateModelTarget();
  161. });
  162. it('targetFull of other query should update', () => {
  163. expect(ctx.ctrl.panel.targets[1].targetFull).toBe('sumSeries(metrics.a.count)');
  164. });
  165. });
  166. describe('when adding seriesByTag function', () => {
  167. beforeEach(() => {
  168. ctx.ctrl.target.target = '';
  169. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  170. ctx.ctrl.parseTarget();
  171. ctx.ctrl.addFunction(gfunc.getFuncDef('seriesByTag'));
  172. });
  173. it('should update functions', () => {
  174. expect(ctx.ctrl.queryModel.getSeriesByTagFuncIndex()).toBe(0);
  175. });
  176. it('should update seriesByTagUsed flag', () => {
  177. expect(ctx.ctrl.queryModel.seriesByTagUsed).toBe(true);
  178. });
  179. it('should update target', () => {
  180. expect(ctx.ctrl.target.target).toBe('seriesByTag()');
  181. });
  182. it('should call refresh', () => {
  183. expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
  184. });
  185. });
  186. describe('when parsing seriesByTag function', () => {
  187. beforeEach(() => {
  188. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  189. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  190. ctx.ctrl.parseTarget();
  191. });
  192. it('should add tags', () => {
  193. const expected = [
  194. { key: 'tag1', operator: '=', value: 'value1' },
  195. { key: 'tag2', operator: '!=~', value: 'value2' },
  196. ];
  197. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  198. });
  199. it('should add plus button', () => {
  200. expect(ctx.ctrl.addTagSegments.length).toBe(1);
  201. });
  202. });
  203. describe('when tag added', () => {
  204. beforeEach(() => {
  205. ctx.ctrl.target.target = 'seriesByTag()';
  206. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  207. ctx.ctrl.parseTarget();
  208. ctx.ctrl.addNewTag({ value: 'tag1' });
  209. });
  210. it('should update tags with default value', () => {
  211. const expected = [{ key: 'tag1', operator: '=', value: '' }];
  212. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  213. });
  214. it('should update target', () => {
  215. const expected = "seriesByTag('tag1=')";
  216. expect(ctx.ctrl.target.target).toEqual(expected);
  217. });
  218. });
  219. describe('when tag changed', () => {
  220. beforeEach(() => {
  221. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  222. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  223. ctx.ctrl.parseTarget();
  224. ctx.ctrl.tagChanged({ key: 'tag1', operator: '=', value: 'new_value' }, 0);
  225. });
  226. it('should update tags', () => {
  227. const expected = [
  228. { key: 'tag1', operator: '=', value: 'new_value' },
  229. { key: 'tag2', operator: '!=~', value: 'value2' },
  230. ];
  231. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  232. });
  233. it('should update target', () => {
  234. const expected = "seriesByTag('tag1=new_value', 'tag2!=~value2')";
  235. expect(ctx.ctrl.target.target).toEqual(expected);
  236. });
  237. });
  238. describe('when tag removed', () => {
  239. beforeEach(() => {
  240. ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
  241. ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
  242. ctx.ctrl.parseTarget();
  243. ctx.ctrl.removeTag(0);
  244. });
  245. it('should update tags', () => {
  246. const expected = [{ key: 'tag2', operator: '!=~', value: 'value2' }];
  247. expect(ctx.ctrl.queryModel.tags).toEqual(expected);
  248. });
  249. it('should update target', () => {
  250. const expected = "seriesByTag('tag2!=~value2')";
  251. expect(ctx.ctrl.target.target).toEqual(expected);
  252. });
  253. });
  254. });