kbn.js 27 KB

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