ethstats-server/models/collection.js

121 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-02-17 03:04:15 +01:00
var _ = require('lodash');
var Node = require('./node');
var Collection = function Collection()
{
this._list = [];
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-04 23:59:24 +02:00
if(this._bestBlock === null)
{
stats.block.received = (new Date()).getTime();
stats.block.propagation = 0;
this._bestBlock = stats.block;
}
else
{
var oldStats = node.getStats();
if(stats.block.number !== oldStats.stats.block.number)
{
stats.block.received = (new Date()).getTime();
2015-04-05 18:30:26 +02:00
if(this._bestBlock.number < stats.block.number)
2015-04-04 23:59:24 +02:00
{
stats.block.propagation = 0;
this._bestBlock = stats.block;
}
else
{
stats.block.propagation = stats.block.received - this._bestBlock.received;
}
} else {
stats.block.received = oldStats.stats.block.received;
stats.block.propagation = oldStats.stats.block.propagation;
}
}
2015-03-27 10:44:07 +01:00
return node.setStats(stats);
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
}
module.exports = Collection;