kbn.js 23 KB

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