Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function() {
- const Workout = require('../../app/models/workout');
- const fs = require('fs');
- 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.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 },
- };
- let counter = 0;
- Workout.count(query).then(totalWorkoutsCount => {
- const workoutStream = Workout.find(query, {
- title: 1,
- insights: 1,
- userRating: 1,
- topic: 1,
- subtopic: 1,
- level: 1
- }).populate('topic','name').populate('subtopic','name').populate('level', 'label')
- .limit(30).lean().stream();
- workoutStream.on('data', workout => {
- workoutStream.pause();
- 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);
- workoutStream.resume();
- });
- workoutStream.on('end', () => {
- 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);
- })
- });
- workoutStream.on('error', err =>{
- console.log(err);
- process.exit(1);
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement