ethstats-server/models/collection.js

199 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-02-17 03:04:15 +01:00
var _ = require('lodash');
2015-04-17 11:10:20 +02:00
var Blockchain = require('./history');
2015-02-17 03:04:15 +01:00
var Node = require('./node');
var Collection = function Collection()
{
2015-04-29 07:49:43 +02:00
this._items = [];
this._blockchain = new Blockchain();
2015-02-17 03:04:15 +01:00
return this;
}
Collection.prototype.add = function(data)
{
2015-02-17 06:12:44 +01:00
var node = this.getNodeOrNew({ id : data.id }, data);
2015-02-17 10:30:41 +01:00
node.setInfo(data);
2015-02-17 03:04:15 +01:00
2015-02-17 06:12:44 +01:00
return node.getInfo();
2015-02-17 03:04:15 +01:00
}
2015-02-17 06:12:44 +01:00
Collection.prototype.update = function(id, stats)
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
var node = this.getNode({ id: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-02-17 06:12:44 +01:00
return false;
2015-04-29 07:49:43 +02:00
var block = this._blockchain.add(stats.block, id);
2015-04-28 01:50:28 +02:00
2015-05-19 10:07:48 +02:00
if (!block)
2015-04-28 01:50:28 +02:00
return false;
2015-04-29 07:49:43 +02:00
var propagationHistory = this._blockchain.getNodePropagation(id);
2015-04-17 11:10:20 +02:00
stats.block.arrived = block.arrived;
stats.block.received = block.received;
stats.block.propagation = block.propagation;
return node.setStats(stats, propagationHistory);
2015-02-17 03:04:15 +01:00
}
2015-06-01 20:42:50 +02:00
Collection.prototype.updatePending = function(id, stats)
{
var node = this.getNode({ id: id });
if (!node)
return false;
return node.setPending(stats);
}
2015-04-28 09:43:56 +02:00
Collection.prototype.addHistory = function(id, blocks)
{
var node = this.getNode({ id: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-04-28 09:43:56 +02:00
return false;
2015-04-29 07:58:11 +02:00
blocks = blocks.reverse();
2015-04-29 07:49:43 +02:00
for (var i = 0; i <= blocks.length - 1; i++)
{
this._blockchain.add(blocks[i], id);
2015-04-28 09:43:56 +02:00
};
return this.getCharts();
}
2015-04-03 06:00:06 +02:00
Collection.prototype.updateLatency = function(id, latency)
{
var node = this.getNode({ id: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-04-03 06:00:06 +02:00
return false;
return node.setLatency(latency);
}
2015-02-18 07:06:41 +01:00
Collection.prototype.inactive = function(id)
{
var node = this.getNode({ spark: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-02-18 07:06:41 +01:00
return false;
2015-03-27 11:28:55 +01:00
node.setState(false);
2015-02-18 07:06:41 +01:00
return node.getStats();
}
2015-02-17 03:04:15 +01:00
Collection.prototype.getIndex = function(search)
{
2015-04-29 07:49:43 +02:00
return _.findIndex(this._items, search);
2015-02-17 03:04:15 +01:00
}
Collection.prototype.getNode = function(search)
{
var index = this.getIndex(search);
2015-02-17 06:12:44 +01:00
if(index >= 0)
2015-04-29 07:49:43 +02:00
return this._items[index];
2015-02-17 03:04:15 +01:00
return false;
}
2015-02-17 06:12:44 +01:00
Collection.prototype.getNodeByIndex = function(index)
{
2015-04-29 07:49:43 +02:00
if(this._items[index])
return this._items[index];
2015-02-17 06:12:44 +01:00
return false;
}
Collection.prototype.getIndexOrNew = function(search, data)
{
var index = this.getIndex(search);
2015-04-29 07:49:43 +02:00
return (index >= 0 ? index : this._items.push(new Node(data)) - 1);
2015-02-17 06:12:44 +01:00
}
Collection.prototype.getNodeOrNew = function(search, data)
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
return this.getNodeByIndex(this.getIndexOrNew(search, data));
2015-02-17 03:04:15 +01:00
}
2015-02-17 06:12:44 +01:00
Collection.prototype.all = function()
2015-02-17 03:04:15 +01:00
{
2015-05-11 20:41:37 +02:00
this.removeOldNodes();
2015-04-29 07:49:43 +02:00
return this._items;
2015-02-17 03:04:15 +01:00
}
2015-05-11 20:41:37 +02:00
Collection.prototype.removeOldNodes = function()
{
var deleteList = []
for(var i = this._items.length - 1; i >= 0; i--)
{
var node = this._items[i];
if( node.isInactiveAndOld() )
{
deleteList.push(i);
}
}
if(deleteList.length > 0)
{
for(var i = 0; i < deleteList.length; i++)
{
this._items.splice(deleteList[i], 1);
}
}
}
2015-04-17 11:10:20 +02:00
Collection.prototype.blockPropagationChart = function()
{
2015-04-29 07:49:43 +02:00
return this._blockchain.getBlockPropagation();
2015-04-17 11:10:20 +02:00
}
2015-04-24 05:23:26 +02:00
Collection.prototype.getUncleCount = function()
{
2015-04-29 07:49:43 +02:00
return this._blockchain.getUncleCount();
2015-04-24 05:23:26 +02:00
}
2015-04-28 09:43:56 +02:00
Collection.prototype.getCharts = function()
{
2015-04-29 07:49:43 +02:00
return this._blockchain.getCharts();
2015-04-28 09:43:56 +02:00
}
Collection.prototype.getHistory = function()
{
2015-04-29 07:49:43 +02:00
return this._blockchain;
2015-04-28 09:43:56 +02:00
}
2015-04-28 10:12:47 +02:00
Collection.prototype.canNodeUpdate = function(id)
{
var node = this.getNode({id: id});
2015-04-28 10:12:47 +02:00
if(!node)
return false;
if(node.canUpdate())
{
2015-04-29 07:49:43 +02:00
var diff = this._blockchain.bestBlockNumber() - node.getBlockNumber();
2015-04-29 07:49:43 +02:00
return (diff <= 0);
}
return false;
2015-04-28 10:12:47 +02:00
}
2015-04-29 07:49:43 +02:00
Collection.prototype.requiresUpdate = function(id)
{
return ( this.canNodeUpdate(id) && this._blockchain.requiresUpdate() );
}
2015-04-29 11:00:01 +02:00
module.exports = Collection;