Skip to content

Instantly share code, notes, and snippets.

@loopiezlol
Last active June 22, 2017 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loopiezlol/4b28611cc2d1675511337ab6f97514bb to your computer and use it in GitHub Desktop.
Save loopiezlol/4b28611cc2d1675511337ab6f97514bb to your computer and use it in GitHub Desktop.
const co = require('co');
const moment = require('moment');
const User = require('../../app/models/user');
const Workout = require('../../app/models/workout');
/*
Script used to get an accurate estimate of returning users on a specific date
( + the following day ).
E.g. On 13th of July we sent an email. The question is how many users that dind't
open the application in X (range) days have opened it after receiving the email
(on 13th and 14th).
*/
module.exports = function() {
co(function*() {
// given date
const date = moment('2017-06-13');
// given range of innactivity (e.g. 60, 30)
const range = 30;
// count of returning users
let count = 0;
// counter used for iteration status
let status = 0;
// get all users with their last workouts prepared in the given range
const users = yield User.find({
'workoutPrepStatus.preparedAt': { $gte: date.clone().subtract(range + 1, 'days').toDate()}
}, '_id')
.lean();
// iterate over users one by one
//eslint-disable-next-line
for (const u of users) {
status +=1 ;
console.log(`${status} / ${users.length}`);
// get the fetched workouts of the user on the given date + next day
const returningWorkouts = yield Workout.find({
userId: u._id,
fetched: true,
preparedFor: {
$gte: date.toDate(),
$lte: date.clone().add(2, 'days').toDate(),
}
}, '_id').lean();
// if he has fetched a workout -> he opened the app -> he is a returning user
if (returningWorkouts.length > 0) {
// get all workouts geerated for user in the given range
const previousWorkouts = yield Workout.find({
userId: u._id,
preparedFor: {
$gte: date.clone().subtract(range, 'days').toDate(),
$lte: date.toDate(),
}
}, 'fetched')
.lean();
// if all these workouts weren't fetched -> user didn't open the app
// -> he was inactive those day
if (previousWorkouts.every(x => x.fetched === false)) {
// if this is the case then increment the counter of ret. users
count += 1;
}
}
}
console.log('found ', count, ' returning users');
process.exit(0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment