def __preRegister(self, userEmail): from dao.user import User from main import userclient if User.isRegistered(userEmail): User.fromEmail(userEmail).setStudy(self.clientName) else: pendingDoc = { 'user_email': userEmail, 'study': self.clientName, 'last_update': datetime.now() } # Should I do insert or upsert here? If a user has pre-registered for one # study and then pre-registers for another study before registering, do we # want to throw an error or just update silently? # Update silently for now writeResult = get_pending_signup_db().update( {'user_email': userEmail}, pendingDoc, upsert=True) print 'in __preRegister, writeResult = %s' % writeResult if 'err' in writeResult and writeResult['err'] is not None: e = Exception() e.code = writeResult['err'][0]["code"] e.msg = writeResult['err'][0]["errmsg"] raise e return (get_pending_signup_db().find({ 'study': self.clientName }).count(), userclient.countForStudy(self.clientName))
def uid(): logger.info('rec a uid request') res = {'code': 1, 'msg': 'success', 'uid': 'unkown'} b = BaseDao() try: user_dao = User(base=b) macid = request.args.get("macid") or '' androidid = request.args.get("androidid") or '' if validSqlInject(macid) or validSqlInject(androidid): return jsonify({'code': 1, 'msg': 'invalid param'}) if macid or androidid: user = user_dao.get_user_info(macid, androidid) if not user: uid = get_user_id(macid, androidid) user_dao.add_user_id(uid, macid, androidid) else: uid = user.user_id res['uid'] = uid else: res['uid'] = get_user_id() logger.info('success|uid request serve success') except: logger.error('fail|exception|uid request serve error|%s' % traceback.format_exc()) finally: b.close() return jsonify(res)
def testClientSpecificPrecompute(self): for email in self.testUsers: currUser = User.fromEmail(email) self.assertEqual(currUser.getProfile().get("testfield1"), None) self.assertEqual(currUser.getProfile().get("testfield2"), None) self.assertEqual(data.getCarbonFootprint(currUser), None) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.pr.precomputeResults() self.assertEqual(user.getProfile()['testfield1'], 'value1') self.assertEqual(user.getProfile()['testfield2'], 'value2') for email in self.testUsers: if email != fakeEmail: currUser = User.fromEmail(email) carbonFootprint = data.getCarbonFootprint(currUser) self.assertEqual(len(carbonFootprint), 12)
def setUp(self): self.testUsers = ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"] self.serverName = 'localhost' # 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 tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() # self.ModesColl.remove() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() # self.SectionsColl.remove() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.walkExpect = 1057.2524056424411 self.busExpect = 2162.668467546699 self.busCarbon = 267.0/1609 self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks = 1) tests.common.updateSections(self)
def testQueryUnclassifiedSectionsWeekAgo(self): # Add some old sections that shouldn't be returned by the query # This one is just over a week old old_sec_1 = self.SectionsColl.find_one({'$and': [{'user_id': User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':1}]}) old_sec_1['_id'] = 'old_sec_1' old_sec_1['section_start_datetime'] = self.weekago - timedelta(minutes = 30) old_sec_1['section_end_datetime'] = self.weekago - timedelta(minutes = 5) logging.debug("Inserting old_sec_1 %s" % old_sec_1) self.SectionsColl.insert(old_sec_1) # This one is a month old monthago = self.now - timedelta(days = 30) old_sec_2 = self.SectionsColl.find_one({'$and': [{'user_id':User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':4}]}) old_sec_2['_id'] = 'old_sec_2' old_sec_2['section_start_datetime'] = monthago - timedelta(minutes = 30) old_sec_2['section_end_datetime'] = monthago - timedelta(minutes = 5) logging.debug("Inserting old_sec_2 %s" % old_sec_2) self.SectionsColl.insert(old_sec_2) # This one is missing the predicted mode monthago = self.now - timedelta(days = 30) un_pred_sec = self.SectionsColl.find_one({'$and': [{'user_id':User.fromEmail('*****@*****.**').uuid}, {'type':'move'}, {'mode':4}]}) un_pred_sec['_id'] = 'un_pred_sec' del un_pred_sec['predicted_mode'] logging.debug("Inserting un_pred_sec %s" % un_pred_sec) self.SectionsColl.insert(un_pred_sec) queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail('*****@*****.**').uuid) self.assertEqual(queriedUnclassifiedSections.count(), 2)
def testChangeUpdateTs(self): from datetime import datetime, timedelta user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) user.changeUpdateTs(timedelta(days = -20)) self.assertEqual((datetime.now() - user.getUpdateTS()).days, 20)
def testQueryUnclassifiedSectionsLowConfidence(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 2) # Set the auto_confirmed values for the trips for section in queriedUnclassifiedSections: print section['_id'] self.SectionsColl.update({'_id': section['_id']}, {'test_auto_confirmed': {'mode': section['mode'], 'prob': 0.95}}) # Now, set the update timestamp to two weeks ago so that we will start filtering tests.common.updateUserCreateTime(user.uuid) queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 0)
def setUp(self): self.testUsers = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**" ] self.serverName = 'localhost' # 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 tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() # self.ModesColl.remove() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() # self.SectionsColl.remove() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.walkExpect = 1057.2524056424411 self.busExpect = 2162.668467546699 self.busCarbon = 267.0 / 1609 self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks=1) tests.common.updateSections(self)
def testRunBackgroundTasksForDay(self): self.testUsers = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**" ] load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.SectionsColl = get_section_db() tests.common.updateSections(self) self.assertNotEqual(len(self.uuid_list), 0) # Can access the zeroth element because we know that then length is greater than zero # (see above) test_uuid = self.uuid_list[0] test_user = User.fromUUID(test_uuid) self.assertNotIn('carbon_footprint', test_user.getProfile().keys()) default.runBackgroundTasks(test_user.uuid) self.assertIn('carbon_footprint', test_user.getProfile().keys())
def testChangeUpdateTs(self): from datetime import datetime, timedelta user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) user.changeUpdateTs(timedelta(days=-20)) self.assertEqual((datetime.now() - user.getUpdateTS()).days, 20)
def testClientSpecificPrecompute(self): for email in self.testUsers: currUser = User.fromEmail(email) self.assertEqual(currUser.getProfile().get("testfield1"), None) self.assertEqual(currUser.getProfile().get("testfield2"), None) self.assertEqual(default.getCarbonFootprint(currUser), None) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey=False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.pr.precomputeResults() self.assertEqual(user.getProfile()['testfield1'], 'value1') self.assertEqual(user.getProfile()['testfield2'], 'value2') for email in self.testUsers: if email != fakeEmail: currUser = User.fromEmail(email) carbonFootprint = default.getCarbonFootprint(currUser) self.assertEqual(len(carbonFootprint), 12)
def post(self: tornado.web.RequestHandler): client = Client(name=str(self.get_argument('FirstName')), surname=str(self.get_argument('LastName'))) User.create_user( User(login=str(self.get_argument('Login')), password=str(self.get_argument('Password')), client=Client.create_client(client))) self.redirect('/login')
def setUp(self): self.testUsers = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**" ] self.serverName = 'localhost' # 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 tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testModeInferFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks=1) for section in self.SectionsColl.find(): section['section_start_datetime'] = self.dayago section['section_end_datetime'] = self.dayago + timedelta(hours=1) if (section['confirmed_mode'] == 5): # We only cluster bus and train trips # And our test data only has bus trips section['section_start_point'] = { u'type': u'Point', u'coordinates': [-122.270039042, 37.8800285728] } section['section_end_point'] = { u'type': u'Point', u'coordinates': [-122.2690412952, 37.8739578595] } # print("Section start = %s, section end = %s" % # (section['section_start_datetime'], section['section_end_datetime'])) # Replace the user email with the UUID section['user_id'] = User.fromEmail(section['user_id']).uuid self.SectionsColl.save(section) self.pipeline = pipeline.ModeInferencePipeline() self.testLoadTrainingData()
def testGetFirstStudy(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail('*****@*****.**') self.assertEqual(user.getFirstStudy(), 'testclient')
def testSetClientSpecificFields(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) # Check that the field doesn't exist initially self.assertTrue(user.getProfile().get('test_field', 'blank'), 'blank') # Check that a simple value update works user.setClientSpecificProfileFields({'test_field': 'something beautiful'}) self.assertTrue(user.getProfile().get('test_field', 'blank'), 'something beautiful') # Check that a data structure update works user.setClientSpecificProfileFields({'test_field': {'something': 'beautiful'}}) self.assertTrue(user.getProfile().get('test_field', 'blank'), {'something': 'beautiful'})
def create_user(self, json): valid_params = self.verify_params(json, User.USER_REQUIRED_PARAMETERS) if valid_params: try: new_user = User(**valid_params) created_user = new_user.create() result = { "message": "Success!", "user": created_user.to_dict(), } return jsonify(result), 201 except: return jsonify(message="Server error!"), 500 else: return jsonify(message="Bad Request!"), 400
def getResult(user_uuid): # This is in here, as opposed to the top level as recommended by the PEP # because then we don't have to worry about loading bottle in the unit tests from bottle import template import base64 from dao.user import User from dao.client import Client user = User.fromUUID(user_uuid) renderedTemplate = template( "clients/choice/result_template.html", variables=json.dumps({ 'curr_view': getCurrView(user_uuid), 'uuid': str(user_uuid), 'client_key': Client("choice").getClientKey() }), gameResult=base64.b64encode(gamified.getResult(user_uuid)), leaderboardResult=base64.b64encode(leaderboard.getResult(user_uuid)), dataResult=base64.b64encode(data.getResult(user_uuid)), commonTripsResult=base64.b64encode(commontrips.getResult(user_uuid)), recommendationResult=base64.b64encode( recommendation.getResult(user_uuid))) return renderedTemplate
def setUp(self): import tests.common # 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 tests.common.dropAllCollections(get_db()) user = User.register("*****@*****.**") self.uuid = user.uuid
def savePredictionsStep(self): from dao.user import User from dao.client import Client uniqueModes = sorted(set(self.cleanedResultVector)) for i in range(self.predictedProb.shape[0]): currSectionId = self.sectionIds[i] currProb = self.convertPredictedProbToMap(self.modeList, uniqueModes, self.predictedProb[i]) logging.debug("Updating probability for section with id = %s" % currSectionId) self.Sections.update({'_id': currSectionId}, {"$set": { "predicted_mode": currProb }}) currUser = User.fromUUID(self.sectionUserIds[i]) clientSpecificUpdate = Client( currUser.getFirstStudy()).clientSpecificSetters( currUser.uuid, currSectionId, currProb) if clientSpecificUpdate != None: self.Sections.update({'_id': currSectionId}, clientSpecificUpdate)
def update_user(uid, json): valid_params = UserHandler.verify_params(json, User.USER_REQUIRED_PARAMETERS) if uid and valid_params: try: user_to_update = User.get_user_by_id(uid) if user_to_update: for key, value in valid_params.items(): if key == "password": if value != user_to_update.password and not \ bcrypt.checkpw(value.encode('utf-8'), user_to_update.password.encode('utf-8')): user_to_update.update_password(value) else: setattr(user_to_update, key, value) user_to_update.update() result = { "message": "Success!", "user": user_to_update.to_dict(), } return jsonify(result), 200 else: return jsonify(message="Not Found!"), 404 except Exception as err: return jsonify(message="Server Error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def getScoreComponents(user_uuid, start, end): # The score is based on the following components: # - Percentage of trips classified. We are not auto-classifying high # confidence trips, so don't need to handle those here user = User.fromUUID(user_uuid) pctClassified = common.getClassifiedRatio(user_uuid, start, end) (myModeShareCount, avgModeShareCount, myModeShareDistance, avgModeShareDistance, myModeCarbonFootprint, avgModeCarbonFootprint, myModeCarbonFootprintNoLongMotorized, avgModeCarbonFootprintNoLongMotorized, myOptimalCarbonFootprint, avgOptimalCarbonFootprint, myOptimalCarbonFootprintNoLongMotorized, avgOptimalCarbonFootprintNoLongMotorized) = carbon.getFootprintCompareForRange(user.uuid, start, end) carbon.delLongMotorizedModes(myModeShareDistance) myAllDrive = carbon.getAllDrive(user.uuid, myModeShareDistance) myCarbonFootprintSum = sum(myModeCarbonFootprintNoLongMotorized.values()) myOptimalFootprintSum = sum(myOptimalCarbonFootprintNoLongMotorized.values()) logging.debug("myCarbonFootprintSum = %s, myOptimalFootprintSum = %s, myAllDrive = %s" % (myCarbonFootprintSum, myOptimalFootprintSum, myAllDrive)) handleZero = lambda x, y: 0 if y == 0 else float(x)/y components = [pctClassified, handleZero(myCarbonFootprintSum - myOptimalFootprintSum, myOptimalFootprintSum), handleZero(myAllDrive - myCarbonFootprintSum, myAllDrive), handleZero(sb375DailyGoal - myCarbonFootprintSum, sb375DailyGoal)] return components
def runBackgroundTasksForDay(user_uuid, today): today_dt = datetime.combine(today, time.max) user = User.fromUUID(user_uuid) # carbon compare results is a tuple. Tuples are converted to arrays # by mongodb # In [44]: testUser.setScores(('a','b', 'c', 'd'), ('s', 't', 'u', 'v')) # In [45]: testUser.getScore() # Out[45]: ([u'a', u'b', u'c', u'd'], [u's', u't', u'u', u'v']) weekago = today_dt - timedelta(days=7) carbonCompareResults = carbon.getFootprintCompareForRange(user_uuid, weekago, today_dt) setCarbonFootprint(user, carbonCompareResults) (myModeShareCount, avgModeShareCount, myModeShareDistance, avgModeShareDistance, myModeCarbonFootprint, avgModeCarbonFootprint, myModeCarbonFootprintNoLongMotorized, avgModeCarbonFootprintNoLongMotorized, # ignored myOptimalCarbonFootprint, avgOptimalCarbonFootprint, myOptimalCarbonFootprintNoLongMotorized, avgOptimalCarbonFootprintNoLongMotorized) = carbonCompareResults # We only compute server stats in the background, because including them in # the set call means that they may be invoked when the user makes a call and # the cached value is None, which would potentially slow down user response time msNow = systime.time() stats.storeResultEntry(user_uuid, stats.STAT_MY_CARBON_FOOTPRINT, msNow, getCategorySum(myModeCarbonFootprint)) stats.storeResultEntry(user_uuid, stats.STAT_MY_CARBON_FOOTPRINT_NO_AIR, msNow, getCategorySum(myModeCarbonFootprintNoLongMotorized)) stats.storeResultEntry(user_uuid, stats.STAT_MY_OPTIMAL_FOOTPRINT, msNow, getCategorySum(myOptimalCarbonFootprint)) stats.storeResultEntry(user_uuid, stats.STAT_MY_OPTIMAL_FOOTPRINT_NO_AIR, msNow, getCategorySum(myOptimalCarbonFootprintNoLongMotorized)) stats.storeResultEntry(user_uuid, stats.STAT_MY_ALLDRIVE_FOOTPRINT, msNow, getCategorySum(myModeShareDistance) * (278.0/(1609 * 1000))) stats.storeResultEntry(user_uuid, stats.STAT_MEAN_FOOTPRINT, msNow, getCategorySum(avgModeCarbonFootprint)) stats.storeResultEntry(user_uuid, stats.STAT_MEAN_FOOTPRINT_NO_AIR, msNow, getCategorySum(avgModeCarbonFootprintNoLongMotorized))
def getResult(user_uuid): # This is in here, as opposed to the top level as recommended by the PEP # because then we don't have to worry about loading bottle in the unit tests from bottle import template user = User.fromUUID(user_uuid) currFootprint = getCarbonFootprint(user) if currFootprint is None: currFootprint = carbon.getFootprintCompare(user_uuid) setCarbonFootprint(user, currFootprint) (myModeShareCount, avgModeShareCount, myModeShareDistance, avgModeShareDistance, myModeCarbonFootprint, avgModeCarbonFootprint, myModeCarbonFootprintNoLongMotorized, avgModeCarbonFootprintNoLongMotorized, # ignored myOptimalCarbonFootprint, avgOptimalCarbonFootprint, myOptimalCarbonFootprintNoLongMotorized, avgOptimalCarbonFootprintNoLongMotorized) = currFootprint renderedTemplate = template("clients/data/result_template.html", myModeShareCount = json.dumps(myModeShareCount), avgModeShareCount = json.dumps(avgModeShareCount), myModeShareDistance = json.dumps(myModeShareDistance), avgModeShareDistance = json.dumps(avgModeShareDistance), myModeCarbonFootprint = json.dumps(myModeCarbonFootprint), avgModeCarbonFootprint = json.dumps(avgModeCarbonFootprint), myOptimalCarbonFootprint = json.dumps(myOptimalCarbonFootprint), avgOptimalCarbonFootprint = json.dumps(avgOptimalCarbonFootprint)) # logging.debug(renderedTemplate) return renderedTemplate
def testUnsetStudyExists(self): user = User.register('*****@*****.**') user.setStudy('testclient') self.assertEquals(userclient.countForStudy('testclient'), 1) user.unsetStudy('testclient') self.assertEquals(userclient.countForStudy('testclient'), 0)
def setupClientTest(self): # At this point, the more important test is to execute the query and see # how well it works from dao.user import User from dao.client import Client import tests.common from get_database import get_section_db fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') dummyPredModeMap = {'walking': 1.0} dummySection = tests.common.createDummySection( startTime = datetime.now() - timedelta(seconds = 60 * 60), endTime = datetime.now(), startLoc = [-122, 34], endLoc = [-122, 35], predictedMode = dummyPredModeMap) return (user, dummySection, dummyPredModeMap)
def testGetFirstStudy(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) client = Client("testclient") client.update(createKey=False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail('*****@*****.**') self.assertEqual(user.getFirstStudy(), 'testclient')
def getScoreComponents(user_uuid, start, end): # The score is based on the following components: # - Percentage of trips classified. We are not auto-classifying high # confidence trips, so don't need to handle those here user = User.fromUUID(user_uuid) pctClassified = common.getClassifiedRatio(user_uuid, start, end) (myModeShareCount, avgModeShareCount, myModeShareDistance, avgModeShareDistance, myModeCarbonFootprint, avgModeCarbonFootprint, myModeCarbonFootprintNoLongMotorized, avgModeCarbonFootprintNoLongMotorized, myOptimalCarbonFootprint, avgOptimalCarbonFootprint, myOptimalCarbonFootprintNoLongMotorized, avgOptimalCarbonFootprintNoLongMotorized ) = carbon.getFootprintCompareForRange(user.uuid, start, end) carbon.delLongMotorizedModes(myModeShareDistance) myAllDrive = carbon.getAllDrive(user.uuid, myModeShareDistance) myCarbonFootprintSum = sum(myModeCarbonFootprintNoLongMotorized.values()) myOptimalFootprintSum = sum( myOptimalCarbonFootprintNoLongMotorized.values()) logging.debug( "myCarbonFootprintSum = %s, myOptimalFootprintSum = %s, myAllDrive = %s" % (myCarbonFootprintSum, myOptimalFootprintSum, myAllDrive)) handleZero = lambda x, y: 0 if y == 0 else float(x) / y components = [ pctClassified, handleZero(myCarbonFootprintSum - myOptimalFootprintSum, myOptimalFootprintSum), handleZero(myAllDrive - myCarbonFootprintSum, myAllDrive), handleZero(sb375DailyGoal - myCarbonFootprintSum, sb375DailyGoal) ] return components
def testSavePredictionsStepWithClient(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.testPredictedProb() self.pipeline.savePredictionsStep() # Confirm that the predictions are saved correctly test_id_1_sec = self.SectionsColl.find_one({'_id': 'test_id_1'}) self.assertIsNotNone(test_id_1_sec['predicted_mode']) self.assertEquals(test_id_1_sec['predicted_mode'], {'walking': 1}) self.assertEquals(test_id_1_sec['test_auto_confirmed'], {'mode': 1, 'prob': 1.0}) test_id_2_sec = self.SectionsColl.find_one({'_id': 'test_id_2'}) self.assertIsNotNone(test_id_2_sec['predicted_mode']) self.assertEquals(test_id_2_sec['predicted_mode'], {'bus': 1}) self.assertEquals(test_id_2_sec['test_auto_confirmed'], {'mode': 5, 'prob': 1.0}) # Let's make sure that we didn't accidentally mess up other fields self.assertIsNotNone(test_id_1_sec['distance']) self.assertIsNotNone(test_id_2_sec['trip_id'])
def getUUIDFromToken(token): userEmail = verifyUserToken(token) user=User.fromEmail(userEmail) if user is None: return None user_uuid=user.uuid return user_uuid
def generate_fund_userlist(data): ''' 将基金表数据整合为:{ 基金ID1 : [{用户对象}], 基金ID2 : [{用户对象}] } ''' fund_user = {} for r in data: fid = r.get('id') r['id'] = r.get('user_id') if fund_user.get(fid) != None: fund_user[fid].append(User(r)) else: fund_user[fid] = [User(r)] return fund_user
def testClientSpecificSettersWithOverride(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') dummyPredModeMap = {'walking': 1.0} dummySection = common.createDummySection(startTime = datetime.now() - timedelta(seconds = 60 * 60), endTime = datetime.now(), startLoc = [-122, 34], endLoc = [-122, 35], predictedMode = dummyPredModeMap) clientSetQuery = client.clientSpecificSetters(user.uuid, dummySection['_id'], dummyPredModeMap) self.assertEqual(clientSetQuery, {'$set': {'test_auto_confirmed': {'mode': 1, 'prob': 1.0}}}) # Apply the change get_section_db().update({'_id': dummySection['_id']}, clientSetQuery) retrievedSection = get_section_db().find_one({'_id': dummySection['_id']}) self.assertEqual(retrievedSection['test_auto_confirmed']['mode'], 1)
def getCurrView(uuid): user = User.fromUUID(uuid) profile = user.getProfile() if profile is None: logging.debug("profile is None, returning data") return "data" logging.debug("profile.get('curr_view', 'dummy') is %s" % profile.get("curr_view", "data")) return profile.get("curr_view", "data")
def testMergeDict(self): dict1 = {'a': 'a1', 'b': 'b1', 'c': 'c1'} dict2 = {'d': 'd2', 'b': 'b2', 'c': 'c2'} mergedDict = User.mergeDicts(dict1, dict2) self.assertEqual(len(mergedDict), 4) self.assertEqual(mergedDict['a'], 'a1') self.assertEqual(mergedDict, {'a': 'a1', 'b': 'b2', 'c': 'c2', 'd': 'd2'})
def testCarbonFootprintStore(self): user = User.fromUUID(self.uuid) # Tuple of JSON objects, similar to the real footprint dummyCarbonFootprint = ({'myModeShareCount': 10}, {'avgModeShareCount': 20}) self.assertEquals(data.getCarbonFootprint(user), None) data.setCarbonFootprint(user, dummyCarbonFootprint) # recall that pymongo converts tuples to lists somewhere down the line self.assertEquals(data.getCarbonFootprint(user), list(dummyCarbonFootprint))
def getUserClient(user_uuid): study = User.fromUUID(user_uuid).getFirstStudy() if study != None: client = Client(study) return client else: # User is not part of any study, so no additional filtering is needed return None
def testRegisterExistingUser(self): user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), []) client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail("*****@*****.**") self.assertEquals(user.getStudy(), ['testclient']) # Here's the key difference, now register again user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), ['testclient'])
def calc_car_cost(trip_id, distance): uuid = sectiondb.find_one({'trip_id': trip_id})['user_id'] our_user = User.fromUUID(uuid) ave_mpg = our_user.getAvgMpg() gallons = meters_to_miles(distance) / ave_mpg price = urllib2.urlopen('http://www.fueleconomy.gov/ws/rest/fuelprices') xml = price.read() p = ET.fromstring(xml)[-1] return float(p.text) * gallons
def setUp(self): # 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 tests.common.dropAllCollections(get_db()) user = User.register("*****@*****.**") self.uuid = user.uuid self.serverName = "localhost" self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks = 1)
def getResult(user_uuid): # This is in here, as opposed to the top level as recommended by the PEP # because then we don't have to worry about loading bottle in the unit tests from bottle import template (prevScore, currScore) = getStoredScore(User.fromUUID(user_uuid)) (level, sublevel) = getLevel(currScore) otherCurrScoreList = [] for user_uuid_dict in get_uuid_db().find({}, {'uuid': 1, '_id': 0}): (currPrevScore, currCurrScore) = getStoredScore(User.fromUUID(user_uuid_dict['uuid'])) otherCurrScoreList.append(currCurrScore) otherCurrScoreList.sort() renderedTemplate = template("clients/leaderboard/result_template.html", level_picture_filename = getFileName(level, sublevel), prevScore = prevScore, currScore = currScore, otherCurrScoreList = otherCurrScoreList) return renderedTemplate
def create_user(json): valid_params = UserHandler.verify_params(json, User.USER_REQUIRED_PARAMETERS) if valid_params: try: print(valid_params) username_exists = User.verify_username(valid_params.get('username')) if username_exists: return jsonify(message="Username already taken."), 400 new_user = User(**valid_params) created_user = new_user.create() result = { "message": "Success!", "user": created_user.to_dict(), } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def testGetSettingsCustomUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertRegexpMatches(user.getSettings()['result_url'], ".*/test/test/test")
def loadMovesInputFile(userEmail, fileName): from dao.user import User user = User.fromEmail(userEmail) savedTokens = auth.getAccessToken(user.uuid) print savedTokens if len(savedTokens) == 0: auth.saveAccessToken(sampleAuthMessage, user.uuid) result = json.load(open(fileName)) print json.dumps(result) collect.processResult(user.uuid, result)
def testRegisterStudyUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), ['testclient'])
def testRunBackgroundTasksForDay(self): self.testUsers = ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"] load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testCarbonFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.SectionsColl = get_section_db() tests.common.updateSections(self) self.assertNotEqual(len(self.uuid_list), 0) # Can access the zeroth element because we know that then length is greater than zero # (see above) test_uuid = self.uuid_list[0] test_user = User.fromUUID(test_uuid) self.assertNotIn('carbon_footprint', test_user.getProfile().keys()) data.runBackgroundTasks(test_user.uuid) self.assertIn('carbon_footprint', test_user.getProfile().keys())
def setUp(self): self.testUsers = ["*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"] self.serverName = 'localhost' # 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 tests.common.dropAllCollections(get_db()) self.ModesColl = get_mode_db() self.assertEquals(self.ModesColl.find().count(), 0) self.SectionsColl = get_section_db() self.assertEquals(self.SectionsColl.find().count(), 0) load_database_json.loadTable(self.serverName, "Stage_Modes", "tests/data/modes.json") load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testModeInferFile") # Let's make sure that the users are registered so that they have profiles for userEmail in self.testUsers: User.register(userEmail) self.now = datetime.now() self.dayago = self.now - timedelta(days=1) self.weekago = self.now - timedelta(weeks = 1) for section in self.SectionsColl.find(): section['section_start_datetime'] = self.dayago section['section_end_datetime'] = self.dayago + timedelta(hours = 1) if (section['confirmed_mode'] == 5): # We only cluster bus and train trips # And our test data only has bus trips section['section_start_point'] = {u'type': u'Point', u'coordinates': [-122.270039042, 37.8800285728]} section['section_end_point'] = {u'type': u'Point', u'coordinates': [-122.2690412952, 37.8739578595]} # print("Section start = %s, section end = %s" % # (section['section_start_datetime'], section['section_end_datetime'])) # Replace the user email with the UUID section['user_id'] = User.fromEmail(section['user_id']).uuid self.SectionsColl.save(section) self.pr = precompute_results.PrecomputeResults()
def getResult(user_uuid): # This is in here, as opposed to the top level as recommended by the PEP # because then we don't have to worry about loading bottle in the unit tests from bottle import template (prevScore, currScore) = getStoredScore(User.fromUUID(user_uuid)) (level, sublevel) = getLevel(currScore) renderedTemplate = template("clients/gamified/result_template.html", level_picture_filename = getFileName(level, sublevel), prevScore = prevScore, currScore = currScore) return renderedTemplate
def loadMovesInputFile(userEmail, fileName): # load_database_json.loadTable("localhost", "Test_Groups", "tests/data/groups.json") # load_database_json.loadTable("localhost", "Test_Modes", "tests/data/modes.json") from dao.user import User user = User.fromEmail(userEmail) savedTokens = auth.getAccessToken(user.uuid) print savedTokens if len(savedTokens) == 0: auth.saveAccessToken(sampleAuthMessage, user.uuid) result = json.load(open(fileName)) print json.dumps(result) collect.processResult(user.uuid, result)
def createUserProfile(): logging.debug("Called createUserProfile") userToken = request.json['user'] # This is the only place we should use the email, since we may not have a # UUID yet. All others should only use the UUID. if skipAuth: userEmail = userToken else: userEmail = verifyUserToken(userToken) logging.debug("userEmail = %s" % userEmail) user = User.register(userEmail) logging.debug("Looked up user = %s" % user) logging.debug("Returning result %s" % {'uuid': str(user.uuid)}) return {'uuid': str(user.uuid)}
def testGetUnclassifiedSectionsFiltered(self): """ Tests that queryUnclassifiedSections never returns a section with section['retained'] == False. A section is only returned if section['retained'] == True and all other query conditions are met """ # Clear previous Stage_Sections data and load new data # specific to filtering self.SectionsColl.remove() load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testFilterFile") tests.common.updateSections(self) # Extra updates to Sections necessary for testing filtering for section in self.SectionsColl.find(): section['section_start_point'] = "filler start point" section['section_end_point'] = "filler end point" self.SectionsColl.save(section) from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') unclassifiedSections = tripManager.getUnclassifiedSections(User.fromEmail(fakeEmail).uuid)['sections'] # Check that of the valid sections in the testFilterFile (2/3), only one of them is returned by the query self.assertEqual(len(unclassifiedSections), 1) # Check that the second entry in the testFilterFile is the only section # that is loaded into the database self.assertEqual('20140401T095742-0700',unclassifiedSections[0]['trip_id'])
def __preRegister(self, userEmail): from dao.user import User from main import userclient if User.isRegistered(userEmail): User.fromEmail(userEmail).setStudy(self.clientName) else: pendingDoc = { 'user_email': userEmail, 'study': self.clientName, 'last_update': datetime.now()} # Should I do insert or upsert here? If a user has pre-registered for one # study and then pre-registers for another study before registering, do we # want to throw an error or just update silently? # Update silently for now writeResult = get_pending_signup_db().update({'user_email': userEmail}, pendingDoc, upsert=True) print 'in __preRegister, writeResult = %s' % writeResult if 'err' in writeResult and writeResult['err'] is not None: e = Exception() e.code = writeResult['err'][0]["code"] e.msg = writeResult['err'][0]["errmsg"] raise e return (get_pending_signup_db().find({'study': self.clientName}).count(), userclient.countForStudy(self.clientName))
def getSectionFilter(uuid): from dao.user import User from datetime import datetime, timedelta logging.info("testclient.getSectionFilter called for user %s" % uuid) # If this is the first two weeks, show everything user = User.fromUUID(uuid) # Note that this is the last time that the profile was updated. So if the # user goes to the "Auth" screen and signs in again, it will be updated, and # we will reset the clock. If this is not acceptable, we need to ensure that # we have a create ts that is never updated updateTS = user.getUpdateTS() if (datetime.now() - updateTS) < timedelta(days = 14): # In the first two weeks, don't do any filtering return [] else: return [{'test_auto_confirmed.prob': {'$lt': 0.9}}]
def load_config(config_json): """ Loads the specified config file into the specified user cache. If there is an existing entry with the same key in the cache, it will be overwritten. If there is no existing entry with the same key in the cache, an entry will be inserted. """ for entry in config_json: userEmail = entry['userEmail'] userObj = User.fromEmail(userEmail) if userObj is None: print "user with email %s is not found, ignoring...." % userEmail else: uc = ucauc.UserCache.getUserCache(userObj.uuid) key = entry['key'] value = entry['value'] uc.putDocument(key, value) print "successfully loaded key %s for user %s" % (key, userEmail)