kbn.js 27 KB

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