def storeResultEntries(fname): df = pandas.read_csv(fname) for c in range(len(df)): try: user = df['user'][c] stat = df['stat'][c] ts = int(df['ts'][c]) reading = float(df['reading'][c]) storeResultEntry(user, stat, ts, reading) except Exception as e: print(e)
def getScore(user_uuid, start, end): components = getScoreComponents(user_uuid, start, end) [pctClassified, mineMinusOptimal, allDriveMinusMine, sb375DailyGoal] = components stats.storeResultEntry(user_uuid, stats.STAT_PCT_CLASSIFIED, time.time(), pctClassified) stats.storeResultEntry(user_uuid, stats.STAT_MINE_MINUS_OPTIMAL, time.time(), mineMinusOptimal) stats.storeResultEntry(user_uuid, stats.STAT_ALL_DRIVE_MINUS_MINE, time.time(), allDriveMinusMine) stats.storeResultEntry(user_uuid, stats.STAT_SB375_DAILY_GOAL, time.time(), sb375DailyGoal) return calcScore(components)
def updateScoreForDay(user_uuid, today): yesterday = today - timedelta(days = 1) dayBeforeYesterday = today - timedelta(days = 2) dayBeforeYesterdayStart = datetime.combine(dayBeforeYesterday, dttime.min) yesterdayStart = datetime.combine(yesterday, dttime.min) todayStart = datetime.combine(today, dttime.min) user = User.fromUUID(user_uuid) (discardedScore, prevScore) = getStoredScore(user) # Using score from dayBeforeYesterday instead of yesterday because there is # currently a significant lag in the time for e-mission to prompt for # entries, so people might not confirm yesterday's trips until sometime # today, which means that it won't be counted in their score newScore = prevScore + getScore(user_uuid, dayBeforeYesterdayStart, yesterdayStart) if newScore < 0: newScore = 0 stats.storeResultEntry(user_uuid, stats.STAT_GAME_SCORE, time.time(), newScore) setScores(user, prevScore, newScore)
def setCurrView(uuid, newView): user = User.fromUUID(uuid) user.setClientSpecificProfileFields({'curr_view': newView}) stats.storeResultEntry(uuid, stats.STAT_VIEW_CHOICE, time.time(), newView)
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 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))