kbn.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. define([
  2. 'jquery',
  3. 'lodash'
  4. ],
  5. function($, _) {
  6. 'use strict';
  7. var kbn = {};
  8. kbn.valueFormats = {};
  9. kbn.regexEscape = function(value) {
  10. return value.replace(/[\\^$*+?.()|[\]{}\/]/g, '\\$&');
  11. };
  12. ///// HELPER FUNCTIONS /////
  13. kbn.round_interval = function(interval) {
  14. switch (true) {
  15. // 0.015s
  16. case (interval <= 15):
  17. return 10; // 0.01s
  18. // 0.035s
  19. case (interval <= 35):
  20. return 20; // 0.02s
  21. // 0.075s
  22. case (interval <= 75):
  23. return 50; // 0.05s
  24. // 0.15s
  25. case (interval <= 150):
  26. return 100; // 0.1s
  27. // 0.35s
  28. case (interval <= 350):
  29. return 200; // 0.2s
  30. // 0.75s
  31. case (interval <= 750):
  32. return 500; // 0.5s
  33. // 1.5s
  34. case (interval <= 1500):
  35. return 1000; // 1s
  36. // 3.5s
  37. case (interval <= 3500):
  38. return 2000; // 2s
  39. // 7.5s
  40. case (interval <= 7500):
  41. return 5000; // 5s
  42. // 12.5s
  43. case (interval <= 12500):
  44. return 10000; // 10s
  45. // 17.5s
  46. case (interval <= 17500):
  47. return 15000; // 15s
  48. // 25s
  49. case (interval <= 25000):
  50. return 20000; // 20s
  51. // 45s
  52. case (interval <= 45000):
  53. return 30000; // 30s
  54. // 1.5m
  55. case (interval <= 90000):
  56. return 60000; // 1m
  57. // 3.5m
  58. case (interval <= 210000):
  59. return 120000; // 2m
  60. // 7.5m
  61. case (interval <= 450000):
  62. return 300000; // 5m
  63. // 12.5m
  64. case (interval <= 750000):
  65. return 600000; // 10m
  66. // 12.5m
  67. case (interval <= 1050000):
  68. return 900000; // 15m
  69. // 25m
  70. case (interval <= 1500000):
  71. return 1200000; // 20m
  72. // 45m
  73. case (interval <= 2700000):
  74. return 1800000; // 30m
  75. // 1.5h
  76. case (interval <= 5400000):
  77. return 3600000; // 1h
  78. // 2.5h
  79. case (interval <= 9000000):
  80. return 7200000; // 2h
  81. // 4.5h
  82. case (interval <= 16200000):
  83. return 10800000; // 3h
  84. // 9h
  85. case (interval <= 32400000):
  86. return 21600000; // 6h
  87. // 24h
  88. case (interval <= 86400000):
  89. return 43200000; // 12h
  90. // 48h
  91. case (interval <= 172800000):
  92. return 86400000; // 24h
  93. // 1w
  94. case (interval <= 604800000):
  95. return 86400000; // 24h
  96. // 3w
  97. case (interval <= 1814400000):
  98. return 604800000; // 1w
  99. // 2y
  100. case (interval < 3628800000):
  101. return 2592000000; // 30d
  102. default:
  103. return 31536000000; // 1y
  104. }
  105. };
  106. kbn.secondsToHms = function(seconds) {
  107. var numyears = Math.floor(seconds / 31536000);
  108. if(numyears){
  109. return numyears + 'y';
  110. }
  111. var numdays = Math.floor((seconds % 31536000) / 86400);
  112. if(numdays){
  113. return numdays + 'd';
  114. }
  115. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  116. if(numhours){
  117. return numhours + 'h';
  118. }
  119. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  120. if(numminutes){
  121. return numminutes + 'm';
  122. }
  123. var numseconds = Math.floor((((seconds % 31536000) % 86400) % 3600) % 60);
  124. if(numseconds){
  125. return numseconds + 's';
  126. }
  127. var nummilliseconds = Math.floor(seconds * 1000.0);
  128. if(nummilliseconds){
  129. return nummilliseconds + 'ms';
  130. }
  131. return 'less then a millisecond'; //'just now' //or other string you like;
  132. };
  133. kbn.to_percent = function(number,outof) {
  134. return Math.floor((number/outof)*10000)/100 + "%";
  135. };
  136. kbn.addslashes = function(str) {
  137. str = str.replace(/\\/g, '\\\\');
  138. str = str.replace(/\'/g, '\\\'');
  139. str = str.replace(/\"/g, '\\"');
  140. str = str.replace(/\0/g, '\\0');
  141. return str;
  142. };
  143. kbn.interval_regex = /(\d+(?:\.\d+)?)(ms|[Mwdhmsy])/;
  144. // histogram & trends
  145. kbn.intervals_in_seconds = {
  146. y: 31536000,
  147. M: 2592000,
  148. w: 604800,
  149. d: 86400,
  150. h: 3600,
  151. m: 60,
  152. s: 1,
  153. ms: 0.001
  154. };
  155. kbn.calculateInterval = function(range, resolution, userInterval) {
  156. var lowLimitMs = 1; // 1 millisecond default low limit
  157. var intervalMs, lowLimitInterval;
  158. if (userInterval) {
  159. if (userInterval[0] === '>') {
  160. lowLimitInterval = userInterval.slice(1);
  161. lowLimitMs = kbn.interval_to_ms(lowLimitInterval);
  162. }
  163. else {
  164. return {
  165. intervalMs: kbn.interval_to_ms(userInterval),
  166. interval: userInterval,
  167. };
  168. }
  169. }
  170. intervalMs = kbn.round_interval((range.to.valueOf() - range.from.valueOf()) / resolution);
  171. if (lowLimitMs > intervalMs) {
  172. intervalMs = lowLimitMs;
  173. }
  174. return {
  175. intervalMs: intervalMs,
  176. interval: kbn.secondsToHms(intervalMs / 1000),
  177. };
  178. };
  179. kbn.describe_interval = function (string) {
  180. var matches = string.match(kbn.interval_regex);
  181. if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
  182. throw new Error('Invalid interval string, expecting a number followed by one of "Mwdhmsy"');
  183. } else {
  184. return {
  185. sec: kbn.intervals_in_seconds[matches[2]],
  186. type: matches[2],
  187. count: parseInt(matches[1], 10)
  188. };
  189. }
  190. };
  191. kbn.interval_to_ms = function(string) {
  192. var info = kbn.describe_interval(string);
  193. return info.sec * 1000 * info.count;
  194. };
  195. kbn.interval_to_seconds = function (string) {
  196. var info = kbn.describe_interval(string);
  197. return info.sec * info.count;
  198. };
  199. kbn.query_color_dot = function (color, diameter) {
  200. return '<div class="icon-circle" style="' + [
  201. 'display:inline-block',
  202. 'color:' + color,
  203. 'font-size:' + diameter + 'px',
  204. ].join(';') + '"></div>';
  205. };
  206. kbn.slugifyForUrl = function(str) {
  207. return str
  208. .toLowerCase()
  209. .replace(/[^\w ]+/g,'')
  210. .replace(/ +/g,'-');
  211. };
  212. kbn.stringToJsRegex = function(str) {
  213. if (str[0] !== '/') {
  214. return new RegExp('^' + str + '$');
  215. }
  216. var match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
  217. return new RegExp(match[1], match[2]);
  218. };
  219. kbn.toFixed = function(value, decimals) {
  220. if (value === null) {
  221. return "";
  222. }
  223. var factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
  224. var formatted = String(Math.round(value * factor) / factor);
  225. // if exponent return directly
  226. if (formatted.indexOf('e') !== -1 || value === 0) {
  227. return formatted;
  228. }
  229. // If tickDecimals was specified, ensure that we have exactly that
  230. // much precision; otherwise default to the value's own precision.
  231. if (decimals != null) {
  232. var decimalPos = formatted.indexOf(".");
  233. var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1;
  234. if (precision < decimals) {
  235. return (precision ? formatted : formatted + ".") + (String(factor)).substr(1, decimals - precision);
  236. }
  237. }
  238. return formatted;
  239. };
  240. kbn.toFixedScaled = function(value, decimals, scaledDecimals, additionalDecimals, ext) {
  241. if (scaledDecimals === null) {
  242. return kbn.toFixed(value, decimals) + ext;
  243. } else {
  244. return kbn.toFixed(value, scaledDecimals + additionalDecimals) + ext;
  245. }
  246. };
  247. kbn.roundValue = function (num, decimals) {
  248. if (num === null) { return null; }
  249. var n = Math.pow(10, decimals);
  250. return Math.round((n * num).toFixed(decimals)) / n;
  251. };
  252. ///// FORMAT FUNCTION CONSTRUCTORS /////
  253. kbn.formatBuilders = {};
  254. // Formatter which always appends a fixed unit string to the value. No
  255. // scaling of the value is performed.
  256. kbn.formatBuilders.fixedUnit = function(unit) {
  257. return function(size, decimals) {
  258. if (size === null) { return ""; }
  259. return kbn.toFixed(size, decimals) + ' ' + unit;
  260. };
  261. };
  262. // Formatter which scales the unit string geometrically according to the given
  263. // numeric factor. Repeatedly scales the value down by the factor until it is
  264. // less than the factor in magnitude, or the end of the array is reached.
  265. kbn.formatBuilders.scaledUnits = function(factor, extArray) {
  266. return function(size, decimals, scaledDecimals) {
  267. if (size === null) {
  268. return "";
  269. }
  270. var steps = 0;
  271. var limit = extArray.length;
  272. while (Math.abs(size) >= factor) {
  273. steps++;
  274. size /= factor;
  275. if (steps >= limit) { return "NA"; }
  276. }
  277. if (steps > 0 && scaledDecimals !== null) {
  278. decimals = scaledDecimals + (3 * steps);
  279. }
  280. return kbn.toFixed(size, decimals) + extArray[steps];
  281. };
  282. };
  283. // Extension of the scaledUnits builder which uses SI decimal prefixes. If an
  284. // offset is given, it adjusts the starting units at the given prefix; a value
  285. // of 0 starts at no scale; -3 drops to nano, +2 starts at mega, etc.
  286. kbn.formatBuilders.decimalSIPrefix = function(unit, offset) {
  287. var prefixes = ['n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
  288. prefixes = prefixes.slice(3 + (offset || 0));
  289. var units = prefixes.map(function(p) { return ' ' + p + unit; });
  290. return kbn.formatBuilders.scaledUnits(1000, units);
  291. };
  292. // Extension of the scaledUnits builder which uses SI binary prefixes. If
  293. // offset is given, it starts the units at the given prefix; otherwise, the
  294. // offset defaults to zero and the initial unit is not prefixed.
  295. kbn.formatBuilders.binarySIPrefix = function(unit, offset) {
  296. var prefixes = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'].slice(offset);
  297. var units = prefixes.map(function(p) { return ' ' + p + unit; });
  298. return kbn.formatBuilders.scaledUnits(1024, units);
  299. };
  300. // Currency formatter for prefixing a symbol onto a number. Supports scaling
  301. // up to the trillions.
  302. kbn.formatBuilders.currency = function(symbol) {
  303. var units = ['', 'K', 'M', 'B', 'T'];
  304. var scaler = kbn.formatBuilders.scaledUnits(1000, units);
  305. return function(size, decimals, scaledDecimals) {
  306. if (size === null) { return ""; }
  307. var scaled = scaler(size, decimals, scaledDecimals);
  308. return symbol + scaled;
  309. };
  310. };
  311. kbn.formatBuilders.simpleCountUnit = function(symbol) {
  312. var units = ['', 'K', 'M', 'B', 'T'];
  313. var scaler = kbn.formatBuilders.scaledUnits(1000, units);
  314. return function(size, decimals, scaledDecimals) {
  315. if (size === null) { return ""; }
  316. var scaled = scaler(size, decimals, scaledDecimals);
  317. return scaled + " " + symbol;
  318. };
  319. };
  320. ///// VALUE FORMATS /////
  321. // Dimensionless Units
  322. kbn.valueFormats.none = kbn.toFixed;
  323. kbn.valueFormats.short = kbn.formatBuilders.scaledUnits(1000, ['', ' K', ' Mil', ' Bil', ' Tri', ' Quadr', ' Quint', ' Sext', ' Sept']);
  324. kbn.valueFormats.dB = kbn.formatBuilders.fixedUnit('dB');
  325. kbn.valueFormats.ppm = kbn.formatBuilders.fixedUnit('ppm');
  326. kbn.valueFormats.percent = function(size, decimals) {
  327. if (size === null) { return ""; }
  328. return kbn.toFixed(size, decimals) + '%';
  329. };
  330. kbn.valueFormats.percentunit = function(size, decimals) {
  331. if (size === null) { return ""; }
  332. return kbn.toFixed(100*size, decimals) + '%';
  333. };
  334. /* Formats the value to hex. Uses float if specified decimals are not 0.
  335. * There are two options, one with 0x, and one without */
  336. kbn.valueFormats.hex = function(value, decimals) {
  337. if (value == null) { return ""; }
  338. return parseFloat(kbn.toFixed(value, decimals)).toString(16).toUpperCase();
  339. };
  340. kbn.valueFormats.hex0x = function(value, decimals) {
  341. if (value == null) { return ""; }
  342. var hexString = kbn.valueFormats.hex(value, decimals);
  343. if (hexString.substring(0,1) === "-") {
  344. return "-0x" + hexString.substring(1);
  345. }
  346. return "0x" + hexString;
  347. };
  348. // Currencies
  349. kbn.valueFormats.currencyUSD = kbn.formatBuilders.currency('$');
  350. kbn.valueFormats.currencyGBP = kbn.formatBuilders.currency('£');
  351. kbn.valueFormats.currencyEUR = kbn.formatBuilders.currency('€');
  352. kbn.valueFormats.currencyJPY = kbn.formatBuilders.currency('¥');
  353. kbn.valueFormats.currencyRUB = kbn.formatBuilders.currency('₽');
  354. // Data (Binary)
  355. kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b');
  356. kbn.valueFormats.bytes = kbn.formatBuilders.binarySIPrefix('B');
  357. kbn.valueFormats.kbytes = kbn.formatBuilders.binarySIPrefix('B', 1);
  358. kbn.valueFormats.mbytes = kbn.formatBuilders.binarySIPrefix('B', 2);
  359. kbn.valueFormats.gbytes = kbn.formatBuilders.binarySIPrefix('B', 3);
  360. // Data (Decimal)
  361. kbn.valueFormats.decbits = kbn.formatBuilders.decimalSIPrefix('b');
  362. kbn.valueFormats.decbytes = kbn.formatBuilders.decimalSIPrefix('B');
  363. kbn.valueFormats.deckbytes = kbn.formatBuilders.decimalSIPrefix('B', 1);
  364. kbn.valueFormats.decmbytes = kbn.formatBuilders.decimalSIPrefix('B', 2);
  365. kbn.valueFormats.decgbytes = kbn.formatBuilders.decimalSIPrefix('B', 3);
  366. // Data Rate
  367. kbn.valueFormats.pps = kbn.formatBuilders.decimalSIPrefix('pps');
  368. kbn.valueFormats.bps = kbn.formatBuilders.decimalSIPrefix('bps');
  369. kbn.valueFormats.Bps = kbn.formatBuilders.decimalSIPrefix('Bps');
  370. kbn.valueFormats.KBs = kbn.formatBuilders.decimalSIPrefix('Bs', 1);
  371. kbn.valueFormats.Kbits = kbn.formatBuilders.decimalSIPrefix('bps', 1);
  372. kbn.valueFormats.MBs = kbn.formatBuilders.decimalSIPrefix('Bs', 2);
  373. kbn.valueFormats.Mbits = kbn.formatBuilders.decimalSIPrefix('bps', 2);
  374. kbn.valueFormats.GBs = kbn.formatBuilders.decimalSIPrefix('Bs', 3);
  375. kbn.valueFormats.Gbits = kbn.formatBuilders.decimalSIPrefix('bps', 3);
  376. // Throughput
  377. kbn.valueFormats.ops = kbn.formatBuilders.simpleCountUnit('ops');
  378. kbn.valueFormats.rps = kbn.formatBuilders.simpleCountUnit('rps');
  379. kbn.valueFormats.wps = kbn.formatBuilders.simpleCountUnit('wps');
  380. kbn.valueFormats.iops = kbn.formatBuilders.simpleCountUnit('iops');
  381. kbn.valueFormats.opm = kbn.formatBuilders.simpleCountUnit('opm');
  382. kbn.valueFormats.rpm = kbn.formatBuilders.simpleCountUnit('rpm');
  383. kbn.valueFormats.wpm = kbn.formatBuilders.simpleCountUnit('wpm');
  384. // Energy
  385. kbn.valueFormats.watt = kbn.formatBuilders.decimalSIPrefix('W');
  386. kbn.valueFormats.kwatt = kbn.formatBuilders.decimalSIPrefix('W', 1);
  387. kbn.valueFormats.voltamp = kbn.formatBuilders.decimalSIPrefix('VA');
  388. kbn.valueFormats.kvoltamp = kbn.formatBuilders.decimalSIPrefix('VA', 1);
  389. kbn.valueFormats.voltampreact = kbn.formatBuilders.decimalSIPrefix('var');
  390. kbn.valueFormats.watth = kbn.formatBuilders.decimalSIPrefix('Wh');
  391. kbn.valueFormats.kwatth = kbn.formatBuilders.decimalSIPrefix('Wh', 1);
  392. kbn.valueFormats.joule = kbn.formatBuilders.decimalSIPrefix('J');
  393. kbn.valueFormats.ev = kbn.formatBuilders.decimalSIPrefix('eV');
  394. kbn.valueFormats.amp = kbn.formatBuilders.decimalSIPrefix('A');
  395. kbn.valueFormats.volt = kbn.formatBuilders.decimalSIPrefix('V');
  396. kbn.valueFormats.dBm = kbn.formatBuilders.decimalSIPrefix('dBm');
  397. // Temperature
  398. kbn.valueFormats.celsius = kbn.formatBuilders.fixedUnit('°C');
  399. kbn.valueFormats.farenheit = kbn.formatBuilders.fixedUnit('°F');
  400. kbn.valueFormats.kelvin = kbn.formatBuilders.fixedUnit('K');
  401. kbn.valueFormats.humidity = kbn.formatBuilders.fixedUnit('%H');
  402. // Pressure
  403. kbn.valueFormats.pressurembar = kbn.formatBuilders.fixedUnit('mbar');
  404. kbn.valueFormats.pressurehpa = kbn.formatBuilders.fixedUnit('hPa');
  405. kbn.valueFormats.pressurehg = kbn.formatBuilders.fixedUnit('"Hg');
  406. kbn.valueFormats.pressurepsi = kbn.formatBuilders.scaledUnits(1000, [' psi', ' ksi', ' Mpsi']);
  407. // Length
  408. kbn.valueFormats.lengthm = kbn.formatBuilders.decimalSIPrefix('m');
  409. kbn.valueFormats.lengthmm = kbn.formatBuilders.decimalSIPrefix('m', -1);
  410. kbn.valueFormats.lengthkm = kbn.formatBuilders.decimalSIPrefix('m', 1);
  411. kbn.valueFormats.lengthmi = kbn.formatBuilders.fixedUnit('mi');
  412. // Velocity
  413. kbn.valueFormats.velocityms = kbn.formatBuilders.fixedUnit('m/s');
  414. kbn.valueFormats.velocitykmh = kbn.formatBuilders.fixedUnit('km/h');
  415. kbn.valueFormats.velocitymph = kbn.formatBuilders.fixedUnit('mph');
  416. kbn.valueFormats.velocityknot = kbn.formatBuilders.fixedUnit('kn');
  417. // Volume
  418. kbn.valueFormats.litre = kbn.formatBuilders.decimalSIPrefix('L');
  419. kbn.valueFormats.mlitre = kbn.formatBuilders.decimalSIPrefix('L', -1);
  420. kbn.valueFormats.m3 = kbn.formatBuilders.decimalSIPrefix('m3');
  421. // Time
  422. kbn.valueFormats.hertz = kbn.formatBuilders.decimalSIPrefix('Hz');
  423. kbn.valueFormats.ms = function(size, decimals, scaledDecimals) {
  424. if (size === null) { return ""; }
  425. if (Math.abs(size) < 1000) {
  426. return kbn.toFixed(size, decimals) + " ms";
  427. }
  428. // Less than 1 min
  429. else if (Math.abs(size) < 60000) {
  430. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " s");
  431. }
  432. // Less than 1 hour, devide in minutes
  433. else if (Math.abs(size) < 3600000) {
  434. return kbn.toFixedScaled(size / 60000, decimals, scaledDecimals, 5, " min");
  435. }
  436. // Less than one day, devide in hours
  437. else if (Math.abs(size) < 86400000) {
  438. return kbn.toFixedScaled(size / 3600000, decimals, scaledDecimals, 7, " hour");
  439. }
  440. // Less than one year, devide in days
  441. else if (Math.abs(size) < 31536000000) {
  442. return kbn.toFixedScaled(size / 86400000, decimals, scaledDecimals, 8, " day");
  443. }
  444. return kbn.toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, " year");
  445. };
  446. kbn.valueFormats.s = function(size, decimals, scaledDecimals) {
  447. if (size === null) { return ""; }
  448. // Less than 1 µs, devide in ns
  449. if (Math.abs(size) < 0.000001) {
  450. return kbn.toFixedScaled(size * 1.e9, decimals, scaledDecimals - decimals, -9, " ns");
  451. }
  452. // Less than 1 ms, devide in µs
  453. if (Math.abs(size) < 0.001) {
  454. return kbn.toFixedScaled(size * 1.e6, decimals, scaledDecimals - decimals, -6, " µs");
  455. }
  456. // Less than 1 second, devide in ms
  457. if (Math.abs(size) < 1) {
  458. return kbn.toFixedScaled(size * 1.e3, decimals, scaledDecimals - decimals, -3, " ms");
  459. }
  460. if (Math.abs(size) < 60) {
  461. return kbn.toFixed(size, decimals) + " s";
  462. }
  463. // Less than 1 hour, devide in minutes
  464. else if (Math.abs(size) < 3600) {
  465. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 1, " min");
  466. }
  467. // Less than one day, devide in hours
  468. else if (Math.abs(size) < 86400) {
  469. return kbn.toFixedScaled(size / 3600, decimals, scaledDecimals, 4, " hour");
  470. }
  471. // Less than one week, devide in days
  472. else if (Math.abs(size) < 604800) {
  473. return kbn.toFixedScaled(size / 86400, decimals, scaledDecimals, 5, " day");
  474. }
  475. // Less than one year, devide in week
  476. else if (Math.abs(size) < 31536000) {
  477. return kbn.toFixedScaled(size / 604800, decimals, scaledDecimals, 6, " week");
  478. }
  479. return kbn.toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, " year");
  480. };
  481. kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) {
  482. if (size === null) { return ""; }
  483. if (Math.abs(size) < 1000) {
  484. return kbn.toFixed(size, decimals) + " µs";
  485. }
  486. else if (Math.abs(size) < 1000000) {
  487. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " ms");
  488. }
  489. else {
  490. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " s");
  491. }
  492. };
  493. kbn.valueFormats.ns = function(size, decimals, scaledDecimals) {
  494. if (size === null) { return ""; }
  495. if (Math.abs(size) < 1000) {
  496. return kbn.toFixed(size, decimals) + " ns";
  497. }
  498. else if (Math.abs(size) < 1000000) {
  499. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " µs");
  500. }
  501. else if (Math.abs(size) < 1000000000) {
  502. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " ms");
  503. }
  504. else if (Math.abs(size) < 60000000000){
  505. return kbn.toFixedScaled(size / 1000000000, decimals, scaledDecimals, 9, " s");
  506. }
  507. else {
  508. return kbn.toFixedScaled(size / 60000000000, decimals, scaledDecimals, 12, " min");
  509. }
  510. };
  511. kbn.valueFormats.m = function(size, decimals, scaledDecimals) {
  512. if (size === null) { return ""; }
  513. if (Math.abs(size) < 60) {
  514. return kbn.toFixed(size, decimals) + " min";
  515. }
  516. else if (Math.abs(size) < 1440) {
  517. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 2, " hour");
  518. }
  519. else if (Math.abs(size) < 10080) {
  520. return kbn.toFixedScaled(size / 1440, decimals, scaledDecimals, 3, " day");
  521. }
  522. else if (Math.abs(size) < 604800) {
  523. return kbn.toFixedScaled(size / 10080, decimals, scaledDecimals, 4, " week");
  524. }
  525. else {
  526. return kbn.toFixedScaled(size / 5.25948e5, decimals, scaledDecimals, 5, " year");
  527. }
  528. };
  529. kbn.valueFormats.h = function(size, decimals, scaledDecimals) {
  530. if (size === null) { return ""; }
  531. if (Math.abs(size) < 24) {
  532. return kbn.toFixed(size, decimals) + " hour";
  533. }
  534. else if (Math.abs(size) < 168) {
  535. return kbn.toFixedScaled(size / 24, decimals, scaledDecimals, 2, " day");
  536. }
  537. else if (Math.abs(size) < 8760) {
  538. return kbn.toFixedScaled(size / 168, decimals, scaledDecimals, 3, " week");
  539. }
  540. else {
  541. return kbn.toFixedScaled(size / 8760, decimals, scaledDecimals, 4, " year");
  542. }
  543. };
  544. kbn.valueFormats.d = function(size, decimals, scaledDecimals) {
  545. if (size === null) { return ""; }
  546. if (Math.abs(size) < 7) {
  547. return kbn.toFixed(size, decimals) + " day";
  548. }
  549. else if (Math.abs(size) < 365) {
  550. return kbn.toFixedScaled(size / 7, decimals, scaledDecimals, 2, " week");
  551. }
  552. else {
  553. return kbn.toFixedScaled(size / 365, decimals, scaledDecimals, 3, " year");
  554. }
  555. };
  556. kbn.toDuration = function(size, decimals, timeScale) {
  557. if (size === null) { return ""; }
  558. if (size === 0) { return "0 " + timeScale + "s"; }
  559. if (size < 0) { return kbn.toDuration(-size, decimals, timeScale) + " ago"; }
  560. var units = [
  561. {short: "y", long: "year"},
  562. {short: "M", long: "month"},
  563. {short: "w", long: "week"},
  564. {short: "d", long: "day"},
  565. {short: "h", long: "hour"},
  566. {short: "m", long: "minute"},
  567. {short: "s", long: "second"},
  568. {short: "ms", long: "millisecond"}
  569. ];
  570. // convert $size to milliseconds
  571. // intervals_in_seconds uses seconds (duh), convert them to milliseconds here to minimize floating point errors
  572. size *= kbn.intervals_in_seconds[units.find(function(e) { return e.long === timeScale; }).short] * 1000;
  573. var string = [];
  574. // after first value >= 1 print only $decimals more
  575. var decrementDecimals = false;
  576. for (var i = 0; i < units.length && decimals >= 0; i++) {
  577. var interval = kbn.intervals_in_seconds[units[i].short] * 1000;
  578. var value = size / interval;
  579. if (value >= 1 || decrementDecimals) {
  580. decrementDecimals = true;
  581. var floor = Math.floor(value);
  582. var unit = units[i].long + (floor !== 1 ? "s" : "");
  583. string.push(floor + " " + unit);
  584. size = size % interval;
  585. decimals--;
  586. }
  587. }
  588. return string.join(", ");
  589. };
  590. kbn.valueFormats.dtdurationms = function(size, decimals) {
  591. return kbn.toDuration(size, decimals, 'millisecond');
  592. };
  593. kbn.valueFormats.dtdurations = function(size, decimals) {
  594. return kbn.toDuration(size, decimals, 'second');
  595. };
  596. ///// FORMAT MENU /////
  597. kbn.getUnitFormats = function() {
  598. return [
  599. {
  600. text: 'none',
  601. submenu: [
  602. {text: 'none' , value: 'none' },
  603. {text: 'short', value: 'short' },
  604. {text: 'percent (0-100)', value: 'percent' },
  605. {text: 'percent (0.0-1.0)', value: 'percentunit'},
  606. {text: 'Humidity (%H)', value: 'humidity' },
  607. {text: 'ppm', value: 'ppm' },
  608. {text: 'decibel', value: 'dB' },
  609. {text: 'hexadecimal (0x)', value: 'hex0x' },
  610. {text: 'hexadecimal', value: 'hex' },
  611. ]
  612. },
  613. {
  614. text: 'currency',
  615. submenu: [
  616. {text: 'Dollars ($)', value: 'currencyUSD'},
  617. {text: 'Pounds (£)', value: 'currencyGBP'},
  618. {text: 'Euro (€)', value: 'currencyEUR'},
  619. {text: 'Yen (¥)', value: 'currencyJPY'},
  620. {text: 'Rubles (₽)', value: 'currencyRUB'},
  621. ]
  622. },
  623. {
  624. text: 'time',
  625. submenu: [
  626. {text: 'Hertz (1/s)', value: 'hertz'},
  627. {text: 'nanoseconds (ns)' , value: 'ns' },
  628. {text: 'microseconds (µs)', value: 'µs' },
  629. {text: 'milliseconds (ms)', value: 'ms' },
  630. {text: 'seconds (s)', value: 's' },
  631. {text: 'minutes (m)', value: 'm' },
  632. {text: 'hours (h)', value: 'h' },
  633. {text: 'days (d)', value: 'd' },
  634. {text: 'duration (ms)', value: 'dtdurationms' },
  635. {text: 'duration (s)', value: 'dtdurations' }
  636. ]
  637. },
  638. {
  639. text: 'data (IEC)',
  640. submenu: [
  641. {text: 'bits', value: 'bits' },
  642. {text: 'bytes', value: 'bytes' },
  643. {text: 'kibibytes', value: 'kbytes'},
  644. {text: 'mebibytes', value: 'mbytes'},
  645. {text: 'gibibytes', value: 'gbytes'},
  646. ]
  647. },
  648. {
  649. text: 'data (Metric)',
  650. submenu: [
  651. {text: 'bits', value: 'decbits' },
  652. {text: 'bytes', value: 'decbytes' },
  653. {text: 'kilobytes', value: 'deckbytes'},
  654. {text: 'megabytes', value: 'decmbytes'},
  655. {text: 'gigabytes', value: 'decgbytes'},
  656. ]
  657. },
  658. {
  659. text: 'data rate',
  660. submenu: [
  661. {text: 'packets/sec', value: 'pps'},
  662. {text: 'bits/sec', value: 'bps'},
  663. {text: 'bytes/sec', value: 'Bps'},
  664. {text: 'kilobits/sec', value: 'Kbits'},
  665. {text: 'kilobytes/sec', value: 'KBs'},
  666. {text: 'megabits/sec', value: 'Mbits'},
  667. {text: 'megabytes/sec', value: 'MBs'},
  668. {text: 'gigabytes/sec', value: 'GBs'},
  669. {text: 'gigabits/sec', value: 'Gbits'},
  670. ]
  671. },
  672. {
  673. text: 'throughput',
  674. submenu: [
  675. {text: 'ops/sec (ops)', value: 'ops' },
  676. {text: 'reads/sec (rps)', value: 'rps' },
  677. {text: 'writes/sec (wps)', value: 'wps' },
  678. {text: 'I/O ops/sec (iops)', value: 'iops'},
  679. {text: 'ops/min (opm)', value: 'opm' },
  680. {text: 'reads/min (rpm)', value: 'rpm' },
  681. {text: 'writes/min (wpm)', value: 'wpm' },
  682. ]
  683. },
  684. {
  685. text: 'length',
  686. submenu: [
  687. {text: 'millimetre (mm)', value: 'lengthmm'},
  688. {text: 'meter (m)', value: 'lengthm' },
  689. {text: 'kilometer (km)', value: 'lengthkm'},
  690. {text: 'mile (mi)', value: 'lengthmi'},
  691. ]
  692. },
  693. {
  694. text: 'velocity',
  695. submenu: [
  696. {text: 'm/s', value: 'velocityms' },
  697. {text: 'km/h', value: 'velocitykmh' },
  698. {text: 'mph', value: 'velocitymph' },
  699. {text: 'knot (kn)', value: 'velocityknot'},
  700. ]
  701. },
  702. {
  703. text: 'volume',
  704. submenu: [
  705. {text: 'millilitre', value: 'mlitre'},
  706. {text: 'litre', value: 'litre' },
  707. {text: 'cubic metre', value: 'm3' },
  708. ]
  709. },
  710. {
  711. text: 'energy',
  712. submenu: [
  713. {text: 'watt (W)', value: 'watt' },
  714. {text: 'kilowatt (kW)', value: 'kwatt' },
  715. {text: 'volt-ampere (VA)', value: 'voltamp' },
  716. {text: 'kilovolt-ampere (kVA)', value: 'kvoltamp' },
  717. {text: 'volt-ampere reactive (var)', value: 'voltampreact'},
  718. {text: 'watt-hour (Wh)', value: 'watth' },
  719. {text: 'kilowatt-hour (kWh)', value: 'kwatth' },
  720. {text: 'joule (J)', value: 'joule' },
  721. {text: 'electron volt (eV)', value: 'ev' },
  722. {text: 'Ampere (A)', value: 'amp' },
  723. {text: 'Volt (V)', value: 'volt' },
  724. {text: 'Decibel-milliwatt (dBm)', value: 'dBm' },
  725. ]
  726. },
  727. {
  728. text: 'temperature',
  729. submenu: [
  730. {text: 'Celcius (°C)', value: 'celsius' },
  731. {text: 'Farenheit (°F)', value: 'farenheit' },
  732. {text: 'Kelvin (K)', value: 'kelvin' },
  733. ]
  734. },
  735. {
  736. text: 'pressure',
  737. submenu: [
  738. {text: 'Millibars', value: 'pressurembar'},
  739. {text: 'Hectopascals', value: 'pressurehpa' },
  740. {text: 'Inches of mercury', value: 'pressurehg' },
  741. {text: 'PSI', value: 'pressurepsi' },
  742. ]
  743. }
  744. ];
  745. };
  746. return kbn;
  747. });