Created
July 12, 2017 13:36
-
-
Save loopiezlol/5e097a1ef48f7b2056d9b9bd214c35cf 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 util = require('util'); | |
const co = require('co'); | |
const fs = require('fs'); | |
const C = require('common/constants'); | |
const User = require('../../app/models/user'); | |
const InsightReview = require('../../app/models/insightReview'); | |
const Topic = require('../../app/models/topic'); | |
const Insight = require('../../app/models/insight'); | |
const Level = require('../../app/models/level'); | |
require('../../app/models/subtopic'); | |
function* hasUserFinishedTopic(user, userTopic, insights, levels) { | |
let hasFinished = true; | |
if(hasFinished) { | |
//eslint-disable-next-line | |
for(const level of levels) { | |
hasFinished = yield hasUserFinsishedTopicLevel(user, userTopic, level, insights, levels); | |
} | |
} | |
return hasFinished; | |
} | |
function* hasUserFinsishedTopicLevel(user, userTopic, userLevel, insights, topics) { | |
let hasFinished = true; | |
// TODO to remove subtopics user unsubscribed from | |
//eslint-disable-next-line | |
for(const s of topics.find(t => t._id.toString() === userTopic.topicId._id.toString()).subtopics.filter(s => !s.deprecated)) { | |
if(hasFinished) { | |
const insightsOfSubtopic = insights.filter(x => x.subtopic.toString() === s._id.toString() && x.levels.map(l => l.toString()).includes(userLevel._id.toString())); | |
// console.log('found', insightsOfSubtopic.length, 'insights in', s.name, userLevel.label) | |
if (insightsOfSubtopic.length - (yield InsightReview.count({ | |
userId: user._id, | |
insightId: { $in: insightsOfSubtopic.map(i => i._id)}, | |
type: { $in: C.INSIGHT.REVIEWS } | |
})) > 5) { | |
hasFinished = false; | |
} | |
} | |
} | |
return hasFinished; | |
} | |
module.exports = function() { | |
co(function*() { | |
const levels = yield Level.find({}, '') | |
.lean(); | |
console.log(`fetched ${levels.length} levels`); | |
const topics = yield Topic.find({published: true}, 'name subtopics') | |
.populate('subtopics', 'deprecated name') | |
.lean(); | |
console.log(`fetched ${topics.length} topics`); | |
const insights = yield Insight.find({status: 'approved', subtopic: {$exists: true}}, 'subtopic levels').lean(); | |
console.log(`fetched ${insights.length} insights`); | |
let userCount = 0; | |
const uFinishedTopic = []; | |
const uFinishedTopicLevel = []; | |
const users = yield User.find({}, 'username topics.levelId topics.topicId topics.subtopics') | |
.populate('topics.levelId', 'label') | |
.populate('topics.topicId', 'name') | |
.limit(5) | |
.lean(); | |
// .cursor(); | |
//eslint-disable-next-line | |
for (const user of users) { | |
userCount += 1; | |
console.log(`at ${userCount}`); | |
//eslint-disable-next-line | |
for (const t of user.topics) { | |
// const hasFinishedTopicLevel = yield hasUserFinsishedTopicLevel(user, t, t.levelId, insights, topics); | |
if (yield hasUserFinsishedTopicLevel(user, t, t.levelId, insights, topics)) { | |
if (yield hasUserFinishedTopic(user, t, insights, topics)) { | |
uFinishedTopic.push(user.username); | |
} else { | |
uFinishedTopicLevel.push(user.username); | |
} | |
} | |
} | |
} | |
console.log('done finding users'); | |
fs.writeFile('finishedTopic.txt', | |
uFinishedTopic.reduce((acc, val) => acc + '\n' + val), e => console.log(e)); | |
fs.writeFile('finishedTopicLevel.txt', | |
uFinishedTopicLevel.reduce((acc, val) => acc + '\n' + val), e => console.log(e)); | |
process.exit(0); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment