ethstats-server/app.js

245 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-04-29 07:49:43 +02:00
var _ = require('lodash');
2015-02-17 06:12:44 +01:00
var express = require('express');
var app = express();
2014-12-03 04:08:49 +01:00
var path = require('path');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
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-02-17 06:12:44 +01:00
var server = require('http').createServer(app);
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) )
{
data.stats.latency = spark.latency;
var stats = Nodes.update(data.id, data.stats);
if(stats !== false)
{
client.write({
action: 'update',
data: stats
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
}
}
});
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;
client.write({
action: 'charts',
data: Nodes.getCharts()
});
});
spark.on('node-ping', function(data)
{
spark.emit('node-pong');
});
spark.on('latency', function(data)
{
if( !_.isUndefined(data.id) )
{
var stats = Nodes.updateLatency(data.id, data.latency);
client.write({
action: 'latency',
data: stats
});
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-04-29 07:49:43 +02:00
client.on('connection', function(clientSpark)
{
clientSpark.on('ready', function(data)
{
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-04-29 07:49:43 +02:00
clientSpark.on('client-pong', function(data)
{
var latency = Math.ceil( (_.now() - clientLatency) / 2 );
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 ()
{
clientLatency = _.now();
client.write({ action: 'client-ping' });
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);
2014-12-03 04:08:49 +01:00
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
2015-03-27 17:52:42 +01:00
app.use(favicon(path.join(__dirname, '/public/images/favicon.png')));
2014-12-03 04:08:49 +01:00
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
2015-02-17 06:12:44 +01:00
app.get('/', function(req, res) {
res.render('index', { title: 'Ethereum Network Status' });
});
2014-12-03 04:08:49 +01:00
// catch 404 and forward to error handler
app.use(function(req, res, next) {
2015-04-29 07:49:43 +02:00
var err = new Error('Not Found');
err.status = 404;
next(err);
2014-12-03 04:08:49 +01:00
});
// error handlers
if (app.get('env') === 'development') {
2015-04-29 07:49:43 +02:00
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
2014-12-03 04:08:49 +01:00
}
// production error handler
app.use(function(err, req, res, next) {
2015-04-29 07:49:43 +02:00
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
2014-12-03 04:08:49 +01:00
});
2015-02-17 06:12:44 +01:00
server.listen(process.env.PORT || 3000);
2014-12-03 04:08:49 +01:00
module.exports = app;