ethstats-server/models/collection.js

81 lines
1.3 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 = [];
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-03-27 10:44:07 +01:00
return node.setStats(stats);
2015-02-17 03:04:15 +01:00
}
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;