Esempio n. 1
0
	def handle(self, *args, **options):
		
		self.stdout.write("Starting")
		client = MongoClient()
		self.db = client.data
		answer_queue = self.db.answer_queue
		rec_collection = self.db.recs
		products_to_process = self.db.toprocess
		product_collection = self.db.processedproducts
		top_friends = self.db.top_friends

		mod_ids = []
		answer_records = answer_queue.find()
		for answer in answer_records:
			self.stdout.write("Inside first for loop")

			rec = rec_collection.find_one({"userId": answer["forFacebookId"]})
			user_id = answer["forFacebookId"]
			wrong_product = answer["wrongProduct"]
			chosen_product = answer["chosenProduct"]
			if rec:
				winning_score = 1600
				losing_score = 1600

				try:
					winning_score = rec[answer["chosenProduct"]]
				except:
					wining_score = 1600

				try:
					losing_score = rec[answer['wrongProduct']]
				except:
					losing_score = 1600
				self.stdout.write("Before calculating elo rank")
				new_chosen_score, new_wrong_score = calculate_elo_rank(winning_score, losing_score)
				rec_collection.update({"_id": ObjectId(rec['_id'])}, {'$set': {str(answer['chosenProduct']): new_chosen_score, str(answer['wrongProduct']): new_wrong_score}})
				self.stdout.write( "Found rec for user")
			else:
				self.stdout.write("Before calculating elo rank")
				new_chosen_score, new_wrong_score = calculate_elo_rank()
				self.stdout.write("Scores:")
				self.stdout.write( str(new_chosen_score))
				self.stdout.write( str(new_wrong_score))
				rec_collection.insert({'userId': answer["forFacebookId"], str(answer["wrongProduct"]): new_wrong_score, str(answer["chosenProduct"]): new_chosen_score})
				self.stdout.write( "inserted new rec to database")

			products_to_process.insert({"product": chosen_product, "userId": user_id})
			products_to_process.insert({"product": wrong_product, "userId": user_id})	
			self.stdout.write("After products to process")	
			# update friends 
			scores_dict = self.updateDBTopFriends(answer["fromUser"], [[answer["forFacebookId"], 10]])
			mod_ids.append(answer["fromUser"])

			answer_queue.remove({"_id": ObjectId(answer["_id"])})
		self.sendTopFriends(mod_ids)
	def handle(self, *args, **options):
		
		client = MongoClient()
		self.db = client.data
		answer_queue = self.db.answer_queue
		rec_collection = self.db.recs
		products_to_process = self.db.toprocess
		product_collection = self.db.processedproducts
		top_friends = self.db.top_friends
		product_attributes_collection = self.db.product_attributes
		user_attribute_scores = self.db.user_attributes
				
		mod_ids = []
		answer_records = answer_queue.find()
		for answer in answer_records:
			user_id = answer["forFacebookId"]
			wrong_product = answer["wrongProduct"]
			chosen_product = answer["chosenProduct"]
			chosen_p_attributes = product_attributes_collection.findOne({chosen_product})
			wrong_p_attributes = product_attributs_collection.findOne({wrong_product})
			
			# if the chosen product was not in the database
			if (not chosen_p_attributes):
				answer_queue.remove({"_id": ObjectId(answer["_id"])})
				continue
				
			if rec:
				
				new_chosen_score, new_wrong_score = calculate_elo_rank(winning_score, losing_score)
				rec_collection.update({"_id": ObjectId(rec['_id'])}, {'$set': {str(answer['chosenProduct']): new_chosen_score, str(answer['wrongProduct']): new_wrong_score}})
				self.stdout.write( "Found rec for user")
			else:
				self.stdout.write("Before calculating elo rank")
				new_chosen_score, new_wrong_score = calculate_elo_rank()
				self.stdout.write("Scores:")
				self.stdout.write( str(new_chosen_score))
				self.stdout.write( str(new_wrong_score))
				rec_collection.insert({'userId': answer["forFacebookId"], str(answer["wrongProduct"]): new_wrong_score, str(answer["chosenProduct"]): new_chosen_score})
				self.stdout.write( "inserted new rec to database")

			products_to_process.insert({"product": chosen_product, "userId": user_id})
			products_to_process.insert({"product": wrong_product, "userId": user_id})	
			self.stdout.write("After products to process")	
			# update friends 
			scores_dict = self.updateDBTopFriends(answer["fromUser"], [[answer["forFacebookId"], 10]])
			mod_ids.append(answer["fromUser"])

			answer_queue.remove({"_id": ObjectId(answer["_id"])})
		self.sendTopFriends(mod_ids)