|
| 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