Refactor filter of bytes/bits

This commit is contained in:
Floran Brutel 2015-05-06 14:10:54 +02:00
parent c207219a4d
commit b1ab37ad36
5 changed files with 63 additions and 29 deletions

View File

@ -5,7 +5,7 @@
</div>
<div class="table-row">
<div class="table-cell text-left">total:</div>
<div class="table-cell">{{result["mem"].total | bytes:2}}</div>
<div class="table-cell">{{result["mem"].total | bytes}}</div>
</div>
<div class="table-row">
<div class="table-cell text-left">used:</div>
@ -15,6 +15,6 @@
</div>
<div class="table-row">
<div class="table-cell text-left">free:</div>
<div class="table-cell">{{result["mem"].free | bytes:2}}</div>
<div class="table-cell">{{result["mem"].free | bytes}}</div>
</div>
</div>

View File

@ -1,18 +1,18 @@
<div class="table">
<div class="table-row">
<div class="table-cell text-left">active:</div>
<div class="table-cell">{{result["mem"].active | bytes:2}}</div>
<div class="table-cell">{{result["mem"].active | bytes}}</div>
</div>
<div class="table-row">
<div class="table-cell text-left">inactive:</div>
<div class="table-cell">{{result["mem"].inactive | bytes:2}}</div>
<div class="table-cell">{{result["mem"].inactive | bytes}}</div>
</div>
<div class="table-row">
<div class="table-cell text-left">buffers:</div>
<div class="table-cell">{{result["mem"].buffers | bytes:2}}</div>
<div class="table-cell">{{result["mem"].buffers | bytes}}</div>
</div>
<div class="table-row">
<div class="table-cell text-left">cached:</div>
<div class="table-cell">{{result["mem"].cached | bytes:2}}</div>
<div class="table-cell">{{result["mem"].cached | bytes}}</div>
</div>
</div>

View File

@ -5,16 +5,16 @@
</div>
<div class="table-row">
<div class="table-cell text-left">total:</div>
<div class="table-cell">{{result["memswap"].total | bytes:2}}</div>
<div class="table-cell">{{result["memswap"].total | bytes}}</div>
</div>
<div class="table-row">
<div class="table-cell text-left">used:</div>
<div class="table-cell" ng-class="getClass('memswap', 'memswap_', result['memswap'].percent, 1)">
{{result["memswap"].used | bytes:2}}
{{result["memswap"].used | bytes}}
</div>
</div>
<div class="table-row">
<div class="table-cell text-left">free:</div>
<div class="table-cell">{{result["memswap"].free | bytes:2}}</div>
<div class="table-cell">{{result["memswap"].free | bytes}}</div>
</div>
</div>

View File

@ -16,8 +16,8 @@
<div class="table-row" ng-repeat="process in result['processlist'] | orderBy:sorter.column:sorter.isReverseColumn(sorter.column)">
<div class="table-cell" ng-class="getClass('processlist', 'processlist_cpu_', process.cpu_percent, 0)">{{process.cpu_percent | number:1}}</div>
<div class="table-cell" ng-class="getClass('processlist', 'processlist_mem_', process.memory_percent, 0)">{{process.memory_percent | number:1}}</div>
<div class="table-cell hidden-xs hidden-sm">{{process.memvirt | bytes:1}}</div>
<div class="table-cell hidden-xs hidden-sm">{{process.memres | bytes:1}}</div>
<div class="table-cell hidden-xs hidden-sm">{{process.memvirt | bytes}}</div>
<div class="table-cell hidden-xs hidden-sm">{{process.memres | bytes}}</div>
<div class="table-cell">{{process.pid}}</div>
<div class="table-cell text-left">{{process.username}}</div>
<div class="table-cell">{{process.nice | exclamation}}</div>

View File

@ -16,29 +16,63 @@ glancesApp.filter('exclamation', function() {
};
});
/**
* Fork from https://gist.github.com/thomseddon/3511330
* &nbsp; => \u00A0
* WARNING : kilobyte (kB) != kibibyte (KiB) (more info here : http://en.wikipedia.org/wiki/Byte )
**/
glancesApp.filter('bytes', function() {
return function (bytes, precision) {
return function (bytes, low_precision) {
low_precision = low_precision || false;
if (isNaN(parseFloat(bytes)) || !isFinite(bytes) || bytes == 0){
return '0B';
return '0';
}
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1000));
return (bytes / Math.pow(1000, Math.floor(number))).toFixed(precision) + units[number];
var symbols = ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
var prefix = {
'Y': 1208925819614629174706176,
'Z': 1180591620717411303424,
'E': 1152921504606846976,
'P': 1125899906842624,
'T': 1099511627776,
'G': 1073741824,
'M': 1048576,
'K': 1024
};
var reverseSymbols = _(symbols).reverse().value();
for (var i = 0; i < reverseSymbols.length; i++) {
var symbol = reverseSymbols[i];
var value = bytes / prefix[symbol];
if(value > 1) {
var decimal_precision = 0;
if(value < 10) {
decimal_precision = 2;
}
else if(value < 100) {
decimal_precision = 1;
}
if(low_precision) {
if(symbol == 'MK') {
decimal_precision = 0;
}
else {
decimal_precision = _.min([1, decimal_precision]);
}
}
else if(symbol == 'K') {
decimal_precision = 0;
}
return parseFloat(value).toFixed(decimal_precision) + symbol;
}
}
return bytes;
}
});
glancesApp.filter('bits', function() {
return function (bits, precision) {
if (isNaN(parseFloat(bits)) || !isFinite(bits) || bits == 0){
return '0b';
}
var units = ['b', 'kb', 'Mb', 'Gb', 'Tb', 'Pb'],
number = Math.floor(Math.log(bits) / Math.log(1000));
return (bits / Math.pow(1000, Math.floor(number))).toFixed(precision) + units[number];
glancesApp.filter('bits', function($filter) {
return function (bits, low_precision) {
bits = Math.round(bits) * 8;
return $filter('bytes')(bits, low_precision) + 'b';
}
});