| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  | /* eslint-disable no-process-exit */ | 
					
						
							|  |  |  | require('dotenv').load(); | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  | var Rx = require('rx'), | 
					
						
							| 
									
										
										
										
											2015-06-15 20:53:43 -04:00
										 |  |  |   uuid = require('node-uuid'), | 
					
						
							|  |  |  |   assign = require('lodash/object/assign'), | 
					
						
							|  |  |  |   mongodb = require('mongodb'), | 
					
						
							|  |  |  |   secrets = require('../config/secrets'); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 03:45:00 -07:00
										 |  |  | const batchSize = 20; | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  | var MongoClient = mongodb.MongoClient; | 
					
						
							| 
									
										
										
										
											2015-06-26 01:47:59 -07:00
										 |  |  | Rx.config.longStackSupport = true; | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  | var providers = [ | 
					
						
							|  |  |  |   'facebook', | 
					
						
							|  |  |  |   'twitter', | 
					
						
							|  |  |  |   'google', | 
					
						
							|  |  |  |   'github', | 
					
						
							|  |  |  |   'linkedin' | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  | // create async console.logs
 | 
					
						
							|  |  |  | function debug() { | 
					
						
							|  |  |  |   var args = [].slice.call(arguments); | 
					
						
							|  |  |  |   process.nextTick(function() { | 
					
						
							|  |  |  |     console.log.apply(console, args); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  | function createConnection(URI) { | 
					
						
							|  |  |  |   return Rx.Observable.create(function(observer) { | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |     debug('connecting to db'); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |     MongoClient.connect(URI, function(err, database) { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         return observer.onError(err); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |       debug('db connected'); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |       observer.onNext(database); | 
					
						
							| 
									
										
										
										
											2015-06-15 20:53:43 -04:00
										 |  |  |       observer.onCompleted(); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  | function createQuery(db, collection, options, batchSize) { | 
					
						
							| 
									
										
										
										
											2015-08-05 10:47:08 -07:00
										 |  |  |   return Rx.Observable.create(function(observer) { | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  |     var cursor = db.collection(collection).find({}, options); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |     cursor.batchSize(batchSize || 20); | 
					
						
							|  |  |  |     // Cursor.each will yield all doc from a batch in the same tick,
 | 
					
						
							|  |  |  |     // or schedule getting next batch on nextTick
 | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |     debug('opening cursor for %s', collection); | 
					
						
							| 
									
										
										
										
											2015-08-05 10:47:08 -07:00
										 |  |  |     cursor.each(function(err, doc) { | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |       if (err) { | 
					
						
							|  |  |  |         return observer.onError(err); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (!doc) { | 
					
						
							| 
									
										
										
										
											2015-06-15 20:53:43 -04:00
										 |  |  |         console.log('onCompleted'); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |         return observer.onCompleted(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       observer.onNext(doc); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-05 10:47:08 -07:00
										 |  |  |     return Rx.Disposable.create(function() { | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |       debug('closing cursor for %s', collection); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |       cursor.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  | function getUserCount(db) { | 
					
						
							|  |  |  |   return Rx.Observable.create(function(observer) { | 
					
						
							|  |  |  |     var cursor = db.collection('users').count(function(err, count) { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         return observer.onError(err); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       observer.onNext(count); | 
					
						
							|  |  |  |       observer.onCompleted(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return Rx.Disposable.create(function() { | 
					
						
							|  |  |  |         debug('closing user count'); | 
					
						
							|  |  |  |         cursor.close(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  | function insertMany(db, collection, users, options) { | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |   return Rx.Observable.create(function(observer) { | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  |     db.collection(collection).insertMany(users, options, function(err) { | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |       if (err) { | 
					
						
							|  |  |  |         return observer.onError(err); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  |       observer.onNext(); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |       observer.onCompleted(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var count = 0; | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  | // will supply our db object
 | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  | var dbObservable = createConnection(secrets.db).replay(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var totalUser = dbObservable | 
					
						
							|  |  |  |   .flatMap(function(db) { | 
					
						
							|  |  |  |     return getUserCount(db); | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   .shareReplay(); | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | var users = dbObservable | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |   .flatMap(function(db) { | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  |     // returns user document, n users per loop where n is the batchsize.
 | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |     return createQuery(db, 'users', {}, batchSize); | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |   }) | 
					
						
							|  |  |  |   .map(function(user) { | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  |     // flatten user
 | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |     assign(user, user.portfolio, user.profile); | 
					
						
							| 
									
										
										
										
											2015-08-12 20:30:08 -07:00
										 |  |  |     if (!user.username) { | 
					
						
							|  |  |  |       user.username = 'fcc' + uuid.v4().slice(0, 8); | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-05 11:22:14 -07:00
										 |  |  |     if (user.github) { | 
					
						
							|  |  |  |       user.isGithubCool = true; | 
					
						
							| 
									
										
										
										
											2015-08-05 11:29:19 -07:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       user.isMigrationGrandfathered = true; | 
					
						
							| 
									
										
										
										
											2015-08-05 11:22:14 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-12 20:30:08 -07:00
										 |  |  |     providers.forEach(function(provider) { | 
					
						
							|  |  |  |       user[provider + 'id'] = user[provider]; | 
					
						
							|  |  |  |       user[provider] = null; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-08-15 12:19:36 -07:00
										 |  |  |     user.rand = Math.random(); | 
					
						
							| 
									
										
										
										
											2015-08-12 20:30:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  |     return user; | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   .shareReplay(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // batch them into arrays of twenty documents
 | 
					
						
							|  |  |  | var userSavesCount = users | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |   .bufferWithCount(batchSize) | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  |   // get bd object ready for insert
 | 
					
						
							|  |  |  |   .withLatestFrom(dbObservable, function(users, db) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       users: users, | 
					
						
							|  |  |  |       db: db | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  |   .flatMap(function(dats) { | 
					
						
							|  |  |  |     // bulk insert into new collection for loopback
 | 
					
						
							|  |  |  |     return insertMany(dats.db, 'user', dats.users, { w: 1 }); | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |   .flatMap(function() { | 
					
						
							|  |  |  |     return totalUser; | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   .doOnNext(function(totalUsers) { | 
					
						
							|  |  |  |     count = count + batchSize; | 
					
						
							|  |  |  |     debug('user progress %s', count / totalUsers * 100); | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2015-06-07 22:06:57 -07:00
										 |  |  |   // count how many times insert completes
 | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  |   .count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // create User Identities
 | 
					
						
							|  |  |  | var userIdentityCount = users | 
					
						
							|  |  |  |   .flatMap(function(user) { | 
					
						
							|  |  |  |     var ids = providers | 
					
						
							|  |  |  |       .map(function(provider) { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           provider: provider, | 
					
						
							| 
									
										
										
										
											2015-08-12 20:30:08 -07:00
										 |  |  |           externalId: user[provider + 'id'], | 
					
						
							| 
									
										
										
										
											2015-08-05 11:22:14 -07:00
										 |  |  |           userId: user._id || user.id | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  |         }; | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .filter(function(ident) { | 
					
						
							|  |  |  |         return !!ident.externalId; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Rx.Observable.from(ids); | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |   .bufferWithCount(batchSize) | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  |   .withLatestFrom(dbObservable, function(identities, db) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       identities: identities, | 
					
						
							|  |  |  |       db: db | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   .flatMap(function(dats) { | 
					
						
							|  |  |  |     // bulk insert into new collection for loopback
 | 
					
						
							|  |  |  |     return insertMany(dats.db, 'userIdentity', dats.identities, { w: 1 }); | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   // count how many times insert completes
 | 
					
						
							|  |  |  |   .count(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 03:45:00 -07:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  | var storyCount = dbObservable | 
					
						
							|  |  |  |   .flatMap(function(db) { | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |     return createQuery(db, 'stories', {}, batchSize); | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |   .bufferWithCount(batchSize) | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |   .withLatestFrom(dbObservable, function(stories, db) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       stories: stories, | 
					
						
							|  |  |  |       db: db | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   .flatMap(function(dats) { | 
					
						
							| 
									
										
										
										
											2015-06-15 20:53:43 -04:00
										 |  |  |     return insertMany(dats.db, 'story', dats.stories, { w: 1 }); | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |   }) | 
					
						
							|  |  |  |   .count(); | 
					
						
							| 
									
										
										
										
											2015-08-16 03:45:00 -07:00
										 |  |  |   */ | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | Rx.Observable.combineLatest( | 
					
						
							| 
									
										
										
										
											2015-06-12 15:27:51 -07:00
										 |  |  |   userIdentityCount, | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |   userSavesCount, | 
					
						
							| 
									
										
										
										
											2015-08-16 03:45:00 -07:00
										 |  |  |   // storyCount,
 | 
					
						
							|  |  |  |   function(userIdentCount, userCount) { | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  |       userIdentCount: userIdentCount * batchSize, | 
					
						
							| 
									
										
										
										
											2015-08-16 03:45:00 -07:00
										 |  |  |       userCount: userCount * batchSize | 
					
						
							|  |  |  |       // storyCount: storyCount * batchSize
 | 
					
						
							| 
									
										
										
										
											2015-06-15 16:47:50 -07:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-06-15 20:53:43 -04:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2015-06-07 17:15:53 -07:00
										 |  |  |   .subscribe( | 
					
						
							| 
									
										
										
										
											2015-06-25 21:47:25 -07:00
										 |  |  |     function(countObj) { | 
					
						
							|  |  |  |       console.log('next'); | 
					
						
							|  |  |  |       count = countObj; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     function(err) { | 
					
						
							|  |  |  |       console.error('an error occured', err, err.stack); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     function() { | 
					
						
							|  |  |  |       console.log('finished with ', count); | 
					
						
							|  |  |  |       process.exit(0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2015-08-14 17:31:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | dbObservable.connect(); |