kbn.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. // Currencies
  327. kbn.valueFormats.currencyUSD = kbn.formatBuilders.currency('$');
  328. kbn.valueFormats.currencyGBP = kbn.formatBuilders.currency('£');
  329. kbn.valueFormats.currencyEUR = kbn.formatBuilders.currency('€');
  330. kbn.valueFormats.currencyJPY = kbn.formatBuilders.currency('¥');
  331. // Data
  332. kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b');
  333. kbn.valueFormats.bytes = kbn.formatBuilders.binarySIPrefix('B');
  334. kbn.valueFormats.kbytes = kbn.formatBuilders.binarySIPrefix('B', 1);
  335. kbn.valueFormats.mbytes = kbn.formatBuilders.binarySIPrefix('B', 2);
  336. kbn.valueFormats.gbytes = kbn.formatBuilders.binarySIPrefix('B', 3);
  337. // Data Rate
  338. kbn.valueFormats.pps = kbn.formatBuilders.decimalSIPrefix('pps');
  339. kbn.valueFormats.bps = kbn.formatBuilders.decimalSIPrefix('bps');
  340. kbn.valueFormats.Bps = kbn.formatBuilders.decimalSIPrefix('Bps');
  341. kbn.valueFormats.KBs = kbn.formatBuilders.decimalSIPrefix('Bs', 1);
  342. kbn.valueFormats.Kbits = kbn.formatBuilders.decimalSIPrefix('bits', 1);
  343. kbn.valueFormats.MBs = kbn.formatBuilders.decimalSIPrefix('Bs', 2);
  344. kbn.valueFormats.Mbits = kbn.formatBuilders.decimalSIPrefix('bits', 2);
  345. kbn.valueFormats.GBs = kbn.formatBuilders.decimalSIPrefix('Bs', 3);
  346. kbn.valueFormats.Gbits = kbn.formatBuilders.decimalSIPrefix('bits', 3);
  347. // Throughput
  348. kbn.valueFormats.ops = kbn.formatBuilders.simpleCountUnit('ops');
  349. kbn.valueFormats.rps = kbn.formatBuilders.simpleCountUnit('rps');
  350. kbn.valueFormats.wps = kbn.formatBuilders.simpleCountUnit('wps');
  351. kbn.valueFormats.iops = kbn.formatBuilders.simpleCountUnit('iops');
  352. // Energy
  353. kbn.valueFormats.watt = kbn.formatBuilders.decimalSIPrefix('W');
  354. kbn.valueFormats.kwatt = kbn.formatBuilders.decimalSIPrefix('W', 1);
  355. kbn.valueFormats.voltamp = kbn.formatBuilders.decimalSIPrefix('VA');
  356. kbn.valueFormats.kvoltamp = kbn.formatBuilders.decimalSIPrefix('VA', 1);
  357. kbn.valueFormats.voltampreact = kbn.formatBuilders.decimalSIPrefix('var');
  358. kbn.valueFormats.watth = kbn.formatBuilders.decimalSIPrefix('Wh');
  359. kbn.valueFormats.kwatth = kbn.formatBuilders.decimalSIPrefix('Wh', 1);
  360. kbn.valueFormats.joule = kbn.formatBuilders.decimalSIPrefix('J');
  361. kbn.valueFormats.ev = kbn.formatBuilders.decimalSIPrefix('eV');
  362. kbn.valueFormats.amp = kbn.formatBuilders.decimalSIPrefix('A');
  363. kbn.valueFormats.volt = kbn.formatBuilders.decimalSIPrefix('V');
  364. kbn.valueFormats.dBm = kbn.formatBuilders.decimalSIPrefix('dBm');
  365. // Temperature
  366. kbn.valueFormats.celsius = kbn.formatBuilders.fixedUnit('°C');
  367. kbn.valueFormats.farenheit = kbn.formatBuilders.fixedUnit('°F');
  368. kbn.valueFormats.kelvin = kbn.formatBuilders.fixedUnit('K');
  369. kbn.valueFormats.humidity = kbn.formatBuilders.fixedUnit('%H');
  370. // Pressure
  371. kbn.valueFormats.pressurembar = kbn.formatBuilders.fixedUnit('mbar');
  372. kbn.valueFormats.pressurehpa = kbn.formatBuilders.fixedUnit('hPa');
  373. kbn.valueFormats.pressurehg = kbn.formatBuilders.fixedUnit('"Hg');
  374. kbn.valueFormats.pressurepsi = kbn.formatBuilders.scaledUnits(1000, [' psi', ' ksi', ' Mpsi']);
  375. // Length
  376. kbn.valueFormats.lengthm = kbn.formatBuilders.decimalSIPrefix('m');
  377. kbn.valueFormats.lengthmm = kbn.formatBuilders.decimalSIPrefix('m', -1);
  378. kbn.valueFormats.lengthkm = kbn.formatBuilders.decimalSIPrefix('m', 1);
  379. kbn.valueFormats.lengthmi = kbn.formatBuilders.fixedUnit('mi');
  380. // Velocity
  381. kbn.valueFormats.velocityms = kbn.formatBuilders.fixedUnit('m/s');
  382. kbn.valueFormats.velocitykmh = kbn.formatBuilders.fixedUnit('km/h');
  383. kbn.valueFormats.velocitymph = kbn.formatBuilders.fixedUnit('mph');
  384. kbn.valueFormats.velocityknot = kbn.formatBuilders.fixedUnit('kn');
  385. // Volume
  386. kbn.valueFormats.litre = kbn.formatBuilders.decimalSIPrefix('L');
  387. kbn.valueFormats.mlitre = kbn.formatBuilders.decimalSIPrefix('L', -1);
  388. kbn.valueFormats.m3 = kbn.formatBuilders.decimalSIPrefix('m3');
  389. // Time
  390. kbn.valueFormats.hertz = kbn.formatBuilders.decimalSIPrefix('Hz');
  391. kbn.valueFormats.ms = function(size, decimals, scaledDecimals) {
  392. if (size === null) { return ""; }
  393. if (Math.abs(size) < 1000) {
  394. return kbn.toFixed(size, decimals) + " ms";
  395. }
  396. // Less than 1 min
  397. else if (Math.abs(size) < 60000) {
  398. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " s");
  399. }
  400. // Less than 1 hour, devide in minutes
  401. else if (Math.abs(size) < 3600000) {
  402. return kbn.toFixedScaled(size / 60000, decimals, scaledDecimals, 5, " min");
  403. }
  404. // Less than one day, devide in hours
  405. else if (Math.abs(size) < 86400000) {
  406. return kbn.toFixedScaled(size / 3600000, decimals, scaledDecimals, 7, " hour");
  407. }
  408. // Less than one year, devide in days
  409. else if (Math.abs(size) < 31536000000) {
  410. return kbn.toFixedScaled(size / 86400000, decimals, scaledDecimals, 8, " day");
  411. }
  412. return kbn.toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, " year");
  413. };
  414. kbn.valueFormats.s = function(size, decimals, scaledDecimals) {
  415. if (size === null) { return ""; }
  416. if (Math.abs(size) < 60) {
  417. return kbn.toFixed(size, decimals) + " s";
  418. }
  419. // Less than 1 hour, devide in minutes
  420. else if (Math.abs(size) < 3600) {
  421. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 1, " min");
  422. }
  423. // Less than one day, devide in hours
  424. else if (Math.abs(size) < 86400) {
  425. return kbn.toFixedScaled(size / 3600, decimals, scaledDecimals, 4, " hour");
  426. }
  427. // Less than one week, devide in days
  428. else if (Math.abs(size) < 604800) {
  429. return kbn.toFixedScaled(size / 86400, decimals, scaledDecimals, 5, " day");
  430. }
  431. // Less than one year, devide in week
  432. else if (Math.abs(size) < 31536000) {
  433. return kbn.toFixedScaled(size / 604800, decimals, scaledDecimals, 6, " week");
  434. }
  435. return kbn.toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, " year");
  436. };
  437. kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) {
  438. if (size === null) { return ""; }
  439. if (Math.abs(size) < 1000) {
  440. return kbn.toFixed(size, decimals) + " µs";
  441. }
  442. else if (Math.abs(size) < 1000000) {
  443. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " ms");
  444. }
  445. else {
  446. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " s");
  447. }
  448. };
  449. kbn.valueFormats.ns = function(size, decimals, scaledDecimals) {
  450. if (size === null) { return ""; }
  451. if (Math.abs(size) < 1000) {
  452. return kbn.toFixed(size, decimals) + " ns";
  453. }
  454. else if (Math.abs(size) < 1000000) {
  455. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " µs");
  456. }
  457. else if (Math.abs(size) < 1000000000) {
  458. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " ms");
  459. }
  460. else if (Math.abs(size) < 60000000000){
  461. return kbn.toFixedScaled(size / 1000000000, decimals, scaledDecimals, 9, " s");
  462. }
  463. else {
  464. return kbn.toFixedScaled(size / 60000000000, decimals, scaledDecimals, 12, " min");
  465. }
  466. };
  467. kbn.valueFormats.m = function(size, decimals, scaledDecimals) {
  468. if (size === null) { return ""; }
  469. if (Math.abs(size) < 60) {
  470. return kbn.toFixed(size, decimals) + " min";
  471. }
  472. else if (Math.abs(size) < 1440) {
  473. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 2, " hour");
  474. }
  475. else if (Math.abs(size) < 10080) {
  476. return kbn.toFixedScaled(size / 1440, decimals, scaledDecimals, 3, " day");
  477. }
  478. else if (Math.abs(size) < 604800) {
  479. return kbn.toFixedScaled(size / 10080, decimals, scaledDecimals, 4, " week");
  480. }
  481. else {
  482. return kbn.toFixedScaled(size / 5.25948e5, decimals, scaledDecimals, 5, " year");
  483. }
  484. };
  485. kbn.valueFormats.h = function(size, decimals, scaledDecimals) {
  486. if (size === null) { return ""; }
  487. if (Math.abs(size) < 24) {
  488. return kbn.toFixed(size, decimals) + " hour";
  489. }
  490. else if (Math.abs(size) < 168) {
  491. return kbn.toFixedScaled(size / 24, decimals, scaledDecimals, 2, " day");
  492. }
  493. else if (Math.abs(size) < 8760) {
  494. return kbn.toFixedScaled(size / 168, decimals, scaledDecimals, 3, " week");
  495. }
  496. else {
  497. return kbn.toFixedScaled(size / 8760, decimals, scaledDecimals, 4, " year");
  498. }
  499. };
  500. kbn.valueFormats.d = function(size, decimals, scaledDecimals) {
  501. if (size === null) { return ""; }
  502. if (Math.abs(size) < 7) {
  503. return kbn.toFixed(size, decimals) + " day";
  504. }
  505. else if (Math.abs(size) < 365) {
  506. return kbn.toFixedScaled(size / 7, decimals, scaledDecimals, 2, " week");
  507. }
  508. else {
  509. return kbn.toFixedScaled(size / 365, decimals, scaledDecimals, 3, " year");
  510. }
  511. };
  512. kbn.toDuration = function(size, timeScale) {
  513. return moment.duration(size, timeScale);
  514. };
  515. kbn.valueFormats.dtdurationms = function(size) {
  516. return kbn.toDuration(size, 'ms').humanize();
  517. };
  518. kbn.valueFormats.dtdurations = function(size) {
  519. return kbn.toDuration(size, 's').humanize();
  520. };
  521. ///// FORMAT MENU /////
  522. kbn.getUnitFormats = function() {
  523. return [
  524. {
  525. text: 'none',
  526. submenu: [
  527. {text: 'none' , value: 'none' },
  528. {text: 'short', value: 'short' },
  529. {text: 'percent (0-100)', value: 'percent' },
  530. {text: 'percent (0.0-1.0)', value: 'percentunit'},
  531. {text: 'Humidity (%H)', value: 'humidity' },
  532. {text: 'ppm', value: 'ppm' },
  533. {text: 'decibel', value: 'dB' },
  534. ]
  535. },
  536. {
  537. text: 'currency',
  538. submenu: [
  539. {text: 'Dollars ($)', value: 'currencyUSD'},
  540. {text: 'Pounds (£)', value: 'currencyGBP'},
  541. {text: 'Euro (€)', value: 'currencyEUR'},
  542. {text: 'Yen (¥)', value: 'currencyJPY'},
  543. ]
  544. },
  545. {
  546. text: 'time',
  547. submenu: [
  548. {text: 'Hertz (1/s)', value: 'hertz'},
  549. {text: 'nanoseconds (ns)' , value: 'ns' },
  550. {text: 'microseconds (µs)', value: 'µs' },
  551. {text: 'milliseconds (ms)', value: 'ms' },
  552. {text: 'seconds (s)', value: 's' },
  553. {text: 'minutes (m)', value: 'm' },
  554. {text: 'hours (h)', value: 'h' },
  555. {text: 'days (d)', value: 'd' },
  556. {text: 'duration (ms)', value: 'dtdurationms' },
  557. {text: 'duration (s)', value: 'dtdurations' }
  558. ]
  559. },
  560. {
  561. text: 'data',
  562. submenu: [
  563. {text: 'bits', value: 'bits' },
  564. {text: 'bytes', value: 'bytes' },
  565. {text: 'kilobytes', value: 'kbytes'},
  566. {text: 'megabytes', value: 'mbytes'},
  567. {text: 'gigabytes', value: 'gbytes'},
  568. ]
  569. },
  570. {
  571. text: 'data rate',
  572. submenu: [
  573. {text: 'packets/sec', value: 'pps'},
  574. {text: 'bits/sec', value: 'bps'},
  575. {text: 'bytes/sec', value: 'Bps'},
  576. {text: 'kilobits/sec', value: 'Kbits'},
  577. {text: 'kilobytes/sec', value: 'KBs'},
  578. {text: 'megabits/sec', value: 'Mbits'},
  579. {text: 'megabytes/sec', value: 'MBs'},
  580. {text: 'gigabytes/sec', value: 'GBs'},
  581. {text: 'gigabits/sec', value: 'Gbits'},
  582. ]
  583. },
  584. {
  585. text: 'throughput',
  586. submenu: [
  587. {text: 'ops/sec (ops)', value: 'ops' },
  588. {text: 'reads/sec (rps)', value: 'rps' },
  589. {text: 'writes/sec (wps)', value: 'wps' },
  590. {text: 'I/O ops/sec (iops)', value: 'iops'},
  591. ]
  592. },
  593. {
  594. text: 'length',
  595. submenu: [
  596. {text: 'millimetre (mm)', value: 'lengthmm'},
  597. {text: 'meter (m)', value: 'lengthm' },
  598. {text: 'kilometer (km)', value: 'lengthkm'},
  599. {text: 'mile (mi)', value: 'lengthmi'},
  600. ]
  601. },
  602. {
  603. text: 'velocity',
  604. submenu: [
  605. {text: 'm/s', value: 'velocityms' },
  606. {text: 'km/h', value: 'velocitykmh' },
  607. {text: 'mph', value: 'velocitymph' },
  608. {text: 'knot (kn)', value: 'velocityknot'},
  609. ]
  610. },
  611. {
  612. text: 'volume',
  613. submenu: [
  614. {text: 'millilitre', value: 'mlitre'},
  615. {text: 'litre', value: 'litre' },
  616. {text: 'cubic metre', value: 'm3' },
  617. ]
  618. },
  619. {
  620. text: 'energy',
  621. submenu: [
  622. {text: 'watt (W)', value: 'watt' },
  623. {text: 'kilowatt (kW)', value: 'kwatt' },
  624. {text: 'volt-ampere (VA)', value: 'voltamp' },
  625. {text: 'kilovolt-ampere (kVA)', value: 'kvoltamp' },
  626. {text: 'volt-ampere reactive (var)', value: 'voltampreact'},
  627. {text: 'watt-hour (Wh)', value: 'watth' },
  628. {text: 'kilowatt-hour (kWh)', value: 'kwatth' },
  629. {text: 'joule (J)', value: 'joule' },
  630. {text: 'electron volt (eV)', value: 'ev' },
  631. {text: 'Ampere (A)', value: 'amp' },
  632. {text: 'Volt (V)', value: 'volt' },
  633. {text: 'Decibel-milliwatt (dBm)', value: 'dBm' },
  634. ]
  635. },
  636. {
  637. text: 'temperature',
  638. submenu: [
  639. {text: 'Celcius (°C)', value: 'celsius' },
  640. {text: 'Farenheit (°F)', value: 'farenheit' },
  641. {text: 'Kelvin (K)', value: 'kelvin' },
  642. ]
  643. },
  644. {
  645. text: 'pressure',
  646. submenu: [
  647. {text: 'Millibars', value: 'pressurembar'},
  648. {text: 'Hectopascals', value: 'pressurehpa' },
  649. {text: 'Inches of mercury', value: 'pressurehg' },
  650. {text: 'PSI', value: 'pressurepsi' },
  651. ]
  652. }
  653. ];
  654. };
  655. return kbn;
  656. });