query_ctrl_specs.ts 11 KB

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