grafana/src/app/components/kbn.js

669 lines
16 KiB
JavaScript
Raw Normal View History

define([
'jquery',
'lodash',
'moment'
],
function($, _, moment) {
2013-07-20 17:59:09 -05:00
'use strict';
2013-01-25 22:10:28 -06:00
2013-09-13 15:52:13 -05:00
var kbn = {};
2013-01-25 22:10:28 -06:00
kbn.round_interval = function(interval) {
switch (true) {
2013-07-20 17:59:09 -05:00
// 0.5s
2014-06-30 00:56:35 -05:00
case (interval <= 500):
2013-07-20 17:59:09 -05:00
return 100; // 0.1s
// 5s
2014-06-30 00:56:35 -05:00
case (interval <= 5000):
2013-07-20 17:59:09 -05:00
return 1000; // 1s
// 7.5s
2014-06-30 00:56:35 -05:00
case (interval <= 7500):
2013-07-20 17:59:09 -05:00
return 5000; // 5s
// 15s
2014-06-30 00:56:35 -05:00
case (interval <= 15000):
2013-07-20 17:59:09 -05:00
return 10000; // 10s
// 45s
2014-06-30 00:56:35 -05:00
case (interval <= 45000):
2013-07-20 17:59:09 -05:00
return 30000; // 30s
// 3m
2014-06-30 00:56:35 -05:00
case (interval <= 180000):
2013-07-20 17:59:09 -05:00
return 60000; // 1m
// 9m
2014-06-30 00:56:35 -05:00
case (interval <= 450000):
2013-07-20 17:59:09 -05:00
return 300000; // 5m
// 20m
2014-06-30 00:56:35 -05:00
case (interval <= 1200000):
2013-07-20 17:59:09 -05:00
return 600000; // 10m
// 45m
2014-06-30 00:56:35 -05:00
case (interval <= 2700000):
2013-07-20 17:59:09 -05:00
return 1800000; // 30m
// 2h
2014-06-30 00:56:35 -05:00
case (interval <= 7200000):
2013-07-20 17:59:09 -05:00
return 3600000; // 1h
// 6h
2014-06-30 00:56:35 -05:00
case (interval <= 21600000):
2013-07-20 17:59:09 -05:00
return 10800000; // 3h
// 24h
2014-06-30 00:56:35 -05:00
case (interval <= 86400000):
2013-07-20 17:59:09 -05:00
return 43200000; // 12h
// 48h
2014-06-30 00:56:35 -05:00
case (interval <= 172800000):
2013-07-20 17:59:09 -05:00
return 86400000; // 24h
// 1w
2014-06-30 00:56:35 -05:00
case (interval <= 604800000):
2013-07-20 17:59:09 -05:00
return 86400000; // 24h
// 3w
2014-06-30 00:56:35 -05:00
case (interval <= 1814400000):
2013-07-20 17:59:09 -05:00
return 604800000; // 1w
// 2y
2014-06-30 00:56:35 -05:00
case (interval < 3628800000):
2013-07-20 17:59:09 -05:00
return 2592000000; // 30d
default:
2013-07-20 17:59:09 -05:00
return 31536000000; // 1y
}
2013-07-20 17:59:09 -05:00
};
2013-01-25 22:10:28 -06:00
2014-06-06 23:38:33 -05:00
kbn.secondsToHms = function(seconds) {
2013-07-20 17:59:09 -05:00
var numyears = Math.floor(seconds / 31536000);
if(numyears){
return numyears + 'y';
}
var numdays = Math.floor((seconds % 31536000) / 86400);
if(numdays){
return numdays + 'd';
}
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
if(numhours){
return numhours + 'h';
}
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
if(numminutes){
return numminutes + 'm';
}
var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
if(numseconds){
return numseconds + 's';
}
return 'less then a second'; //'just now' //or other string you like;
};
2013-01-25 22:10:28 -06:00
kbn.to_percent = function(number,outof) {
2013-08-20 09:24:04 -05:00
return Math.floor((number/outof)*10000)/100 + "%";
2013-07-20 17:59:09 -05:00
};
2013-01-25 22:10:28 -06:00
kbn.addslashes = function(str) {
str = str.replace(/\\/g, '\\\\');
str = str.replace(/\'/g, '\\\'');
str = str.replace(/\"/g, '\\"');
str = str.replace(/\0/g, '\\0');
return str;
2013-07-20 17:59:09 -05:00
};
2013-01-25 22:10:28 -06:00
kbn.interval_regex = /(\d+(?:\.\d+)?)([Mwdhmsy])/;
// histogram & trends
kbn.intervals_in_seconds = {
y: 31536000,
M: 2592000,
w: 604800,
d: 86400,
h: 3600,
m: 60,
s: 1
};
kbn.calculateInterval = function(range, resolution, userInterval) {
var lowLimitMs = 1; // 1 millisecond default low limit
var intervalMs, lowLimitInterval;
if (userInterval) {
if (userInterval[0] === '>') {
lowLimitInterval = userInterval.slice(1);
lowLimitMs = kbn.interval_to_ms(lowLimitInterval);
}
else {
return userInterval;
}
}
intervalMs = kbn.round_interval((range.to.valueOf() - range.from.valueOf()) / resolution);
if (lowLimitMs > intervalMs) {
intervalMs = lowLimitMs;
}
return kbn.secondsToHms(intervalMs / 1000);
};
kbn.describe_interval = function (string) {
var matches = string.match(kbn.interval_regex);
if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
throw new Error('Invalid interval string, expexcting a number followed by one of "Mwdhmsy"');
} else {
return {
sec: kbn.intervals_in_seconds[matches[2]],
type: matches[2],
count: parseInt(matches[1], 10)
};
}
2013-07-20 17:59:09 -05:00
};
2013-01-25 22:10:28 -06:00
kbn.interval_to_ms = function(string) {
var info = kbn.describe_interval(string);
return info.sec * 1000 * info.count;
};
kbn.interval_to_seconds = function (string) {
var info = kbn.describe_interval(string);
return info.sec * info.count;
};
// This should go away, moment.js can do this
kbn.time_ago = function(string) {
return new Date(new Date().getTime() - (kbn.interval_to_ms(string)));
2013-07-20 17:59:09 -05:00
};
2013-01-25 22:10:28 -06:00
/* This is a simplified version of elasticsearch's date parser */
kbn.parseDate = function(text) {
if(_.isDate(text)) {
return text;
}
var time,
mathString = "",
index,
parseString;
if (text.substring(0,3) === "now") {
time = new Date();
mathString = text.substring("now".length);
} else {
index = text.indexOf("||");
parseString;
if (index === -1) {
parseString = text;
mathString = ""; // nothing else
} else {
parseString = text.substring(0, index);
mathString = text.substring(index + 2);
}
// We're going to just require ISO8601 timestamps, k?
time = new Date(parseString);
}
if (!mathString.length) {
return time;
}
//return [time,parseString,mathString];
return kbn.parseDateMath(mathString, time);
};
kbn.parseDateMath = function(mathString, time, roundUp) {
var dateTime = moment(time);
2014-06-06 23:38:33 -05:00
for (var i = 0; i < mathString.length;) {
var c = mathString.charAt(i++),
type,
num,
unit;
if (c === '/') {
type = 0;
} else if (c === '+') {
type = 1;
} else if (c === '-') {
type = 2;
} else {
return false;
}
if (isNaN(mathString.charAt(i))) {
num = 1;
} else {
var numFrom = i;
while (!isNaN(mathString.charAt(i))) {
i++;
}
num = parseInt(mathString.substring(numFrom, i),10);
}
if (type === 0) {
// rounding is only allowed on whole numbers
if (num !== 1) {
return false;
}
}
unit = mathString.charAt(i++);
switch (unit) {
2013-10-21 10:18:06 -05:00
case 'y':
if (type === 0) {
roundUp ? dateTime.endOf('year') : dateTime.startOf('year');
} else if (type === 1) {
dateTime.add(num, 'years');
2013-10-21 10:18:06 -05:00
} else if (type === 2) {
dateTime.subtract(num, 'years');
2013-10-21 10:18:06 -05:00
}
break;
case 'M':
if (type === 0) {
roundUp ? dateTime.endOf('month') : dateTime.startOf('month');
} else if (type === 1) {
dateTime.add(num, 'months');
} else if (type === 2) {
dateTime.subtract(num, 'months');
}
break;
case 'w':
if (type === 0) {
roundUp ? dateTime.endOf('week') : dateTime.startOf('week');
} else if (type === 1) {
dateTime.add(num, 'weeks');
} else if (type === 2) {
dateTime.subtract(num, 'weeks');
}
break;
case 'd':
if (type === 0) {
roundUp ? dateTime.endOf('day') : dateTime.startOf('day');
} else if (type === 1) {
dateTime.add(num, 'days');
} else if (type === 2) {
dateTime.subtract(num, 'days');
}
break;
case 'h':
case 'H':
if (type === 0) {
roundUp ? dateTime.endOf('hour') : dateTime.startOf('hour');
} else if (type === 1) {
dateTime.add(num, 'hours');
} else if (type === 2) {
dateTime.subtract(num,'hours');
}
break;
case 'm':
if (type === 0) {
roundUp ? dateTime.endOf('minute') : dateTime.startOf('minute');
} else if (type === 1) {
dateTime.add(num, 'minutes');
} else if (type === 2) {
dateTime.subtract(num, 'minutes');
}
break;
case 's':
if (type === 0) {
roundUp ? dateTime.endOf('second') : dateTime.startOf('second');
} else if (type === 1) {
dateTime.add(num, 'seconds');
} else if (type === 2) {
dateTime.subtract(num, 'seconds');
}
break;
default:
return false;
}
}
return dateTime.toDate();
};
kbn.query_color_dot = function (color, diameter) {
return '<div class="icon-circle" style="' + [
2014-06-06 23:38:33 -05:00
'display:inline-block',
'color:' + color,
'font-size:' + diameter + 'px',
].join(';') + '"></div>';
};
kbn.byteFormat = function(size, decimals) {
var ext, steps = 0;
if(_.isUndefined(decimals)) {
decimals = 2;
} else if (decimals === 0) {
decimals = undefined;
}
while (Math.abs(size) >= 1024) {
steps++;
size /= 1024;
}
switch (steps) {
case 0:
ext = " B";
break;
case 1:
ext = " KiB";
break;
case 2:
ext = " MiB";
break;
case 3:
ext = " GiB";
break;
case 4:
ext = " TiB";
break;
case 5:
ext = " PiB";
break;
case 6:
ext = " EiB";
break;
case 7:
ext = " ZiB";
break;
case 8:
ext = " YiB";
break;
}
return (size.toFixed(decimals) + ext);
};
2014-03-01 12:20:42 -06:00
kbn.bitFormat = function(size, decimals) {
var ext, steps = 0;
if(_.isUndefined(decimals)) {
decimals = 2;
} else if (decimals === 0) {
decimals = undefined;
}
while (Math.abs(size) >= 1024) {
steps++;
size /= 1024;
}
switch (steps) {
case 0:
ext = " b";
break;
case 1:
ext = " Kib";
2014-03-01 12:20:42 -06:00
break;
case 2:
ext = " Mib";
2014-03-01 12:20:42 -06:00
break;
case 3:
ext = " Gib";
2014-03-01 12:20:42 -06:00
break;
case 4:
ext = " Tib";
2014-03-01 12:20:42 -06:00
break;
case 5:
ext = " Pib";
2014-03-01 12:20:42 -06:00
break;
case 6:
ext = " Eib";
2014-03-01 12:20:42 -06:00
break;
case 7:
ext = " Zib";
2014-03-01 12:20:42 -06:00
break;
case 8:
ext = " Yib";
2014-03-01 12:20:42 -06:00
break;
}
return (size.toFixed(decimals) + ext);
};
2014-07-17 18:52:12 -05:00
kbn.bpsFormat = function(size, decimals) {
var ext, steps = 0;
if(_.isUndefined(decimals)) {
decimals = 2;
} else if (decimals === 0) {
decimals = undefined;
}
while (Math.abs(size) >= 1000) {
steps++;
size /= 1000;
}
switch (steps) {
case 0:
ext = " bps";
break;
case 1:
ext = " Kbps";
break;
case 2:
ext = " Mbps";
break;
case 3:
ext = " Gbps";
break;
case 4:
ext = " Tbps";
break;
case 5:
ext = " Pbps";
break;
case 6:
ext = " Ebps";
break;
case 7:
ext = " Zbps";
break;
case 8:
ext = " Ybps";
break;
}
return (size.toFixed(decimals) + ext);
};
kbn.shortFormat = function(size, decimals) {
var ext, steps = 0;
if(_.isUndefined(decimals)) {
decimals = 2;
} else if (decimals === 0) {
decimals = undefined;
}
while (Math.abs(size) >= 1000) {
steps++;
size /= 1000;
}
switch (steps) {
case 0:
ext = "";
break;
case 1:
ext = " K";
break;
case 2:
ext = " Mil";
break;
case 3:
ext = " Bil";
break;
case 4:
ext = " Tri";
break;
case 5:
ext = " Quadr";
break;
case 6:
ext = " Quint";
break;
case 7:
ext = " Sext";
break;
case 8:
ext = " Sept";
break;
}
return (size.toFixed(decimals) + ext);
};
2014-02-26 12:46:34 -06:00
kbn.getFormatFunction = function(formatName, decimals) {
switch(formatName) {
case 'short':
return function(val) {
return kbn.shortFormat(val, decimals);
};
case 'bytes':
return function(val) {
return kbn.byteFormat(val, decimals);
};
2014-03-01 12:20:42 -06:00
case 'bits':
return function(val) {
return kbn.bitFormat(val, decimals);
};
2014-07-17 18:52:12 -05:00
case 'bps':
return function(val) {
return kbn.bpsFormat(val, decimals);
};
2014-05-20 03:55:19 -05:00
case 's':
return function(val) {
return kbn.sFormat(val, decimals);
};
2014-02-26 12:46:34 -06:00
case 'ms':
return function(val) {
return kbn.msFormat(val, decimals);
};
case 'µs':
return function(val) {
return kbn.microsFormat(val, decimals);
};
2014-05-03 08:44:48 -05:00
case 'ns':
return function(val) {
return kbn.nanosFormat(val, decimals);
};
2014-02-26 12:46:34 -06:00
default:
return function(val, axis) {
return kbn.noneFormat(val, axis ? axis.tickDecimals : null);
2014-02-26 12:46:34 -06:00
};
}
};
kbn.noneFormat = function(value, decimals) {
var factor = decimals ? Math.pow(10, decimals) : 1;
var formatted = String(Math.round(value * factor) / factor);
// if exponent return directly
if (formatted.indexOf('e') !== -1 || value === 0) {
return formatted;
}
// If tickDecimals was specified, ensure that we have exactly that
// much precision; otherwise default to the value's own precision.
if (decimals != null) {
var decimalPos = formatted.indexOf(".");
var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1;
if (precision < decimals) {
return (precision ? formatted : formatted + ".") + (String(factor)).substr(1, decimals - precision);
}
}
return formatted;
};
2014-02-26 12:46:34 -06:00
kbn.msFormat = function(size, decimals) {
// Less than 1 milli, downscale to micro
if (Math.abs(size) < 1) {
return kbn.microsFormat(size * 1000,decimals);
}
else if (Math.abs(size) < 1000) {
return size.toFixed(decimals) + " ms";
}
// Less than 1 min
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 60000) {
2014-02-26 12:46:34 -06:00
return (size / 1000).toFixed(decimals) + " s";
}
// Less than 1 hour, devide in minutes
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 3600000) {
2014-02-26 12:46:34 -06:00
return (size / 60000).toFixed(decimals) + " min";
}
// Less than one day, devide in hours
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 86400000) {
return (size / 3600000).toFixed(decimals) + " hour";
}
// Less than one year, devide in days
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 31536000000) {
return (size / 86400000).toFixed(decimals) + " day";
}
return (size / 31536000000).toFixed(decimals) + " year";
};
2014-05-20 03:55:19 -05:00
kbn.sFormat = function(size, decimals) {
// Less than 1 sec, downscale to milli
if (Math.abs(size) < 1) {
return kbn.msFormat(size * 1000, decimals);
}
2014-05-20 03:55:19 -05:00
// Less than 10 min, use seconds
else if (Math.abs(size) < 600) {
2014-05-20 03:55:19 -05:00
return size.toFixed(decimals) + " s";
}
// Less than 1 hour, devide in minutes
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 3600) {
2014-05-20 03:55:19 -05:00
return (size / 60).toFixed(decimals) + " min";
}
// Less than one day, devide in hours
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 86400) {
2014-05-20 03:55:19 -05:00
return (size / 3600).toFixed(decimals) + " hour";
}
// Less than one week, devide in days
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 604800) {
2014-05-20 03:55:19 -05:00
return (size / 86400).toFixed(decimals) + " day";
}
// Less than one year, devide in week
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 31536000) {
return (size / 604800).toFixed(decimals) + " week";
}
2014-05-20 03:55:19 -05:00
return (size / 3.15569e7).toFixed(decimals) + " year";
};
2014-02-26 12:46:34 -06:00
kbn.microsFormat = function(size, decimals) {
// Less than 1 micro, downscale to nano
if (Math.abs(size) < 1) {
return kbn.nanosFormat(size * 1000, decimals);
}
else if (Math.abs(size) < 1000) {
return size.toFixed(decimals) + " µs";
}
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 1000000) {
2014-02-26 12:46:34 -06:00
return (size / 1000).toFixed(decimals) + " ms";
}
else {
2014-02-26 12:46:34 -06:00
return (size / 1000000).toFixed(decimals) + " s";
2014-02-25 08:34:56 -06:00
}
};
2014-05-03 08:44:48 -05:00
kbn.nanosFormat = function(size, decimals) {
if (Math.abs(size) < 1) {
return size.toFixed(decimals) + " ns";
}
else if (Math.abs(size) < 1000) {
2014-05-03 08:44:48 -05:00
return size.toFixed(0) + " ns";
}
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 1000000) {
2014-05-03 08:44:48 -05:00
return (size / 1000).toFixed(decimals) + " µs";
}
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 1000000000) {
2014-05-03 08:44:48 -05:00
return (size / 1000000).toFixed(decimals) + " ms";
}
2014-06-27 08:37:11 -05:00
else if (Math.abs(size) < 60000000000){
2014-05-03 08:44:48 -05:00
return (size / 1000000000).toFixed(decimals) + " s";
}
else {
return (size / 60000000000).toFixed(decimals) + " m";
}
};
kbn.stringToJsRegex = function(str) {
if (str[0] !== '/') {
return new RegExp(str);
}
var match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
return new RegExp(match[1], match[2]);
};
2013-09-13 15:52:13 -05:00
return kbn;
});