Skip to content

Instantly share code, notes, and snippets.

@loopiezlol
Last active May 12, 2017 12:50
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/6cea9e31842edf1525c3a5942e57ab31 to your computer and use it in GitHub Desktop.
Save loopiezlol/6cea9e31842edf1525c3a5942e57ab31 to your computer and use it in GitHub Desktop.
const co = require('co');
require('../../app/models/topic');
require('../../app/models/level');
const User = require('../../app/models/user');
const Workout = require('../../app/models/workout');
module.exports = function() {
co(function*() {
const topicLevelMap = {};
const query = {
createdAt: {
$gt: new Date('2017-04-25').toISOString(),
}
}
const totalUsersRegistered = yield User.count(query);
console.log('total', totalUsersRegistered);
const matchedUsers = yield User.find(query,
'_id createdAt')
// .limit(300)
.lean();
//eslint-disable-next-line
for (const u of matchedUsers) {
const createdDate = new Date(u.createdAt);
const nextDate = new Date(createdDate.getTime() + 86400000);
const initialWorkouts = yield Workout.find({
userId: u._id,
hardcoded: true,
date: {
$gte: createdDate.toISOString(),
$lte: nextDate.toISOString(),
},
}, 'topic level')
.populate('topic', 'name')
.populate('level', 'label')
.limit(5)
.lean();
const onboardingValues = initialWorkouts.map(w => {
return ({
topic: w.topic.name,
level: w.level.label,
})}).reduce((acc, val) => {
if (!acc.find(e => e.topic === val.topic)) {
acc.push(val);
}
return acc;
}, []);
onboardingValues.forEach(t => {
if (!topicLevelMap[t.topic]) {
topicLevelMap[t.topic] = {};
}
if(!topicLevelMap[t.topic][t.level]) {
topicLevelMap[t.topic][t.level] = 0;
}
topicLevelMap[t.topic][t.level] += 1;
})
}
// console.log(matchedUsers);
console.log(topicLevelMap);
process.exit(0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment