Skip to content

Commit 911eabc

Browse files
authored
Merge pull request RocketChat#431 from assistify/usage-statistics
Provide detailed anonymized usage-statistics
2 parents 2e69be4 + 3aa2550 commit 911eabc

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

packages/rocketchat-statistics/package.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ Package.onUse(function(api) {
99
api.use([
1010
'mongo',
1111
'ecmascript',
12-
'rocketchat:lib'
12+
'rocketchat:lib',
13+
'sha'
1314
]);
1415

1516
// Statistics
1617
api.addFiles('lib/rocketchat.js', [ 'client', 'server' ]);
1718
api.addFiles([
1819
'server/models/Statistics.js',
20+
'server/functions/getUsages.js',
1921
'server/functions/get.js',
2022
'server/functions/save.js',
2123
'server/methods/getStatistics.js'

packages/rocketchat-statistics/server/functions/get.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* global InstanceStatus, MongoInternals */
22
import _ from 'underscore';
33
import os from 'os';
4+
import {getUsages} from './getUsages';
45

56
const wizardFields = [
67
'Organization_Type',
@@ -96,5 +97,7 @@ RocketChat.statistics.get = function _getStatistics() {
9697
statistics.oplogEnabled = true;
9798
}
9899

100+
statistics.usages = getUsages();
101+
99102
return statistics;
100103
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
3+
export function getUsages() {
4+
const lastStatistics = RocketChat.models.Statistics.findLast();
5+
const lastStatisticsCreatedAt = lastStatistics ? lastStatistics.createdAt : new Date();
6+
const userDB = RocketChat.models.Users;
7+
const subDB = RocketChat.models.Subscriptions;
8+
const messageDB = RocketChat.models.Messages;
9+
const usages = [];
10+
const users = userDB.model.aggregate([
11+
{
12+
$unwind: '$emails'
13+
},
14+
{
15+
$project: { '_id': 1, 'emails.address': 1 }
16+
}
17+
]);
18+
19+
const readsArr = subDB.model.aggregate([
20+
{
21+
$match:
22+
{
23+
ts: { $gt: new Date(lastStatisticsCreatedAt.toISOString()) }
24+
}
25+
},
26+
{
27+
$group:
28+
{
29+
_id: { uid: '$u._id', subType: '$t'},
30+
subs: { $sum: 1 }
31+
}
32+
},
33+
{
34+
$group:
35+
{
36+
_id: '$_id.uid',
37+
roomTypes: {
38+
$addToSet:
39+
{
40+
type: '$_id.subType',
41+
subscriptions: '$subs'
42+
}
43+
}
44+
}
45+
}
46+
]);
47+
const reads = new Map(readsArr.map((i) => [i._id, i.roomTypes]));
48+
49+
const writesArr = messageDB.model.aggregate([
50+
{
51+
$match:
52+
{
53+
ts: { $gt: new Date(lastStatisticsCreatedAt.toISOString()) }
54+
}
55+
},
56+
{
57+
$lookup:
58+
{
59+
from: 'rocketchat_room',
60+
localField: 'rid',
61+
foreignField: '_id',
62+
as: 'msgRooms'
63+
}
64+
},
65+
{
66+
$unwind: '$msgRooms'
67+
},
68+
{
69+
$group:
70+
{
71+
_id: { uid: '$u._id', msgRoom: '$msgRooms.t'},
72+
messages: { $sum: 1 }
73+
}
74+
},
75+
{
76+
$group:
77+
{
78+
_id: '$_id.uid',
79+
roomTypes: {
80+
$addToSet:
81+
{
82+
type: '$_id.msgRoom',
83+
messages: '$messages'
84+
}
85+
}
86+
}
87+
}
88+
]);
89+
const writes = new Map(writesArr.map((i) => [i._id, i.roomTypes]));
90+
91+
for (let i = 0; i < users.length; i++) {
92+
const user = users[i];
93+
let active = false;
94+
if (reads.has(user._id)) {
95+
user.reads = reads.get(user._id);
96+
active = true;
97+
} else {
98+
user.reads = {};
99+
}
100+
if (writes.has(user._id)) {
101+
user.writes = writes.get(user._id);
102+
active = true;
103+
} else {
104+
user.writes = {};
105+
}
106+
user.active = active;
107+
user._id = SHA256(user.emails.address);
108+
delete user.emails.address;
109+
delete user.emails;
110+
usages.push(user);
111+
112+
}
113+
return usages;
114+
}

0 commit comments

Comments
 (0)