sync ACP stats over pubsub

v1.18.x
barisusakli 9 years ago
parent 5e257107cd
commit 1be178f2a5

@ -4,6 +4,8 @@ var async = require('async');
var os = require('os'); var os = require('os');
var exec = require('child_process').exec; var exec = require('child_process').exec;
var rooms = require('../../socket.io/admin/rooms');
var infoController = {}; var infoController = {};
infoController.get = function(req, res, next) { infoController.get = function(req, res, next) {
@ -27,12 +29,14 @@ infoController.get = function(req, res, next) {
release: os.release() release: os.release()
} }
}; };
getGitInfo(function(err, gitInfo) { getGitInfo(function(err, gitInfo) {
if (err) { if (err) {
return next(err); return next(err);
} }
data.git = gitInfo; 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)});
}); });
}; };

@ -1,17 +1,102 @@
'use strict'; 'use strict';
var os = require('os');
var nconf = require('nconf');
var winston = require('winston');
var validator = require('validator'); var validator = require('validator');
var topics = require('../../topics'); var topics = require('../../topics');
var pubsub = require('../../pubsub');
var SocketRooms = {}; 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) { 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 websockets = require('../index');
var io = websockets.server; var io = websockets.server;
if (!io) { if (!io) {
return; return callback();
} }
var roomClients = io.sockets.adapter.rooms; 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, categories: roomClients.categories ? Object.keys(roomClients.categories).length : 0,
recent: roomClients.recent_topics ? Object.keys(roomClients.recent_topics).length : 0, recent: roomClients.recent_topics ? Object.keys(roomClients.recent_topics).length : 0,
unread: roomClients.unread_topics ? Object.keys(roomClients.unread_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, topics: 0,
category: 0 category: 0
}, },
@ -51,23 +135,9 @@ SocketRooms.getAll = function(socket, data, callback) {
return b.count - a.count; return b.count - a.count;
}).slice(0, 10); }).slice(0, 10);
var topTenTids = topTenTopics.map(function(topic) { socketData.topics = topTenTopics;
return topic.tid; 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; module.exports = SocketRooms;

@ -10,4 +10,16 @@
</div> </div>
</div> </div>
</div> </div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Stats</h3>
</div>
<div class="panel-body">
<div class="highlight">
<pre>{stats}</pre>
</div>
</div>
</div>
</div> </div>
Loading…
Cancel
Save