def _init_alternatives(self, user_id, trip_id, num_alternatives): alternatives = [] db = edb.get_alternatives_db() json_object = db.find({'user_id' : user_id, 'trip_id' : trip_id}, limit = num_alternatives) for alternative_json in json_object: alternatives.append(Alternative_Trip(alternative_json)) return alternatives
def save_to_db(self): db = edb.get_alternatives_db() #Unique key is combination of trip, user, and mode. Only one alternative per mode result = db.update({"trip_id": self.trip_id, "user_id": self.user_id, "mode_list":self.mode_list}, {"$set": {"cost" : self.cost}}, upsert=False,multi=False) #print result if not result["updatedExisting"]: self._create_new(db)
def getAlternativeTrips(trip_id): #TODO: clean up datetime, and queries here #d = datetime.datetime.now() - datetime.timedelta(days=6) #query = {'trip_id':trip_id, 'trip_start_datetime':{"$gt":d}} query = {'trip_id':trip_id} alternatives = get_alternatives_db().find(query) if alternatives.count() > 0: logging.debug("Number of alternatives for trip %s is %d" % (trip_id, alternatives.count())) return alternatives raise AlternativesNotFound("No Alternatives Found")
def setUp(self): self.testUUID = "myuuidisverylongandcomplicated" #self.testUserEmails = ["*****@*****.**", "*****@*****.**", "*****@*****.**", # "*****@*****.**", "*****@*****.**"] self.serverName = 'localhost' self.testUsers = [] #for userEmail in self.testUserEmails: # User.register(userEmail) # self.testUsers += [User.fromEmail(section['user_id'])] # can access uuid with .uuid # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries self.ModesColl = edb.get_mode_db() self.ModesColl.remove() edb.get_trip_db().remove() edb.get_section_db().remove() edb.get_alternatives_db().remove() self.assertEquals(self.ModesColl.find().count(), 0) dataJSON = json.load(open("emission/tests/data/modes.json")) for row in dataJSON: self.ModesColl.insert(row) # register each of the users and add sample trips to each user result = self.loadTestJSON("emission/tests/data/missing_trip") collect.processResult(self.testUUID, result) for trip in edb.get_trip_db().find(): trip['trip_start_datetime'] = pydt.datetime.now() + pydt.timedelta( hours=-5) trip['trip_end_datetime'] = pydt.datetime.now() edb.get_trip_db().update({"_id": trip["_id"]}, trip) for section in edb.get_section_db().find(): section['section_start_datetime'] = pydt.datetime.now( ) + pydt.timedelta(hours=-5) section['section_end_datetime'] = pydt.datetime.now() edb.get_section_db().update({"_id": section["_id"]}, section) self.pipeline = AlternativeTripsPipeline()
def _init_alternatives(self, user_id, trip_id, num_alternatives): alternatives = [] db = edb.get_alternatives_db() json_object = db.find({ 'user_id': user_id, 'trip_id': trip_id }, limit=num_alternatives) for alternative_json in json_object: alternatives.append(Alternative_Trip(alternative_json)) return alternatives
def setUp(self): self.testUUID = "myuuidisverylongandcomplicated" #self.testUserEmails = ["*****@*****.**", "*****@*****.**", "*****@*****.**", # "*****@*****.**", "*****@*****.**"] self.serverName = 'localhost' self.testUsers = [] #for userEmail in self.testUserEmails: # User.register(userEmail) # self.testUsers += [User.fromEmail(section['user_id'])] # can access uuid with .uuid # Sometimes, we may have entries left behind in the database if one of the tests failed # or threw an exception, so let us start by cleaning up all entries self.ModesColl = edb.get_mode_db() self.ModesColl.remove() edb.get_trip_db().remove() edb.get_section_db().remove() edb.get_alternatives_db().remove() self.assertEquals(self.ModesColl.find().count(), 0) dataJSON = json.load(open("emission/tests/data/modes.json")) for row in dataJSON: self.ModesColl.insert(row) # register each of the users and add sample trips to each user result = self.loadTestJSON("emission/tests/data/missing_trip") collect.processResult(self.testUUID, result) for trip in edb.get_trip_db().find(): trip['trip_start_datetime'] = pydt.datetime.now() + pydt.timedelta(hours=-5) trip['trip_end_datetime'] = pydt.datetime.now() edb.get_trip_db().update({"_id": trip["_id"]}, trip) for section in edb.get_section_db().find(): section['section_start_datetime'] = pydt.datetime.now() + pydt.timedelta(hours=-5) section['section_end_datetime'] = pydt.datetime.now() edb.get_section_db().update({"_id": section["_id"]}, section) self.pipeline = AlternativeTripsPipeline()
def save_to_db(self): db = edb.get_alternatives_db() #Unique key is combination of trip, user, and mode. Only one alternative per mode result = db.update( { "trip_id": self.trip_id, "user_id": self.user_id, "mode_list": self.mode_list }, {"$set": { "cost": self.cost }}, upsert=False, multi=False) #print result if not result["updatedExisting"]: self._create_new(db)
if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) for user_dict in edb.get_uuid_db().find(): user = ad.AttrDict(user_dict) if user.uuid in estag.TEST_PHONE_IDS: logging.debug("Found test phone, skipping reset") else: new_uuid = uuid.uuid4() logging.debug("Mapping %s -> %s" % (new_uuid, user.uuid)) edb.get_uuid_db().update({"uuid": user.uuid}, {"$set": { "uuid": new_uuid }}) logging.debug("Resetting alternatives...") reset_collection(edb.get_alternatives_db(), user.uuid, new_uuid) logging.debug("Resetting analysis...") reset_collection(edb.get_analysis_timeseries_db(), user.uuid, new_uuid) logging.debug("Resetting client...") reset_collection(edb.get_client_db(), user.uuid, new_uuid) logging.debug("Resetting client_stats_backup...") reset_collection(edb.get_client_stats_db_backup(), user.uuid, new_uuid) logging.debug("Resetting server_stats_backup...") reset_collection(edb.get_server_stats_db_backup(), user.uuid, new_uuid) logging.debug("Resetting result_stats_backup...") reset_collection(edb.get_result_stats_db_backup(), user.uuid, new_uuid) logging.debug("Resetting edb.get_common_place_db...")
logging.debug(coll.update({"user_id": user.uuid}, {"$set": {"user_id": new_uuid}}, multi=True)) if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) for user_dict in edb.get_uuid_db().find(): user = ad.AttrDict(user_dict) if user.uuid in estag.TEST_PHONE_IDS: logging.debug("Found test phone, skipping reset") else: new_uuid = uuid.uuid4() logging.debug("Mapping %s -> %s" % (new_uuid, user.uuid)) edb.get_uuid_db().update({"uuid" : user.uuid}, {"$set": {"uuid" : new_uuid}}) logging.debug("Resetting alternatives...") reset_collection(edb.get_alternatives_db(), user.uuid, new_uuid) logging.debug("Resetting analysis...") reset_collection(edb.get_analysis_timeseries_db(), user.uuid, new_uuid) logging.debug("Resetting client...") reset_collection(edb.get_client_db(), user.uuid, new_uuid) logging.debug("Resetting client_stats_backup...") reset_collection(edb.get_client_stats_db_backup(), user.uuid, new_uuid) logging.debug("Resetting server_stats_backup...") reset_collection(edb.get_server_stats_db_backup(), user.uuid, new_uuid) logging.debug("Resetting result_stats_backup...") reset_collection(edb.get_result_stats_db_backup(), user.uuid, new_uuid) logging.debug("Resetting edb.get_common_place_db...") reset_collection(edb.get_common_place_db(), user.uuid, new_uuid) logging.debug("Resetting edb.get_common_trip_db...") reset_collection(edb.get_common_trip_db(), user.uuid, new_uuid) logging.debug("Resetting edb.get_habitica_db...")