graph.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. jest.mock('app/features/annotations/all', () => ({
  2. EventManager: function() {
  3. return {
  4. on: () => {},
  5. addFlotEvents: () => {},
  6. };
  7. },
  8. }));
  9. jest.mock('app/core/core', () => ({
  10. coreModule: {
  11. directive: () => {},
  12. },
  13. appEvents: {
  14. on: () => {},
  15. },
  16. }));
  17. import '../module';
  18. import { GraphCtrl } from '../module';
  19. import { MetricsPanelCtrl } from 'app/features/panel/metrics_panel_ctrl';
  20. import { PanelCtrl } from 'app/features/panel/panel_ctrl';
  21. import config from 'app/core/config';
  22. import TimeSeries from 'app/core/time_series2';
  23. import moment from 'moment';
  24. import $ from 'jquery';
  25. import { graphDirective } from '../graph';
  26. const ctx = {} as any;
  27. let ctrl;
  28. const scope = {
  29. ctrl: {},
  30. range: {
  31. from: moment([2015, 1, 1]),
  32. to: moment([2015, 11, 20]),
  33. },
  34. $on: () => {},
  35. };
  36. let link;
  37. describe('grafanaGraph', function() {
  38. const setupCtx = (beforeRender?) => {
  39. config.bootData = {
  40. user: {
  41. lightTheme: false,
  42. },
  43. };
  44. GraphCtrl.prototype = {
  45. ...MetricsPanelCtrl.prototype,
  46. ...PanelCtrl.prototype,
  47. ...GraphCtrl.prototype,
  48. height: 200,
  49. panel: {
  50. events: {
  51. on: () => {},
  52. },
  53. legend: {},
  54. grid: {},
  55. yaxes: [
  56. {
  57. min: null,
  58. max: null,
  59. format: 'short',
  60. logBase: 1,
  61. },
  62. {
  63. min: null,
  64. max: null,
  65. format: 'short',
  66. logBase: 1,
  67. },
  68. ],
  69. thresholds: [],
  70. xaxis: {},
  71. seriesOverrides: [],
  72. tooltip: {
  73. shared: true,
  74. },
  75. },
  76. renderingCompleted: jest.fn(),
  77. hiddenSeries: {},
  78. dashboard: {
  79. getTimezone: () => 'browser',
  80. },
  81. range: {
  82. from: moment([2015, 1, 1, 10]),
  83. to: moment([2015, 1, 1, 22]),
  84. },
  85. } as any;
  86. ctx.data = [];
  87. ctx.data.push(
  88. new TimeSeries({
  89. datapoints: [[1, 1], [2, 2]],
  90. alias: 'series1',
  91. })
  92. );
  93. ctx.data.push(
  94. new TimeSeries({
  95. datapoints: [[10, 1], [20, 2]],
  96. alias: 'series2',
  97. })
  98. );
  99. ctrl = new GraphCtrl(
  100. {
  101. $on: () => {},
  102. },
  103. {
  104. get: () => {},
  105. },
  106. {}
  107. );
  108. $.plot = ctrl.plot = jest.fn();
  109. scope.ctrl = ctrl;
  110. link = graphDirective({}, {}, {}).link(scope, { width: () => 500, mouseleave: () => {}, bind: () => {} });
  111. if (typeof beforeRender === 'function') {
  112. beforeRender();
  113. }
  114. link.data = ctx.data;
  115. //Emulate functions called by event listeners
  116. link.buildFlotPairs(link.data);
  117. link.render_panel();
  118. ctx.plotData = ctrl.plot.mock.calls[0][1];
  119. ctx.plotOptions = ctrl.plot.mock.calls[0][2];
  120. };
  121. describe('simple lines options', () => {
  122. beforeEach(() => {
  123. setupCtx(() => {
  124. ctrl.panel.lines = true;
  125. ctrl.panel.fill = 5;
  126. ctrl.panel.linewidth = 3;
  127. ctrl.panel.steppedLine = true;
  128. });
  129. });
  130. it('should configure plot with correct options', () => {
  131. expect(ctx.plotOptions.series.lines.show).toBe(true);
  132. expect(ctx.plotOptions.series.lines.fill).toBe(0.5);
  133. expect(ctx.plotOptions.series.lines.lineWidth).toBe(3);
  134. expect(ctx.plotOptions.series.lines.steps).toBe(true);
  135. });
  136. });
  137. describe('sorting stacked series as legend. disabled', () => {
  138. beforeEach(() => {
  139. setupCtx(() => {
  140. ctrl.panel.legend.sort = undefined;
  141. ctrl.panel.stack = false;
  142. });
  143. });
  144. it('should not modify order of time series', () => {
  145. expect(ctx.plotData[0].alias).toBe('series1');
  146. expect(ctx.plotData[1].alias).toBe('series2');
  147. });
  148. });
  149. describe('sorting stacked series as legend. min descending order', () => {
  150. beforeEach(() => {
  151. setupCtx(() => {
  152. ctrl.panel.legend.sort = 'min';
  153. ctrl.panel.legend.sortDesc = true;
  154. ctrl.panel.stack = true;
  155. });
  156. });
  157. it('highest value should be first', () => {
  158. expect(ctx.plotData[0].alias).toBe('series2');
  159. expect(ctx.plotData[1].alias).toBe('series1');
  160. });
  161. });
  162. describe('sorting stacked series as legend. min ascending order', () => {
  163. beforeEach(() => {
  164. setupCtx(() => {
  165. ctrl.panel.legend.sort = 'min';
  166. ctrl.panel.legend.sortDesc = false;
  167. ctrl.panel.stack = true;
  168. });
  169. });
  170. it('lowest value should be first', () => {
  171. expect(ctx.plotData[0].alias).toBe('series1');
  172. expect(ctx.plotData[1].alias).toBe('series2');
  173. });
  174. });
  175. describe('sorting stacked series as legend. stacking disabled', () => {
  176. beforeEach(() => {
  177. setupCtx(() => {
  178. ctrl.panel.legend.sort = 'min';
  179. ctrl.panel.legend.sortDesc = true;
  180. ctrl.panel.stack = false;
  181. });
  182. });
  183. it('highest value should be first', () => {
  184. expect(ctx.plotData[0].alias).toBe('series1');
  185. expect(ctx.plotData[1].alias).toBe('series2');
  186. });
  187. });
  188. describe('sorting stacked series as legend. current descending order', () => {
  189. beforeEach(() => {
  190. setupCtx(() => {
  191. ctrl.panel.legend.sort = 'current';
  192. ctrl.panel.legend.sortDesc = true;
  193. ctrl.panel.stack = true;
  194. });
  195. });
  196. it('highest last value should be first', () => {
  197. expect(ctx.plotData[0].alias).toBe('series2');
  198. expect(ctx.plotData[1].alias).toBe('series1');
  199. });
  200. });
  201. describe('when logBase is log 10', () => {
  202. beforeEach(() => {
  203. setupCtx(() => {
  204. ctx.data[0] = new TimeSeries({
  205. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  206. alias: 'seriesAutoscale',
  207. });
  208. ctx.data[0].yaxis = 1;
  209. ctx.data[1] = new TimeSeries({
  210. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  211. alias: 'seriesFixedscale',
  212. });
  213. ctx.data[1].yaxis = 2;
  214. ctrl.panel.yaxes[0].logBase = 10;
  215. ctrl.panel.yaxes[1].logBase = 10;
  216. ctrl.panel.yaxes[1].min = '0.05';
  217. ctrl.panel.yaxes[1].max = '1500';
  218. });
  219. });
  220. it('should apply axis transform, autoscaling (if necessary) and ticks', function() {
  221. const axisAutoscale = ctx.plotOptions.yaxes[0];
  222. expect(axisAutoscale.transform(100)).toBe(2);
  223. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  224. expect(axisAutoscale.min).toBeCloseTo(0.001);
  225. expect(axisAutoscale.max).toBe(10000);
  226. expect(axisAutoscale.ticks.length).toBeCloseTo(8);
  227. expect(axisAutoscale.ticks[0]).toBeCloseTo(0.001);
  228. if (axisAutoscale.ticks.length === 7) {
  229. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).toBeCloseTo(1000);
  230. } else {
  231. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).toBe(10000);
  232. }
  233. const axisFixedscale = ctx.plotOptions.yaxes[1];
  234. expect(axisFixedscale.min).toBe(0.05);
  235. expect(axisFixedscale.max).toBe(1500);
  236. expect(axisFixedscale.ticks.length).toBe(5);
  237. expect(axisFixedscale.ticks[0]).toBe(0.1);
  238. expect(axisFixedscale.ticks[4]).toBe(1000);
  239. });
  240. });
  241. describe('when logBase is log 10 and data points contain only zeroes', () => {
  242. beforeEach(() => {
  243. setupCtx(() => {
  244. ctrl.panel.yaxes[0].logBase = 10;
  245. ctx.data[0] = new TimeSeries({
  246. datapoints: [[0, 1], [0, 2], [0, 3], [0, 4]],
  247. alias: 'seriesAutoscale',
  248. });
  249. ctx.data[0].yaxis = 1;
  250. });
  251. });
  252. it('should not set min and max and should create some fake ticks', function() {
  253. const axisAutoscale = ctx.plotOptions.yaxes[0];
  254. expect(axisAutoscale.transform(100)).toBe(2);
  255. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  256. expect(axisAutoscale.min).toBe(undefined);
  257. expect(axisAutoscale.max).toBe(undefined);
  258. expect(axisAutoscale.ticks.length).toBe(2);
  259. expect(axisAutoscale.ticks[0]).toBe(1);
  260. expect(axisAutoscale.ticks[1]).toBe(2);
  261. });
  262. });
  263. // y-min set 0 is a special case for log scale,
  264. // this approximates it by setting min to 0.1
  265. describe('when logBase is log 10 and y-min is set to 0 and auto min is > 0.1', () => {
  266. beforeEach(() => {
  267. setupCtx(() => {
  268. ctrl.panel.yaxes[0].logBase = 10;
  269. ctrl.panel.yaxes[0].min = '0';
  270. ctx.data[0] = new TimeSeries({
  271. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4]],
  272. alias: 'seriesAutoscale',
  273. });
  274. ctx.data[0].yaxis = 1;
  275. });
  276. });
  277. it('should set min to 0.1 and add a tick for 0.1', function() {
  278. const axisAutoscale = ctx.plotOptions.yaxes[0];
  279. expect(axisAutoscale.transform(100)).toBe(2);
  280. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  281. expect(axisAutoscale.min).toBe(0.1);
  282. expect(axisAutoscale.max).toBe(10000);
  283. expect(axisAutoscale.ticks.length).toBe(6);
  284. expect(axisAutoscale.ticks[0]).toBe(0.1);
  285. expect(axisAutoscale.ticks[5]).toBe(10000);
  286. });
  287. });
  288. describe('when logBase is log 2 and y-min is set to 0 and num of ticks exceeds max', () => {
  289. beforeEach(() => {
  290. setupCtx(() => {
  291. const heightForApprox5Ticks = 125;
  292. ctrl.height = heightForApprox5Ticks;
  293. ctrl.panel.yaxes[0].logBase = 2;
  294. ctrl.panel.yaxes[0].min = '0';
  295. ctx.data[0] = new TimeSeries({
  296. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4], [10000, 5], [100000, 6]],
  297. alias: 'seriesAutoscale',
  298. });
  299. ctx.data[0].yaxis = 1;
  300. });
  301. });
  302. it('should regenerate ticks so that if fits on the y-axis', function() {
  303. const axisAutoscale = ctx.plotOptions.yaxes[0];
  304. expect(axisAutoscale.min).toBe(0.1);
  305. expect(axisAutoscale.ticks.length).toBe(8);
  306. expect(axisAutoscale.ticks[0]).toBe(0.1);
  307. expect(axisAutoscale.ticks[7]).toBe(262144);
  308. expect(axisAutoscale.max).toBe(262144);
  309. });
  310. it('should set axis max to be max tick value', function() {
  311. expect(ctx.plotOptions.yaxes[0].max).toBe(262144);
  312. });
  313. });
  314. describe('dashed lines options', () => {
  315. beforeEach(() => {
  316. setupCtx(() => {
  317. ctrl.panel.lines = true;
  318. ctrl.panel.linewidth = 2;
  319. ctrl.panel.dashes = true;
  320. });
  321. });
  322. it('should configure dashed plot with correct options', function() {
  323. expect(ctx.plotOptions.series.lines.show).toBe(true);
  324. expect(ctx.plotOptions.series.dashes.lineWidth).toBe(2);
  325. expect(ctx.plotOptions.series.dashes.show).toBe(true);
  326. });
  327. });
  328. describe('should use timeStep for barWidth', () => {
  329. beforeEach(() => {
  330. setupCtx(() => {
  331. ctrl.panel.bars = true;
  332. ctx.data[0] = new TimeSeries({
  333. datapoints: [[1, 10], [2, 20]],
  334. alias: 'series1',
  335. });
  336. });
  337. });
  338. it('should set barWidth', function() {
  339. expect(ctx.plotOptions.series.bars.barWidth).toBe(1 / 1.5);
  340. });
  341. });
  342. describe('series option overrides, fill & points', () => {
  343. beforeEach(() => {
  344. setupCtx(() => {
  345. ctrl.panel.lines = true;
  346. ctrl.panel.fill = 5;
  347. ctx.data[0].zindex = 10;
  348. ctx.data[1].alias = 'test';
  349. ctx.data[1].lines = { fill: 0.001 };
  350. ctx.data[1].points = { show: true };
  351. });
  352. });
  353. it('should match second series and fill zero, and enable points', function() {
  354. expect(ctx.plotOptions.series.lines.fill).toBe(0.5);
  355. expect(ctx.plotData[1].lines.fill).toBe(0.001);
  356. expect(ctx.plotData[1].points.show).toBe(true);
  357. });
  358. });
  359. describe('should order series order according to zindex', () => {
  360. beforeEach(() => {
  361. setupCtx(() => {
  362. ctx.data[1].zindex = 1;
  363. ctx.data[0].zindex = 10;
  364. });
  365. });
  366. it('should move zindex 2 last', function() {
  367. expect(ctx.plotData[0].alias).toBe('series2');
  368. expect(ctx.plotData[1].alias).toBe('series1');
  369. });
  370. });
  371. describe('when series is hidden', () => {
  372. beforeEach(() => {
  373. setupCtx(() => {
  374. ctrl.hiddenSeries = { series2: true };
  375. });
  376. });
  377. it('should remove datapoints and disable stack', function() {
  378. expect(ctx.plotData[0].alias).toBe('series1');
  379. expect(ctx.plotData[1].data.length).toBe(0);
  380. expect(ctx.plotData[1].stack).toBe(false);
  381. });
  382. });
  383. describe('when stack and percent', () => {
  384. beforeEach(() => {
  385. setupCtx(() => {
  386. ctrl.panel.percentage = true;
  387. ctrl.panel.stack = true;
  388. });
  389. });
  390. it('should show percentage', function() {
  391. const axis = ctx.plotOptions.yaxes[0];
  392. expect(axis.tickFormatter(100, axis)).toBe('100%');
  393. });
  394. });
  395. describe('when panel too narrow to show x-axis dates in same granularity as wide panels', () => {
  396. //Set width to 10px
  397. describe('and the range is less than 24 hours', function() {
  398. beforeEach(() => {
  399. setupCtx(() => {
  400. ctrl.range.from = moment([2015, 1, 1, 10]);
  401. ctrl.range.to = moment([2015, 1, 1, 22]);
  402. });
  403. });
  404. it('should format dates as hours minutes', function() {
  405. const axis = ctx.plotOptions.xaxis;
  406. expect(axis.timeformat).toBe('%H:%M');
  407. });
  408. });
  409. describe('and the range is less than one year', function() {
  410. beforeEach(() => {
  411. setupCtx(() => {
  412. ctrl.range.from = moment([2015, 1, 1]);
  413. ctrl.range.to = moment([2015, 11, 20]);
  414. });
  415. });
  416. it('should format dates as month days', function() {
  417. const axis = ctx.plotOptions.xaxis;
  418. expect(axis.timeformat).toBe('%m/%d');
  419. });
  420. });
  421. });
  422. describe('when graph is histogram, and enable stack', () => {
  423. beforeEach(() => {
  424. setupCtx(() => {
  425. ctrl.panel.xaxis.mode = 'histogram';
  426. ctrl.panel.stack = true;
  427. ctrl.hiddenSeries = {};
  428. ctx.data[0] = new TimeSeries({
  429. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  430. alias: 'series1',
  431. });
  432. ctx.data[1] = new TimeSeries({
  433. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  434. alias: 'series2',
  435. });
  436. });
  437. });
  438. it('should calculate correct histogram', function() {
  439. expect(ctx.plotData[0].data[0][0]).toBe(100);
  440. expect(ctx.plotData[0].data[0][1]).toBe(2);
  441. expect(ctx.plotData[1].data[0][0]).toBe(100);
  442. expect(ctx.plotData[1].data[0][1]).toBe(2);
  443. });
  444. });
  445. describe('when graph is histogram, and some series are hidden', () => {
  446. beforeEach(() => {
  447. setupCtx(() => {
  448. ctrl.panel.xaxis.mode = 'histogram';
  449. ctrl.panel.stack = false;
  450. ctrl.hiddenSeries = { series2: true };
  451. ctx.data[0] = new TimeSeries({
  452. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  453. alias: 'series1',
  454. });
  455. ctx.data[1] = new TimeSeries({
  456. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  457. alias: 'series2',
  458. });
  459. });
  460. });
  461. it('should calculate correct histogram', function() {
  462. expect(ctx.plotData[0].data[0][0]).toBe(100);
  463. expect(ctx.plotData[0].data[0][1]).toBe(2);
  464. });
  465. });
  466. });