time_series.jest.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import TimeSeries from 'app/core/time_series2';
  2. describe('TimeSeries', function() {
  3. var points, series;
  4. var yAxisFormats = ['short', 'ms'];
  5. var testData;
  6. beforeEach(function() {
  7. testData = {
  8. alias: 'test',
  9. datapoints: [[1, 2], [null, 3], [10, 4], [8, 5]],
  10. };
  11. });
  12. describe('when getting flot pairs', function() {
  13. it('with connected style, should ignore nulls', function() {
  14. series = new TimeSeries(testData);
  15. points = series.getFlotPairs('connected', yAxisFormats);
  16. expect(points.length).toBe(3);
  17. });
  18. it('with null as zero style, should replace nulls with zero', function() {
  19. series = new TimeSeries(testData);
  20. points = series.getFlotPairs('null as zero', yAxisFormats);
  21. expect(points.length).toBe(4);
  22. expect(points[1][1]).toBe(0);
  23. });
  24. it('if last is null current should pick next to last', function() {
  25. series = new TimeSeries({
  26. datapoints: [[10, 1], [null, 2]],
  27. });
  28. series.getFlotPairs('null', yAxisFormats);
  29. expect(series.stats.current).toBe(10);
  30. });
  31. it('max value should work for negative values', function() {
  32. series = new TimeSeries({
  33. datapoints: [[-10, 1], [-4, 2]],
  34. });
  35. series.getFlotPairs('null', yAxisFormats);
  36. expect(series.stats.max).toBe(-4);
  37. });
  38. it('average value should ignore nulls', function() {
  39. series = new TimeSeries(testData);
  40. series.getFlotPairs('null', yAxisFormats);
  41. expect(series.stats.avg).toBe(6.333333333333333);
  42. });
  43. it('the delta value should account for nulls', function() {
  44. series = new TimeSeries({
  45. datapoints: [[1, 2], [3, 3], [null, 4], [10, 5], [15, 6]],
  46. });
  47. series.getFlotPairs('null', yAxisFormats);
  48. expect(series.stats.delta).toBe(14);
  49. });
  50. it('the delta value should account for nulls on first', function() {
  51. series = new TimeSeries({
  52. datapoints: [[null, 2], [1, 3], [10, 4], [15, 5]],
  53. });
  54. series.getFlotPairs('null', yAxisFormats);
  55. expect(series.stats.delta).toBe(14);
  56. });
  57. it('the delta value should account for nulls on last', function() {
  58. series = new TimeSeries({
  59. datapoints: [[1, 2], [5, 3], [10, 4], [null, 5]],
  60. });
  61. series.getFlotPairs('null', yAxisFormats);
  62. expect(series.stats.delta).toBe(9);
  63. });
  64. it('the delta value should account for resets', function() {
  65. series = new TimeSeries({
  66. datapoints: [[1, 2], [5, 3], [10, 4], [0, 5], [10, 6]],
  67. });
  68. series.getFlotPairs('null', yAxisFormats);
  69. expect(series.stats.delta).toBe(19);
  70. });
  71. it('the delta value should account for resets on last', function() {
  72. series = new TimeSeries({
  73. datapoints: [[1, 2], [2, 3], [10, 4], [8, 5]],
  74. });
  75. series.getFlotPairs('null', yAxisFormats);
  76. expect(series.stats.delta).toBe(17);
  77. });
  78. it('the range value should be max - min', function() {
  79. series = new TimeSeries(testData);
  80. series.getFlotPairs('null', yAxisFormats);
  81. expect(series.stats.range).toBe(9);
  82. });
  83. it('first value should ingone nulls', function() {
  84. series = new TimeSeries(testData);
  85. series.getFlotPairs('null', yAxisFormats);
  86. expect(series.stats.first).toBe(1);
  87. series = new TimeSeries({
  88. datapoints: [[null, 2], [1, 3], [10, 4], [8, 5]],
  89. });
  90. series.getFlotPairs('null', yAxisFormats);
  91. expect(series.stats.first).toBe(1);
  92. });
  93. it('with null as zero style, average value should treat nulls as 0', function() {
  94. series = new TimeSeries(testData);
  95. series.getFlotPairs('null as zero', yAxisFormats);
  96. expect(series.stats.avg).toBe(4.75);
  97. });
  98. it('average value should be null if all values is null', function() {
  99. series = new TimeSeries({
  100. datapoints: [[null, 2], [null, 3], [null, 4], [null, 5]],
  101. });
  102. series.getFlotPairs('null');
  103. expect(series.stats.avg).toBe(null);
  104. });
  105. });
  106. describe('When checking if ms resolution is needed', function() {
  107. describe('msResolution with second resolution timestamps', function() {
  108. beforeEach(function() {
  109. series = new TimeSeries({
  110. datapoints: [[45, 1234567890], [60, 1234567899]],
  111. });
  112. });
  113. it('should set hasMsResolution to false', function() {
  114. expect(series.hasMsResolution).toBe(false);
  115. });
  116. });
  117. describe('msResolution with millisecond resolution timestamps', function() {
  118. beforeEach(function() {
  119. series = new TimeSeries({
  120. datapoints: [[55, 1236547890001], [90, 1234456709000]],
  121. });
  122. });
  123. it('should show millisecond resolution tooltip', function() {
  124. expect(series.hasMsResolution).toBe(true);
  125. });
  126. });
  127. describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() {
  128. beforeEach(function() {
  129. series = new TimeSeries({
  130. datapoints: [[45, 1234567890000], [60, 1234567899000]],
  131. });
  132. });
  133. it('should not show millisecond resolution tooltip', function() {
  134. expect(series.hasMsResolution).toBe(false);
  135. });
  136. });
  137. });
  138. describe('can detect if series contains ms precision', function() {
  139. var fakedata;
  140. beforeEach(function() {
  141. fakedata = testData;
  142. });
  143. it('missing datapoint with ms precision', function() {
  144. fakedata.datapoints[0] = [1337, 1234567890000];
  145. series = new TimeSeries(fakedata);
  146. expect(series.isMsResolutionNeeded()).toBe(false);
  147. });
  148. it('contains datapoint with ms precision', function() {
  149. fakedata.datapoints[0] = [1337, 1236547890001];
  150. series = new TimeSeries(fakedata);
  151. expect(series.isMsResolutionNeeded()).toBe(true);
  152. });
  153. });
  154. describe('series overrides', function() {
  155. var series;
  156. beforeEach(function() {
  157. series = new TimeSeries(testData);
  158. });
  159. describe('fill & points', function() {
  160. beforeEach(function() {
  161. series.alias = 'test';
  162. series.applySeriesOverrides([{ alias: 'test', fill: 0, points: true }]);
  163. });
  164. it('should set fill zero, and enable points', function() {
  165. expect(series.lines.fill).toBe(0.001);
  166. expect(series.points.show).toBe(true);
  167. });
  168. });
  169. describe('series option overrides, bars, true & lines false', function() {
  170. beforeEach(function() {
  171. series.alias = 'test';
  172. series.applySeriesOverrides([
  173. { alias: 'test', bars: true, lines: false },
  174. ]);
  175. });
  176. it('should disable lines, and enable bars', function() {
  177. expect(series.lines.show).toBe(false);
  178. expect(series.bars.show).toBe(true);
  179. });
  180. });
  181. describe('series option overrides, linewidth, stack', function() {
  182. beforeEach(function() {
  183. series.alias = 'test';
  184. series.applySeriesOverrides([
  185. { alias: 'test', linewidth: 5, stack: false },
  186. ]);
  187. });
  188. it('should disable stack, and set lineWidth', function() {
  189. expect(series.stack).toBe(false);
  190. expect(series.lines.lineWidth).toBe(5);
  191. });
  192. });
  193. describe('series option overrides, dashes and lineWidth', function() {
  194. beforeEach(function() {
  195. series.alias = 'test';
  196. series.applySeriesOverrides([
  197. { alias: 'test', linewidth: 5, dashes: true },
  198. ]);
  199. });
  200. it('should enable dashes, set dashes lineWidth to 5 and lines lineWidth to 0', function() {
  201. expect(series.dashes.show).toBe(true);
  202. expect(series.dashes.lineWidth).toBe(5);
  203. expect(series.lines.lineWidth).toBe(0);
  204. });
  205. });
  206. describe('series option overrides, fill below to', function() {
  207. beforeEach(function() {
  208. series.alias = 'test';
  209. series.applySeriesOverrides([{ alias: 'test', fillBelowTo: 'min' }]);
  210. });
  211. it('should disable line fill and add fillBelowTo', function() {
  212. expect(series.fillBelowTo).toBe('min');
  213. });
  214. });
  215. describe('series option overrides, pointradius, steppedLine', function() {
  216. beforeEach(function() {
  217. series.alias = 'test';
  218. series.applySeriesOverrides([
  219. { alias: 'test', pointradius: 5, steppedLine: true },
  220. ]);
  221. });
  222. it('should set pointradius, and set steppedLine', function() {
  223. expect(series.points.radius).toBe(5);
  224. expect(series.lines.steps).toBe(true);
  225. });
  226. });
  227. describe('override match on regex', function() {
  228. beforeEach(function() {
  229. series.alias = 'test_01';
  230. series.applySeriesOverrides([{ alias: '/.*01/', lines: false }]);
  231. });
  232. it('should match second series', function() {
  233. expect(series.lines.show).toBe(false);
  234. });
  235. });
  236. describe('override series y-axis, and z-index', function() {
  237. beforeEach(function() {
  238. series.alias = 'test';
  239. series.applySeriesOverrides([{ alias: 'test', yaxis: 2, zindex: 2 }]);
  240. });
  241. it('should set yaxis', function() {
  242. expect(series.yaxis).toBe(2);
  243. });
  244. it('should set zindex', function() {
  245. expect(series.zindex).toBe(2);
  246. });
  247. });
  248. });
  249. describe('value formatter', function() {
  250. var series;
  251. beforeEach(function() {
  252. series = new TimeSeries(testData);
  253. });
  254. it('should format non-numeric values as empty string', function() {
  255. expect(series.formatValue(null)).toBe('');
  256. expect(series.formatValue(undefined)).toBe('');
  257. expect(series.formatValue(NaN)).toBe('');
  258. expect(series.formatValue(Infinity)).toBe('');
  259. expect(series.formatValue(-Infinity)).toBe('');
  260. });
  261. });
  262. });