Last active
April 25, 2017 13:17
-
-
Save loopiezlol/e7e13204d20f86dd4124daf8bdb91243 to your computer and use it in GitHub Desktop.
script to get avg rating on all hardcoded workouts
This file contains hidden or 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
| module.exports = function() { | |
| const Workout = require('../../app/models/workout'); | |
| const fs = require('fs'); | |
| const co = require('co'); | |
| require('../../app/models/level'); | |
| require('../../app/models/topic'); | |
| require('../../app/models/subtopic'); | |
| const getWorkoutTitle = function(workout) { | |
| return workout.title || (workout.topic || {}).name + ' ' + | |
| (workout.subtopic || {}).name + ' ' + (workout.level || {}).label; | |
| } | |
| const encodeWorkoutsIds = function(workout) { | |
| return workout.insights.sort().map(i => i.toString()).join('+'); | |
| } | |
| const workoutsList = {}; | |
| const query = { | |
| hardcoded: true, | |
| done: true, | |
| userRating: { $exists: true }, | |
| topic: { $exists: true }, | |
| subtopic: { $exists: true }, | |
| level: { $exists: true }, | |
| }; | |
| co(function*() { | |
| yield new Promise((resolve, reject) => { | |
| let counter = 0; | |
| Workout.count(query).then(totalWorkoutsCount => { | |
| const wCursor = Workout.find(query, { | |
| title: 1, | |
| insights: 1, | |
| userRating: 1, | |
| topic: 1, | |
| subtopic: 1, | |
| level: 1 | |
| }).populate('topic','name').populate('subtopic','name').populate('level', 'label') | |
| .lean().cursor(); | |
| wCursor.eachAsync(workout => { | |
| const encoded = encodeWorkoutsIds(workout); | |
| if(!workoutsList[encoded]) { | |
| workoutsList[encoded] = { | |
| title: getWorkoutTitle(workout), | |
| insights: workout.insights.map(i => i.toString()), | |
| avg: workout.userRating, | |
| count: 1, | |
| sum: workout.userRating, | |
| topic: (workout.topic || {}).name, | |
| subtopics: (workout.subtopic || {}).name, | |
| level: (workout.level || {}).label, | |
| } | |
| } else { | |
| workoutsList[encoded].count += 1; | |
| workoutsList[encoded].sum += workout.userRating; | |
| workoutsList[encoded].avg = workoutsList[encoded].sum / | |
| workoutsList[encoded].count; | |
| } | |
| counter += 1; | |
| console.log(counter + '/' + totalWorkoutsCount); | |
| }).then(() => { | |
| console.log('done'); | |
| // console.log(workoutsList); | |
| fs.writeFile('output.txt', JSON.stringify(workoutsList, null, 2), err => { | |
| if (err) console.log('something wrong happened', err); | |
| console.log('done writing to file'); | |
| process.exit(0); | |
| resolve(); | |
| }) | |
| }).catch(err => { | |
| console.log(err); | |
| reject(); | |
| process.exit(1); | |
| }) | |
| }); | |
| }) | |
| }) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment