-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewMostActiveUserCompanyHistory.js
66 lines (57 loc) · 2.02 KB
/
viewMostActiveUserCompanyHistory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* This script will group the historical entries for a user and company.
* The most active company will be listed first
*/
const user = 'JerrenSaunders';
// const company = '';
const MAX_HISTORY_SIZE = 1000;
db.getCollection("LinkActivity").aggregate([
// { $limit: 10}, // For debugging
// Split the query params up first so that the order of the params doesn't matter
// and we can have better options when grouping
{
$addFields: {
queryParamsObject: {
$arrayToObject: {
$map: {
input: { $split: ["$req.rawQueryString", "&"] }, // Separate query params first on '&'
as: "keyValue",
in: { $split: ["$$keyValue", "="] } // Split each key-value pair
}
}
}
}
},
{
$match: {
'queryParamsObject.user': user
// 'queryParamsObject.co': company
}
},
// We want to see the newest request first after grouping, so sort now
{ $sort: { ts: -1 } },
// Group the query params that were used most
{
$group: {
_id: {
user: '$queryParamsObject.user',
co: '$queryParamsObject.co'
},
history: {
$push: {
url: { $concat: ['$req.httpMethod', ' ', { $ifNull: ['$req.httpReferrer', 'Unknown'] }, '?', { $ifNull: ['$req.rawQueryString', ''] }] },
ts: '$ts',
redirectedTo: '$action.redirectedTo'
}
}
}
},
// Show the most frequently visited at the top & limit the history
{
$addFields: {
historyCount: { $size: '$history' }, // So we can sort
history: { $slice: ['$history', MAX_HISTORY_SIZE] } // Limit the history to avoid the individual documents from getting too large (`historyCount` will still contain the original size)
}
},
{ $sort: { historyCount: -1 } }
])