2 * This script will get a count of all active users and aggregates it by month.
3 * Users we consider active:
4 * - sent a message during the time period
5 * By default it gets last 6 months (ignoring current month because it's not finished yet) but this 6 month window
6 * can be shifted by any amount of months. This mechanism is mainly here to prevent from running hour long queries on the DB.
9 * `mongo gitter --eval "var skipMonth=6" active-users-by-month.mongo`
11 * e.g. running this on 25th of November 2019 will result in getting user count for 2018-11 - 2019-04
15 load('./fail-on-master.js');
17 load('./date-time-utils.js');
19 // this is how we pass arguments to this script https://stackoverflow.com/a/10114802/606571
20 // eslint-disable-next-line no-use-before-define
21 var skipMonth = skipMonth || 0;
23 for (var i = 0; i < 6; i++) {
24 var startXMonthsAgo = skipMonth + i;
25 var start = startOfUtcMonth(-1 - startXMonthsAgo);
26 var end = startOfUtcMonth(-startXMonthsAgo);
28 var x = db.chatmessages.aggregate([
32 $lt: createIdForTimestamp(end),
33 $gt: createIdForTimestamp(start)
42 // only users who didn't sign up given month
46 $lt: createIdForTimestamp(start)
58 const count = (x.toArray() && x.toArray()[0] && x.toArray()[0].count) || 0;
59 print(tojson({ date: start.toISOString(), count: count }));