ethstats-server/app.js

309 lines
5.8 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-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: {}
});
});
var server = require('http').createServer(app);
}
else
{
var server = require('http').createServer();
}
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-02-17 03:04:15 +01:00
api.on('connection', function(spark) {
2015-04-29 07:49:43 +02:00
console.log('Latency: ', spark.latency);
console.log(spark.id);
console.log(spark.address);
console.log(spark.query);
spark.on('hello', function(data)
{
console.log('Latency: ', spark.latency);
console.log('Got hello data from ', spark.id);
console.log(data);
if( _.isUndefined(data.secret) || data.secret !== WS_SECRET )
{
spark.end(undefined, { reconnect: false });
return false;
}
if( !_.isUndefined(data.id) && !_.isUndefined(data.info) )
{
data.ip = spark.address.ip;
data.spark = spark.id;
data.latency = spark.latency;
2015-04-29 11:00:01 +02:00
var info = Nodes.add( data );
2015-04-29 07:49:43 +02:00
spark.emit('ready');
client.write({
action: 'add',
data: info
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
}
});
spark.on('update', function(data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
var stats = Nodes.update(data.id, data.stats);
if(stats !== false)
{
client.write({
action: 'update',
data: stats
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
}
}
});
2015-06-01 21:51:31 +02:00
spark.on('block', function(data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.block) )
{
var stats = Nodes.addBlock(data.id, data.block);
if(stats !== false)
{
client.write({
action: 'block',
data: stats
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
}
}
});
2015-06-01 20:42:50 +02:00
spark.on('pending', function(data)
{
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-01 23:04:34 +02:00
spark.on('stats', function(data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
var stats = Nodes.updateStats(data.id, data.stats);
if(stats !== false)
{
client.write({
action: 'stats',
data: stats
});
}
}
});
2015-04-29 07:49:43 +02:00
spark.on('history', function(data)
{
console.log("got history from " + data.id);
client.write({
action: 'charts',
data: Nodes.addHistory(data.id, data.history)
});
askedForHistory = false;
});
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-04-29 07:49:43 +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
client.write({
action: 'latency',
2015-06-01 20:42:50 +02:00
data: latency
2015-04-29 07:49:43 +02:00
});
if( Nodes.requiresUpdate(data.id) && (!askedForHistory || _.now() - askedForHistoryTime > 200000) )
{
var range = Nodes.getHistory().getHistoryRequestRange();
2015-04-29 11:03:05 +02:00
console.log("asked " + data.id + " for history: " + range.min + " - " + range.max);
2015-04-29 07:49:43 +02:00
spark.emit('history', range);
askedForHistory = true;
askedForHistoryTime = _.now();
}
}
});
spark.on('end', function(data)
{
var stats = Nodes.inactive(spark.id);
client.write({
action: 'inactive',
data: stats
});
});
2015-02-17 06:12:44 +01:00
});
2015-02-17 03:04:15 +01: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;