time_series.jest.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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([{ alias: 'test', bars: true, lines: false }]);
  173. });
  174. it('should disable lines, and enable bars', function() {
  175. expect(series.lines.show).toBe(false);
  176. expect(series.bars.show).toBe(true);
  177. });
  178. });
  179. describe('series option overrides, linewidth, stack', function() {
  180. beforeEach(function() {
  181. series.alias = 'test';
  182. series.applySeriesOverrides([{ alias: 'test', linewidth: 5, stack: false }]);
  183. });
  184. it('should disable stack, and set lineWidth', function() {
  185. expect(series.stack).toBe(false);
  186. expect(series.lines.lineWidth).toBe(5);
  187. });
  188. });
  189. describe('series option overrides, dashes and lineWidth', function() {
  190. beforeEach(function() {
  191. series.alias = 'test';
  192. series.applySeriesOverrides([{ alias: 'test', linewidth: 5, dashes: true }]);
  193. });
  194. it('should enable dashes, set dashes lineWidth to 5 and lines lineWidth to 0', function() {
  195. expect(series.dashes.show).toBe(true);
  196. expect(series.dashes.lineWidth).toBe(5);
  197. expect(series.lines.lineWidth).toBe(0);
  198. });
  199. });
  200. describe('series option overrides, fill below to', function() {
  201. beforeEach(function() {
  202. series.alias = 'test';
  203. series.applySeriesOverrides([{ alias: 'test', fillBelowTo: 'min' }]);
  204. });
  205. it('should disable line fill and add fillBelowTo', function() {
  206. expect(series.fillBelowTo).toBe('min');
  207. });
  208. });
  209. describe('series option overrides, pointradius, steppedLine', function() {
  210. beforeEach(function() {
  211. series.alias = 'test';
  212. series.applySeriesOverrides([{ alias: 'test', pointradius: 5, steppedLine: true }]);
  213. });
  214. it('should set pointradius, and set steppedLine', function() {
  215. expect(series.points.radius).toBe(5);
  216. expect(series.lines.steps).toBe(true);
  217. });
  218. });
  219. describe('override match on regex', function() {
  220. beforeEach(function() {
  221. series.alias = 'test_01';
  222. series.applySeriesOverrides([{ alias: '/.*01/', lines: false }]);
  223. });
  224. it('should match second series', function() {
  225. expect(series.lines.show).toBe(false);
  226. });
  227. });
  228. describe('override series y-axis, and z-index', function() {
  229. beforeEach(function() {
  230. series.alias = 'test';
  231. series.applySeriesOverrides([{ alias: 'test', yaxis: 2, zindex: 2 }]);
  232. });
  233. it('should set yaxis', function() {
  234. expect(series.yaxis).toBe(2);
  235. });
  236. it('should set zindex', function() {
  237. expect(series.zindex).toBe(2);
  238. });
  239. });
  240. });
  241. describe('value formatter', function() {
  242. var series;
  243. beforeEach(function() {
  244. series = new TimeSeries(testData);
  245. });
  246. it('should format non-numeric values as empty string', function() {
  247. expect(series.formatValue(null)).toBe('');
  248. expect(series.formatValue(undefined)).toBe('');
  249. expect(series.formatValue(NaN)).toBe('');
  250. expect(series.formatValue(Infinity)).toBe('');
  251. expect(series.formatValue(-Infinity)).toBe('');
  252. });
  253. });
  254. });