kbn.js 23 KB

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