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 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 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