time_series.jest.ts 9.3 KB

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