kbn.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. define([
  2. 'jquery',
  3. 'lodash',
  4. 'moment'
  5. ],
  6. function($, _, moment) {
  7. 'use strict';
  8. var kbn = {};
  9. /**
  10. * Calculate a graph interval
  11. *
  12. * from:: Date object containing the start time
  13. * to:: Date object containing the finish time
  14. * size:: Calculate to approximately this many bars
  15. * user_interval:: User specified histogram interval
  16. *
  17. */
  18. kbn.calculate_interval = function(from,to,size,user_interval) {
  19. if(_.isObject(from)) {
  20. from = from.valueOf();
  21. }
  22. if(_.isObject(to)) {
  23. to = to.valueOf();
  24. }
  25. return user_interval === 0 ? kbn.round_interval((to - from)/size) : user_interval;
  26. };
  27. kbn.round_interval = function(interval) {
  28. switch (true) {
  29. // 0.5s
  30. case (interval <= 500):
  31. return 100; // 0.1s
  32. // 5s
  33. case (interval <= 5000):
  34. return 1000; // 1s
  35. // 7.5s
  36. case (interval <= 7500):
  37. return 5000; // 5s
  38. // 15s
  39. case (interval <= 15000):
  40. return 10000; // 10s
  41. // 45s
  42. case (interval <= 45000):
  43. return 30000; // 30s
  44. // 3m
  45. case (interval <= 180000):
  46. return 60000; // 1m
  47. // 9m
  48. case (interval <= 450000):
  49. return 300000; // 5m
  50. // 20m
  51. case (interval <= 1200000):
  52. return 600000; // 10m
  53. // 45m
  54. case (interval <= 2700000):
  55. return 1800000; // 30m
  56. // 2h
  57. case (interval <= 7200000):
  58. return 3600000; // 1h
  59. // 6h
  60. case (interval <= 21600000):
  61. return 10800000; // 3h
  62. // 24h
  63. case (interval <= 86400000):
  64. return 43200000; // 12h
  65. // 48h
  66. case (interval <= 172800000):
  67. return 86400000; // 24h
  68. // 1w
  69. case (interval <= 604800000):
  70. return 86400000; // 24h
  71. // 3w
  72. case (interval <= 1814400000):
  73. return 604800000; // 1w
  74. // 2y
  75. case (interval < 3628800000):
  76. return 2592000000; // 30d
  77. default:
  78. return 31536000000; // 1y
  79. }
  80. };
  81. kbn.secondsToHms = function(seconds) {
  82. var numyears = Math.floor(seconds / 31536000);
  83. if(numyears){
  84. return numyears + 'y';
  85. }
  86. var numdays = Math.floor((seconds % 31536000) / 86400);
  87. if(numdays){
  88. return numdays + 'd';
  89. }
  90. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  91. if(numhours){
  92. return numhours + 'h';
  93. }
  94. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  95. if(numminutes){
  96. return numminutes + 'm';
  97. }
  98. var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
  99. if(numseconds){
  100. return numseconds + 's';
  101. }
  102. return 'less then a second'; //'just now' //or other string you like;
  103. };
  104. kbn.to_percent = function(number,outof) {
  105. return Math.floor((number/outof)*10000)/100 + "%";
  106. };
  107. kbn.addslashes = function(str) {
  108. str = str.replace(/\\/g, '\\\\');
  109. str = str.replace(/\'/g, '\\\'');
  110. str = str.replace(/\"/g, '\\"');
  111. str = str.replace(/\0/g, '\\0');
  112. return str;
  113. };
  114. kbn.interval_regex = /(\d+(?:\.\d+)?)([Mwdhmsy])/;
  115. // histogram & trends
  116. kbn.intervals_in_seconds = {
  117. y: 31536000,
  118. M: 2592000,
  119. w: 604800,
  120. d: 86400,
  121. h: 3600,
  122. m: 60,
  123. s: 1
  124. };
  125. kbn.describe_interval = function (string) {
  126. var matches = string.match(kbn.interval_regex);
  127. if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
  128. throw new Error('Invalid interval string, expexcting a number followed by one of "Mwdhmsy"');
  129. } else {
  130. return {
  131. sec: kbn.intervals_in_seconds[matches[2]],
  132. type: matches[2],
  133. count: parseInt(matches[1], 10)
  134. };
  135. }
  136. };
  137. kbn.interval_to_ms = function(string) {
  138. var info = kbn.describe_interval(string);
  139. return info.sec * 1000 * info.count;
  140. };
  141. kbn.interval_to_seconds = function (string) {
  142. var info = kbn.describe_interval(string);
  143. return info.sec * info.count;
  144. };
  145. // This should go away, moment.js can do this
  146. kbn.time_ago = function(string) {
  147. return new Date(new Date().getTime() - (kbn.interval_to_ms(string)));
  148. };
  149. /* This is a simplified version of elasticsearch's date parser */
  150. kbn.parseDate = function(text) {
  151. if(_.isDate(text)) {
  152. return text;
  153. }
  154. var time,
  155. mathString = "",
  156. index,
  157. parseString;
  158. if (text.substring(0,3) === "now") {
  159. time = new Date();
  160. mathString = text.substring("now".length);
  161. } else {
  162. index = text.indexOf("||");
  163. parseString;
  164. if (index === -1) {
  165. parseString = text;
  166. mathString = ""; // nothing else
  167. } else {
  168. parseString = text.substring(0, index);
  169. mathString = text.substring(index + 2);
  170. }
  171. // We're going to just require ISO8601 timestamps, k?
  172. time = new Date(parseString);
  173. }
  174. if (!mathString.length) {
  175. return time;
  176. }
  177. //return [time,parseString,mathString];
  178. return kbn.parseDateMath(mathString, time);
  179. };
  180. kbn.parseDateMath = function(mathString, time, roundUp) {
  181. var dateTime = moment(time);
  182. for (var i = 0; i < mathString.length;) {
  183. var c = mathString.charAt(i++),
  184. type,
  185. num,
  186. unit;
  187. if (c === '/') {
  188. type = 0;
  189. } else if (c === '+') {
  190. type = 1;
  191. } else if (c === '-') {
  192. type = 2;
  193. } else {
  194. return false;
  195. }
  196. if (isNaN(mathString.charAt(i))) {
  197. num = 1;
  198. } else {
  199. var numFrom = i;
  200. while (!isNaN(mathString.charAt(i))) {
  201. i++;
  202. }
  203. num = parseInt(mathString.substring(numFrom, i),10);
  204. }
  205. if (type === 0) {
  206. // rounding is only allowed on whole numbers
  207. if (num !== 1) {
  208. return false;
  209. }
  210. }
  211. unit = mathString.charAt(i++);
  212. switch (unit) {
  213. case 'y':
  214. if (type === 0) {
  215. roundUp ? dateTime.endOf('year') : dateTime.startOf('year');
  216. } else if (type === 1) {
  217. dateTime.add(num, 'years');
  218. } else if (type === 2) {
  219. dateTime.subtract(num, 'years');
  220. }
  221. break;
  222. case 'M':
  223. if (type === 0) {
  224. roundUp ? dateTime.endOf('month') : dateTime.startOf('month');
  225. } else if (type === 1) {
  226. dateTime.add(num, 'months');
  227. } else if (type === 2) {
  228. dateTime.subtract(num, 'months');
  229. }
  230. break;
  231. case 'w':
  232. if (type === 0) {
  233. roundUp ? dateTime.endOf('week') : dateTime.startOf('week');
  234. } else if (type === 1) {
  235. dateTime.add(num, 'weeks');
  236. } else if (type === 2) {
  237. dateTime.subtract(num, 'weeks');
  238. }
  239. break;
  240. case 'd':
  241. if (type === 0) {
  242. roundUp ? dateTime.endOf('day') : dateTime.startOf('day');
  243. } else if (type === 1) {
  244. dateTime.add(num, 'days');
  245. } else if (type === 2) {
  246. dateTime.subtract(num, 'days');
  247. }
  248. break;
  249. case 'h':
  250. case 'H':
  251. if (type === 0) {
  252. roundUp ? dateTime.endOf('hour') : dateTime.startOf('hour');
  253. } else if (type === 1) {
  254. dateTime.add(num, 'hours');
  255. } else if (type === 2) {
  256. dateTime.subtract(num,'hours');
  257. }
  258. break;
  259. case 'm':
  260. if (type === 0) {
  261. roundUp ? dateTime.endOf('minute') : dateTime.startOf('minute');
  262. } else if (type === 1) {
  263. dateTime.add(num, 'minutes');
  264. } else if (type === 2) {
  265. dateTime.subtract(num, 'minutes');
  266. }
  267. break;
  268. case 's':
  269. if (type === 0) {
  270. roundUp ? dateTime.endOf('second') : dateTime.startOf('second');
  271. } else if (type === 1) {
  272. dateTime.add(num, 'seconds');
  273. } else if (type === 2) {
  274. dateTime.subtract(num, 'seconds');
  275. }
  276. break;
  277. default:
  278. return false;
  279. }
  280. }
  281. return dateTime.toDate();
  282. };
  283. kbn.query_color_dot = function (color, diameter) {
  284. return '<div class="icon-circle" style="' + [
  285. 'display:inline-block',
  286. 'color:' + color,
  287. 'font-size:' + diameter + 'px',
  288. ].join(';') + '"></div>';
  289. };
  290. kbn.byteFormat = function(size, decimals) {
  291. var ext, steps = 0;
  292. if(_.isUndefined(decimals)) {
  293. decimals = 2;
  294. } else if (decimals === 0) {
  295. decimals = undefined;
  296. }
  297. while (Math.abs(size) >= 1024) {
  298. steps++;
  299. size /= 1024;
  300. }
  301. switch (steps) {
  302. case 0:
  303. ext = " B";
  304. break;
  305. case 1:
  306. ext = " KiB";
  307. break;
  308. case 2:
  309. ext = " MiB";
  310. break;
  311. case 3:
  312. ext = " GiB";
  313. break;
  314. case 4:
  315. ext = " TiB";
  316. break;
  317. case 5:
  318. ext = " PiB";
  319. break;
  320. case 6:
  321. ext = " EiB";
  322. break;
  323. case 7:
  324. ext = " ZiB";
  325. break;
  326. case 8:
  327. ext = " YiB";
  328. break;
  329. }
  330. return (size.toFixed(decimals) + ext);
  331. };
  332. kbn.bitFormat = function(size, decimals) {
  333. var ext, steps = 0;
  334. if(_.isUndefined(decimals)) {
  335. decimals = 2;
  336. } else if (decimals === 0) {
  337. decimals = undefined;
  338. }
  339. while (Math.abs(size) >= 1024) {
  340. steps++;
  341. size /= 1024;
  342. }
  343. switch (steps) {
  344. case 0:
  345. ext = " b";
  346. break;
  347. case 1:
  348. ext = " Kib";
  349. break;
  350. case 2:
  351. ext = " Mib";
  352. break;
  353. case 3:
  354. ext = " Gib";
  355. break;
  356. case 4:
  357. ext = " Tib";
  358. break;
  359. case 5:
  360. ext = " Pib";
  361. break;
  362. case 6:
  363. ext = " Eib";
  364. break;
  365. case 7:
  366. ext = " Zib";
  367. break;
  368. case 8:
  369. ext = " Yib";
  370. break;
  371. }
  372. return (size.toFixed(decimals) + ext);
  373. };
  374. kbn.bpsFormat = function(size, decimals) {
  375. var ext, steps = 0;
  376. if(_.isUndefined(decimals)) {
  377. decimals = 2;
  378. } else if (decimals === 0) {
  379. decimals = undefined;
  380. }
  381. while (Math.abs(size) >= 1000) {
  382. steps++;
  383. size /= 1000;
  384. }
  385. switch (steps) {
  386. case 0:
  387. ext = " bps";
  388. break;
  389. case 1:
  390. ext = " Kbps";
  391. break;
  392. case 2:
  393. ext = " Mbps";
  394. break;
  395. case 3:
  396. ext = " Gbps";
  397. break;
  398. case 4:
  399. ext = " Tbps";
  400. break;
  401. case 5:
  402. ext = " Pbps";
  403. break;
  404. case 6:
  405. ext = " Ebps";
  406. break;
  407. case 7:
  408. ext = " Zbps";
  409. break;
  410. case 8:
  411. ext = " Ybps";
  412. break;
  413. }
  414. return (size.toFixed(decimals) + ext);
  415. };
  416. kbn.shortFormat = function(size, decimals) {
  417. var ext, steps = 0;
  418. if(_.isUndefined(decimals)) {
  419. decimals = 2;
  420. } else if (decimals === 0) {
  421. decimals = undefined;
  422. }
  423. while (Math.abs(size) >= 1000) {
  424. steps++;
  425. size /= 1000;
  426. }
  427. switch (steps) {
  428. case 0:
  429. ext = "";
  430. break;
  431. case 1:
  432. ext = " K";
  433. break;
  434. case 2:
  435. ext = " Mil";
  436. break;
  437. case 3:
  438. ext = " Bil";
  439. break;
  440. case 4:
  441. ext = " Tri";
  442. break;
  443. case 5:
  444. ext = " Quadr";
  445. break;
  446. case 6:
  447. ext = " Quint";
  448. break;
  449. case 7:
  450. ext = " Sext";
  451. break;
  452. case 8:
  453. ext = " Sept";
  454. break;
  455. }
  456. return (size.toFixed(decimals) + ext);
  457. };
  458. kbn.getFormatFunction = function(formatName, decimals) {
  459. switch(formatName) {
  460. case 'short':
  461. return function(val) {
  462. return kbn.shortFormat(val, decimals);
  463. };
  464. case 'bytes':
  465. return function(val) {
  466. return kbn.byteFormat(val, decimals);
  467. };
  468. case 'bits':
  469. return function(val) {
  470. return kbn.bitFormat(val, decimals);
  471. };
  472. case 'bps':
  473. return function(val) {
  474. return kbn.bpsFormat(val, decimals);
  475. };
  476. case 's':
  477. return function(val) {
  478. return kbn.sFormat(val, decimals);
  479. };
  480. case 'ms':
  481. return function(val) {
  482. return kbn.msFormat(val, decimals);
  483. };
  484. case 'µs':
  485. return function(val) {
  486. return kbn.microsFormat(val, decimals);
  487. };
  488. case 'ns':
  489. return function(val) {
  490. return kbn.nanosFormat(val, decimals);
  491. };
  492. default:
  493. return function(val, axis) {
  494. return kbn.noneFormat(val, axis ? axis.tickDecimals : null);
  495. };
  496. }
  497. };
  498. kbn.noneFormat = function(value, decimals) {
  499. var factor = decimals ? Math.pow(10, decimals) : 1;
  500. var formatted = String(Math.round(value * factor) / factor);
  501. // if exponent return directly
  502. if (formatted.indexOf('e') !== -1 || value === 0) {
  503. return formatted;
  504. }
  505. // If tickDecimals was specified, ensure that we have exactly that
  506. // much precision; otherwise default to the value's own precision.
  507. if (decimals != null) {
  508. var decimalPos = formatted.indexOf(".");
  509. var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1;
  510. if (precision < decimals) {
  511. return (precision ? formatted : formatted + ".") + (String(factor)).substr(1, decimals - precision);
  512. }
  513. }
  514. return formatted;
  515. };
  516. kbn.msFormat = function(size, decimals) {
  517. // Less than 1 milli, downscale to micro
  518. if (Math.abs(size) < 1) {
  519. return kbn.microsFormat(size * 1000,decimals);
  520. }
  521. else if (Math.abs(size) < 1000) {
  522. return size.toFixed(decimals) + " ms";
  523. }
  524. // Less than 1 min
  525. else if (Math.abs(size) < 60000) {
  526. return (size / 1000).toFixed(decimals) + " s";
  527. }
  528. // Less than 1 hour, devide in minutes
  529. else if (Math.abs(size) < 3600000) {
  530. return (size / 60000).toFixed(decimals) + " min";
  531. }
  532. // Less than one day, devide in hours
  533. else if (Math.abs(size) < 86400000) {
  534. return (size / 3600000).toFixed(decimals) + " hour";
  535. }
  536. // Less than one year, devide in days
  537. else if (Math.abs(size) < 31536000000) {
  538. return (size / 86400000).toFixed(decimals) + " day";
  539. }
  540. return (size / 31536000000).toFixed(decimals) + " year";
  541. };
  542. kbn.sFormat = function(size, decimals) {
  543. // Less than 1 sec, downscale to milli
  544. if (Math.abs(size) < 1) {
  545. return kbn.msFormat(size * 1000, decimals);
  546. }
  547. // Less than 10 min, use seconds
  548. else if (Math.abs(size) < 600) {
  549. return size.toFixed(decimals) + " s";
  550. }
  551. // Less than 1 hour, devide in minutes
  552. else if (Math.abs(size) < 3600) {
  553. return (size / 60).toFixed(decimals) + " min";
  554. }
  555. // Less than one day, devide in hours
  556. else if (Math.abs(size) < 86400) {
  557. return (size / 3600).toFixed(decimals) + " hour";
  558. }
  559. // Less than one week, devide in days
  560. else if (Math.abs(size) < 604800) {
  561. return (size / 86400).toFixed(decimals) + " day";
  562. }
  563. // Less than one year, devide in week
  564. else if (Math.abs(size) < 31536000) {
  565. return (size / 604800).toFixed(decimals) + " week";
  566. }
  567. return (size / 3.15569e7).toFixed(decimals) + " year";
  568. };
  569. kbn.microsFormat = function(size, decimals) {
  570. // Less than 1 micro, downscale to nano
  571. if (Math.abs(size) < 1) {
  572. return kbn.nanosFormat(size * 1000, decimals);
  573. }
  574. else if (Math.abs(size) < 1000) {
  575. return size.toFixed(decimals) + " µs";
  576. }
  577. else if (Math.abs(size) < 1000000) {
  578. return (size / 1000).toFixed(decimals) + " ms";
  579. }
  580. else {
  581. return (size / 1000000).toFixed(decimals) + " s";
  582. }
  583. };
  584. kbn.nanosFormat = function(size, decimals) {
  585. if (Math.abs(size) < 1) {
  586. return size.toFixed(decimals) + " ns";
  587. }
  588. else if (Math.abs(size) < 1000) {
  589. return size.toFixed(0) + " ns";
  590. }
  591. else if (Math.abs(size) < 1000000) {
  592. return (size / 1000).toFixed(decimals) + " µs";
  593. }
  594. else if (Math.abs(size) < 1000000000) {
  595. return (size / 1000000).toFixed(decimals) + " ms";
  596. }
  597. else if (Math.abs(size) < 60000000000){
  598. return (size / 1000000000).toFixed(decimals) + " s";
  599. }
  600. else {
  601. return (size / 60000000000).toFixed(decimals) + " m";
  602. }
  603. };
  604. return kbn;
  605. });