4 function createIdForTimestampString(timestamp) {
5 var hexSeconds = Math.floor(timestamp/1000).toString(16);
7 while(hexSeconds.length < 8) {
8 hexSeconds = "0" + hexSeconds;
10 return ObjectId(hexSeconds + "0000000000000000");
13 function aggregate(start, end) {
14 return db.chatmessages.aggregate([{
17 $lt: createIdForTimestampString(end),
18 $gt: createIdForTimestampString(start)
20 sent: { $type: 'date' }
26 $addToSet: { $dayOfYear: '$sent' }
29 $addToSet: '$toTroupeId'
36 days: { $size: '$days' },
37 rooms: { $size: '$rooms' },
44 function findSignups(start,end) {
45 return db.users.find({
47 $lt: createIdForTimestampString(end),
48 $gt: createIdForTimestampString(start)
54 var endP1 = new Date('2016-10-01T00:00:00Z');
55 var startP1 = new Date('2016-09-01T00:00:00Z');
56 var startP0 = new Date('2016-08-01T00:00:00Z');
61 aggregate(startP1, endP1).forEach(function(d) {
62 engageHash[d._id] = d;
65 findSignups(startP0, startP1).forEach(function(d) {
66 var engagement = engageHash[d._id];
67 // if (!engagement) return;
69 if (d.username.indexOf('_twitter') >= 0) {
70 tw.push(engagement || {})
72 gh.push(engagement || {});
77 return function(memo, v) {
78 return memo + (v && v[c] || 0);
82 var twTotalDays = tw.reduce(sum('days'), 0);
83 var ghTotalDays = gh.reduce(sum('days'),0);
88 avgEngagedDays: twTotalDays/tw.length,
92 avgEngagedDays: ghTotalDays/gh.length,