From 0b94a49ac28c846cf2148d7dffc520a9db45001e Mon Sep 17 00:00:00 2001 From: cubedro Date: Fri, 27 Mar 2015 12:28:55 +0200 Subject: [PATCH] node added uptime history --- models/collection.js | 2 +- models/node.js | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/models/collection.js b/models/collection.js index c7917b1..7d38788 100644 --- a/models/collection.js +++ b/models/collection.js @@ -33,7 +33,7 @@ Collection.prototype.inactive = function(id) if(!node) return false; - node.stats.active = false; + node.setState(false); return node.getStats(); } diff --git a/models/node.js b/models/node.js index 35afd86..8f1e202 100644 --- a/models/node.js +++ b/models/node.js @@ -28,6 +28,15 @@ var Node = function Node(data) uptime: 0, lastUpdate: 0 }; + this.uptime = { + started: null, + history: [] + }; + + if(this.id === null) { + this.uptime.started = (new Date()).getTime(); + this.setState(true); + } if(typeof data.id !== 'undefined') this.id = data.id; @@ -66,6 +75,9 @@ Node.prototype.setInfo = function(data) if(typeof data.spark !== 'undefined') this.spark = data.spark; + + if(this.uptime.history.length > 0 && this.uptime.history[this.uptime.history.length - 1].status == 'down') + this.setState(true); } Node.prototype.getInfo = function() @@ -77,7 +89,7 @@ Node.prototype.setStats = function(stats) { if(typeof stats !== undefined && typeof stats.block !== undefined && typeof stats.block.number !== undefined) { - if(stats.block.number !== this.stats.number){ + if(stats.block.number !== this.stats.number) { stats.block.received == (new Date()).getTime() - stats.block.arrival; } else { stats.block.received = this.stats.block.received; @@ -96,4 +108,10 @@ Node.prototype.getStats = function() return {id: this.id, stats: this.stats}; } +Node.prototype.setState = function(active) +{ + this.stats.active = active; + this.uptime.history.push({status: (active ? 'up' : 'down'), time: (new Date()).getTime()}); +} + module.exports = Node;