formated logs

This commit is contained in:
cubedro 2015-06-02 18:00:32 +03:00
parent ca4a63bd7d
commit f81773a348

81
app.js
View File

@ -12,6 +12,7 @@ var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret";
var Collection = require('./models/collection'); var Collection = require('./models/collection');
var Nodes = new Collection(); var Nodes = new Collection();
var server;
var env = 'production'; var env = 'production';
if( process.env.NODE_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 else
{ {
var server = require('http').createServer(); server = require('http').createServer();
} }
api = new Primus(server, { api = new Primus(server, {
@ -85,16 +86,16 @@ client.use('emit', require('primus-emit'));
api.on('connection', function (spark) 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) 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 ) if( _.isUndefined(data.secret) || data.secret !== WS_SECRET )
{ {
spark.end(undefined, { reconnect: false }); 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; return false;
} }
@ -108,22 +109,27 @@ api.on('connection', function (spark)
var info = Nodes.add( data ); var info = Nodes.add( data );
spark.emit('ready'); spark.emit('ready');
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connected', data.id);
client.write({ client.write({
action: 'add', action: 'add',
data: info data: info
}); });
var time = (new Date()).toJSON();
console.time(time + ' [COL] [CHR] Got charts in');
client.write({ client.write({
action: 'charts', action: 'charts',
data: Nodes.getCharts() data: Nodes.getCharts()
}); });
console.timeEnd(time + ' [COL] [CHR] Got charts in');
} }
}); });
spark.on('update', function (data) spark.on('update', function (data)
{ {
console.log(_.now(), "Socket api update", data);
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{ {
var stats = Nodes.update(data.id, data.stats); var stats = Nodes.update(data.id, data.stats);
@ -135,18 +141,28 @@ api.on('connection', function (spark)
data: stats 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({ client.write({
action: 'charts', action: 'charts',
data: Nodes.getCharts() 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) spark.on('block', function (data)
{ {
console.log(_.now(), "Socket api block", data);
if( !_.isUndefined(data.id) && !_.isUndefined(data.block) ) if( !_.isUndefined(data.id) && !_.isUndefined(data.block) )
{ {
var stats = Nodes.addBlock(data.id, data.block); var stats = Nodes.addBlock(data.id, data.block);
@ -158,18 +174,27 @@ api.on('connection', function (spark)
data: stats 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({ client.write({
action: 'charts', action: 'charts',
data: Nodes.getCharts() 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) spark.on('pending', function (data)
{ {
console.log(_.now(), "Socket api pending", data);
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{ {
var stats = Nodes.updatePending(data.id, data.stats); var stats = Nodes.updatePending(data.id, data.stats);
@ -181,15 +206,20 @@ api.on('connection', function (spark)
data: stats 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) spark.on('stats', function (data)
{ {
console.log(_.now(), "Socket api stats", data);
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{ {
var stats = Nodes.updateStats(data.id, data.stats); var stats = Nodes.updateStats(data.id, data.stats);
if(stats !== false) if(stats !== false)
@ -199,19 +229,31 @@ api.on('connection', function (spark)
data: stats 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) 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({ client.write({
action: 'charts', action: 'charts',
data: Nodes.addHistory(data.id, data.history) data: Nodes.addHistory(data.id, data.history)
}); });
console.timeEnd(time + ' [COL] [CHR] Got charts in');
askedForHistory = false; askedForHistory = false;
}); });
spark.on('node-ping', function (data) spark.on('node-ping', function (data)
@ -222,6 +264,8 @@ api.on('connection', function (spark)
clientTime: start, clientTime: start,
serverTime: _.now() serverTime: _.now()
}); });
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Ping from:', data['id']);
}); });
spark.on('latency', function (data) spark.on('latency', function (data)
@ -235,13 +279,14 @@ api.on('connection', function (spark)
data: latency data: latency
}); });
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Latency:', latency, 'from:', data.id);
if( Nodes.requiresUpdate(data.id) && (!askedForHistory || _.now() - askedForHistoryTime > 200000) ) if( Nodes.requiresUpdate(data.id) && (!askedForHistory || _.now() - askedForHistoryTime > 200000) )
{ {
var range = Nodes.getHistory().getHistoryRequestRange(); var range = Nodes.getHistory().getHistoryRequestRange();
console.log("asked " + data.id + " for history: " + range.min + " - " + range.max);
spark.emit('history', range); spark.emit('history', range);
console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Asked:', data.id, 'for history:', range.min, '-', range.max);
askedForHistory = true; askedForHistory = true;
askedForHistoryTime = _.now(); askedForHistoryTime = _.now();
@ -251,14 +296,14 @@ api.on('connection', function (spark)
spark.on('end', function (data) spark.on('end', function (data)
{ {
console.log(_.now(), "Socket api connection end", data);
var stats = Nodes.inactive(spark.id); var stats = Nodes.inactive(spark.id);
client.write({ client.write({
action: 'inactive', action: 'inactive',
data: stats data: stats
}); });
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connection with:', spark.id, 'ended:', data);
}); });
}); });