graph_specs.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. ///<reference path="../../../../headers/common.d.ts" />
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common';
  3. import '../module';
  4. import angular from 'angular';
  5. import $ from 'jquery';
  6. import helpers from 'test/specs/helpers';
  7. import TimeSeries from 'app/core/time_series2';
  8. import moment from 'moment';
  9. import {Emitter} from 'app/core/core';
  10. describe('grafanaGraph', function() {
  11. beforeEach(angularMocks.module('grafana.directives'));
  12. function graphScenario(desc, func, elementWidth = 500) {
  13. describe(desc, function() {
  14. var ctx: any = {};
  15. ctx.setup = function(setupFunc) {
  16. beforeEach(angularMocks.module(function($provide) {
  17. $provide.value("timeSrv", new helpers.TimeSrvStub());
  18. }));
  19. beforeEach(angularMocks.inject(function($rootScope, $compile) {
  20. var ctrl: any = {
  21. events: new Emitter(),
  22. height: 200,
  23. panel: {
  24. legend: {},
  25. grid: { },
  26. yaxes: [
  27. {
  28. min: null,
  29. max: null,
  30. format: 'short',
  31. logBase: 1
  32. },
  33. {
  34. min: null,
  35. max: null,
  36. format: 'short',
  37. logBase: 1
  38. }
  39. ],
  40. xaxis: {},
  41. seriesOverrides: [],
  42. tooltip: {
  43. shared: true
  44. }
  45. },
  46. renderingCompleted: sinon.spy(),
  47. hiddenSeries: {},
  48. dashboard: {
  49. getTimezone: sinon.stub().returns('browser')
  50. },
  51. range: {
  52. from: moment([2015, 1, 1, 10]),
  53. to: moment([2015, 1, 1, 22]),
  54. },
  55. };
  56. var scope = $rootScope.$new();
  57. scope.ctrl = ctrl;
  58. $rootScope.onAppEvent = sinon.spy();
  59. ctx.data = [];
  60. ctx.data.push(new TimeSeries({
  61. datapoints: [[1,1],[2,2]],
  62. alias: 'series1'
  63. }));
  64. ctx.data.push(new TimeSeries({
  65. datapoints: [[1,1],[2,2]],
  66. alias: 'series2'
  67. }));
  68. setupFunc(ctrl, ctx.data);
  69. var element = angular.element("<div style='width:" + elementWidth + "px' grafana-graph><div>");
  70. $compile(element)(scope);
  71. scope.$digest();
  72. $.plot = ctx.plotSpy = sinon.spy();
  73. ctrl.events.emit('render', ctx.data);
  74. ctx.plotData = ctx.plotSpy.getCall(0).args[1];
  75. ctx.plotOptions = ctx.plotSpy.getCall(0).args[2];
  76. }));
  77. };
  78. func(ctx);
  79. });
  80. }
  81. graphScenario('simple lines options', function(ctx) {
  82. ctx.setup(function(ctrl) {
  83. ctrl.panel.lines = true;
  84. ctrl.panel.fill = 5;
  85. ctrl.panel.linewidth = 3;
  86. ctrl.panel.steppedLine = true;
  87. });
  88. it('should configure plot with correct options', function() {
  89. expect(ctx.plotOptions.series.lines.show).to.be(true);
  90. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  91. expect(ctx.plotOptions.series.lines.lineWidth).to.be(3);
  92. expect(ctx.plotOptions.series.lines.steps).to.be(true);
  93. });
  94. });
  95. graphScenario('grid thresholds 100, 200', function(ctx) {
  96. ctx.setup(function(ctrl) {
  97. ctrl.panel.alert = {
  98. warn: { op: ">", value: 100},
  99. crit: { op: ">", value: 200}
  100. };
  101. });
  102. it('should add crit fill', function() {
  103. var markings = ctx.plotOptions.grid.markings;
  104. expect(markings[0].yaxis.from).to.be(200);
  105. expect(markings[0].yaxis.to).to.be(Infinity);
  106. expect(markings[0].color).to.be('rgba(234, 112, 112, 0.10)');
  107. });
  108. it('should add crit line', function() {
  109. var markings = ctx.plotOptions.grid.markings;
  110. expect(markings[1].yaxis.from).to.be(200);
  111. expect(markings[1].yaxis.to).to.be(200);
  112. expect(markings[1].color).to.be('#ed2e18');
  113. });
  114. it('should add warn fill', function() {
  115. var markings = ctx.plotOptions.grid.markings;
  116. expect(markings[2].yaxis.from).to.be(100);
  117. expect(markings[2].yaxis.to).to.be(200);
  118. expect(markings[2].color).to.be('rgba(216, 200, 27, 0.10)');
  119. });
  120. it('should add warn line', function() {
  121. var markings = ctx.plotOptions.grid.markings;
  122. expect(markings[3].yaxis.from).to.be(100);
  123. expect(markings[3].yaxis.to).to.be(100);
  124. expect(markings[3].color).to.be('#F79520');
  125. });
  126. });
  127. graphScenario('inverted grid thresholds 200, 100', function(ctx) {
  128. ctx.setup(function(ctrl) {
  129. ctrl.panel.alert = {
  130. warn: { op: "<", value: 200},
  131. crit: { op: "<", value: 100}
  132. };
  133. });
  134. it('should add crit fill', function() {
  135. var markings = ctx.plotOptions.grid.markings;
  136. expect(markings[0].yaxis.from).to.be(100);
  137. expect(markings[0].yaxis.to).to.be(-Infinity);
  138. expect(markings[0].color).to.be('rgba(234, 112, 112, 0.10)');
  139. });
  140. it('should add crit line', function() {
  141. var markings = ctx.plotOptions.grid.markings;
  142. expect(markings[1].yaxis.from).to.be(100);
  143. expect(markings[1].yaxis.to).to.be(100);
  144. expect(markings[1].color).to.be('#ed2e18');
  145. });
  146. it('should add warn fill', function() {
  147. var markings = ctx.plotOptions.grid.markings;
  148. expect(markings[2].yaxis.from).to.be(200);
  149. expect(markings[2].yaxis.to).to.be(100);
  150. expect(markings[2].color).to.be('rgba(216, 200, 27, 0.10)');
  151. });
  152. it('should add warn line', function() {
  153. var markings = ctx.plotOptions.grid.markings;
  154. expect(markings[3].yaxis.from).to.be(200);
  155. expect(markings[3].yaxis.to).to.be(200);
  156. expect(markings[3].color).to.be('#F79520');
  157. });
  158. });
  159. graphScenario('grid warn thresholds from zero', function(ctx) {
  160. ctx.setup(function(ctrl) {
  161. ctrl.panel.alert = {
  162. warn: { op: ">", value: 0},
  163. crit: { op: ">", value: undefined}
  164. };
  165. });
  166. it('should add warn fill', function() {
  167. var markings = ctx.plotOptions.grid.markings;
  168. expect(markings[0].yaxis.from).to.be(0);
  169. expect(markings[0].yaxis.to).to.be(Infinity);
  170. expect(markings[0].color).to.be('rgba(216, 200, 27, 0.10)');
  171. });
  172. it('should add warn line', function() {
  173. var markings = ctx.plotOptions.grid.markings;
  174. expect(markings[1].yaxis.from).to.be(0);
  175. expect(markings[1].yaxis.to).to.be(0);
  176. expect(markings[1].color).to.be('#F79520');
  177. });
  178. });
  179. graphScenario('when logBase is log 10', function(ctx) {
  180. ctx.setup(function(ctrl) {
  181. ctrl.panel.yaxes[0].logBase = 10;
  182. });
  183. it('should apply axis transform and ticks', function() {
  184. var axis = ctx.plotOptions.yaxes[0];
  185. expect(axis.transform(100)).to.be(Math.log(100+0.1));
  186. expect(axis.ticks[0]).to.be(0);
  187. expect(axis.ticks[1]).to.be(1);
  188. });
  189. });
  190. graphScenario('should use timeStep for barWidth', function(ctx) {
  191. ctx.setup(function(ctrl, data) {
  192. ctrl.panel.bars = true;
  193. data[0] = new TimeSeries({
  194. datapoints: [[1,10],[2,20]],
  195. alias: 'series1',
  196. });
  197. });
  198. it('should set barWidth', function() {
  199. expect(ctx.plotOptions.series.bars.barWidth).to.be(10/1.5);
  200. });
  201. });
  202. graphScenario('series option overrides, fill & points', function(ctx) {
  203. ctx.setup(function(ctrl, data) {
  204. ctrl.panel.lines = true;
  205. ctrl.panel.fill = 5;
  206. data[0].zindex = 10;
  207. data[1].alias = 'test';
  208. data[1].lines = {fill: 0.001};
  209. data[1].points = {show: true};
  210. });
  211. it('should match second series and fill zero, and enable points', function() {
  212. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  213. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  214. expect(ctx.plotData[1].points.show).to.be(true);
  215. });
  216. });
  217. graphScenario('should order series order according to zindex', function(ctx) {
  218. ctx.setup(function(ctrl, data) {
  219. data[1].zindex = 1;
  220. data[0].zindex = 10;
  221. });
  222. it('should move zindex 2 last', function() {
  223. expect(ctx.plotData[0].alias).to.be('series2');
  224. expect(ctx.plotData[1].alias).to.be('series1');
  225. });
  226. });
  227. graphScenario('when series is hidden', function(ctx) {
  228. ctx.setup(function(ctrl) {
  229. ctrl.hiddenSeries = {'series2': true};
  230. });
  231. it('should remove datapoints and disable stack', function() {
  232. expect(ctx.plotData[0].alias).to.be('series1');
  233. expect(ctx.plotData[1].data.length).to.be(0);
  234. expect(ctx.plotData[1].stack).to.be(false);
  235. });
  236. });
  237. graphScenario('when stack and percent', function(ctx) {
  238. ctx.setup(function(ctrl) {
  239. ctrl.panel.percentage = true;
  240. ctrl.panel.stack = true;
  241. });
  242. it('should show percentage', function() {
  243. var axis = ctx.plotOptions.yaxes[0];
  244. expect(axis.tickFormatter(100, axis)).to.be("100%");
  245. });
  246. });
  247. graphScenario('when panel too narrow to show x-axis dates in same granularity as wide panels', function(ctx) {
  248. describe('and the range is less than 24 hours', function() {
  249. ctx.setup(function(ctrl) {
  250. ctrl.range.from = moment([2015, 1, 1, 10]);
  251. ctrl.range.to = moment([2015, 1, 1, 22]);
  252. });
  253. it('should format dates as hours minutes', function() {
  254. var axis = ctx.plotOptions.xaxis;
  255. expect(axis.timeformat).to.be('%H:%M');
  256. });
  257. });
  258. describe('and the range is less than one year', function() {
  259. ctx.setup(function(scope) {
  260. scope.range.from = moment([2015, 1, 1]);
  261. scope.range.to = moment([2015, 11, 20]);
  262. });
  263. it('should format dates as month days', function() {
  264. var axis = ctx.plotOptions.xaxis;
  265. expect(axis.timeformat).to.be('%m/%d');
  266. });
  267. });
  268. }, 10);
  269. });