time_series_specs.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. define([
  2. 'app/core/time_series'
  3. ], function(TimeSeries) {
  4. 'use strict';
  5. describe("TimeSeries", function() {
  6. var points, series;
  7. var yAxisFormats = ['short', 'ms'];
  8. var testData = {
  9. alias: 'test',
  10. datapoints: [
  11. [1,2],[null,3],[10,4],[8,5]
  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).to.be(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).to.be(4);
  24. expect(points[1][1]).to.be(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).to.be(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).to.be(-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).to.be(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).to.be(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).to.be(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).to.be(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).to.be(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).to.be(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).to.be(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).to.be(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).to.be(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).to.be(4.75);
  99. });
  100. });
  101. describe('When checking if ms resolution is needed', function() {
  102. describe('msResolution with second resolution timestamps', function() {
  103. beforeEach(function() {
  104. series = new TimeSeries({datapoints: [[45, 1234567890], [60, 1234567899]]});
  105. });
  106. it('should set hasMsResolution to false', function() {
  107. expect(series.hasMsResolution).to.be(false);
  108. });
  109. });
  110. describe('msResolution with millisecond resolution timestamps', function() {
  111. beforeEach(function() {
  112. series = new TimeSeries({datapoints: [[55, 1236547890001], [90, 1234456709000]]});
  113. });
  114. it('should show millisecond resolution tooltip', function() {
  115. expect(series.hasMsResolution).to.be(true);
  116. });
  117. });
  118. describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() {
  119. beforeEach(function() {
  120. series = new TimeSeries({datapoints: [[45, 1234567890000], [60, 1234567899000]]});
  121. });
  122. it('should not show millisecond resolution tooltip', function() {
  123. expect(series.hasMsResolution).to.be(false);
  124. });
  125. });
  126. });
  127. describe('can detect if series contains ms precision', function() {
  128. var fakedata;
  129. beforeEach(function() {
  130. fakedata = testData;
  131. });
  132. it('missing datapoint with ms precision', function() {
  133. fakedata.datapoints[0] = [1337, 1234567890000];
  134. series = new TimeSeries(fakedata);
  135. expect(series.isMsResolutionNeeded()).to.be(false);
  136. });
  137. it('contains datapoint with ms precision', function() {
  138. fakedata.datapoints[0] = [1337, 1236547890001];
  139. series = new TimeSeries(fakedata);
  140. expect(series.isMsResolutionNeeded()).to.be(true);
  141. });
  142. });
  143. describe('series overrides', function() {
  144. var series;
  145. beforeEach(function() {
  146. series = new TimeSeries(testData);
  147. });
  148. describe('fill & points', function() {
  149. beforeEach(function() {
  150. series.alias = 'test';
  151. series.applySeriesOverrides([{ alias: 'test', fill: 0, points: true }]);
  152. });
  153. it('should set fill zero, and enable points', function() {
  154. expect(series.lines.fill).to.be(0.001);
  155. expect(series.points.show).to.be(true);
  156. });
  157. });
  158. describe('series option overrides, bars, true & lines false', function() {
  159. beforeEach(function() {
  160. series.alias = 'test';
  161. series.applySeriesOverrides([{ alias: 'test', bars: true, lines: false }]);
  162. });
  163. it('should disable lines, and enable bars', function() {
  164. expect(series.lines.show).to.be(false);
  165. expect(series.bars.show).to.be(true);
  166. });
  167. });
  168. describe('series option overrides, linewidth, stack', function() {
  169. beforeEach(function() {
  170. series.alias = 'test';
  171. series.applySeriesOverrides([{ alias: 'test', linewidth: 5, stack: false }]);
  172. });
  173. it('should disable stack, and set lineWidth', function() {
  174. expect(series.stack).to.be(false);
  175. expect(series.lines.lineWidth).to.be(5);
  176. });
  177. });
  178. describe('series option overrides, fill below to', function() {
  179. beforeEach(function() {
  180. series.alias = 'test';
  181. series.applySeriesOverrides([{ alias: 'test', fillBelowTo: 'min' }]);
  182. });
  183. it('should disable line fill and add fillBelowTo', function() {
  184. expect(series.fillBelowTo).to.be('min');
  185. });
  186. });
  187. describe('series option overrides, pointradius, steppedLine', function() {
  188. beforeEach(function() {
  189. series.alias = 'test';
  190. series.applySeriesOverrides([{ alias: 'test', pointradius: 5, steppedLine: true }]);
  191. });
  192. it('should set pointradius, and set steppedLine', function() {
  193. expect(series.points.radius).to.be(5);
  194. expect(series.lines.steps).to.be(true);
  195. });
  196. });
  197. describe('override match on regex', function() {
  198. beforeEach(function() {
  199. series.alias = 'test_01';
  200. series.applySeriesOverrides([{ alias: '/.*01/', lines: false }]);
  201. });
  202. it('should match second series', function() {
  203. expect(series.lines.show).to.be(false);
  204. });
  205. });
  206. describe('override series y-axis, and z-index', function() {
  207. beforeEach(function() {
  208. series.alias = 'test';
  209. series.applySeriesOverrides([{ alias: 'test', yaxis: 2, zindex: 2 }]);
  210. });
  211. it('should set yaxis', function() {
  212. expect(series.yaxis).to.be(2);
  213. });
  214. it('should set zindex', function() {
  215. expect(series.zindex).to.be(2);
  216. });
  217. });
  218. });
  219. });
  220. });