graph.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. jest.mock('app/features/annotations/all', () => ({
  2. EventManager: () => {
  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', () => {
  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. // @ts-ignore
  109. $.plot = ctrl.plot = jest.fn();
  110. scope.ctrl = ctrl;
  111. link = graphDirective({}, {}, {}).link(scope, { width: () => 500, mouseleave: () => {}, bind: () => {} });
  112. if (typeof beforeRender === 'function') {
  113. beforeRender();
  114. }
  115. link.data = ctx.data;
  116. //Emulate functions called by event listeners
  117. link.buildFlotPairs(link.data);
  118. link.renderPanel();
  119. ctx.plotData = ctrl.plot.mock.calls[0][1];
  120. ctx.plotOptions = ctrl.plot.mock.calls[0][2];
  121. };
  122. describe('simple lines options', () => {
  123. beforeEach(() => {
  124. setupCtx(() => {
  125. ctrl.panel.lines = true;
  126. ctrl.panel.fill = 5;
  127. ctrl.panel.linewidth = 3;
  128. ctrl.panel.steppedLine = true;
  129. });
  130. });
  131. it('should configure plot with correct options', () => {
  132. expect(ctx.plotOptions.series.lines.show).toBe(true);
  133. expect(ctx.plotOptions.series.lines.fill).toBe(0.5);
  134. expect(ctx.plotOptions.series.lines.lineWidth).toBe(3);
  135. expect(ctx.plotOptions.series.lines.steps).toBe(true);
  136. });
  137. });
  138. describe('sorting stacked series as legend. disabled', () => {
  139. beforeEach(() => {
  140. setupCtx(() => {
  141. ctrl.panel.legend.sort = undefined;
  142. ctrl.panel.stack = false;
  143. });
  144. });
  145. it('should not modify order of time series', () => {
  146. expect(ctx.plotData[0].alias).toBe('series1');
  147. expect(ctx.plotData[1].alias).toBe('series2');
  148. });
  149. });
  150. describe('sorting stacked series as legend. min descending order', () => {
  151. beforeEach(() => {
  152. setupCtx(() => {
  153. ctrl.panel.legend.sort = 'min';
  154. ctrl.panel.legend.sortDesc = true;
  155. ctrl.panel.stack = true;
  156. });
  157. });
  158. it('highest value should be first', () => {
  159. expect(ctx.plotData[0].alias).toBe('series2');
  160. expect(ctx.plotData[1].alias).toBe('series1');
  161. });
  162. });
  163. describe('sorting stacked series as legend. min ascending order', () => {
  164. beforeEach(() => {
  165. setupCtx(() => {
  166. ctrl.panel.legend.sort = 'min';
  167. ctrl.panel.legend.sortDesc = false;
  168. ctrl.panel.stack = true;
  169. });
  170. });
  171. it('lowest value should be first', () => {
  172. expect(ctx.plotData[0].alias).toBe('series1');
  173. expect(ctx.plotData[1].alias).toBe('series2');
  174. });
  175. });
  176. describe('sorting stacked series as legend. stacking disabled', () => {
  177. beforeEach(() => {
  178. setupCtx(() => {
  179. ctrl.panel.legend.sort = 'min';
  180. ctrl.panel.legend.sortDesc = true;
  181. ctrl.panel.stack = false;
  182. });
  183. });
  184. it('highest value should be first', () => {
  185. expect(ctx.plotData[0].alias).toBe('series1');
  186. expect(ctx.plotData[1].alias).toBe('series2');
  187. });
  188. });
  189. describe('sorting stacked series as legend. current descending order', () => {
  190. beforeEach(() => {
  191. setupCtx(() => {
  192. ctrl.panel.legend.sort = 'current';
  193. ctrl.panel.legend.sortDesc = true;
  194. ctrl.panel.stack = true;
  195. });
  196. });
  197. it('highest last value should be first', () => {
  198. expect(ctx.plotData[0].alias).toBe('series2');
  199. expect(ctx.plotData[1].alias).toBe('series1');
  200. });
  201. });
  202. describe('when logBase is log 10', () => {
  203. beforeEach(() => {
  204. setupCtx(() => {
  205. ctx.data[0] = new TimeSeries({
  206. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  207. alias: 'seriesAutoscale',
  208. });
  209. ctx.data[0].yaxis = 1;
  210. ctx.data[1] = new TimeSeries({
  211. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  212. alias: 'seriesFixedscale',
  213. });
  214. ctx.data[1].yaxis = 2;
  215. ctrl.panel.yaxes[0].logBase = 10;
  216. ctrl.panel.yaxes[1].logBase = 10;
  217. ctrl.panel.yaxes[1].min = '0.05';
  218. ctrl.panel.yaxes[1].max = '1500';
  219. });
  220. });
  221. it('should apply axis transform, autoscaling (if necessary) and ticks', () => {
  222. const axisAutoscale = ctx.plotOptions.yaxes[0];
  223. expect(axisAutoscale.transform(100)).toBe(2);
  224. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  225. expect(axisAutoscale.min).toBeCloseTo(0.001);
  226. expect(axisAutoscale.max).toBe(10000);
  227. expect(axisAutoscale.ticks.length).toBeCloseTo(8);
  228. expect(axisAutoscale.ticks[0]).toBeCloseTo(0.001);
  229. if (axisAutoscale.ticks.length === 7) {
  230. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).toBeCloseTo(1000);
  231. } else {
  232. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).toBe(10000);
  233. }
  234. const axisFixedscale = ctx.plotOptions.yaxes[1];
  235. expect(axisFixedscale.min).toBe(0.05);
  236. expect(axisFixedscale.max).toBe(1500);
  237. expect(axisFixedscale.ticks.length).toBe(5);
  238. expect(axisFixedscale.ticks[0]).toBe(0.1);
  239. expect(axisFixedscale.ticks[4]).toBe(1000);
  240. });
  241. });
  242. describe('when logBase is log 10 and data points contain only zeroes', () => {
  243. beforeEach(() => {
  244. setupCtx(() => {
  245. ctrl.panel.yaxes[0].logBase = 10;
  246. ctx.data[0] = new TimeSeries({
  247. datapoints: [[0, 1], [0, 2], [0, 3], [0, 4]],
  248. alias: 'seriesAutoscale',
  249. });
  250. ctx.data[0].yaxis = 1;
  251. });
  252. });
  253. it('should not set min and max and should create some fake ticks', () => {
  254. const axisAutoscale = ctx.plotOptions.yaxes[0];
  255. expect(axisAutoscale.transform(100)).toBe(2);
  256. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  257. expect(axisAutoscale.min).toBe(undefined);
  258. expect(axisAutoscale.max).toBe(undefined);
  259. expect(axisAutoscale.ticks.length).toBe(2);
  260. expect(axisAutoscale.ticks[0]).toBe(1);
  261. expect(axisAutoscale.ticks[1]).toBe(2);
  262. });
  263. });
  264. // y-min set 0 is a special case for log scale,
  265. // this approximates it by setting min to 0.1
  266. describe('when logBase is log 10 and y-min is set to 0 and auto min is > 0.1', () => {
  267. beforeEach(() => {
  268. setupCtx(() => {
  269. ctrl.panel.yaxes[0].logBase = 10;
  270. ctrl.panel.yaxes[0].min = '0';
  271. ctx.data[0] = new TimeSeries({
  272. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4]],
  273. alias: 'seriesAutoscale',
  274. });
  275. ctx.data[0].yaxis = 1;
  276. });
  277. });
  278. it('should set min to 0.1 and add a tick for 0.1', () => {
  279. const axisAutoscale = ctx.plotOptions.yaxes[0];
  280. expect(axisAutoscale.transform(100)).toBe(2);
  281. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  282. expect(axisAutoscale.min).toBe(0.1);
  283. expect(axisAutoscale.max).toBe(10000);
  284. expect(axisAutoscale.ticks.length).toBe(6);
  285. expect(axisAutoscale.ticks[0]).toBe(0.1);
  286. expect(axisAutoscale.ticks[5]).toBe(10000);
  287. });
  288. });
  289. describe('when logBase is log 2 and y-min is set to 0 and num of ticks exceeds max', () => {
  290. beforeEach(() => {
  291. setupCtx(() => {
  292. const heightForApprox5Ticks = 125;
  293. ctrl.height = heightForApprox5Ticks;
  294. ctrl.panel.yaxes[0].logBase = 2;
  295. ctrl.panel.yaxes[0].min = '0';
  296. ctx.data[0] = new TimeSeries({
  297. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4], [10000, 5], [100000, 6]],
  298. alias: 'seriesAutoscale',
  299. });
  300. ctx.data[0].yaxis = 1;
  301. });
  302. });
  303. it('should regenerate ticks so that if fits on the y-axis', () => {
  304. const axisAutoscale = ctx.plotOptions.yaxes[0];
  305. expect(axisAutoscale.min).toBe(0.1);
  306. expect(axisAutoscale.ticks.length).toBe(8);
  307. expect(axisAutoscale.ticks[0]).toBe(0.1);
  308. expect(axisAutoscale.ticks[7]).toBe(262144);
  309. expect(axisAutoscale.max).toBe(262144);
  310. });
  311. it('should set axis max to be max tick value', () => {
  312. expect(ctx.plotOptions.yaxes[0].max).toBe(262144);
  313. });
  314. });
  315. describe('dashed lines options', () => {
  316. beforeEach(() => {
  317. setupCtx(() => {
  318. ctrl.panel.lines = true;
  319. ctrl.panel.linewidth = 2;
  320. ctrl.panel.dashes = true;
  321. });
  322. });
  323. it('should configure dashed plot with correct options', () => {
  324. expect(ctx.plotOptions.series.lines.show).toBe(true);
  325. expect(ctx.plotOptions.series.dashes.lineWidth).toBe(2);
  326. expect(ctx.plotOptions.series.dashes.show).toBe(true);
  327. });
  328. });
  329. describe('should use timeStep for barWidth', () => {
  330. beforeEach(() => {
  331. setupCtx(() => {
  332. ctrl.panel.bars = true;
  333. ctx.data[0] = new TimeSeries({
  334. datapoints: [[1, 10], [2, 20]],
  335. alias: 'series1',
  336. });
  337. });
  338. });
  339. it('should set barWidth', () => {
  340. expect(ctx.plotOptions.series.bars.barWidth).toBe(1 / 1.5);
  341. });
  342. });
  343. describe('series option overrides, fill & points', () => {
  344. beforeEach(() => {
  345. setupCtx(() => {
  346. ctrl.panel.lines = true;
  347. ctrl.panel.fill = 5;
  348. ctx.data[0].zindex = 10;
  349. ctx.data[1].alias = 'test';
  350. ctx.data[1].lines = { fill: 0.001 };
  351. ctx.data[1].points = { show: true };
  352. });
  353. });
  354. it('should match second series and fill zero, and enable points', () => {
  355. expect(ctx.plotOptions.series.lines.fill).toBe(0.5);
  356. expect(ctx.plotData[1].lines.fill).toBe(0.001);
  357. expect(ctx.plotData[1].points.show).toBe(true);
  358. });
  359. });
  360. describe('should order series order according to zindex', () => {
  361. beforeEach(() => {
  362. setupCtx(() => {
  363. ctx.data[1].zindex = 1;
  364. ctx.data[0].zindex = 10;
  365. });
  366. });
  367. it('should move zindex 2 last', () => {
  368. expect(ctx.plotData[0].alias).toBe('series2');
  369. expect(ctx.plotData[1].alias).toBe('series1');
  370. });
  371. });
  372. describe('when series is hidden', () => {
  373. beforeEach(() => {
  374. setupCtx(() => {
  375. ctrl.hiddenSeries = { series2: true };
  376. });
  377. });
  378. it('should remove datapoints and disable stack', () => {
  379. expect(ctx.plotData[0].alias).toBe('series1');
  380. expect(ctx.plotData[1].data.length).toBe(0);
  381. expect(ctx.plotData[1].stack).toBe(false);
  382. });
  383. });
  384. describe('when stack and percent', () => {
  385. beforeEach(() => {
  386. setupCtx(() => {
  387. ctrl.panel.percentage = true;
  388. ctrl.panel.stack = true;
  389. });
  390. });
  391. it('should show percentage', () => {
  392. const axis = ctx.plotOptions.yaxes[0];
  393. expect(axis.tickFormatter(100, axis)).toBe('100%');
  394. });
  395. });
  396. describe('when panel too narrow to show x-axis dates in same granularity as wide panels', () => {
  397. //Set width to 10px
  398. describe('and the range is less than 24 hours', () => {
  399. beforeEach(() => {
  400. setupCtx(() => {
  401. ctrl.range.from = moment([2015, 1, 1, 10]);
  402. ctrl.range.to = moment([2015, 1, 1, 22]);
  403. });
  404. });
  405. it('should format dates as hours minutes', () => {
  406. const axis = ctx.plotOptions.xaxis;
  407. expect(axis.timeformat).toBe('%H:%M');
  408. });
  409. });
  410. describe('and the range is less than one year', () => {
  411. beforeEach(() => {
  412. setupCtx(() => {
  413. ctrl.range.from = moment([2015, 1, 1]);
  414. ctrl.range.to = moment([2015, 11, 20]);
  415. });
  416. });
  417. it('should format dates as month days', () => {
  418. const axis = ctx.plotOptions.xaxis;
  419. expect(axis.timeformat).toBe('%m/%d');
  420. });
  421. });
  422. });
  423. describe('when graph is histogram, and enable stack', () => {
  424. beforeEach(() => {
  425. setupCtx(() => {
  426. ctrl.panel.xaxis.mode = 'histogram';
  427. ctrl.panel.stack = true;
  428. ctrl.hiddenSeries = {};
  429. ctx.data[0] = new TimeSeries({
  430. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  431. alias: 'series1',
  432. });
  433. ctx.data[1] = new TimeSeries({
  434. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  435. alias: 'series2',
  436. });
  437. });
  438. });
  439. it('should calculate correct histogram', () => {
  440. expect(ctx.plotData[0].data[0][0]).toBe(100);
  441. expect(ctx.plotData[0].data[0][1]).toBe(2);
  442. expect(ctx.plotData[1].data[0][0]).toBe(100);
  443. expect(ctx.plotData[1].data[0][1]).toBe(2);
  444. });
  445. });
  446. describe('when graph is histogram, and some series are hidden', () => {
  447. beforeEach(() => {
  448. setupCtx(() => {
  449. ctrl.panel.xaxis.mode = 'histogram';
  450. ctrl.panel.stack = false;
  451. ctrl.hiddenSeries = { series2: true };
  452. ctx.data[0] = new TimeSeries({
  453. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  454. alias: 'series1',
  455. });
  456. ctx.data[1] = new TimeSeries({
  457. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  458. alias: 'series2',
  459. });
  460. });
  461. });
  462. it('should calculate correct histogram', () => {
  463. expect(ctx.plotData[0].data[0][0]).toBe(100);
  464. expect(ctx.plotData[0].data[0][1]).toBe(2);
  465. });
  466. });
  467. describe('when graph is histogram, and xaxis min is set', () => {
  468. beforeEach(() => {
  469. setupCtx(() => {
  470. ctrl.panel.xaxis.mode = 'histogram';
  471. ctrl.panel.xaxis.min = 150;
  472. ctrl.panel.stack = false;
  473. ctrl.hiddenSeries = {};
  474. ctx.data[0] = new TimeSeries({
  475. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  476. alias: 'series1',
  477. });
  478. });
  479. });
  480. it('should not contain values lower than min', () => {
  481. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  482. expect(Math.min.apply(Math, nonZero.map(t => t[0])) === 200);
  483. expect(Math.max.apply(Math, nonZero.map(t => t[0])) === 300);
  484. });
  485. });
  486. describe('when graph is histogram, and xaxis max is set', () => {
  487. beforeEach(() => {
  488. setupCtx(() => {
  489. ctrl.panel.xaxis.mode = 'histogram';
  490. ctrl.panel.xaxis.max = 250;
  491. ctrl.panel.stack = false;
  492. ctrl.hiddenSeries = {};
  493. ctx.data[0] = new TimeSeries({
  494. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  495. alias: 'series1',
  496. });
  497. });
  498. });
  499. it('should not contain values lower than min', () => {
  500. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  501. expect(Math.min.apply(Math, nonZero.map(t => t[0])) === 100);
  502. expect(Math.max.apply(Math, nonZero.map(t => t[0])) === 200);
  503. });
  504. });
  505. describe('when graph is histogram, and xaxis min and max are set', () => {
  506. beforeEach(() => {
  507. setupCtx(() => {
  508. ctrl.panel.xaxis.mode = 'histogram';
  509. ctrl.panel.xaxis.min = 150;
  510. ctrl.panel.xaxis.max = 250;
  511. ctrl.panel.stack = false;
  512. ctrl.hiddenSeries = {};
  513. ctx.data[0] = new TimeSeries({
  514. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  515. alias: 'series1',
  516. });
  517. });
  518. });
  519. it('should not contain values lower than min', () => {
  520. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  521. expect(Math.min.apply(Math, nonZero.map(t => t[0])) === 200);
  522. expect(Math.max.apply(Math, nonZero.map(t => t[0])) === 200);
  523. });
  524. });
  525. });