kbn_specs.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. define([
  2. 'app/core/utils/kbn',
  3. 'app/core/utils/datemath'
  4. ], function(kbn, dateMath) {
  5. 'use strict';
  6. describe('unit format menu', function() {
  7. var menu = kbn.getUnitFormats();
  8. menu.map(function(submenu) {
  9. describe('submenu ' + submenu.text, function() {
  10. it('should have a title', function() { expect(submenu.text).to.be.a('string'); });
  11. it('should have a submenu', function() { expect(submenu.submenu).to.be.an('array'); });
  12. submenu.submenu.map(function(entry) {
  13. describe('entry ' + entry.text, function() {
  14. it('should have a title', function() { expect(entry.text).to.be.a('string'); });
  15. it('should have a format', function() { expect(entry.value).to.be.a('string'); });
  16. it('should have a valid format', function() {
  17. expect(kbn.valueFormats[entry.value]).to.be.a('function');
  18. });
  19. });
  20. });
  21. });
  22. });
  23. });
  24. function describeValueFormat(desc, value, tickSize, tickDecimals, result) {
  25. describe('value format: ' + desc, function() {
  26. it('should translate ' + value + ' as ' + result, function() {
  27. var scaledDecimals = tickDecimals - Math.floor(Math.log(tickSize) / Math.LN10);
  28. var str = kbn.valueFormats[desc](value, tickDecimals, scaledDecimals);
  29. expect(str).to.be(result);
  30. });
  31. });
  32. }
  33. describeValueFormat('ms', 0.0024, 0.0005, 4, '0.0024 ms');
  34. describeValueFormat('ms', 100, 1, 0, '100 ms');
  35. describeValueFormat('ms', 1250, 10, 0, '1.25 s');
  36. describeValueFormat('ms', 1250, 300, 0, '1.3 s');
  37. describeValueFormat('ms', 65150, 10000, 0, '1.1 min');
  38. describeValueFormat('ms', 6515000, 1500000, 0, '1.8 hour');
  39. describeValueFormat('ms', 651500000, 150000000, 0, '8 day');
  40. describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
  41. describeValueFormat('none', 0, 0, 2, '0');
  42. describeValueFormat('dB', 10, 1000, 2, '10.00 dB');
  43. describeValueFormat('percent', 0, 0, 0, '0%');
  44. describeValueFormat('percent', 53, 0, 1, '53.0%');
  45. describeValueFormat('percentunit', 0.0, 0, 0, '0%');
  46. describeValueFormat('percentunit', 0.278, 0, 1, '27.8%');
  47. describeValueFormat('percentunit', 1.0, 0, 0, '100%');
  48. describeValueFormat('currencyUSD', 7.42, 10000, 2, '$7.42');
  49. describeValueFormat('currencyUSD', 1532.82, 1000, 1, '$1.53K');
  50. describeValueFormat('currencyUSD', 18520408.7, 10000000, 0, '$19M');
  51. describeValueFormat('bytes', -1.57e+308, -1.57e+308, 2, 'NA');
  52. describeValueFormat('ns', 25, 1, 0, '25 ns');
  53. describeValueFormat('ns', 2558, 50, 0, '2.56 µs');
  54. describeValueFormat('ops', 123, 1, 0, '123 ops');
  55. describeValueFormat('rps', 456000, 1000, -1, '456K rps');
  56. describeValueFormat('rps', 123456789, 1000000, 2, '123.457M rps');
  57. describeValueFormat('wps', 789000000, 1000000, -1, '789M wps');
  58. describeValueFormat('iops', 11000000000, 1000000000, -1, '11B iops');
  59. describeValueFormat('s', 1.23456789e-7, 1e-10, 8, '123.5 ns');
  60. describeValueFormat('s', 1.23456789e-4, 1e-7, 5, '123.5 µs');
  61. describeValueFormat('s', 1.23456789e-3, 1e-6, 4, '1.235 ms');
  62. describeValueFormat('s', 1.23456789e-2, 1e-5, 3, '12.35 ms');
  63. describeValueFormat('s', 1.23456789e-1, 1e-4, 2, '123.5 ms');
  64. describeValueFormat('s', 24, 1, 0, '24 s');
  65. describeValueFormat('s', 246, 1, 0, '4.1 min');
  66. describeValueFormat('s', 24567, 100, 0, '6.82 hour');
  67. describeValueFormat('s', 24567890, 10000, 0, '40.62 week');
  68. describeValueFormat('s', 24567890000, 1000000, 0, '778.53 year');
  69. describeValueFormat('m', 24, 1, 0, '24 min');
  70. describeValueFormat('m', 246, 10, 0, '4.1 hour');
  71. describeValueFormat('m', 6545, 10, 0, '4.55 day');
  72. describeValueFormat('m', 24567, 100, 0, '2.44 week');
  73. describeValueFormat('m', 24567892, 10000, 0, '46.7 year');
  74. describeValueFormat('h', 21, 1, 0, '21 hour');
  75. describeValueFormat('h', 145, 1, 0, '6.04 day');
  76. describeValueFormat('h', 1234, 100, 0, '7.3 week');
  77. describeValueFormat('h', 9458, 1000, 0, '1.08 year');
  78. describeValueFormat('d', 3, 1, 0, '3 day');
  79. describeValueFormat('d', 245, 100, 0, '35 week');
  80. describeValueFormat('d', 2456, 10, 0, '6.73 year');
  81. describe('kbn.toFixed and negative decimals', function() {
  82. it('should treat as zero decimals', function() {
  83. var str = kbn.toFixed(186.123, -2);
  84. expect(str).to.be('186');
  85. });
  86. });
  87. describe('kbn ms format when scaled decimals is null do not use it', function() {
  88. it('should use specified decimals', function() {
  89. var str = kbn.valueFormats['ms'](10000086.123, 1, null);
  90. expect(str).to.be('2.8 hour');
  91. });
  92. });
  93. describe('kbn kbytes format when scaled decimals is null do not use it', function() {
  94. it('should use specified decimals', function() {
  95. var str = kbn.valueFormats['kbytes'](10000000, 3, null);
  96. expect(str).to.be('9.537 GiB');
  97. });
  98. });
  99. describe('kbn deckbytes format when scaled decimals is null do not use it', function() {
  100. it('should use specified decimals', function() {
  101. var str = kbn.valueFormats['deckbytes'](10000000, 3, null);
  102. expect(str).to.be('10.000 GB');
  103. });
  104. });
  105. describe('kbn roundValue', function() {
  106. it('should should handle null value', function() {
  107. var str = kbn.roundValue(null, 2);
  108. expect(str).to.be(null);
  109. });
  110. });
  111. describe('calculateInterval', function() {
  112. it('1h 100 resultion', function() {
  113. var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') };
  114. var str = kbn.calculateInterval(range, 100, null);
  115. expect(str).to.be('30s');
  116. });
  117. it('10m 1600 resolution', function() {
  118. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  119. var str = kbn.calculateInterval(range, 1600, null);
  120. expect(str).to.be('500ms');
  121. });
  122. it('fixed user interval', function() {
  123. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  124. var str = kbn.calculateInterval(range, 1600, '10s');
  125. expect(str).to.be('10s');
  126. });
  127. it('short time range and user low limit', function() {
  128. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  129. var str = kbn.calculateInterval(range, 1600, '>10s');
  130. expect(str).to.be('10s');
  131. });
  132. it('large time range and user low limit', function() {
  133. var range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') };
  134. var str = kbn.calculateInterval(range, 1000, '>10s');
  135. expect(str).to.be('20m');
  136. });
  137. it('10s 900 resolution and user low limit in ms', function() {
  138. var range = { from: dateMath.parse('now-10s'), to: dateMath.parse('now') };
  139. var str = kbn.calculateInterval(range, 900, '>15ms');
  140. expect(str).to.be('15ms');
  141. });
  142. });
  143. describe('hex', function() {
  144. it('positive integer', function() {
  145. var str = kbn.valueFormats.hex(100, 0);
  146. expect(str).to.be('64');
  147. });
  148. it('negative integer', function() {
  149. var str = kbn.valueFormats.hex(-100, 0);
  150. expect(str).to.be('-64');
  151. });
  152. it('null', function() {
  153. var str = kbn.valueFormats.hex(null, 0);
  154. expect(str).to.be('');
  155. });
  156. it('positive float', function() {
  157. var str = kbn.valueFormats.hex(50.52, 1);
  158. expect(str).to.be('32.8');
  159. });
  160. it('negative float', function() {
  161. var str = kbn.valueFormats.hex(-50.333, 2);
  162. expect(str).to.be('-32.547AE147AE14');
  163. });
  164. });
  165. describe('hex 0x', function() {
  166. it('positive integeter', function() {
  167. var str = kbn.valueFormats.hex0x(7999,0);
  168. expect(str).to.be('0x1F3F');
  169. });
  170. it('negative integer', function() {
  171. var str = kbn.valueFormats.hex0x(-584,0);
  172. expect(str).to.be('-0x248');
  173. });
  174. it('null', function() {
  175. var str = kbn.valueFormats.hex0x(null, 0);
  176. expect(str).to.be('');
  177. });
  178. it('positive float', function() {
  179. var str = kbn.valueFormats.hex0x(74.443, 3);
  180. expect(str).to.be('0x4A.716872B020C4');
  181. });
  182. it('negative float', function() {
  183. var str = kbn.valueFormats.hex0x(-65.458, 1);
  184. expect(str).to.be('-0x41.8');
  185. });
  186. });
  187. });