From 0b2f01b8bc47f672b49db6ea975948da81903b6c Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 9 Mar 2016 13:19:37 +0200 Subject: [PATCH] info page --- .gitignore | 1 + src/controllers/admin/info.js | 50 ++++++++++++++---- src/controllers/users.js | 12 ++++- src/socket.io/admin/rooms.js | 78 ++++++++++++++++++++-------- src/socket.io/index.js | 57 -------------------- src/socket.io/user.js | 4 -- src/views/admin/development/info.tpl | 34 ++++++++++-- 7 files changed, 137 insertions(+), 99 deletions(-) diff --git a/.gitignore b/.gitignore index ddb37093ba..144cfe268a 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ pidfile ## Directory-based project format: .idea/ +.vscode/ ## File-based project format: *.ipr diff --git a/src/controllers/admin/info.js b/src/controllers/admin/info.js index ec8b79e0a3..4f2c878db2 100644 --- a/src/controllers/admin/info.js +++ b/src/controllers/admin/info.js @@ -2,24 +2,45 @@ var async = require('async'); var os = require('os'); +var winston = require('winston'); var nconf = require('nconf'); var exec = require('child_process').exec; +var pubsub = require('../../pubsub'); var rooms = require('../../socket.io/admin/rooms'); var infoController = {}; +var info = []; + infoController.get = function(req, res, next) { + info = []; + pubsub.publish('sync:node:info:start'); + setTimeout(function() { + res.render('admin/development/info', {info: info, infoJSON: JSON.stringify(info, null, 4), host: os.hostname(), port: nconf.get('port')}); + }, 100); +}; + +pubsub.on('sync:node:info:start', function() { + getNodeInfo(function(err, data) { + if (err) { + return winston.error(err); + } + pubsub.publish('sync:node:info:end', data); + }); +}); + +pubsub.on('sync:node:info:end', function(data) { + info.push(data); +}); +function getNodeInfo(callback) { var data = { process: { port: nconf.get('port'), pid: process.pid, title: process.title, - arch: process.arch, - platform: process.platform, version: process.version, - versions: process.versions, memoryUsage: process.memoryUsage(), uptime: process.uptime() }, @@ -28,19 +49,28 @@ infoController.get = function(req, res, next) { type: os.type(), platform: os.platform(), arch: os.arch(), - release: os.release() + release: os.release(), + load: os.loadavg().map(function(load){ return load.toFixed(2); }).join(', ') } }; - getGitInfo(function(err, gitInfo) { + async.parallel({ + pubsub: function(next) { + pubsub.publish('sync:stats:start'); + next(); + }, + gitInfo: function(next) { + getGitInfo(next); + } + }, function(err, results) { if (err) { - return next(err); + return callback(err); } - data.git = gitInfo; - - res.render('admin/development/info', {info: JSON.stringify(data, null, 4), stats: JSON.stringify(rooms.getStats(), null, 4)}); + data.git = results.gitInfo; + data.stats = rooms.stats[data.os.hostname + ':' + data.process.port]; + callback(null, data); }); -}; +} function getGitInfo(callback) { function get(cmd, callback) { diff --git a/src/controllers/users.js b/src/controllers/users.js index b014ac3452..cefa6b6cc7 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -13,10 +13,18 @@ var helpers = require('./helpers'); var usersController = {}; usersController.getOnlineUsers = function(req, res, next) { - usersController.getUsers('users:online', req.uid, req.query.page, function(err, userData) { + async.parallel({ + users: function(next) { + usersController.getUsers('users:online', req.uid, req.query.page, next); + }, + guests: function(next) { + require('../socket.io/admin/rooms').getTotalGuestCount(next); + } + }, function(err, results) { if (err) { return next(err); } + var userData = results.users; var hiddenCount = 0; if (!userData.isAdminOrGlobalMod) { userData.users = userData.users.filter(function(user) { @@ -27,7 +35,7 @@ usersController.getOnlineUsers = function(req, res, next) { }); } - userData.anonymousUserCount = require('../socket.io').getOnlineAnonCount() + hiddenCount; + userData.anonymousUserCount = results.guests + hiddenCount; render(req, res, userData, next); }); diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index d5e6dd5da6..6d8843b8ca 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -8,9 +8,13 @@ var validator = require('validator'); var topics = require('../../topics'); var pubsub = require('../../pubsub'); -var SocketRooms = {}; - var stats = {}; +var totals = {}; +var SocketRooms = { + stats: stats, + totals: totals +}; + pubsub.on('sync:stats:start', function() { getLocalStats(function(err, stats) { @@ -25,20 +29,42 @@ pubsub.on('sync:stats:end', function(data) { stats[data.id] = data.stats; }); +SocketRooms.getTotalGuestCount = function(callback) { + var count = 0; + pubsub.once('sync:stats:guests', function() { + 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); + }); + + pubsub.on('sync:stats:guests:end', function(guestCount) { + count += guestCount; + }); + + pubsub.publish('sync:stats:guests'); + + setTimeout(function() { + pubsub.removeAllListeners('sync:stats:guests:end'); + callback(null, count); + }, 100); +} + + 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: {} + + totals.onlineGuestCount = 0; + totals.onlineRegisteredCount = 0; + totals.socketCount = 0; + totals.topics = {}; + totals.users = { + categories: 0, + recent: 0, + unread: 0, + topics: 0, + category: 0 }; for(var instance in stats) { @@ -88,22 +114,32 @@ SocketRooms.getAll = function(socket, data, callback) { }); }; -SocketRooms.getStats = function() { - return stats; +SocketRooms.getOnlineUserCount = function(io) { + 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; }; function getLocalStats(callback) { - var websockets = require('../index'); - var io = websockets.server; + var io = require('../index').server; + if (!io) { return callback(); } var roomClients = io.sockets.adapter.rooms; var socketData = { - onlineGuestCount: websockets.getOnlineAnonCount(), - onlineRegisteredCount: websockets.getOnlineUserCount(), - socketCount: websockets.getSocketCount(), + onlineGuestCount: roomClients.online_guests ? roomClients.online_guests.length : 0, + onlineRegisteredCount: SocketRooms.getOnlineUserCount(io), + socketCount: Object.keys(io.sockets.sockets).length, users: { categories: roomClients.categories ? roomClients.categories.length : 0, recent: roomClients.recent_topics ? roomClients.recent_topics.length : 0, diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 1ce84f076d..73c98f34ea 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -213,14 +213,6 @@ Sockets.in = function(room) { return io.in(room); }; -Sockets.getSocketCount = function() { - if (!io) { - return 0; - } - - return Object.keys(io.sockets.sockets).length; -}; - Sockets.getUserSocketCount = function(uid) { if (!io) { return 0; @@ -230,27 +222,6 @@ Sockets.getUserSocketCount = function(uid) { return room ? room.length : 0; }; -Sockets.getOnlineUserCount = function() { - 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; -}; - -Sockets.getOnlineAnonCount = function () { - if (!io) { - return 0; - } - var room = io.sockets.adapter.rooms.online_guests; - return room ? room.length : 0; -}; Sockets.reqFromSocket = function(socket) { var headers = socket.request.headers; @@ -268,33 +239,5 @@ Sockets.reqFromSocket = function(socket) { }; }; -Sockets.isUserOnline = function(uid) { - winston.warn('[deprecated] Sockets.isUserOnline'); - return false; -}; - -Sockets.isUsersOnline = function(uids, callback) { - winston.warn('[deprecated] Sockets.isUsersOnline'); - callback(null, uids.map(function() { return false; })); -}; - -Sockets.getUsersInRoom = function (uid, roomName, start, stop, callback) { - winston.warn('[deprecated] Sockets.getUsersInRoom'); - callback(null, { - users: [], - room: roomName, - total: 0, - hidden: 0 - }); - return; -}; - -Sockets.getUidsInRoom = function(roomName, callback) { - winston.warn('[deprecated] Sockets.getUidsInRoom'); - callback = callback || function() {}; - callback(null, []); -}; - -/* Exporting */ module.exports = Sockets; diff --git a/src/socket.io/user.js b/src/socket.io/user.js index ed6caae39e..2f33ef9f87 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -217,10 +217,6 @@ SocketUser.setCategorySort = function(socket, sort, callback) { } }; -SocketUser.getOnlineAnonCount = function(socket, data, callback) { - callback(null, module.parent.exports.getOnlineAnonCount()); -}; - SocketUser.getUnreadCount = function(socket, data, callback) { if (!socket.uid) { return callback(null, 0); diff --git a/src/views/admin/development/info.tpl b/src/views/admin/development/info.tpl index 2581c9c2ad..c4f9ddde1b 100644 --- a/src/views/admin/development/info.tpl +++ b/src/views/admin/development/info.tpl @@ -1,24 +1,48 @@
-

Info

+

Info - You are on {host}:{port}

-
-
{info}
+ + + + + + + + + + + + + + + + + + + + + + + + + +
hostpidnodejsonlinegitloaduptime
{info.os.hostname}:{info.process.port}{info.process.pid}{info.process.version}{info.stats.onlineRegisteredCount} / {info.stats.onlineGuestCount} / {info.stats.socketCount}{info.git.branch}@{info.git.hash}{info.os.load}{info.process.uptime}
-

Stats

+

Info

-
{stats}
+
{infoJSON}