diff --git a/app.js b/app.js index 68a73f6..45ccf65 100644 --- a/app.js +++ b/app.js @@ -12,6 +12,7 @@ var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret"; var Collection = require('./models/collection'); var Nodes = new Collection(); +var server; var env = 'production'; if( process.env.NODE_ENV !== 'production' ) @@ -57,11 +58,11 @@ if( process.env.NODE_ENV !== 'production' ) }); }); - var server = require('http').createServer(app); + server = require('http').createServer(app); } else { - var server = require('http').createServer(); + server = require('http').createServer(); } api = new Primus(server, { @@ -85,16 +86,16 @@ client.use('emit', require('primus-emit')); api.on('connection', function (spark) { - console.log(_.now(), "Socket api connection:", spark.address.ip); + console.log((new Date()).toJSON(), '[API]', '[CON]', 'Open:', spark.address.ip); spark.on('hello', function (data) { - console.log(_.now(), "Socket api hello", data); + console.log((new Date()).toJSON(), '[API]', '[CON]', 'Hello', data['id']); if( _.isUndefined(data.secret) || data.secret !== WS_SECRET ) { spark.end(undefined, { reconnect: false }); - console.log(_.now(), "Socket api connection closed - wrong auth", data); + console.log((new Date()).toJSON(), '[API]', '[CON]', 'Closed - wrong auth', data); return false; } @@ -108,22 +109,27 @@ api.on('connection', function (spark) var info = Nodes.add( data ); spark.emit('ready'); + console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connected', data.id); + client.write({ action: 'add', data: info }); + var time = (new Date()).toJSON(); + console.time(time + ' [COL] [CHR] Got charts in'); + client.write({ action: 'charts', data: Nodes.getCharts() }); + + console.timeEnd(time + ' [COL] [CHR] Got charts in'); } }); spark.on('update', function (data) { - console.log(_.now(), "Socket api update", data); - if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) { var stats = Nodes.update(data.id, data.stats); @@ -135,18 +141,28 @@ api.on('connection', function (spark) data: stats }); + console.log((new Date()).toJSON(), '[API]', '[UPD]', 'Update from:', data.id, 'for:', data.stats); + + var time = (new Date()).toJSON(); + console.time(time + ' [COL] [CHR] Got charts in'); + client.write({ action: 'charts', data: Nodes.getCharts() }); + + console.timeEnd(time + ' [COL] [CHR] Got charts in'); } + + } + else + { + console.log((new Date()).toJSON(), '[API]', '[UPD]', 'Update error:', data); } }); spark.on('block', function (data) { - console.log(_.now(), "Socket api block", data); - if( !_.isUndefined(data.id) && !_.isUndefined(data.block) ) { var stats = Nodes.addBlock(data.id, data.block); @@ -158,18 +174,27 @@ api.on('connection', function (spark) data: stats }); + console.log((new Date()).toJSON(), '[API]', '[BLK]', 'Block:', data.block['number'], 'from:', data.id); + + var time = (new Date()).toJSON(); + console.time(time + ' [COL] [CHR] Got charts in'); + client.write({ action: 'charts', data: Nodes.getCharts() }); + + console.timeEnd(time + ' [COL] [CHR] Got charts in'); } } + else + { + console.log((new Date()).toJSON(), '[API]', '[BLK]', 'Block error:', data); + } }); spark.on('pending', function (data) { - console.log(_.now(), "Socket api pending", data); - if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) { var stats = Nodes.updatePending(data.id, data.stats); @@ -181,15 +206,20 @@ api.on('connection', function (spark) data: stats }); } + + console.log((new Date()).toJSON(), '[API]', '[TXS]', 'Pending:', data.stats['pending'], 'from:', data.id); + } + else + { + console.log((new Date()).toJSON(), '[API]', '[TXS]', 'Pending error:', data); } }); spark.on('stats', function (data) { - console.log(_.now(), "Socket api stats", data); - if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) { + var stats = Nodes.updateStats(data.id, data.stats); if(stats !== false) @@ -199,19 +229,31 @@ api.on('connection', function (spark) data: stats }); } + + console.log((new Date()).toJSON(), '[API]', '[STA]', 'Stats from:', data.id); + } + else + { + console.log((new Date()).toJSON(), '[API]', '[STA]', 'Stats error:', data); } }); spark.on('history', function (data) { - console.log("got history from " + data.id); + console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Got from:', data.id); + + var time = (new Date()).toJSON(); + console.time(time + ' [COL] [CHR] Got charts in'); client.write({ action: 'charts', data: Nodes.addHistory(data.id, data.history) }); + console.timeEnd(time + ' [COL] [CHR] Got charts in'); + askedForHistory = false; + }); spark.on('node-ping', function (data) @@ -222,6 +264,8 @@ api.on('connection', function (spark) clientTime: start, serverTime: _.now() }); + + console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Ping from:', data['id']); }); spark.on('latency', function (data) @@ -235,13 +279,14 @@ api.on('connection', function (spark) data: latency }); + console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Latency:', latency, 'from:', data.id); + if( Nodes.requiresUpdate(data.id) && (!askedForHistory || _.now() - askedForHistoryTime > 200000) ) { var range = Nodes.getHistory().getHistoryRequestRange(); - console.log("asked " + data.id + " for history: " + range.min + " - " + range.max); - spark.emit('history', range); + console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Asked:', data.id, 'for history:', range.min, '-', range.max); askedForHistory = true; askedForHistoryTime = _.now(); @@ -251,14 +296,14 @@ api.on('connection', function (spark) spark.on('end', function (data) { - console.log(_.now(), "Socket api connection end", data); - var stats = Nodes.inactive(spark.id); client.write({ action: 'inactive', data: stats }); + + console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connection with:', spark.id, 'ended:', data); }); });