diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index b0af8b8a389..b40cfe0d076 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -1278,7 +1278,12 @@ "Stats_Online_Users": "Online Users", "Stats_Total_Channels": "Total Channels", "Stats_Total_Direct_Messages": "Total Direct Message Rooms", + "Stats_Total_Livechat_Rooms": "Total Livechat Rooms", "Stats_Total_Messages": "Total Messages", + "Stats_Total_Messages_Channel": "Total Messages in Channels", + "Stats_Total_Messages_PrivateGroup": "Total Messages in Private Groups", + "Stats_Total_Messages_Direct": "Total Messages in Direct Messages", + "Stats_Total_Messages_Livechat": "Total Messages in Livechats", "Stats_Total_Private_Groups": "Total Private Groups", "Stats_Total_Rooms": "Total Rooms", "Stats_Total_Users": "Total Users", diff --git a/packages/rocketchat-lib/server/lib/notifyUsersOnMessage.js b/packages/rocketchat-lib/server/lib/notifyUsersOnMessage.js index bd61e4a197d..d7bd95dcbc5 100644 --- a/packages/rocketchat-lib/server/lib/notifyUsersOnMessage.js +++ b/packages/rocketchat-lib/server/lib/notifyUsersOnMessage.js @@ -1,12 +1,18 @@ import moment from 'moment'; RocketChat.callbacks.add('afterSaveMessage', function(message, room) { - // skips this callback if the message was edited - if (message.editedAt) { + // skips this callback if the message was edited and increments it if the edit was way in the past (aka imported) + if (message.editedAt && Math.abs(moment(message.editedAt).diff()) > 60000) { + //TODO: Review as I am not sure how else to get around this as the incrementing of the msgs count shouldn't be in this callback + RocketChat.models.Rooms.incMsgCountById(message.rid, 1); + return message; + } else if (message.editedAt) { + // skips this callback if the message was edited return message; } if (message.ts && Math.abs(moment(message.ts).diff()) > 60000) { + RocketChat.models.Rooms.incMsgCountById(message.rid, 1); return message; } diff --git a/packages/rocketchat-lib/server/models/Messages.coffee b/packages/rocketchat-lib/server/models/Messages.coffee index 363fd2cac7a..8ac7b4efcd7 100644 --- a/packages/rocketchat-lib/server/models/Messages.coffee +++ b/packages/rocketchat-lib/server/models/Messages.coffee @@ -385,6 +385,7 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base _.extend record, extraData record._id = @insertOrUpsert record + RocketChat.models.Rooms.incMsgCountById(room._id, 1) return record createUserJoinWithRoomIdAndUser: (roomId, user, extraData) -> diff --git a/packages/rocketchat-lib/server/models/Rooms.coffee b/packages/rocketchat-lib/server/models/Rooms.coffee index 1c497021c2a..0830b53f773 100644 --- a/packages/rocketchat-lib/server/models/Rooms.coffee +++ b/packages/rocketchat-lib/server/models/Rooms.coffee @@ -397,6 +397,16 @@ class ModelRooms extends RocketChat.models._Base return @update query, update + incMsgCountById: (_id, inc=1) -> + query = + _id: _id + + update = + $inc: + msgs: inc + + return @update query, update + incMsgCountAndSetLastMessageTimestampById: (_id, inc=1, lastMessageTimestamp) -> query = _id: _id diff --git a/packages/rocketchat-statistics/package.js b/packages/rocketchat-statistics/package.js index 9e862ada23d..e495b32450a 100644 --- a/packages/rocketchat-statistics/package.js +++ b/packages/rocketchat-statistics/package.js @@ -16,7 +16,7 @@ Package.onUse(function(api) { api.addFiles('lib/rocketchat.coffee', [ 'client', 'server' ]); api.addFiles([ 'server/models/Statistics.coffee', - 'server/functions/get.coffee', + 'server/functions/get.js', 'server/functions/save.coffee', 'server/methods/getStatistics.coffee' ], 'server'); diff --git a/packages/rocketchat-statistics/server/functions/get.coffee b/packages/rocketchat-statistics/server/functions/get.coffee deleted file mode 100644 index 0e48d5231d3..00000000000 --- a/packages/rocketchat-statistics/server/functions/get.coffee +++ /dev/null @@ -1,57 +0,0 @@ -RocketChat.statistics.get = -> - statistics = {} - - # Version - statistics.uniqueId = RocketChat.settings.get("uniqueID") - statistics.installedAt = RocketChat.models.Settings.findOne("uniqueID")?.createdAt - statistics.version = RocketChat.Info?.version - statistics.tag = RocketChat.Info?.tag - statistics.branch = RocketChat.Info?.branch - - # User statistics - statistics.totalUsers = Meteor.users.find().count() - statistics.activeUsers = Meteor.users.find({ active: true }).count() - statistics.nonActiveUsers = statistics.totalUsers - statistics.activeUsers - statistics.onlineUsers = Meteor.users.find({ statusConnection: 'online' }).count() - statistics.awayUsers = Meteor.users.find({ statusConnection: 'away' }).count() - statistics.offlineUsers = statistics.totalUsers - statistics.onlineUsers - statistics.awayUsers - - # Room statistics - statistics.totalRooms = RocketChat.models.Rooms.find().count() - statistics.totalChannels = RocketChat.models.Rooms.findByType('c').count() - statistics.totalPrivateGroups = RocketChat.models.Rooms.findByType('p').count() - statistics.totalDirect = RocketChat.models.Rooms.findByType('d').count() - - # Message statistics - statistics.totalMessages = RocketChat.models.Messages.find().count() - - statistics.lastLogin = RocketChat.models.Users.getLastLogin() - statistics.lastMessageSentAt = RocketChat.models.Messages.getLastTimestamp() - statistics.lastSeenSubscription = RocketChat.models.Subscriptions.getLastSeen() - - migration = Migrations?._getControl() - if migration - statistics.migration = _.pick(migration, 'version', 'locked') - - os = Npm.require('os') - statistics.os = - type: os.type() - platform: os.platform() - arch: os.arch() - release: os.release() - uptime: os.uptime() - loadavg: os.loadavg() - totalmem: os.totalmem() - freemem: os.freemem() - cpus: os.cpus() - - statistics.process = - nodeVersion: process.version - pid: process.pid - uptime: process.uptime() - - statistics.migration = RocketChat.Migrations._getControl() - - statistics.instanceCount = InstanceStatus.getCollection().find().count() - - return statistics diff --git a/packages/rocketchat-statistics/server/functions/get.js b/packages/rocketchat-statistics/server/functions/get.js new file mode 100644 index 00000000000..aa0a070f09b --- /dev/null +++ b/packages/rocketchat-statistics/server/functions/get.js @@ -0,0 +1,66 @@ +/* global InstanceStatus */ +RocketChat.statistics.get = function _getStatistics() { + const statistics = {}; + + // Version + statistics.uniqueId = RocketChat.settings.get('uniqueID') + if (RocketChat.models.Settings.findOne('uniqueID')) { + statistics.installedAt = RocketChat.models.Settings.findOne('uniqueID').createdAt; + } + + if (RocketChat.Info) { + statistics.version = RocketChat.Info.version; + statistics.tag = RocketChat.Info.tag; + statistics.branch = RocketChat.Info.branch; + } + + // User statistics + statistics.totalUsers = Meteor.users.find().count(); + statistics.activeUsers = Meteor.users.find({ active: true }).count(); + statistics.nonActiveUsers = statistics.totalUsers - statistics.activeUsers; + statistics.onlineUsers = Meteor.users.find({ statusConnection: 'online' }).count(); + statistics.awayUsers = Meteor.users.find({ statusConnection: 'away' }).count(); + statistics.offlineUsers = statistics.totalUsers - statistics.onlineUsers - statistics.awayUsers; + + // Room statistics + statistics.totalRooms = RocketChat.models.Rooms.find().count(); + statistics.totalChannels = RocketChat.models.Rooms.findByType('c').count(); + statistics.totalPrivateGroups = RocketChat.models.Rooms.findByType('p').count(); + statistics.totalDirect = RocketChat.models.Rooms.findByType('d').count(); + statistics.totlalLivechat = RocketChat.models.Rooms.findByType('l').count(); + + // Message statistics + statistics.totalMessages = RocketChat.models.Messages.find().count(); + statistics.totalChannelMessages = _.reduce(RocketChat.models.Rooms.findByType('c', { fields: { 'msgs': 1 }}).fetch(), function _countChannelMessages(num, room) { return num + room.msgs; }, 0); + statistics.totalPrivateGroupMessages = _.reduce(RocketChat.models.Rooms.findByType('p', { fields: { 'msgs': 1 }}).fetch(), function _countPrivateGroupMessages(num, room) { return num + room.msgs; }, 0); + statistics.totalDirectMessages = _.reduce(RocketChat.models.Rooms.findByType('d', { fields: { 'msgs': 1 }}).fetch(), function _countDirectMessages(num, room) { return num + room.msgs; }, 0); + statistics.totalLivechatMessages = _.reduce(RocketChat.models.Rooms.findByType('l', { fields: { 'msgs': 1 }}).fetch(), function _countLivechatMessages(num, room) { return num + room.msgs; }, 0); + + statistics.lastLogin = RocketChat.models.Users.getLastLogin(); + statistics.lastMessageSentAt = RocketChat.models.Messages.getLastTimestamp(); + statistics.lastSeenSubscription = RocketChat.models.Subscriptions.getLastSeen(); + + const os = Npm.require('os'); + statistics.os = { + type: os.type(), + platform: os.platform(), + arch: os.arch(), + release: os.release(), + uptime: os.uptime(), + loadavg: os.loadavg(), + totalmem: os.totalmem(), + freemem: os.freemem(), + cpus: os.cpus() + }; + + statistics.process = { + nodeVersion: process.version, + pid: process.pid, + uptime: process.uptime() + }; + + statistics.migration = RocketChat.Migrations._getControl(); + statistics.instanceCount = InstanceStatus.getCollection().find().count(); + + return statistics; +}; diff --git a/packages/rocketchat-ui-admin/admin/adminInfo.html b/packages/rocketchat-ui-admin/admin/adminInfo.html index b116f63ff15..782464f1bdc 100644 --- a/packages/rocketchat-ui-admin/admin/adminInfo.html +++ b/packages/rocketchat-ui-admin/admin/adminInfo.html @@ -186,10 +186,30 @@ {{_ "Stats_Total_Direct_Messages"}} {{statistics.totalDirect}} + + {{_ "Stats_Total_Livechat_Rooms"}} + {{statistics.totalDirect}} + {{_ "Stats_Total_Messages"}} {{statistics.totalMessages}} + + {{_ "Stats_Total_Messages_Channel"}} + {{statistics.totalChannelMessages}} + + + {{_ "Stats_Total_Messages_PrivateGroup"}} + {{statistics.totalPrivateGroupMessages}} + + + {{_ "Stats_Total_Messages_Direct"}} + {{statistics.totalDirectMessages}} + + + {{_ "Stats_Total_Messages_Livechat"}} + {{statistics.totalLivechatMessages}} + diff --git a/server/startup/migrations/v080.js b/server/startup/migrations/v080.js new file mode 100644 index 00000000000..2e5d1d8a314 --- /dev/null +++ b/server/startup/migrations/v080.js @@ -0,0 +1,8 @@ +RocketChat.Migrations.add({ + version: 80, + up: function() { + RocketChat.models.Rooms.find().forEach((room) => { + RocketChat.models.Rooms.incMsgCountById(room._id, RocketChat.models.Messages.find({ rid: room._id, t: { $exists: true }}).count()); + }); + } +});