ethstats-server/models/collection.js

115 lines
2.1 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()
{
this._list = [];
2015-04-17 11:10:20 +02:00
this._history = new Blockchain();
2015-04-04 23:59:24 +02:00
this._bestBlock = null;
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 });
if(!node)
return false;
2015-04-17 11:10:20 +02:00
var block = this._history.add(stats.block, id);
2015-04-28 01:50:28 +02:00
if(! block)
return false;
2015-04-17 11:10:20 +02:00
var propagationHistory = this._history.getNodePropagation(id);
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-04-03 06:00:06 +02:00
Collection.prototype.updateLatency = function(id, latency)
{
var node = this.getNode({ id: id });
if(!node)
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 });
if(!node)
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)
{
return _.findIndex(this._list, search);
}
Collection.prototype.getNode = function(search)
{
var index = this.getIndex(search);
2015-02-17 06:12:44 +01:00
if(index >= 0)
2015-02-17 03:04:15 +01:00
return this._list[index];
return false;
}
2015-02-17 06:12:44 +01:00
Collection.prototype.getNodeByIndex = function(index)
{
if(this._list[index])
return this._list[index];
return false;
}
Collection.prototype.getIndexOrNew = function(search, data)
{
var index = this.getIndex(search);
return (index >= 0 ? index : this._list.push(new Node(data)) - 1);
}
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-02-17 06:12:44 +01:00
return this._list;
2015-02-17 03:04:15 +01:00
}
2015-04-17 11:10:20 +02:00
Collection.prototype.blockPropagationChart = function()
{
return this._history.getBlockPropagation();
}
2015-04-24 05:23:26 +02:00
Collection.prototype.getUncleCount = function()
{
return this._history.getUncleCount();
}
2015-02-17 03:04:15 +01:00
module.exports = Collection;