Last active
July 14, 2017 08:59
-
-
Save loopiezlol/97beb13890212bb158fd2b2fb6a03f12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const co = require('co'); | |
const fs = require('fs'); | |
const User = require('../../app/models/user'); | |
const Topic = require('../../app/models/topic'); | |
const Insight = require('../../app/models/insight'); | |
const Level = require('../../app/models/level'); | |
const Subtopic = require('../../app/models/subtopic'); | |
// function to dynamically instantinate nested objects by assignment | |
const nest = (obj, keys, v) => { | |
if (keys.length === 1) { | |
// eslint-disable-next-line | |
obj[keys[0]] = v; | |
} else { | |
const key = keys.shift(); | |
// eslint-disable-next-line | |
obj[key] = nest(typeof obj[key] === 'undefined' ? {} : obj[key], keys, v); | |
} | |
return obj; | |
}; | |
module.exports = function() { | |
co(function* () { | |
// arrays to store users | |
// a - users who finished a topicLevel combinations | |
// b - users who finished an entire topic | |
const finishedTopicLevel = []; | |
const finishedTopic = []; | |
// create combinations subtopic - level - no of workouts | |
// and count for subtopic - no workouts for all levels | |
const subtopicLevelWorkoutsMap = {}; | |
const subtopics = yield Subtopic.find({deprecated: false}, 'premium topic name') | |
.lean(); | |
const levels = yield Level.find({}, 'label name') | |
.lean(); | |
const topics = yield Topic.find({published: true}, 'name subtopics') | |
.populate('subtopics', 'deprecated name') | |
.lean(); | |
// eslint-disable-next-line | |
for (const subtopic of subtopics) { | |
// eslint-disable-next-line | |
for(const level of levels) { | |
const insights = yield Insight.count({ | |
status: 'approved', | |
subtopic: subtopic._id, | |
levels: level._id, | |
// 'topic.published': true, | |
// doesn't work :( | |
}); | |
// .lean(); | |
// if (insights > 5) { | |
nest(subtopicLevelWorkoutsMap, | |
[subtopic._id.toString(), level._id.toString()], | |
Math.floor(insights / 5)); | |
// } | |
} | |
} | |
// console.log(JSON.stringify(subtopicWorkoutsMap, null, 2)); | |
// iterate over users | |
// for every topic they have check if all subtopics of that folder | |
// - subtoics user users unsubscribed from < 5 | |
// if so add to list | |
let userCount = 0; | |
const users = yield User.find({ | |
workoutsDone: {$gte: 10} | |
}, { | |
username: 1, | |
'topics.levelId': 1, | |
'topics.topicId': 1, | |
'topics.subtopics': 1, | |
'topics.workoutsDonePerLevel': 1, | |
'topics.workoutsDone': 1, | |
}) | |
// .limit(300) | |
.lean(); | |
//eslint-disable-next-line | |
for (const user of users) { | |
userCount += 1; | |
if (userCount % 100 === 0) console.log(`at ${userCount}`); | |
try { | |
user.topics.forEach(ut => { | |
const userSubtopics = ((topics.find(t => ut.topicId.toString() === t._id.toString()) || {}).subtopics || []) | |
.filter(s => !s.deprecated && !ut.subtopics.map(us => us.subtopicId).includes((s._id || {}).toString())) | |
if (!finishedTopic.includes(user.username)) { | |
const maxWorkoutsInTopic = userSubtopics.reduce((acc, val) => | |
acc + (Object.values(subtopicLevelWorkoutsMap[val._id.toString()]) || []) | |
.reduce((a, v) => a + v, 0), 0); | |
const workoutsDoneInTopic = ut.workoutsDone || 0; | |
if (workoutsDoneInTopic >= maxWorkoutsInTopic) { | |
// user finished topic | |
finishedTopic.push(user.username); | |
console.log(`OY ${user.username} finished topic`); | |
} | |
} else if (!finishedTopicLevel.includes(user.username)) { | |
const maxWorkoutsInTopicLevel = userSubtopics.reduce((acc, val) => | |
acc + subtopicLevelWorkoutsMap[val._id.toString()][ut.levelId.toString()], 0); | |
const workoutsDoneInTopicLevel = (ut.workoutsDonePerLevel || {})[ut.levelId.toString()] || 0; | |
if (workoutsDoneInTopicLevel >= maxWorkoutsInTopicLevel) { | |
// user finsiehd level | |
finishedTopicLevel.push(user.username); | |
console.log(`AY ${user.username} finished topic-level`); | |
} | |
} | |
}) | |
} catch (e) { | |
// DO NOTHING BIACH | |
} | |
} | |
// save results to file | |
let finfirst = false; | |
let finsecond = false; | |
fs.writeFile('finishedTopic.txt', | |
finishedTopic.reduce((acc, val) => acc + '\n' + val, ''), | |
err => { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
finfirst = true; | |
console.log('saved first file succesfully'); | |
if (finsecond) process.exit(0); | |
}) | |
fs.writeFile('finishedTopicLevel.txt', | |
finishedTopicLevel.reduce((acc, val) => acc + '\n' + val, ''), | |
err => { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
finsecond = true; | |
console.log('saved second file succesfully'); | |
if (finfirst) process.exit(0); | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment