added getHistory

This commit is contained in:
cubedro
2015-04-28 10:43:56 +03:00
parent df47834021
commit abaef56982
7 changed files with 116 additions and 37 deletions

View File

@@ -40,6 +40,20 @@ Collection.prototype.update = function(id, stats)
return node.setStats(stats, propagationHistory);
}
Collection.prototype.addHistory = function(id, blocks)
{
var node = this.getNode({ id: id });
if(!node)
return false;
for (var i = blocks.length - 1; i >= 0; i--) {
this._history.add(blocks[i], id);
};
return this.getCharts();
}
Collection.prototype.updateLatency = function(id, latency)
{
var node = this.getNode({ id: id });
@@ -112,4 +126,14 @@ Collection.prototype.getUncleCount = function()
return this._history.getUncleCount();
}
Collection.prototype.getCharts = function()
{
return this._history.getCharts();
}
Collection.prototype.getHistory = function()
{
return this._history;
}
module.exports = Collection;

View File

@@ -33,7 +33,7 @@ var History = function History(data)
History.prototype.add = function(block, id)
{
if(typeof block !== 'undefined' && typeof block.number !== 'undefined' && typeof block.uncles !== 'undefined' && typeof block.transactions !== 'undefined' && typeof block.difficulty !== 'undefined')
if(typeof block !== 'undefined' && typeof block.number !== 'undefined' && typeof block.uncles !== 'undefined' && typeof block.transactions !== 'undefined' && typeof block.difficulty !== 'undefined' && (this._items.length === 0 || block.number >= (this.bestBlock().height - MAX_HISTORY + 1)))
{
var historyBlock = this.search(block.number);
@@ -68,7 +68,9 @@ History.prototype.add = function(block, id)
if(prevBlock)
{
block.time = block.arrived - prevBlock.block.arrived;
block.time_old = block.timestamp - prevBlock.block.timestamp;
if(block.number < this.bestBlock().height)
block.time = (block.timestamp - prevBlock.block.timestamp)*1000;
}
else
{
@@ -92,10 +94,10 @@ History.prototype.add = function(block, id)
History.prototype._save = function(block)
{
this._items.push(block);
this._items.unshift(block);
if(this._items.length > MAX_HISTORY){
this._items.shift();
this._items.pop();
}
this._items = _.sortByOrder(this._items, 'height', false);
@@ -279,7 +281,8 @@ History.prototype.getCharts = function()
.map(function(item)
{
var chart = {
blocktime: item.block.time,
height: item.height,
blocktime: item.block.time/1000,
difficulty: item.block.difficulty,
uncles: item.block.uncles.length,
transactions: item.block.transactions.length,
@@ -289,12 +292,39 @@ History.prototype.getCharts = function()
})
.value();
return chartHistory;
var chart = {
height: _.pluck(chartHistory, 'height'),
blocktime: _.pluck(chartHistory, 'blocktime'),
difficulty: _.pluck(chartHistory, 'difficulty'),
uncles: _.pluck(chartHistory, 'uncles'),
transactions: _.pluck(chartHistory, 'transactions'),
gasSpending: _.pluck(chartHistory, 'gasSpending'),
propagation: this.getBlockPropagation(),
uncleCount: this.getUncleCount()
}
return chart;
}
History.prototype.history = function()
{
return _.chain(this._items).sortBy('height').reverse().value();
return this._items;
}
History.prototype.requiresUpdate = function()
{
return ! (this._items.length === MAX_HISTORY);
}
History.prototype.getHistoryRequestInterval = function()
{
if(this._items.length === 0)
return null;
var max = _.min(this._items, 'height').height - 1;
var min = max - Math.min(100, (MAX_HISTORY - this._items.length + 1)) + 1;
return {max: max, min: min};
}
module.exports = History;

View File

@@ -1,7 +1,7 @@
var geoip = require('geoip-lite');
var _ = require('lodash');
var MAX_HISTORY = 36;
var MAX_HISTORY = 40;
var Node = function Node(data)
{
@@ -49,9 +49,13 @@ var Node = function Node(data)
if(typeof data.id !== 'undefined')
this.id = data.id;
if(typeof data.info !== 'undefined')
if(typeof data.info !== 'undefined') {
this.info = data.info;
if(typeof data.info.canUpdateHistory === 'undefined')
data.info.canUpdateHistory = false;
}
if(typeof data.ip !== 'undefined'){
if(data.ip === '::ffff:127.0.0.1')
data.ip = '84.117.82.122';
@@ -75,9 +79,13 @@ Node.prototype.setGeo = function(ip)
Node.prototype.setInfo = function(data)
{
if(typeof data.info !== 'undefined')
if(typeof data.info !== 'undefined') {
this.info = data.info;
if(typeof data.info.canUpdateHistory === 'undefined')
data.info.canUpdateHistory = false;
}
if(typeof data.ip !== 'undefined'){
this.info.ip = data.ip;
this.setGeo(data.ip);
@@ -131,4 +139,9 @@ Node.prototype.setState = function(active)
this.uptime.history.push({status: (active ? 'up' : 'down'), time: (new Date()).getTime()});
}
Node.prototype.canUpdate = function()
{
return this.info.canUpdateHistory || false;
}
module.exports = Node;