ethstats-server/src/poa/js/filters.js

620 lines
13 KiB
JavaScript
Raw Normal View History

2015-01-20 19:29:31 +01:00
/* Filters */
2015-01-29 17:50:16 +01:00
angular.module('netStatsApp.filters', [])
2015-02-05 00:30:16 +01:00
.filter('nodesActiveClass', function() {
return function(active, total) {
var ratio = active/total;
if(ratio >= 0.9)
return 'text-success';
if(ratio >= 0.75)
return 'text-info';
if(ratio >= 0.5)
return 'text-warning';
return 'text-danger';
};
})
2015-05-19 01:41:14 +02:00
.filter('nodePinClass', function() {
return function(pinned) {
if(pinned)
return 'icon-check-o';
return 'icon-loader';
};
})
2015-01-30 22:37:38 +01:00
.filter('mainClass', function() {
2015-02-05 05:32:42 +01:00
return function(node, bestBlock) {
2015-02-08 16:03:05 +01:00
return mainClass(node, bestBlock);
2015-02-05 05:32:42 +01:00
};
2015-01-30 22:37:38 +01:00
})
2015-01-29 17:50:16 +01:00
.filter('peerClass', function() {
2015-04-09 00:48:27 +02:00
return function(peers, active) {
return peerClass(peers, active);
2015-02-05 05:32:42 +01:00
};
2015-01-29 17:50:16 +01:00
})
.filter('miningClass', function() {
2015-04-09 00:48:27 +02:00
return function(mining, active) {
if(! active)
return 'text-gray';
2015-02-05 05:59:24 +01:00
return (! mining ? 'text-danger' : 'text-success');
2015-02-05 05:32:42 +01:00
};
2015-01-29 17:50:16 +01:00
})
.filter('miningIconClass', function() {
2015-02-05 05:32:42 +01:00
return function(mining) {
return (! mining ? 'icon-cancel' : 'icon-check');
};
2015-01-29 17:50:16 +01:00
})
2015-04-24 11:39:29 +02:00
.filter('hashrateClass', function() {
return function(mining, active) {
if(! mining || ! active)
2015-04-24 00:38:09 +02:00
return 'text-gray';
return 'text-success';
};
})
2015-05-04 16:27:37 +02:00
.filter('hashrateFilter', ['$sce', '$filter', function($sce, filter) {
return function(hashes, isMining) {
if( !isMining )
return $sce.trustAsHtml('<i class="icon-cancel"></i>');
2018-04-05 11:35:57 +02:00
var result = hashes;
var units = ['', 'K', 'M', 'G', 'T', 'P', 'E'];
2018-04-05 11:35:57 +02:00
var unit = 'K';
2015-05-04 16:27:37 +02:00
2018-04-05 11:35:57 +02:00
for(var i = 1; result > 1000; i++)
{
result /= 1000;
unit = units[i];
2015-05-04 16:27:37 +02:00
}
return $sce.trustAsHtml('<span class="small">' + filter('number')(result.toFixed(1)) + ' <span class="small-hash">' + unit + 'H/s</span></span>');
};
2015-04-24 11:39:29 +02:00
}])
2015-08-04 12:24:01 +02:00
.filter('totalDifficultyFilter', function() {
return function(hashes) {
2018-04-05 11:35:57 +02:00
var result = hashes;
var units = ['', 'K', 'M', 'G', 'T', 'P', 'E'];
2015-08-04 12:24:01 +02:00
var unit = '';
2018-04-05 11:35:57 +02:00
for(var i = 1; result > 1000; i++)
{
result /= 1000;
unit = units[i];
2015-08-04 12:24:01 +02:00
}
return result.toFixed(2) + ' ' + unit + 'H';
};
})
2015-02-17 16:29:26 +01:00
.filter('nodeVersion', function($sce) {
2015-02-17 15:07:50 +01:00
return function(version) {
2016-11-08 13:15:10 +01:00
if(version)
2015-04-17 03:35:08 +02:00
{
var tmp = version.split('/');
tmp[0] = tmp[0].replace('Ethereum(++)', 'Eth');
2015-04-23 15:17:31 +02:00
if(tmp[0].indexOf('pyethapp') === 0)
{
tmp[0] = 'pyeth';
}
if(tmp[1] && tmp[1][0] !== 'v' && tmp[1][2] !== '.')
2015-04-17 03:35:08 +02:00
{
tmp.splice(1,1);
}
if(tmp[2] && tmp[2] === 'Release'){
2015-04-17 03:35:08 +02:00
tmp.splice(2,1);
}
if(tmp[2] && tmp[2].indexOf('Linux') === 0)
2015-04-17 03:35:08 +02:00
tmp[2] = 'linux';
if(tmp[2] && tmp[2].indexOf('Darwin') === 0)
2015-04-17 03:35:08 +02:00
tmp[2] = 'darwin';
return $sce.trustAsHtml(tmp.join('/'));
}
return '';
2015-02-17 16:29:26 +01:00
};
2015-02-17 15:07:50 +01:00
})
2015-01-29 17:50:16 +01:00
.filter('blockClass', function() {
2015-02-05 05:32:42 +01:00
return function(current, best) {
2015-04-09 00:48:27 +02:00
if( ! current.active)
return 'text-gray';
2015-04-26 16:47:31 +02:00
return (best - current.block.number < 1 ? 'text-success' : (best - current.block.number === 1 ? 'text-warning' : (best - current.block.number > 1 && best - current.block.number < 4 ? 'text-orange' : 'text-danger')));
2015-02-05 05:32:42 +01:00
};
2015-02-05 02:33:58 +01:00
})
2015-04-24 10:44:21 +02:00
.filter('gasPriceFilter', ['$filter', function(filter) {
2015-04-24 11:39:29 +02:00
var numberFilter = filter('number');
2015-04-24 10:44:21 +02:00
return function(price) {
if(typeof price === 'undefined')
return "0 wei";
2015-04-24 10:44:21 +02:00
if(price.length < 4)
2015-06-22 22:22:56 +02:00
return numberFilter(price) + " wei";
2015-04-24 10:44:21 +02:00
if(price.length < 7)
2015-06-22 22:22:56 +02:00
return numberFilter(price/1000) + " kwei";
2015-04-24 10:44:21 +02:00
if(price.length < 10)
2015-06-22 22:22:56 +02:00
return numberFilter(price/1000000) + " mwei";
2015-04-24 10:44:21 +02:00
if(price.length < 13)
2015-06-22 22:22:56 +02:00
return numberFilter(price/1000000000) + " gwei";
2015-04-24 10:44:21 +02:00
if(price.length < 16)
2015-06-22 22:22:56 +02:00
return numberFilter(price/1000000000000) + " szabo";
2015-04-24 10:44:21 +02:00
if(price.length < 19)
2015-06-22 22:22:56 +02:00
return numberFilter(price.substr(0, price.length - 15)) + " finney";
2015-04-24 10:44:21 +02:00
return numberFilter(price.substr(0, price.length - 18)) + " ether";
}
}])
2015-02-09 16:44:44 +01:00
.filter('gasFilter', function() {
return function(gas) {
return (typeof gas !== 'undefined' ? parseInt(gas) : '?');
}
})
2015-02-19 18:34:20 +01:00
.filter('hashFilter', function() {
return function(hash) {
2015-04-28 14:38:26 +02:00
if(typeof hash === 'undefined')
return "?";
2015-04-17 11:10:20 +02:00
if(hash.substr(0,2) === '0x')
hash = hash.substr(2,64);
2015-04-06 15:13:49 +02:00
return hash.substr(0, 8) + '...' + hash.substr(56, 8);
2015-02-19 18:34:20 +01:00
}
})
2015-02-05 02:33:58 +01:00
.filter('timeClass', function() {
2015-04-09 00:48:27 +02:00
return function(timestamp, active) {
if( ! active)
return 'text-gray';
2015-02-05 05:32:42 +01:00
return timeClass(timestamp);
};
2015-02-05 12:05:21 +01:00
})
2015-02-24 22:38:14 +01:00
.filter('propagationTimeClass', function() {
2015-04-09 00:48:27 +02:00
return function(stats, bestBlock) {
if( ! stats.active)
2015-04-06 12:47:11 +02:00
return 'text-gray';
2015-04-09 00:48:27 +02:00
if(stats.block.number < bestBlock)
return 'text-gray';
if(stats.block.propagation == 0)
2015-04-09 23:36:29 +02:00
return 'text-info';
2015-04-06 13:02:52 +02:00
2015-04-09 00:48:27 +02:00
if(stats.block.propagation < 1000)
2015-04-09 23:36:29 +02:00
return 'text-success';
2015-02-24 22:38:14 +01:00
2015-04-09 00:48:27 +02:00
if(stats.block.propagation < 3000)
2015-04-09 23:36:29 +02:00
return 'text-warning';
2015-04-06 12:47:11 +02:00
2015-04-09 00:48:27 +02:00
if(stats.block.propagation < 7000)
2015-04-09 23:36:29 +02:00
return 'text-orange';
2015-02-24 22:38:14 +01:00
2015-04-09 23:36:29 +02:00
return 'text-danger'
2015-02-24 22:38:14 +01:00
};
})
2015-04-29 12:30:38 +02:00
.filter('propagationNodeAvgTimeClass', function() {
return function(stats, bestBlock) {
if( ! stats.active)
return 'text-gray';
if(stats.block.number < bestBlock)
return 'text-gray';
if(stats.propagationAvg == 0)
return 'text-info';
if(stats.propagationAvg < 1000)
return 'text-success';
if(stats.propagationAvg < 3000)
return 'text-warning';
if(stats.propagationAvg < 7000)
return 'text-orange';
return 'text-danger'
};
})
2015-04-28 17:22:11 +02:00
.filter('propagationAvgTimeClass', function() {
2015-04-28 17:44:41 +02:00
return function(propagationAvg, active) {
if( ! active)
return 'text-gray';
2015-04-28 17:44:41 +02:00
if(propagationAvg == 0)
2015-04-28 17:22:11 +02:00
return 'text-info';
2015-04-28 17:44:41 +02:00
if(propagationAvg < 1000)
2015-04-28 17:22:11 +02:00
return 'text-success';
2015-04-28 17:44:41 +02:00
if(propagationAvg < 3000)
2015-04-28 17:22:11 +02:00
return 'text-warning';
2015-04-28 17:44:41 +02:00
if(propagationAvg < 7000)
2015-04-28 17:22:11 +02:00
return 'text-orange';
return 'text-danger'
};
})
2015-03-27 12:35:23 +01:00
.filter('latencyFilter', function() {
return function(stats) {
if(stats.active === false)
return 'offline';
else
return stats.latency + ' ms';
}
})
2015-02-24 22:38:14 +01:00
.filter('latencyClass', function() {
2015-03-27 12:35:23 +01:00
return function(stats) {
if(stats.active === false)
2015-04-09 00:48:27 +02:00
return 'text-danger';
2015-03-27 12:35:23 +01:00
if(stats.latency <= 100)
2015-02-24 22:38:14 +01:00
return 'text-success';
2015-03-27 12:35:23 +01:00
if(stats.latency <= 1000)
2015-02-24 22:38:14 +01:00
return 'text-warning';
return 'text-danger'
};
})
2015-02-18 09:57:45 +01:00
.filter('blockTimeFilter', function() {
2015-02-18 10:10:33 +01:00
return function(timestamp) {
2015-02-18 10:11:26 +01:00
if(timestamp === 0)
return '∞';
2015-04-04 21:35:45 +02:00
// var time = Math.floor((new Date()).getTime() / 1000);
var time = (new Date()).getTime();
var diff = Math.floor((time - timestamp)/1000);
2015-02-18 10:10:33 +01:00
if(diff < 60)
2015-02-23 14:57:41 +01:00
return Math.round(diff) + ' s ago';
2015-02-18 10:10:33 +01:00
return moment.duration(Math.round(diff), 's').humanize() + ' ago';
2015-02-18 09:57:45 +01:00
};
2015-03-04 23:11:51 +01:00
})
2015-04-29 12:14:18 +02:00
.filter('networkHashrateFilter', ['$sce', '$filter', function($sce, filter) {
return function(hashes, isMining) {
2015-05-27 08:20:57 +02:00
if(hashes === null)
hashes = 0;
2018-04-05 11:35:57 +02:00
var result = hashes;
var units = ['', 'K', 'M', 'G', 'T', 'P'];
2015-04-29 12:14:18 +02:00
var unit = 'K';
2015-04-29 11:39:51 +02:00
2018-04-05 11:35:57 +02:00
for(var i = 1; result > 1000; i++)
{
result /= 1000;
unit = units[i];
2015-04-29 11:39:51 +02:00
}
2015-04-29 12:14:18 +02:00
if( !isMining )
return $sce.trustAsHtml(filter('number')(result.toFixed(1)) + ' <span class="small-hash">' + unit + 'H/s</span>');
return $sce.trustAsHtml('? <span class="small-hash">' + unit + 'KH/s</span>');
2015-04-29 11:39:51 +02:00
};
2015-04-29 12:14:18 +02:00
}])
2015-03-04 23:11:51 +01:00
.filter('blockPropagationFilter', function() {
2015-04-28 17:44:41 +02:00
return function(ms, prefix) {
if(typeof prefix === 'undefined')
prefix = '+';
2015-03-04 23:11:51 +01:00
var result = 0;
2015-03-05 16:04:58 +01:00
if(ms < 1000) {
2015-04-28 17:44:41 +02:00
return (ms === 0 ? "" : prefix) + ms + " ms";
2015-03-04 23:20:57 +01:00
}
2015-03-05 16:04:58 +01:00
if(ms < 1000*60) {
2015-03-04 23:20:57 +01:00
result = ms/1000;
2015-04-28 17:44:41 +02:00
return prefix + result.toFixed(1) + " s";
2015-03-04 23:11:51 +01:00
}
2015-03-05 16:04:58 +01:00
if(ms < 1000*60*60) {
2015-03-04 23:20:57 +01:00
result = ms/1000/60;
2015-04-28 17:44:41 +02:00
return prefix + Math.round(result) + " min";
2015-03-04 23:11:51 +01:00
}
2015-03-05 16:04:58 +01:00
if(ms < 1000*60*60*24) {
2015-03-04 23:20:57 +01:00
result = ms/1000/60/60;
2015-04-28 17:44:41 +02:00
return prefix + Math.round(result) + " h";
2015-03-04 23:11:51 +01:00
}
result = ms/1000/60/60/24;
return prefix + Math.round(result) + " days";
};
})
.filter('blockPropagationAvgFilter', function() {
return function(stats, bestBlock) {
var ms = stats.propagationAvg;
if(bestBlock - stats.block.number > 40)
return "∞";
//ms = _.now() - stats.block.received;
prefix = '';
var result = 0;
if(ms < 1000) {
return (ms === 0 ? "" : prefix) + ms + " ms";
}
if(ms < 1000*60) {
result = ms/1000;
return prefix + result.toFixed(1) + " s";
}
if(ms < 1000*60*60) {
result = ms/1000/60;
return prefix + Math.round(result) + " min";
}
if(ms < 1000*60*60*24) {
result = ms/1000/60/60;
return prefix + Math.round(result) + " h";
}
2015-03-04 23:20:57 +01:00
result = ms/1000/60/60/24;
2015-04-28 17:44:41 +02:00
return prefix + Math.round(result) + " days";
2015-03-04 23:11:51 +01:00
};
})
.filter('avgTimeFilter', function() {
2015-02-18 09:57:45 +01:00
return function(time) {
2015-02-18 10:10:33 +01:00
if(time < 60)
2015-07-30 20:54:34 +02:00
return parseFloat(time).toFixed(2) + ' s';
2015-02-18 10:10:33 +01:00
2015-02-18 09:57:45 +01:00
return moment.duration(Math.round(time), 's').humanize();
2015-02-18 08:54:04 +01:00
};
})
.filter('avgTimeClass', function() {
return function(time) {
return blockTimeClass(time);
}
})
2015-02-05 12:05:21 +01:00
.filter('upTimeFilter', function() {
return function(uptime) {
return Math.round(uptime) + '%';
};
})
.filter('upTimeClass', function() {
2015-04-09 00:48:27 +02:00
return function(uptime, active) {
if( ! active )
return 'text-gray';
2015-02-05 12:05:21 +01:00
if(uptime >= 90)
return 'text-success';
if(uptime >= 75)
return 'text-warning';
return 'text-danger';
};
})
.filter('geoTooltip', function() {
2015-04-17 00:30:11 +02:00
return function(node) {
var tooltip = [];
var string = '';
2016-11-08 13:15:10 +01:00
if(node.info.node) {
2015-04-17 00:30:11 +02:00
var eth_version = node.info.node.split('/');
if(eth_version[1]){
if(eth_version[1][0] !== 'v' && eth_version[1][2] !== '.')
{
eth_version.splice(1,1);
}
2015-04-17 00:30:11 +02:00
string = "<b>" + node.info.node + "</b>";
tooltip.push(string);
2015-04-17 00:30:11 +02:00
string = "Version: <b>" + (eth_version[1]) + "</b>";
tooltip.push(string);
}
2015-04-17 00:30:11 +02:00
}
if(node.info.net !== '') {
2015-04-17 02:18:04 +02:00
string = "Network: <b>" + (typeof node.info.net !== 'undefined' ? node.info.net : '-') + "</b>";
2015-04-17 00:30:11 +02:00
tooltip.push(string);
}
if(node.info.protocol !== '') {
2015-04-17 02:18:04 +02:00
string = "Protocol: <b>" + (typeof node.info.protocol !== 'undefined' ? node.info.protocol : '-') + "</b>";
2015-04-17 00:30:11 +02:00
tooltip.push(string);
}
2015-04-17 04:16:01 +02:00
if(node.info.port !== '') {
string = "Port: <b>" + (typeof node.info.port !== 'undefined' ? node.info.port : '30303') + "</b>";
tooltip.push(string);
}
2015-04-17 00:30:11 +02:00
if(node.info.api !== '') {
string = "Web3: <b>" + node.info.api + "</b>";
tooltip.push(string);
}
if(node.info.client !== '') {
2015-04-17 02:20:44 +02:00
string = "API: <b>" + (typeof node.info.client !== 'undefined' ? node.info.client : '<= 0.0.3') + "</b>";
2015-04-17 00:30:11 +02:00
tooltip.push(string);
}
2015-04-17 03:35:08 +02:00
if(node.info.os !== '') {
string = "OS: <b>" + (typeof node.info.os !== 'undefined' ? node.info.os + ' ' + node.info.os_v : '?') + "</b>";
tooltip.push(string);
}
if(node.geo !== null)
{
string = "Location: <b>";
if(node.geo.city !== '')
string += node.geo.city + ", ";
string += node.geo.country + "</b>";
tooltip.push(string);
}
if(node.info.contact !== '') {
string = "Contact: <b>" + (typeof node.info.contact !== 'undefined' ? node.info.contact : '-') + "</b>";
tooltip.push(string);
}
2015-04-17 00:30:11 +02:00
return tooltip.join("<br>");
2015-02-05 09:27:12 +01:00
};
2015-02-08 16:03:05 +01:00
})
.filter('bubbleClass', function() {
return function(node, bestBlock) {
return mainClass(node, bestBlock).replace('text-', '');
2015-04-06 01:28:42 +02:00
};
})
.filter('minerNameFilter', function() {
2015-04-06 20:10:27 +02:00
return function(address, name) {
if(typeof name !== 'undefined' && name !== false && name.length > 0)
return name;
return address.replace('0x', '');
2015-04-06 01:28:42 +02:00
};
})
.filter('minerBlocksClass', function() {
2015-04-17 03:10:08 +02:00
return function(blocks, prefix) {
if(typeof prefix === 'undefined')
prefix = 'bg-';
2015-04-06 01:28:42 +02:00
if(blocks <= 6)
2015-04-17 03:10:08 +02:00
return prefix + 'success';
2015-04-06 01:28:42 +02:00
if(blocks <= 12)
2015-04-17 03:10:08 +02:00
return prefix + 'info';
2015-04-06 01:28:42 +02:00
if(blocks <= 18)
2015-04-17 03:10:08 +02:00
return prefix + 'warning';
2015-04-06 01:28:42 +02:00
2015-04-17 03:10:08 +02:00
return prefix + 'danger';
2015-04-06 01:28:42 +02:00
};
2015-04-28 03:25:49 +02:00
})
.filter('nodeClientClass', function() {
return function(info, current) {
if(typeof info === 'undefined' || typeof info.client === 'undefined' || typeof info.client === '')
return 'text-danger';
2015-05-27 12:00:10 +02:00
if(compareVersions(info.client, '<', current))
2015-06-02 17:20:00 +02:00
return 'text-danger';
2015-04-28 03:25:49 +02:00
return 'hidden';
};
2015-07-19 17:26:34 +02:00
})
.filter('consensusClass', function() {
return function(nodes, bestBlock) {
var status = 'success';
var now = _.now();
for(var x = 0; x < nodes.length; x++)
{
if(nodes[x].stats.block.number === bestBlock.number && nodes[x].stats.block.hash !== bestBlock.hash)
return 'danger';
if((bestBlock.number - nodes[x].stats.block.number) > 1 && (now - bestBlock.received) >= 20*1000)
status = 'orange';
if((bestBlock.number - nodes[x].stats.block.number) === 1 && (now - bestBlock.received) >= 10*1000 && status !== 'orange')
status = 'warning';
}
return status;
};
})
.filter('consensusFilter', function() {
return function(nodes, bestBlock) {
var cnt = 0;
for(var x = 0; x < nodes.length; x++)
{
if(nodes[x].stats.block.number === bestBlock.number && nodes[x].stats.block.hash === bestBlock.hash)
cnt++;
}
return cnt + '/' + nodes.length;
};
2015-02-05 05:32:42 +01:00
});
2015-02-05 02:33:58 +01:00
2015-05-27 12:41:35 +02:00
function compareVersions(v1, comparator, v2)
{
2015-05-27 12:00:10 +02:00
comparator = comparator == '=' ? '==' : comparator;
2015-05-27 12:41:35 +02:00
2015-05-27 12:00:10 +02:00
var v1parts = v1.split('.'), v2parts = v2.split('.');
var maxLen = Math.max(v1parts.length, v2parts.length);
var part1, part2;
var cmp = 0;
2015-05-27 12:41:35 +02:00
for(var i = 0; i < maxLen && !cmp; i++)
{
2015-05-27 12:00:10 +02:00
part1 = parseInt(v1parts[i], 10) || 0;
part2 = parseInt(v2parts[i], 10) || 0;
if(part1 < part2)
cmp = 1;
if(part1 > part2)
cmp = -1;
}
2015-05-27 12:41:35 +02:00
2015-05-27 12:00:10 +02:00
return eval('0' + comparator + cmp);
}
2015-02-08 16:03:05 +01:00
function mainClass(node, bestBlock)
{
if( ! node.active)
2015-04-09 00:10:06 +02:00
return 'text-gray';
2015-02-08 16:03:05 +01:00
if(node.peers === 0)
return 'text-danger';
2015-04-09 00:48:27 +02:00
return peerClass(node.peers, node.active);
2015-02-08 16:03:05 +01:00
}
2015-04-09 00:48:27 +02:00
function peerClass(peers, active)
2015-02-05 05:32:42 +01:00
{
2015-04-09 00:48:27 +02:00
if( ! active)
return 'text-gray';
2015-02-05 05:32:42 +01:00
return (peers <= 1 ? 'text-danger' : (peers > 1 && peers < 4 ? 'text-warning' : 'text-success'));
}
2015-02-05 02:33:58 +01:00
2015-02-05 05:32:42 +01:00
function timeClass(timestamp)
{
2015-04-06 12:47:11 +02:00
var diff = ((new Date()).getTime() - timestamp)/1000;
2015-02-05 02:33:58 +01:00
2015-02-18 08:54:04 +01:00
return blockTimeClass(diff);
}
function blockTimeClass(diff)
{
2015-07-31 12:40:02 +02:00
if(diff <= 13)
2015-02-05 05:32:42 +01:00
return 'text-success';
2015-02-05 02:33:58 +01:00
2015-02-05 05:32:42 +01:00
if(diff <= 20)
2015-04-06 12:55:33 +02:00
return 'text-warning';
2015-02-05 05:32:42 +01:00
if(diff <= 30)
2015-04-06 12:55:33 +02:00
return 'text-orange';
2015-02-05 05:32:42 +01:00
2015-02-18 08:54:04 +01:00
return 'text-danger'
2016-11-08 13:15:10 +01:00
}