diff --git a/src/controllers/admin/info.js b/src/controllers/admin/info.js index 5ea0441809..5f36692c29 100644 --- a/src/controllers/admin/info.js +++ b/src/controllers/admin/info.js @@ -4,6 +4,8 @@ var async = require('async'); var os = require('os'); var exec = require('child_process').exec; +var rooms = require('../../socket.io/admin/rooms'); + var infoController = {}; infoController.get = function(req, res, next) { @@ -27,12 +29,14 @@ infoController.get = function(req, res, next) { release: os.release() } }; + getGitInfo(function(err, gitInfo) { if (err) { return next(err); } data.git = gitInfo; - res.render('admin/development/info', {info: JSON.stringify(data, null, 4)}); + + res.render('admin/development/info', {info: JSON.stringify(data, null, 4), stats: JSON.stringify(rooms.getStats(), null, 4)}); }); }; diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index 51f55336a1..618d5b9c0b 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -1,17 +1,102 @@ 'use strict'; +var os = require('os'); +var nconf = require('nconf'); +var winston = require('winston'); var validator = require('validator'); var topics = require('../../topics'); +var pubsub = require('../../pubsub'); var SocketRooms = {}; +var stats = {}; + +pubsub.on('sync:stats:start', function() { + getLocalStats(function(err, stats) { + if (err) { + return winston.error(err); + } + pubsub.publish('sync:stats:end', {stats: stats, id: os.hostname() + ':' + nconf.get('port')}); + }); +}); + +pubsub.on('sync:stats:end', function(data) { + stats[data.id] = data.stats; +}); + SocketRooms.getAll = function(socket, data, callback) { + pubsub.publish('sync:stats:start'); + var totals = { + onlineGuestCount: 0, + onlineRegisteredCount: 0, + socketCount: 0, + users: { + categories: 0, + recent: 0, + unread: 0, + topics: 0, + category: 0 + }, + topics: {} + }; + + for(var instance in stats) { + if (stats.hasOwnProperty(instance)) { + totals.onlineGuestCount += stats[instance].onlineGuestCount; + totals.onlineRegisteredCount += stats[instance].onlineRegisteredCount; + totals.socketCount += stats[instance].socketCount; + totals.users.categories += stats[instance].users.categories; + totals.users.recent += stats[instance].users.recent; + totals.users.unread += stats[instance].users.unread; + totals.users.topics += stats[instance].users.topics; + totals.users.category += stats[instance].users.category; + + stats[instance].topics.forEach(function(topic) { + totals.topics[topic.tid] = totals.topics[topic.tid] || {count: 0, tid: topic.tid}; + totals.topics[topic.tid].count += topic.count; + }); + } + } + + var topTenTopics = []; + Object.keys(totals.topics).forEach(function(tid) { + topTenTopics.push({tid: tid, count: totals.topics[tid].count}); + }); + + topTenTopics = topTenTopics.sort(function(a, b) { + return b.count - a.count; + }).slice(0, 10); + + var topTenTids = topTenTopics.map(function(topic) { + return topic.tid; + }); + topics.getTopicsFields(topTenTids, ['title'], function(err, titles) { + if (err) { + return callback(err); + } + totals.topics = {}; + topTenTopics.forEach(function(topic, index) { + totals.topics[topic.tid] = { + value: topic.count || 0, + title: validator.escape(titles[index].title) + }; + }); + + callback(null, totals); + }); +}; + +SocketRooms.getStats = function() { + return stats; +}; + +function getLocalStats(callback) { var websockets = require('../index'); var io = websockets.server; if (!io) { - return; + return callback(); } var roomClients = io.sockets.adapter.rooms; @@ -23,7 +108,6 @@ SocketRooms.getAll = function(socket, data, callback) { categories: roomClients.categories ? Object.keys(roomClients.categories).length : 0, recent: roomClients.recent_topics ? Object.keys(roomClients.recent_topics).length : 0, unread: roomClients.unread_topics ? Object.keys(roomClients.unread_topics).length: 0, - popular: roomClients.popular_topics ? Object.keys(roomClients.popular_topics).length: 0, topics: 0, category: 0 }, @@ -51,23 +135,9 @@ SocketRooms.getAll = function(socket, data, callback) { return b.count - a.count; }).slice(0, 10); - var topTenTids = topTenTopics.map(function(topic) { - return topic.tid; - }); + socketData.topics = topTenTopics; + callback(null, socketData); +} - topics.getTopicsFields(topTenTids, ['title'], function(err, titles) { - if (err) { - return callback(err); - } - topTenTopics.forEach(function(topic, index) { - socketData.topics[topic.tid] = { - value: topic.count || 0, - title: validator.escape(titles[index].title) - }; - }); - - callback(null, socketData); - }); -}; module.exports = SocketRooms; \ No newline at end of file diff --git a/src/views/admin/development/info.tpl b/src/views/admin/development/info.tpl index 563d12c11f..2581c9c2ad 100644 --- a/src/views/admin/development/info.tpl +++ b/src/views/admin/development/info.tpl @@ -10,4 +10,16 @@ + +
{stats}
+