query_ctrl.test.ts 9.9 KB

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