query_ctrl.test.ts 9.9 KB

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