ethstats-server/app.js

375 lines
7.7 KiB
JavaScript
Raw Normal View History

2015-04-29 07:49:43 +02:00
var _ = require('lodash');
2015-05-12 17:02:51 +02:00
2015-04-28 09:43:56 +02:00
var askedForHistory = false;
2015-04-28 16:42:44 +02:00
var askedForHistoryTime = 0;
2015-02-05 00:55:48 +01:00
2015-02-17 03:04:15 +01:00
var Primus = require('primus'),
2015-04-29 07:49:43 +02:00
api,
client;
2015-02-26 22:58:22 +01:00
var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret";
2015-02-17 03:04:15 +01:00
var Collection = require('./models/collection');
var Nodes = new Collection();
2015-06-02 17:00:32 +02:00
var server;
2015-05-12 17:02:51 +02:00
var env = 'production';
if( process.env.NODE_ENV !== 'production' )
{
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
// view engine setup
app.set('views', path.join(__dirname, 'src/views'));
2015-05-12 17:02:51 +02:00
app.set('view engine', 'jade');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/', function(req, res) {
res.render('index');
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
// production error handler
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
2015-06-02 17:00:32 +02:00
server = require('http').createServer(app);
2015-05-12 17:02:51 +02:00
}
else
{
2015-06-02 17:00:32 +02:00
server = require('http').createServer();
2015-05-12 17:02:51 +02:00
}
2015-02-17 06:12:44 +01:00
2015-02-17 03:04:15 +01:00
api = new Primus(server, {
2015-04-29 07:49:43 +02:00
transformer: 'websockets',
pathname: '/api',
parser: 'JSON'
2015-02-17 03:04:15 +01:00
});
api.use('emit', require('primus-emit'));
2015-02-23 14:57:41 +01:00
api.use('spark-latency', require('primus-spark-latency'));
2015-02-17 03:04:15 +01:00
2015-02-17 06:12:44 +01:00
var client = new Primus(server, {
2015-04-29 07:49:43 +02:00
transformer: 'websockets',
pathname: '/primus',
parser: 'JSON'
2015-02-17 06:12:44 +01:00
});
2015-04-06 07:33:01 +02:00
var clientLatency = 0;
2015-02-17 06:12:44 +01:00
client.use('emit', require('primus-emit'));
2015-06-02 15:55:23 +02:00
api.on('connection', function (spark)
{
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Open:', spark.address.ip);
2015-04-29 07:49:43 +02:00
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('hello', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Hello', data['id']);
2015-04-29 07:49:43 +02:00
if( _.isUndefined(data.secret) || data.secret !== WS_SECRET )
{
spark.end(undefined, { reconnect: false });
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Closed - wrong auth', data);
2015-04-29 07:49:43 +02:00
return false;
}
if( !_.isUndefined(data.id) && !_.isUndefined(data.info) )
{
data.ip = spark.address.ip;
data.spark = spark.id;
2015-06-03 02:51:19 +02:00
data.latency = spark.latency || 0;
2015-04-29 07:49:43 +02:00
2015-04-29 11:00:01 +02:00
var info = Nodes.add( data );
2015-04-29 07:49:43 +02:00
spark.emit('ready');
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connected', data.id);
2015-04-29 07:49:43 +02:00
client.write({
action: 'add',
data: info
});
2015-06-02 17:00:32 +02:00
var time = (new Date()).toJSON();
console.time(time + ' [COL] [CHR] Got charts in');
2015-04-29 07:49:43 +02:00
client.write({
action: 'charts',
data: Nodes.getCharts()
});
2015-06-02 17:00:32 +02:00
console.timeEnd(time + ' [COL] [CHR] Got charts in');
2015-04-29 07:49:43 +02:00
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('update', function (data)
2015-04-29 07:49:43 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
var stats = Nodes.update(data.id, data.stats);
if(stats !== false)
{
client.write({
action: 'update',
data: stats
});
2015-06-02 17:00:32 +02:00
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');
2015-04-29 07:49:43 +02:00
client.write({
action: 'charts',
data: Nodes.getCharts()
});
2015-06-02 17:00:32 +02:00
console.timeEnd(time + ' [COL] [CHR] Got charts in');
2015-04-29 07:49:43 +02:00
}
2015-06-02 17:00:32 +02:00
}
else
{
console.log((new Date()).toJSON(), '[API]', '[UPD]', 'Update error:', data);
2015-04-29 07:49:43 +02:00
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('block', function (data)
2015-06-01 21:51:31 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.block) )
{
var stats = Nodes.addBlock(data.id, data.block);
if(stats !== false)
{
client.write({
action: 'block',
data: stats
});
2015-06-02 17:00:32 +02:00
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');
2015-06-01 21:51:31 +02:00
client.write({
action: 'charts',
data: Nodes.getCharts()
});
2015-06-02 17:00:32 +02:00
console.timeEnd(time + ' [COL] [CHR] Got charts in');
2015-06-01 21:51:31 +02:00
}
}
2015-06-02 17:00:32 +02:00
else
{
console.log((new Date()).toJSON(), '[API]', '[BLK]', 'Block error:', data);
}
2015-06-01 21:51:31 +02:00
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('pending', function (data)
2015-06-01 20:42:50 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
var stats = Nodes.updatePending(data.id, data.stats);
if(stats !== false)
{
client.write({
action: 'pending',
data: stats
});
}
2015-06-02 17:00:32 +02:00
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);
2015-06-01 20:42:50 +02:00
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('stats', function (data)
2015-06-01 23:04:34 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
2015-06-02 17:00:32 +02:00
2015-06-01 23:04:34 +02:00
var stats = Nodes.updateStats(data.id, data.stats);
if(stats !== false)
{
client.write({
action: 'stats',
data: stats
});
}
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[STA]', 'Stats from:', data.id);
}
else
{
console.log((new Date()).toJSON(), '[API]', '[STA]', 'Stats error:', data);
2015-06-01 23:04:34 +02:00
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('history', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Got from:', data.id);
var time = (new Date()).toJSON();
console.time(time + ' [COL] [CHR] Got charts in');
2015-04-29 07:49:43 +02:00
client.write({
action: 'charts',
data: Nodes.addHistory(data.id, data.history)
});
2015-06-02 17:00:32 +02:00
console.timeEnd(time + ' [COL] [CHR] Got charts in');
2015-04-29 07:49:43 +02:00
askedForHistory = false;
2015-06-02 17:00:32 +02:00
2015-04-29 07:49:43 +02:00
});
2015-06-03 02:51:19 +02:00
2015-06-02 04:06:53 +02:00
spark.on('node-ping', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-02 03:53:26 +02:00
var start = (!_.isUndefined(data) && !_.isUndefined(data.clientTime) ? data.clientTime : null);
2015-06-02 02:06:11 +02:00
spark.emit('node-pong', {
2015-06-02 03:53:26 +02:00
clientTime: start,
2015-06-02 02:06:11 +02:00
serverTime: _.now()
});
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Ping from:', data['id']);
2015-04-29 07:49:43 +02:00
});
2015-06-03 02:51:19 +02:00
2015-06-02 04:06:53 +02:00
spark.on('latency', function (data)
2015-04-29 07:49:43 +02:00
{
if( !_.isUndefined(data.id) )
{
2015-06-01 20:42:50 +02:00
var latency = Nodes.updateLatency(data.id, data.latency);
2015-04-29 07:49:43 +02:00
2015-06-03 02:51:19 +02:00
if(latency)
{
client.write({
action: 'latency',
data: latency
});
}
2015-04-29 07:49:43 +02:00
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Latency:', latency, 'from:', data.id);
2015-04-29 07:49:43 +02:00
if( Nodes.requiresUpdate(data.id) && (!askedForHistory || _.now() - askedForHistoryTime > 200000) )
{
var range = Nodes.getHistory().getHistoryRequestRange();
spark.emit('history', range);
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Asked:', data.id, 'for history:', range.min, '-', range.max);
2015-04-29 07:49:43 +02:00
askedForHistory = true;
askedForHistoryTime = _.now();
}
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('end', function (data)
2015-04-29 07:49:43 +02:00
{
var stats = Nodes.inactive(spark.id);
client.write({
action: 'inactive',
data: stats
});
2015-06-02 17:00:32 +02:00
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connection with:', spark.id, 'ended:', data);
2015-04-29 07:49:43 +02:00
});
2015-02-17 06:12:44 +01:00
});
2015-02-17 03:04:15 +01:00
2015-06-03 02:51:19 +02:00
2015-06-02 03:33:17 +02:00
client.on('connection', function (clientSpark)
2015-04-29 07:49:43 +02:00
{
2015-06-02 03:33:17 +02:00
clientSpark.on('ready', function (data)
2015-04-29 07:49:43 +02:00
{
clientSpark.emit('init', { nodes: Nodes.all() });
2015-04-17 11:10:20 +02:00
2015-04-29 07:49:43 +02:00
clientSpark.write({
action: 'charts',
data: Nodes.getCharts()
});
});
2015-04-06 07:33:01 +02:00
2015-06-02 03:33:17 +02:00
clientSpark.on('client-pong', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-02 03:53:26 +02:00
var start = (!_.isUndefined(data) && !_.isUndefined(data.serverTime) ? data.serverTime : clientLatency);
2015-06-02 04:06:53 +02:00
var latency = Math.ceil( (_.now() - start) / 2 );
2015-04-29 07:49:43 +02:00
clientSpark.emit('client-latency', { latency: latency });
});
2015-02-17 06:12:44 +01:00
});
2014-12-03 04:08:49 +01:00
2015-04-29 07:49:43 +02:00
var latencyTimeout = setInterval( function ()
{
2015-06-02 03:49:34 +02:00
clientLatency = _.now();
2015-06-02 03:33:17 +02:00
client.write({
action: 'client-ping',
data: {
2015-06-02 03:49:34 +02:00
serverTime: clientLatency
2015-06-02 03:33:17 +02:00
}
});
2015-04-06 07:33:01 +02:00
}, 5000);
// Cleanup old inactive nodes
var nodeCleanupTimeout = setInterval( function ()
{
client.write({
action: 'init',
data: Nodes.all()
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
2015-05-11 21:10:19 +02:00
}, 1000*60*60);
2015-02-17 06:12:44 +01:00
server.listen(process.env.PORT || 3000);
2014-12-03 04:08:49 +01:00
2015-05-12 17:02:51 +02:00
module.exports = server;