kbn.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. import kbn from '../utils/kbn';
  2. import * as dateMath from '../utils/datemath';
  3. import moment from 'moment';
  4. describe('unit format menu', function() {
  5. var menu = kbn.getUnitFormats();
  6. menu.map(function(submenu) {
  7. describe('submenu ' + submenu.text, function() {
  8. it('should have a title', function() {
  9. expect(typeof submenu.text).toBe('string');
  10. });
  11. it('should have a submenu', function() {
  12. expect(Array.isArray(submenu.submenu)).toBe(true);
  13. });
  14. submenu.submenu.map(function(entry) {
  15. describe('entry ' + entry.text, function() {
  16. it('should have a title', function() {
  17. expect(typeof entry.text).toBe('string');
  18. });
  19. it('should have a format', function() {
  20. expect(typeof entry.value).toBe('string');
  21. });
  22. it('should have a valid format', function() {
  23. expect(typeof kbn.valueFormats[entry.value]).toBe('function');
  24. });
  25. });
  26. });
  27. });
  28. });
  29. });
  30. function describeValueFormat(desc, value, tickSize, tickDecimals, result) {
  31. describe('value format: ' + desc, function() {
  32. it('should translate ' + value + ' as ' + result, function() {
  33. var scaledDecimals = tickDecimals - Math.floor(Math.log(tickSize) / Math.LN10);
  34. var str = kbn.valueFormats[desc](value, tickDecimals, scaledDecimals);
  35. expect(str).toBe(result);
  36. });
  37. });
  38. }
  39. describeValueFormat('ms', 0.0024, 0.0005, 4, '0.0024 ms');
  40. describeValueFormat('ms', 100, 1, 0, '100 ms');
  41. describeValueFormat('ms', 1250, 10, 0, '1.25 s');
  42. describeValueFormat('ms', 1250, 300, 0, '1.3 s');
  43. describeValueFormat('ms', 65150, 10000, 0, '1.1 min');
  44. describeValueFormat('ms', 6515000, 1500000, 0, '1.8 hour');
  45. describeValueFormat('ms', 651500000, 150000000, 0, '8 day');
  46. describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
  47. describeValueFormat('none', 0, 0, 2, '0');
  48. describeValueFormat('dB', 10, 1000, 2, '10.00 dB');
  49. describeValueFormat('percent', 0, 0, 0, '0%');
  50. describeValueFormat('percent', 53, 0, 1, '53.0%');
  51. describeValueFormat('percentunit', 0.0, 0, 0, '0%');
  52. describeValueFormat('percentunit', 0.278, 0, 1, '27.8%');
  53. describeValueFormat('percentunit', 1.0, 0, 0, '100%');
  54. describeValueFormat('currencyUSD', 7.42, 10000, 2, '$7.42');
  55. describeValueFormat('currencyUSD', 1532.82, 1000, 1, '$1.53K');
  56. describeValueFormat('currencyUSD', 18520408.7, 10000000, 0, '$19M');
  57. describeValueFormat('bytes', -1.57e308, -1.57e308, 2, 'NA');
  58. describeValueFormat('ns', 25, 1, 0, '25 ns');
  59. describeValueFormat('ns', 2558, 50, 0, '2.56 µs');
  60. describeValueFormat('ops', 123, 1, 0, '123 ops');
  61. describeValueFormat('rps', 456000, 1000, -1, '456K rps');
  62. describeValueFormat('rps', 123456789, 1000000, 2, '123.457M rps');
  63. describeValueFormat('wps', 789000000, 1000000, -1, '789M wps');
  64. describeValueFormat('iops', 11000000000, 1000000000, -1, '11B iops');
  65. describeValueFormat('s', 1.23456789e-7, 1e-10, 8, '123.5 ns');
  66. describeValueFormat('s', 1.23456789e-4, 1e-7, 5, '123.5 µs');
  67. describeValueFormat('s', 1.23456789e-3, 1e-6, 4, '1.235 ms');
  68. describeValueFormat('s', 1.23456789e-2, 1e-5, 3, '12.35 ms');
  69. describeValueFormat('s', 1.23456789e-1, 1e-4, 2, '123.5 ms');
  70. describeValueFormat('s', 24, 1, 0, '24 s');
  71. describeValueFormat('s', 246, 1, 0, '4.1 min');
  72. describeValueFormat('s', 24567, 100, 0, '6.82 hour');
  73. describeValueFormat('s', 24567890, 10000, 0, '40.62 week');
  74. describeValueFormat('s', 24567890000, 1000000, 0, '778.53 year');
  75. describeValueFormat('m', 24, 1, 0, '24 min');
  76. describeValueFormat('m', 246, 10, 0, '4.1 hour');
  77. describeValueFormat('m', 6545, 10, 0, '4.55 day');
  78. describeValueFormat('m', 24567, 100, 0, '2.44 week');
  79. describeValueFormat('m', 24567892, 10000, 0, '46.7 year');
  80. describeValueFormat('h', 21, 1, 0, '21 hour');
  81. describeValueFormat('h', 145, 1, 0, '6.04 day');
  82. describeValueFormat('h', 1234, 100, 0, '7.3 week');
  83. describeValueFormat('h', 9458, 1000, 0, '1.08 year');
  84. describeValueFormat('d', 3, 1, 0, '3 day');
  85. describeValueFormat('d', 245, 100, 0, '35 week');
  86. describeValueFormat('d', 2456, 10, 0, '6.73 year');
  87. describe('date time formats', function() {
  88. const epoch = 1505634997920;
  89. const utcTime = moment.utc(epoch);
  90. const browserTime = moment(epoch);
  91. it('should format as iso date', function() {
  92. var expected = browserTime.format('YYYY-MM-DD HH:mm:ss');
  93. var actual = kbn.valueFormats.dateTimeAsIso(epoch);
  94. expect(actual).toBe(expected);
  95. });
  96. it('should format as iso date (in UTC)', function() {
  97. var expected = utcTime.format('YYYY-MM-DD HH:mm:ss');
  98. var actual = kbn.valueFormats.dateTimeAsIso(epoch, true);
  99. expect(actual).toBe(expected);
  100. });
  101. it('should format as iso date and skip date when today', function() {
  102. var now = moment();
  103. var expected = now.format('HH:mm:ss');
  104. var actual = kbn.valueFormats.dateTimeAsIso(now.valueOf(), false);
  105. expect(actual).toBe(expected);
  106. });
  107. it('should format as iso date (in UTC) and skip date when today', function() {
  108. var now = moment.utc();
  109. var expected = now.format('HH:mm:ss');
  110. var actual = kbn.valueFormats.dateTimeAsIso(now.valueOf(), true);
  111. expect(actual).toBe(expected);
  112. });
  113. it('should format as US date', function() {
  114. var expected = browserTime.format('MM/DD/YYYY h:mm:ss a');
  115. var actual = kbn.valueFormats.dateTimeAsUS(epoch, false);
  116. expect(actual).toBe(expected);
  117. });
  118. it('should format as US date (in UTC)', function() {
  119. var expected = utcTime.format('MM/DD/YYYY h:mm:ss a');
  120. var actual = kbn.valueFormats.dateTimeAsUS(epoch, true);
  121. expect(actual).toBe(expected);
  122. });
  123. it('should format as US date and skip date when today', function() {
  124. var now = moment();
  125. var expected = now.format('h:mm:ss a');
  126. var actual = kbn.valueFormats.dateTimeAsUS(now.valueOf(), false);
  127. expect(actual).toBe(expected);
  128. });
  129. it('should format as US date (in UTC) and skip date when today', function() {
  130. var now = moment.utc();
  131. var expected = now.format('h:mm:ss a');
  132. var actual = kbn.valueFormats.dateTimeAsUS(now.valueOf(), true);
  133. expect(actual).toBe(expected);
  134. });
  135. it('should format as from now with days', function() {
  136. var daysAgo = moment().add(-7, 'd');
  137. var expected = '7 days ago';
  138. var actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), false);
  139. expect(actual).toBe(expected);
  140. });
  141. it('should format as from now with days (in UTC)', function() {
  142. var daysAgo = moment.utc().add(-7, 'd');
  143. var expected = '7 days ago';
  144. var actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), true);
  145. expect(actual).toBe(expected);
  146. });
  147. it('should format as from now with minutes', function() {
  148. var daysAgo = moment().add(-2, 'm');
  149. var expected = '2 minutes ago';
  150. var actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), false);
  151. expect(actual).toBe(expected);
  152. });
  153. it('should format as from now with minutes (in UTC)', function() {
  154. var daysAgo = moment.utc().add(-2, 'm');
  155. var expected = '2 minutes ago';
  156. var actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), true);
  157. expect(actual).toBe(expected);
  158. });
  159. });
  160. describe('kbn.toFixed and negative decimals', function() {
  161. it('should treat as zero decimals', function() {
  162. var str = kbn.toFixed(186.123, -2);
  163. expect(str).toBe('186');
  164. });
  165. });
  166. describe('kbn ms format when scaled decimals is null do not use it', function() {
  167. it('should use specified decimals', function() {
  168. var str = kbn.valueFormats['ms'](10000086.123, 1, null);
  169. expect(str).toBe('2.8 hour');
  170. });
  171. });
  172. describe('kbn kbytes format when scaled decimals is null do not use it', function() {
  173. it('should use specified decimals', function() {
  174. var str = kbn.valueFormats['kbytes'](10000000, 3, null);
  175. expect(str).toBe('9.537 GiB');
  176. });
  177. });
  178. describe('kbn deckbytes format when scaled decimals is null do not use it', function() {
  179. it('should use specified decimals', function() {
  180. var str = kbn.valueFormats['deckbytes'](10000000, 3, null);
  181. expect(str).toBe('10.000 GB');
  182. });
  183. });
  184. describe('kbn roundValue', function() {
  185. it('should should handle null value', function() {
  186. var str = kbn.roundValue(null, 2);
  187. expect(str).toBe(null);
  188. });
  189. it('should round value', function() {
  190. var str = kbn.roundValue(200.877, 2);
  191. expect(str).toBe(200.88);
  192. });
  193. });
  194. describe('calculateInterval', function() {
  195. it('1h 100 resultion', function() {
  196. var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') };
  197. var res = kbn.calculateInterval(range, 100, null);
  198. expect(res.interval).toBe('30s');
  199. });
  200. it('10m 1600 resolution', function() {
  201. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  202. var res = kbn.calculateInterval(range, 1600, null);
  203. expect(res.interval).toBe('500ms');
  204. expect(res.intervalMs).toBe(500);
  205. });
  206. it('fixed user min interval', function() {
  207. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  208. var res = kbn.calculateInterval(range, 1600, '10s');
  209. expect(res.interval).toBe('10s');
  210. expect(res.intervalMs).toBe(10000);
  211. });
  212. it('short time range and user low limit', function() {
  213. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  214. var res = kbn.calculateInterval(range, 1600, '>10s');
  215. expect(res.interval).toBe('10s');
  216. });
  217. it('large time range and user low limit', function() {
  218. var range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') };
  219. var res = kbn.calculateInterval(range, 1000, '>10s');
  220. expect(res.interval).toBe('20m');
  221. });
  222. it('10s 900 resolution and user low limit in ms', function() {
  223. var range = { from: dateMath.parse('now-10s'), to: dateMath.parse('now') };
  224. var res = kbn.calculateInterval(range, 900, '>15ms');
  225. expect(res.interval).toBe('15ms');
  226. });
  227. it('1d 1 resolution', function() {
  228. var range = { from: dateMath.parse('now-1d'), to: dateMath.parse('now') };
  229. var res = kbn.calculateInterval(range, 1, null);
  230. expect(res.interval).toBe('1d');
  231. expect(res.intervalMs).toBe(86400000);
  232. });
  233. it('86399s 1 resolution', function() {
  234. var range = {
  235. from: dateMath.parse('now-86390s'),
  236. to: dateMath.parse('now'),
  237. };
  238. var res = kbn.calculateInterval(range, 1, null);
  239. expect(res.interval).toBe('12h');
  240. expect(res.intervalMs).toBe(43200000);
  241. });
  242. });
  243. describe('hex', function() {
  244. it('positive integer', function() {
  245. var str = kbn.valueFormats.hex(100, 0);
  246. expect(str).toBe('64');
  247. });
  248. it('negative integer', function() {
  249. var str = kbn.valueFormats.hex(-100, 0);
  250. expect(str).toBe('-64');
  251. });
  252. it('null', function() {
  253. var str = kbn.valueFormats.hex(null, 0);
  254. expect(str).toBe('');
  255. });
  256. it('positive float', function() {
  257. var str = kbn.valueFormats.hex(50.52, 1);
  258. expect(str).toBe('32.8');
  259. });
  260. it('negative float', function() {
  261. var str = kbn.valueFormats.hex(-50.333, 2);
  262. expect(str).toBe('-32.547AE147AE14');
  263. });
  264. });
  265. describe('hex 0x', function() {
  266. it('positive integeter', function() {
  267. var str = kbn.valueFormats.hex0x(7999, 0);
  268. expect(str).toBe('0x1F3F');
  269. });
  270. it('negative integer', function() {
  271. var str = kbn.valueFormats.hex0x(-584, 0);
  272. expect(str).toBe('-0x248');
  273. });
  274. it('null', function() {
  275. var str = kbn.valueFormats.hex0x(null, 0);
  276. expect(str).toBe('');
  277. });
  278. it('positive float', function() {
  279. var str = kbn.valueFormats.hex0x(74.443, 3);
  280. expect(str).toBe('0x4A.716872B020C4');
  281. });
  282. it('negative float', function() {
  283. var str = kbn.valueFormats.hex0x(-65.458, 1);
  284. expect(str).toBe('-0x41.8');
  285. });
  286. });
  287. describe('duration', function() {
  288. it('null', function() {
  289. var str = kbn.toDuration(null, 0, 'millisecond');
  290. expect(str).toBe('');
  291. });
  292. it('0 milliseconds', function() {
  293. var str = kbn.toDuration(0, 0, 'millisecond');
  294. expect(str).toBe('0 milliseconds');
  295. });
  296. it('1 millisecond', function() {
  297. var str = kbn.toDuration(1, 0, 'millisecond');
  298. expect(str).toBe('1 millisecond');
  299. });
  300. it('-1 millisecond', function() {
  301. var str = kbn.toDuration(-1, 0, 'millisecond');
  302. expect(str).toBe('1 millisecond ago');
  303. });
  304. it('seconds', function() {
  305. var str = kbn.toDuration(1, 0, 'second');
  306. expect(str).toBe('1 second');
  307. });
  308. it('minutes', function() {
  309. var str = kbn.toDuration(1, 0, 'minute');
  310. expect(str).toBe('1 minute');
  311. });
  312. it('hours', function() {
  313. var str = kbn.toDuration(1, 0, 'hour');
  314. expect(str).toBe('1 hour');
  315. });
  316. it('days', function() {
  317. var str = kbn.toDuration(1, 0, 'day');
  318. expect(str).toBe('1 day');
  319. });
  320. it('weeks', function() {
  321. var str = kbn.toDuration(1, 0, 'week');
  322. expect(str).toBe('1 week');
  323. });
  324. it('months', function() {
  325. var str = kbn.toDuration(1, 0, 'month');
  326. expect(str).toBe('1 month');
  327. });
  328. it('years', function() {
  329. var str = kbn.toDuration(1, 0, 'year');
  330. expect(str).toBe('1 year');
  331. });
  332. it('decimal days', function() {
  333. var str = kbn.toDuration(1.5, 2, 'day');
  334. expect(str).toBe('1 day, 12 hours, 0 minutes');
  335. });
  336. it('decimal months', function() {
  337. var str = kbn.toDuration(1.5, 3, 'month');
  338. expect(str).toBe('1 month, 2 weeks, 1 day, 0 hours');
  339. });
  340. it('no decimals', function() {
  341. var str = kbn.toDuration(38898367008, 0, 'millisecond');
  342. expect(str).toBe('1 year');
  343. });
  344. it('1 decimal', function() {
  345. var str = kbn.toDuration(38898367008, 1, 'millisecond');
  346. expect(str).toBe('1 year, 2 months');
  347. });
  348. it('too many decimals', function() {
  349. var str = kbn.toDuration(38898367008, 20, 'millisecond');
  350. expect(str).toBe('1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, 7 seconds, 8 milliseconds');
  351. });
  352. it('floating point error', function() {
  353. var str = kbn.toDuration(36993906007, 8, 'millisecond');
  354. expect(str).toBe('1 year, 2 months, 0 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds');
  355. });
  356. });
  357. describe('volume', function() {
  358. it('1000m3', function() {
  359. var str = kbn.valueFormats['m3'](1000, 1, null);
  360. expect(str).toBe('1000.0 m³');
  361. });
  362. });
  363. describe('hh:mm:ss', function() {
  364. it('00:04:06', function() {
  365. var str = kbn.valueFormats['dthms'](246, 1);
  366. expect(str).toBe('00:04:06');
  367. });
  368. it('24:00:00', function() {
  369. var str = kbn.valueFormats['dthms'](86400, 1);
  370. expect(str).toBe('24:00:00');
  371. });
  372. it('6824413:53:20', function() {
  373. var str = kbn.valueFormats['dthms'](24567890000, 1);
  374. expect(str).toBe('6824413:53:20');
  375. });
  376. });