better error handling

This commit is contained in:
cubedro 2015-04-28 02:50:28 +03:00
parent 068133b021
commit 2fd2145d54
3 changed files with 49 additions and 37 deletions

13
app.js
View File

@ -82,13 +82,16 @@ api.on('connection', function(spark) {
data.stats.latency = spark.latency; data.stats.latency = spark.latency;
var stats = Nodes.update(data.id, data.stats); var stats = Nodes.update(data.id, data.stats);
client.write({action: 'update', data: stats}); if(stats !== false)
{
client.write({action: 'update', data: stats});
var blockPropagationChart = Nodes.blockPropagationChart(); var blockPropagationChart = Nodes.blockPropagationChart();
client.write({action: 'blockPropagationChart', data: blockPropagationChart}); client.write({action: 'blockPropagationChart', data: blockPropagationChart});
var uncleCount = Nodes.getUncleCount(); var uncleCount = Nodes.getUncleCount();
client.write({action: 'uncleCount', data: uncleCount}); client.write({action: 'uncleCount', data: uncleCount});
}
} }
}); });

View File

@ -27,6 +27,10 @@ Collection.prototype.update = function(id, stats)
return false; return false;
var block = this._history.add(stats.block, id); var block = this._history.add(stats.block, id);
if(! block)
return false;
var propagationHistory = this._history.getNodePropagation(id); var propagationHistory = this._history.getNodePropagation(id);
stats.block.arrived = block.arrived; stats.block.arrived = block.arrived;

View File

@ -33,47 +33,52 @@ var History = function History(data)
History.prototype.add = function(block, id) History.prototype.add = function(block, id)
{ {
var historyBlock = this.search(block.number); if(typeof block !== 'undefined' && typeof block.number !== 'undefined' && typeof block.uncles !== 'undefined' && typeof block.transactions !== 'undefined')
var now = (new Date()).getTime();
block.arrived = now;
block.received = now;
block.propagation = 0;
if(historyBlock)
{ {
var propIndex = _.findIndex(historyBlock.propagTimes, {node: id}); var historyBlock = this.search(block.number);
if(propIndex === -1) var now = (new Date()).getTime();
block.arrived = now;
block.received = now;
block.propagation = 0;
if(historyBlock)
{ {
block.arrived = historyBlock.block.arrived; var propIndex = _.findIndex(historyBlock.propagTimes, {node: id});
block.received = now;
block.propagation = now - historyBlock.block.received;
historyBlock.propagTimes.push({node: id, received: now, propagation: block.propagation}); if(propIndex === -1)
{
block.arrived = historyBlock.block.arrived;
block.received = now;
block.propagation = now - historyBlock.block.received;
historyBlock.propagTimes.push({node: id, received: now, propagation: block.propagation});
}
else
{
block.arrived = historyBlock.block.arrived;
block.received = historyBlock.propagTimes[propIndex].received;
block.propagation = historyBlock.propagTimes[propIndex].propagation;
}
} }
else else
{ {
block.arrived = historyBlock.block.arrived; var item = {
block.received = historyBlock.propagTimes[propIndex].received; height: block.number,
block.propagation = historyBlock.propagTimes[propIndex].propagation; block: block,
} propagTimes: []
} }
else
{
var item = {
height: block.number,
block: block,
propagTimes: []
}
item.propagTimes.push({node: id, received: now, propagation: block.propagation}); item.propagTimes.push({node: id, received: now, propagation: block.propagation});
console.log('item: ', item); console.log('item: ', item);
this._save(item); this._save(item);
} }
this.getNodePropagation(id); this.getNodePropagation(id);
return block; return block;
}
return false;
} }
History.prototype._save = function(block) History.prototype._save = function(block)