From 1959e77407b671d20eb128c31464eb360acc6382 Mon Sep 17 00:00:00 2001 From: cubedro Date: Wed, 18 Feb 2015 08:06:41 +0200 Subject: [PATCH] node events notifications --- app.js | 14 ++++++++++---- models/collection.js | 12 ++++++++++++ models/node.js | 6 ++++++ public/js/controllers.js | 5 +++++ public/js/services.js | 2 +- views/index.jade | 4 ++-- 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 519d281..7a23383 100644 --- a/app.js +++ b/app.js @@ -34,13 +34,15 @@ api.on('connection', function(spark) { console.log(spark.address); console.log(spark.query); - spark.on('hello', function(data) { + spark.on('hello', function(data) + { console.log('got hello data from ', spark.id); console.log(data); if(typeof data.id !== 'undefined' && typeof data.info !== 'undefined') { data.ip = spark.address.ip; + data.spark = spark.id; var info = Nodes.add(data); spark.emit('ready'); @@ -49,7 +51,8 @@ api.on('connection', function(spark) { } }); - spark.on('update', function(data) { + spark.on('update', function(data) + { console.log('got update from ' + spark.id); console.log(data); @@ -61,8 +64,11 @@ api.on('connection', function(spark) { } }); - spark.on('end', function(data) { - // + spark.on('end', function(data) + { + var stats = Nodes.inactive(spark.id); + + client.write({action: 'inactive', data: stats}); }); }); diff --git a/models/collection.js b/models/collection.js index 07713ab..c38b5a5 100644 --- a/models/collection.js +++ b/models/collection.js @@ -28,6 +28,18 @@ Collection.prototype.update = function(id, stats) return node.getStats(); } +Collection.prototype.inactive = function(id) +{ + var node = this.getNode({ spark: id }); + + if(!node) + return false; + + node.stats.active = false; + + return node.getStats(); +} + Collection.prototype.getIndex = function(search) { return _.findIndex(this._list, search); diff --git a/models/node.js b/models/node.js index 6178205..beaa0f3 100644 --- a/models/node.js +++ b/models/node.js @@ -36,6 +36,9 @@ var Node = function Node(data) this.setGeo(data.ip); } + if(typeof data.spark !== 'undefined') + this.spark = data.spark; + return this; } @@ -53,6 +56,9 @@ Node.prototype.setInfo = function(data) this.info.ip = data.ip; this.setGeo(data.ip); } + + if(typeof data.spark !== 'undefined') + this.spark = data.spark; } Node.prototype.getInfo = function() diff --git a/public/js/controllers.js b/public/js/controllers.js index cd4a32b..10f676b 100644 --- a/public/js/controllers.js +++ b/public/js/controllers.js @@ -69,6 +69,11 @@ function StatsCtrl($scope, $filter, socket, _, toastr) { case "info": $scope.nodes[findIndex({id: data.id})].info = data.info; break; + + case "inactive": + $scope.nodes[findIndex({id: data.id})].stats = data.stats; + toastr['error']("Node went away!", "Node connection was lost!"); + break; } updateStats(); diff --git a/public/js/services.js b/public/js/services.js index 5d0d272..9baf88a 100644 --- a/public/js/services.js +++ b/public/js/services.js @@ -32,7 +32,7 @@ app.factory('toastr', function ($rootScope) { toastr.options = { "closeButton": false, "debug": false, - "progressBar": true, + "progressBar": false, "newestOnTop": true, "positionClass": "toast-top-right", "preventDuplicates": false, diff --git a/views/index.jade b/views/index.jade index 1d9dd05..b9b3538 100644 --- a/views/index.jade +++ b/views/index.jade @@ -6,7 +6,7 @@ block content div.col-lg-6(ng-cloak) div.col-sm-12 h1= title - p Welcome to #{title} + //- p Welcome to #{title} div.clearfix @@ -79,7 +79,7 @@ block content div.small {{node.info.ip}} td.small div.small(ng-bind-html="node.info.node | nodeVersion") - div.small {{node.info.os}}, {{node.info.os_v}} + //- div.small {{node.info.os}}, {{node.info.os_v}} td(class="{{ node.stats.peers | peerClass }}") {{node.stats.peers}} td(class="{{ node.stats.mining | miningClass }}") i(class="{{ node.stats.mining | miningIconClass }}")