def setUp(self): #load test user self.testUUID = uuid.uuid4() autogen_string = randomGen() autogen_email = autogen_string + '@test.com' self.sampleAuthMessage1 = { 'username': autogen_string, 'email': autogen_email, 'password': "******", 'our_uuid': self.testUUID } sampleAuthMessage1Ad = ad.AttrDict(self.sampleAuthMessage1) proxy.habiticaRegister(sampleAuthMessage1Ad.username, sampleAuthMessage1Ad.email, sampleAuthMessage1Ad.password, sampleAuthMessage1Ad.our_uuid) self.ts = esta.TimeSeries.get_time_series(self.testUUID) bike_habit = { 'type': "habit", 'text': "Bike", 'up': True, 'down': False, 'priority': 2 } bike_habit_id = proxy.create_habit(self.testUUID, bike_habit) walk_habit = { 'type': "habit", 'text': "Walk", 'up': True, 'down': False, 'priority': 2 } walk_habit_id = proxy.create_habit(self.testUUID, walk_habit) logging.debug("in setUp, result = %s" % self.ts)
def setUp(self): #load test user self.testUUID = uuid.uuid4() autogen_string = randomGen() autogen_email = autogen_string + '@test.com' self.sampleAuthMessage1 = {'username': autogen_string, 'email': autogen_email, 'password': "******", 'our_uuid': self.testUUID} sampleAuthMessage1Ad = ad.AttrDict(self.sampleAuthMessage1) proxy.habiticaRegister(sampleAuthMessage1Ad.username, sampleAuthMessage1Ad.email, sampleAuthMessage1Ad.password, sampleAuthMessage1Ad.our_uuid) self.ts = esta.TimeSeries.get_time_series(self.testUUID) bike_habit = {'type': "habit", 'text': "Bike", 'up': True, 'down': False, 'priority': 2} bike_habit_id = proxy.create_habit(self.testUUID, bike_habit) walk_habit = {'type': "habit", 'text': "Walk", 'up': True, 'down': False, 'priority': 2} walk_habit_id = proxy.create_habit(self.testUUID, walk_habit) logging.debug("in setUp, result = %s" % self.ts)
def setUp(self): #load test user party.create_party_leaders() self.testUUID = uuid.uuid4() autogen_string = randomGen() autogen_email = autogen_string + '@test.com' self.sampleAuthMessage1 = { 'username': autogen_string, 'email': autogen_email, 'password': "******", 'our_uuid': self.testUUID } sampleAuthMessage1Ad = ad.AttrDict(self.sampleAuthMessage1) proxy.habiticaRegister(sampleAuthMessage1Ad.username, sampleAuthMessage1Ad.email, sampleAuthMessage1Ad.password, sampleAuthMessage1Ad.our_uuid) edb.get_habitica_db().update({"user_id": self.testUUID}, { "$set": { 'metrics_data.last_timestamp': arrow.Arrow(2016, 5, 1).timestamp } }, upsert=True) self.ts = esta.TimeSeries.get_time_series(self.testUUID) bike_habit = { 'type': "habit", 'text': "Bike", 'up': True, 'down': False, 'priority': 2 } bike_habit_id = proxy.create_habit(self.testUUID, bike_habit) walk_habit = { 'type': "habit", 'text': "Walk", 'up': True, 'down': False, 'priority': 2 } walk_habit_id = proxy.create_habit(self.testUUID, walk_habit) logging.debug("in setUp, result = %s" % self.ts)
def create_new_tasks(uuid): bike_walk_habit = { 'type': "habit", 'text': "Bike and Walk", 'notes': "Automatically get points for every 1 km walked or biked. ***=== DO NOT EDIT BELOW THIS POINT ===*** AUTOCHECK: {\"mapper\": \"active_distance\", \"args\": {\"walk_scale\": 1000, \"bike_scale\": 1000}}", 'up': True, 'down': False, 'priority': 2 } bike_walk_habit_id = proxy.create_habit(uuid, bike_walk_habit) invite_friends = { 'type': "habit", 'text': "Spread the word", 'notes': "Get points for inviting your friends! We're better together.", 'up': True, 'down': False, 'priority': 2 } invite_friends_id = proxy.create_habit(uuid, invite_friends)
def testCreateNewHabit(self): new_habit = {'type': "habit", 'text': randomGen(), 'notes': 'AUTOCHECK: {"mapper": "active_distance",' '"args": {"walk_scale": 1000, "bike_scale": 3000}}'} habit_id = proxy.create_habit(self.testUUID, new_habit) logging.debug("in testCreateNewHabit, the new habit id is = %s" % habit_id) #Get user's list of habits and check that new habit is there response = proxy.habiticaProxy(self.testUUID, 'GET', "/api/v3/tasks/user?type=habits", None) logging.debug("in testCreateNewHabit, GET habits response = %s" % response) habits = response.json() logging.debug("in testCreateNewHabit, this user's list of habits = %s" % habits) self.assertTrue(habit['_id'] == habit_id for habit in habits['data']) self.assertTrue(habit['text'] == new_habit['text'] for habit in habits['data'])
def testCreateNewHabit(self): new_habit = {'type': "habit", 'text': randomGen()} habit_id = proxy.create_habit(self.testUUID, new_habit) logging.debug("in testCreateNewHabit, the new habit id is = %s" % habit_id) #Get user's list of habits and check that new habit is there response = proxy.habiticaProxy(self.testUUID, 'GET', "/api/v3/tasks/user?type=habits", None) logging.debug("in testCreateNewHabit, GET habits response = %s" % response) habits = response.json() logging.debug( "in testCreateNewHabit, this user's list of habits = %s" % habits) self.assertTrue(habit['_id'] == habit_id for habit in habits['data']) self.assertTrue(habit['text'] == new_habit['text'] for habit in habits['data'])
def testCreateExistingHabit(self): #try to create Bike existing_habit = {'type': "habit", 'text': "Bike"} habit_id = proxy.create_habit(self.testUUID, existing_habit) logging.debug("in testCreateExistingHabit, the new habit id is = %s" % habit_id) #search this user's habits for the habit and check if there's exactly one response = proxy.habiticaProxy(self.testUUID, 'GET', "/api/v3/tasks/user?type=habits", None) logging.debug("in testCreateExistingHabit, GET habits response = %s" % response) habits = response.json() logging.debug( "in testCreateExistingHabit, this user's list of habits = %s" % habits) self.assertTrue(habit['_id'] == habit_id for habit in habits['data']) self.assertTrue(habit['text'] == new_habit['text'] for habit in habits['data']) #search this user's habits for the habit and check if there's exactly one occurrences = (1 for habit in habits['data'] if habit['text'] == existing_habit['text']) self.assertEqual(sum(occurrences), 1)
def create_new_tasks(uuid): bike_walk_habit = {'type': "habit", 'text': "Bike and Walk", 'notes': "Automatically get points for every 1 km walked or biked. ***=== DO NOT EDIT BELOW THIS POINT ===*** AUTOCHECK: {\"mapper\": \"active_distance\", \"args\": {\"walk_scale\": 1000, \"bike_scale\": 1000}}", 'up': True, 'down': False, 'priority': 2} bike_walk_habit_id = proxy.create_habit(uuid, bike_walk_habit) invite_friends = {'type': "habit", 'text': "Spread the word", 'notes': "Get points for inviting your friends! We're better together.", 'up': True, 'down': False, 'priority': 2} invite_friends_id = proxy.create_habit(uuid, invite_friends)
def testAutomaticRewardActiveTransportation(self): # Create a task that we can retrieve later self.new_task_text = randomGen() new_habit = { 'type': "habit", 'text': self.new_task_text, 'notes': 'AUTOCHECK: {"mapper": "active_distance",' '"args": {"walk_scale": 1000, "bike_scale": 3000}}' } habit_id = proxy.create_habit(self.testUUID, new_habit) self.dummy_task = enehat.Task() self.dummy_task.task_id = habit_id logging.debug("in testAutomaticRewardActiveTransportation," "the new habit id is = %s and task is %s" % (habit_id, self.dummy_task)) #Create test data -- code copied from TestTimeGrouping key = (2016, 5, 3) test_section_list = [] # # Since PST is UTC-7, all of these will be in the same UTC day # 13:00, 17:00, 21:00 # so we expect the local date and UTC bins to be the same test_section_list.append( self._createTestSection( arrow.Arrow(2016, 5, 3, 6, tzinfo=tz.gettz(PST)), PST)) test_section_list.append( self._createTestSection( arrow.Arrow(2016, 5, 3, 10, tzinfo=tz.gettz(PST)), PST)) test_section_list.append( self._createTestSection( arrow.Arrow(2016, 5, 3, 14, tzinfo=tz.gettz(PST)), PST)) self._fillModeDistanceDuration(test_section_list) #logging.debug("durations = %s" % [s.data.duration for s in test_section_list]) summary_ts = earmt.group_by_timestamp( self.testUUID, arrow.Arrow(2016, 5, 1).timestamp, arrow.Arrow(2016, 6, 1).timestamp, None, [earmts.get_distance]) logging.debug( "in testAutomaticRewardActiveTransportation, result = %s" % summary_ts) #Get user data before scoring user_before = autocheck.get_task_state(self.testUUID, self.dummy_task) self.assertIsNone(user_before) # Needed to work, otherwise sections from may won't show up in the query! modification = { "last_timestamp": arrow.Arrow(2016, 5, 1).timestamp, "bike_count": 0, "walk_count": 0 } autocheck.save_task_state(self.testUUID, self.dummy_task, modification) user_before = autocheck.get_task_state(self.testUUID, self.dummy_task) self.assertEqual(int(user_before['bike_count']), 0) habits_before = proxy.habiticaProxy(self.testUUID, 'GET', "/api/v3/tasks/user?type=habits", None).json() bike_pts_before = [ habit['history'] for habit in habits_before['data'] if habit['text'] == self.new_task_text ] #Score points autocheck.give_points_for_all_tasks(self.testUUID) #Get user data after scoring and check results user_after = autocheck.get_task_state(self.testUUID, self.dummy_task) self.assertEqual(int(user_after['bike_count']), 1500) habits_after = proxy.habiticaProxy(self.testUUID, 'GET', "/api/v3/tasks/user?type=habits", None).json() bike_pts_after = [ habit['history'] for habit in habits_after['data'] if habit['text'] == self.new_task_text ] self.assertTrue(len(bike_pts_after[0]) - len(bike_pts_before[0]) == 2)
def testAutomaticRewardActiveTransportation(self): # Create a task that we can retrieve later self.new_task_text = randomGen() new_habit = {'type': "habit", 'text': self.new_task_text, 'notes': 'AUTOCHECK: {"mapper": "active_distance",' '"args": {"walk_scale": 1000, "bike_scale": 3000}}'} habit_id = proxy.create_habit(self.testUUID, new_habit) self.dummy_task = enehat.Task() self.dummy_task.task_id = habit_id logging.debug("in testAutomaticRewardActiveTransportation," "the new habit id is = %s and task is %s" % (habit_id, self.dummy_task)) #Create test data -- code copied from TestTimeGrouping key = (2016, 5, 3) test_section_list = [] # # Since PST is UTC-7, all of these will be in the same UTC day # 13:00, 17:00, 21:00 # so we expect the local date and UTC bins to be the same test_section_list.append( self._createTestSection(arrow.Arrow(2016,5,3,6, tzinfo=tz.gettz(PST)), PST)) test_section_list.append( self._createTestSection(arrow.Arrow(2016,5,3,10, tzinfo=tz.gettz(PST)), PST)) test_section_list.append( self._createTestSection(arrow.Arrow(2016,5,3,14, tzinfo=tz.gettz(PST)), PST)) self._fillModeDistanceDuration(test_section_list) #logging.debug("durations = %s" % [s.data.duration for s in test_section_list]) summary_ts = earmt.group_by_timestamp(self.testUUID, arrow.Arrow(2016,5,1).timestamp, arrow.Arrow(2016,6,1).timestamp, None, [earmts.get_distance]) logging.debug("in testAutomaticRewardActiveTransportation, result = %s" % summary_ts) #Get user data before scoring user_before = autocheck.get_task_state(self.testUUID, self.dummy_task) self.assertIsNone(user_before) # Needed to work, otherwise sections from may won't show up in the query! modification = {"last_timestamp": arrow.Arrow(2016,5,1).timestamp, "bike_count": 0, "walk_count":0} autocheck.save_task_state(self.testUUID, self.dummy_task, modification) user_before = autocheck.get_task_state(self.testUUID, self.dummy_task) self.assertEqual(int(user_before['bike_count']), 0) habits_before = proxy.habiticaProxy(self.testUUID, 'GET', "/api/v3/tasks/user?type=habits", None).json() bike_pts_before = [habit['history'] for habit in habits_before['data'] if habit['text'] == self.new_task_text] #Score points autocheck.give_points_for_all_tasks(self.testUUID) #Get user data after scoring and check results user_after = autocheck.get_task_state(self.testUUID, self.dummy_task) self.assertEqual(int(user_after['bike_count']),1500) habits_after = proxy.habiticaProxy(self.testUUID, 'GET', "/api/v3/tasks/user?type=habits", None).json() bike_pts_after = [habit['history'] for habit in habits_after['data'] if habit['text'] == self.new_task_text] self.assertTrue(len(bike_pts_after[0]) - len(bike_pts_before[0]) == 2)
def reward_active_transportation(user_id): logging.debug("Entering habitica autocheck for user %s" % user_id) if edb.get_habitica_db().find({'user_id': user_id}).count() == 1: logging.debug("Habitica user: %s" % list(edb.get_habitica_db().find({'user_id': user_id}))) #make sure habits exist #bike bike_habit = { 'type': "habit", 'text': "Bike", 'up': True, 'down': False, 'priority': 2 } bike_habit_id = proxy.create_habit(user_id, bike_habit) #walk walk_habit = { 'type': "habit", 'text': "Walk", 'up': True, 'down': False, 'priority': 2 } walk_habit_id = proxy.create_habit(user_id, walk_habit) #get timestamps user_val = list(edb.get_habitica_db().find({"user_id": user_id }))[0]['metrics_data'] timestamp_from_db = user_val['last_timestamp'] timestamp_now = arrow.utcnow().timestamp #Get metrics summary_ts = earmt.group_by_timestamp(user_id, timestamp_from_db, timestamp_now, None, earmts.get_distance) logging.debug("Metrics response: %s" % summary_ts) #get distances leftover from last timestamp bike_distance = user_val['bike_count'] walk_distance = user_val['walk_count'] #iterate over summary_ts and look for bike/on foot for item in summary_ts: try: bike_distance += item.BICYCLING logging.debug("bike_distance += %s" % item.BICYCLING) except AttributeError: logging.debug("no bike") try: walk_distance += item.ON_FOOT logging.debug("walk_distance += %s" % item.ON_FOOT) except AttributeError: logging.debug("no Android walk") try: walk_distance += item.WALKING logging.debug("walk_distance += %s" % item.WALKING) except AttributeError: logging.debug("no ios walk") try: walk_distance += item.RUNNING logging.debug("walk_distance += %s" % item.RUNNING) except AttributeError: logging.debug("no running") logging.debug("Finished with bike_distance == %s" % bike_distance) logging.debug("Finished with walk_distance == %s" % walk_distance) method_uri_walk = "/api/v3/tasks/" + walk_habit_id + "/score/up" method_uri_bike = "/api/v3/tasks/" + bike_habit_id + "/score/up" #reward user by scoring + habits # Walk: +1 for every km walk_pts = int(walk_distance // 1000) for i in range(walk_pts): res = proxy.habiticaProxy(user_id, 'POST', method_uri_walk, None) logging.debug("Request to score walk points %s" % res) # Bike: +1 for every 3 km bike_pts = int(bike_distance // 3000) for i in range(bike_pts): res2 = proxy.habiticaProxy(user_id, 'POST', method_uri_bike, None) logging.debug("Request to score bike points %s" % res2) #update the timestamp and bike/walk counts in db edb.get_habitica_db().update({"user_id": user_id}, { "$set": { 'metrics_data': { 'last_timestamp': arrow.utcnow().timestamp, 'bike_count': bike_distance % 3000, 'walk_count': walk_distance % 1000 } } }, upsert=True) logging.debug("Habitica user after update: %s" % list(edb.get_habitica_db().find({'user_id': user_id})))