fix peer leak + more stats

This commit is contained in:
Mark Nadal 2019-05-15 10:27:57 -07:00
parent 9b820287d6
commit ed30911ba7
2 changed files with 44 additions and 19 deletions

View File

@ -10,15 +10,31 @@
html, body {
font-family: "Oxygen", sans-serif;
}
.ct-chart * {
svg, .ct-chart * {
overflow: visible;
}
.ct-series-a .ct-line,
.ct-series-a .ct-point {
stroke: blue;
}
.ct-series-b .ct-line,
.ct-series-b .ct-point {
stroke: green;
}
</style>
<input id="url" class="center" placeholder="enter peer stats source url">
<div class="leak" style="padding: 0 2em;">
<div class="leak ct-chart ct-perfect-fourth" style="height: 20em;"></div>
<div class="leak ct-mem ct-perfect-fourth" style="height: 20em;"></div>
</div>
<p id="peers" class="shout center">0</p>
<div class="center"><span class="shout" id="peers">0</span> peers <span class="shout" id="nodes">0</span> nodes</div>
<div class="center leak" style="padding: 0 2em;">
<div class="unit col leak ct-cpu ct-perfect-fourth" style="height: 20em;"></div>
</div>
<script src="./jquery.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
<script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
@ -27,21 +43,29 @@ var stats = {slide: [0,0,0,0,0], peers: [0,0,0,0,0]};
setInterval(function(){
stats.show();
}, 1000 * 15);
stats.show = function(){ $.getJSON(url.value||(location.origin+'/gun/stats.radata'), function(data){ console.log(data)
stats.show = function(){ $.getJSON(url.value||(location.origin+'/gun/stats.radata'), function(data){ console.log(data);
$('#peers').text(data.peers.count);
$('#nodes').text(data.node.count);
var mb = data.memory.heapTotal / 1024 / 1024;
stats.slide.push(mb); stats.slide = stats.slide.slice(1);
$(peers).text(data.peers.count);
var data = {
new Chartist.Line('.ct-mem', {
// A labels array that can contain any sort of values
labels: ['-1min', '-45s', '-30s', '-15s', '0'],
// Our series array that contains series objects or in this case series data arrays
series: [stats.slide]
};
}, {fullWidth: true, low: 0, axisY: {
labelInterpolationFnc: function(v) { return v+'MB' }
}});
// Create a new line chart object where as first parameter we pass in a selector
// that is resolving to our chart container element. The Second parameter
// is the actual data object.
new Chartist.Line('.ct-chart', data);
new Chartist.Line('.ct-cpu', {
// A labels array that can contain any sort of values
labels: ['-15min', '-5min', '1min'],
// Our series array that contains series objects or in this case series data arrays
series: [data.cpu.loadavg.reverse()]
}, {fullWidth: true, low: 0, axisY: {
labelInterpolationFnc: function(v) { return v+'cpu' }
}});
}) }
stats.show();
</script>

17
gun.js
View File

@ -2013,16 +2013,16 @@
var tomap = function(k,i,m){m(k,true)};
;(function(){
var message;
function each(peer){ mesh.say(message, peer) }
mesh.say = function(msg, peer, o){
/*
TODO: Plenty of performance optimizations
that can be made just based off of ordering,
and reducing function calls for cached writes.
*/
if(!peer){
Type.obj.map(opt.peers, function(peer){
mesh.say(msg, peer);
});
if(!peer){ message = msg;
Type.obj.map(opt.peers, each);
return;
}
var tmp, wire = peer.wire || ((opt.wire) && opt.wire(peer)), msh, raw;// || open(peer, ctx); // TODO: Reopen!
@ -2126,10 +2126,9 @@
mesh.hi = function(peer){
var tmp = peer.wire || {};
if(peer.id || peer.url){
Type.obj.del(opt.peers, tmp.pid);
opt.peers[peer.url || peer.id] = peer;
} else {
tmp = peer.id = peer.id || Type.text.random(9);
tmp = peer.id = tmp.pid = peer.id || Type.text.random(9);
mesh.say({dam: '?'}, opt.peers[tmp] = peer);
}
if(!tmp.hied){ ctx.on(tmp.hied = 'hi', peer) }
@ -2155,8 +2154,10 @@
return;
}
if(!peer.wire){ return }
if(peer.wire.pid){ return } // they already set their ID!
peer.id = peer.wire.pid = msg.pid;
if(!peer.wire.pid){ return } // only run code below if wire.pid exists
Type.obj.del(opt.peers, peer.wire.pid || peer.id);
delete peer.wire.pid;
peer.id = msg.pid;
mesh.hi(peer);
}
return mesh;