Files
NodeBB/src/socket.io/admin/rooms.js

178 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-11-04 17:43:43 -05:00
'use strict';
2015-11-05 17:47:06 -05:00
var os = require('os');
var nconf = require('nconf');
var winston = require('winston');
2015-11-04 17:43:43 -05:00
var validator = require('validator');
var topics = require('../../topics');
2015-11-05 17:47:06 -05:00
var pubsub = require('../../pubsub');
2015-11-04 17:43:43 -05:00
2015-11-05 17:47:06 -05:00
var stats = {};
2016-03-09 13:19:37 +02:00
var totals = {};
var SocketRooms = {
stats: stats,
totals: totals
};
2015-11-05 17:47:06 -05:00
pubsub.on('sync:stats:start', function () {
SocketRooms.getLocalStats(function (err, stats) {
2015-11-05 17:47:06 -05:00
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) {
2015-11-05 17:47:06 -05:00
stats[data.id] = data.stats;
});
pubsub.on('sync:stats:guests', function () {
2016-03-13 11:05:59 +02:00
var io = require('../index').server;
var roomClients = io.sockets.adapter.rooms;
var guestCount = roomClients.online_guests ? roomClients.online_guests.length : 0;
pubsub.publish('sync:stats:guests:end', guestCount);
});
SocketRooms.getTotalGuestCount = function (callback) {
2016-03-09 13:19:37 +02:00
var count = 0;
pubsub.on('sync:stats:guests:end', function (guestCount) {
2016-03-09 13:19:37 +02:00
count += guestCount;
});
pubsub.publish('sync:stats:guests');
setTimeout(function () {
2016-03-09 13:19:37 +02:00
pubsub.removeAllListeners('sync:stats:guests:end');
callback(null, count);
}, 100);
2016-08-31 14:25:36 +03:00
};
2016-03-09 13:19:37 +02:00
SocketRooms.getAll = function (socket, data, callback) {
2015-11-05 17:47:06 -05:00
pubsub.publish('sync:stats:start');
2016-03-09 13:19:37 +02:00
totals.onlineGuestCount = 0;
totals.onlineRegisteredCount = 0;
totals.socketCount = 0;
totals.topics = {};
totals.users = {
categories: 0,
recent: 0,
unread: 0,
topics: 0,
category: 0
2015-11-05 17:47:06 -05:00
};
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) {
2015-11-05 17:47:06 -05:00
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) {
2015-11-05 17:47:06 -05:00
topTenTopics.push({tid: tid, count: totals.topics[tid].count});
});
topTenTopics = topTenTopics.sort(function (a, b) {
2015-11-05 17:47:06 -05:00
return b.count - a.count;
}).slice(0, 10);
var topTenTids = topTenTopics.map(function (topic) {
2015-11-05 17:47:06 -05:00
return topic.tid;
});
2015-11-04 17:43:43 -05:00
topics.getTopicsFields(topTenTids, ['title'], function (err, titles) {
2015-11-05 17:47:06 -05:00
if (err) {
return callback(err);
}
totals.topics = {};
topTenTopics.forEach(function (topic, index) {
2015-11-05 17:47:06 -05:00
totals.topics[topic.tid] = {
2015-11-07 12:31:19 -05:00
value: topic.count || 0,
title: validator.escape(String(titles[index].title))
2015-11-05 17:47:06 -05:00
};
});
callback(null, totals);
});
};
SocketRooms.getOnlineUserCount = function (io) {
2016-03-09 13:19:37 +02:00
if (!io) {
return 0;
}
var count = 0;
for (var key in io.sockets.adapter.rooms) {
if (io.sockets.adapter.rooms.hasOwnProperty(key) && key.startsWith('uid_')) {
++ count;
}
}
return count;
2015-11-05 17:47:06 -05:00
};
SocketRooms.getLocalStats = function (callback) {
2016-03-09 13:19:37 +02:00
var io = require('../index').server;
2015-11-04 17:43:43 -05:00
if (!io) {
2015-11-05 17:47:06 -05:00
return callback();
2015-11-04 17:43:43 -05:00
}
var roomClients = io.sockets.adapter.rooms;
var socketData = {
2016-03-09 13:19:37 +02:00
onlineGuestCount: roomClients.online_guests ? roomClients.online_guests.length : 0,
onlineRegisteredCount: SocketRooms.getOnlineUserCount(io),
socketCount: Object.keys(io.sockets.sockets).length,
2015-11-04 17:43:43 -05:00
users: {
2016-03-05 19:20:40 +02:00
categories: roomClients.categories ? roomClients.categories.length : 0,
recent: roomClients.recent_topics ? roomClients.recent_topics.length : 0,
2016-10-13 11:42:29 +02:00
unread: roomClients.unread_topics ? roomClients.unread_topics.length : 0,
2015-11-04 17:43:43 -05:00
topics: 0,
category: 0
},
topics: {}
};
2016-03-05 19:20:40 +02:00
var topTenTopics = [];
var tid;
2015-11-04 17:43:43 -05:00
for (var room in roomClients) {
if (roomClients.hasOwnProperty(room)) {
tid = room.match(/^topic_(\d+)/);
if (tid) {
2016-03-05 19:20:40 +02:00
socketData.users.topics += roomClients[room].length;
topTenTopics.push({tid: tid[1], count: roomClients[room].length});
2015-11-04 17:43:43 -05:00
} else if (room.match(/^category/)) {
2016-03-05 19:20:40 +02:00
socketData.users.category += roomClients[room].length;
2015-11-04 17:43:43 -05:00
}
}
}
topTenTopics = topTenTopics.sort(function (a, b) {
2015-11-04 17:43:43 -05:00
return b.count - a.count;
}).slice(0, 10);
2015-11-05 17:47:06 -05:00
socketData.topics = topTenTopics;
callback(null, socketData);
2016-08-31 14:25:36 +03:00
};
2015-11-04 17:43:43 -05:00
module.exports = SocketRooms;