def heat_map_data(): user = model.get_user_by_email(flask_session["email"]) runs = model.find_all_runs(user) run_dictionary = {} for run in runs: run_dictionary[(run.date_run - datetime(1970,1,1)).total_seconds()] = run.approx_dist run_dictionary = json.dumps(run_dictionary) return run_dictionary
def display_ideal(): """"Will display the conditions under which the user experiences the best runs.""" if flask_session.get("email") == None: flash("You must sign in to view that page.") return redirect("/") user = model.get_user_by_email(flask_session["email"]) # The page variable determines which tabs are active. page = "ideal" runs = model.find_all_runs(user) # Making a generalization about your running habits only makes sense after a certain number of runs. # If you haven't logged a certain number of runs, it will render a template that # will tell you that this functionality will appear after more runs are logged. if len(runs) < 10: return render_template("go_run.html", page = page) run_dictionary = {} for run in runs: run_dictionary[run.id] = run # Finding user's average score and max score. max_score = 0 average_score = 0 for run_key in run_dictionary.keys(): run_score = model.get_run_score(run_key) if run_score > max_score: max_score = run_score average_score = average_score + run_score average_score = average_score/len(run_dictionary.keys()) # Determining the threshold of what is a "highly rated run" for the indivual user. high_rated_run_threshold = (max_score + average_score) * 0.5 # Building the highly rated dictionary. run_dictionary_high_score = {} for run_key in run_dictionary.keys(): run_score = model.get_run_score(run_key) if run_score >= high_rated_run_threshold: run_dictionary_high_score[run_key] = run_dictionary[run_key] # Finding the average distance of highly rated runs. average_dist_high_rated_runs = 0 for run_key in run_dictionary_high_score.keys(): average_dist_high_rated_runs = average_dist_high_rated_runs + run_dictionary_high_score[run_key].approx_dist average_dist_high_rated_runs = average_dist_high_rated_runs / len(run_dictionary_high_score.keys()) # Finding the average distance of all user runs. average_dist_run = 0 for run_key in run_dictionary.keys(): average_dist_run = average_dist_run + run_dictionary[run_key].approx_dist average_dist_run = average_dist_run / len(run_dictionary.keys()) # Finding the conditions that you prefer. locations = [] terrains = [] routes = [] for run_key in run_dictionary_high_score.keys(): locations.append(model.get_location_by_run_id(run_key).select_ans) terrains.append(model.get_terrain_by_run_id(run_key).select_ans) routes.append(model.get_route_by_run_id(run_key).select_ans) prefered_terrain = {} # iterates through all the terrain conditions. for key in model.terrain_dictionary.keys(): # If the prefered terrain dictionary is empty it adds the key to it. if prefered_terrain == {}: prefered_terrain[key] = (model.terrain_dictionary[key], terrains.count(key)) # If the prefered terrain dictionary is not empty, it adds the terrain we are currently # on if they have the same counts. If the current terrain has a higher count, it replaces the # dictionary with one where the current key is the only key. If the count is less than # what is currently in the dictionary, it moves along. elif terrains.count(key) == terrains.count(prefered_terrain.keys()[0]): prefered_terrain[key] = (model.terrain_dictionary[key], terrains.count(key)) elif terrains.count(key) > terrains.count(prefered_terrain.keys()[0]): prefered_terrain = {} prefered_terrain[key] = (model.terrain_dictionary[key], terrains.count(key)) else: pass prefered_route = {} # Iterates through all the route conditions. for key in model.route_dictionary.keys(): # If the prefered route dictionary is empty it adds the key to it. if prefered_route == {}: prefered_route[key] = (model.route_dictionary[key], routes.count(key)) # If the prefered route dictionary is not empty, it adds the route we are currently # on if they have the same counts. If the current route has a higher count, it replaces the # dictionary with one where the current key is the only key. If the count is less than # what is currently in the dictionary, it moves along. elif routes.count(key) == routes.count(prefered_route.keys()[0]): prefered_route[key] = (model.route_dictionary[key], routes.count(key)) elif routes.count(key) > routes.count(prefered_route.keys()[0]): prefered_route = {} prefered_route[key] = (model.route_dictionary[key], routes.count(key)) else: pass prefered_location = {} # Iterates through all the location conditions. for key in model.location_dictionary.keys(): # If the prefered location dictionary is empty it adds the key to it. if prefered_location == {}: prefered_location[key] = (model.location_dictionary[key], locations.count(key)) # If the prefered location dictionary is not empty, it adds the location we are currently # on if they have the same counts. If the current location has a higher count, it replaces the # dictionary with one where the current key is the only key. If the count is less than # what is currently in the dictionary, it moves along. elif locations.count(key) == locations.count(prefered_location.keys()[0]): prefered_location[key] = (model.location_dictionary[key], locations.count(key)) elif locations.count(key) > locations.count(prefered_location.keys()[0]): prefered_location = {} prefered_location[key] = (model.location_dictionary[key], locations.count(key)) else: pass # Determines the days of the week that tend to have your higher rated runs. weekdays = [] weekday_string_list = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] # makes a list of weekdays with highly rated runs. for run_key in run_dictionary_high_score.keys(): weekdays.append(run_dictionary_high_score[run_key].date_run.weekday()) prefered_weekday = {} for weekday in range(7): if prefered_weekday == {}: prefered_weekday[weekday] = weekdays.count(weekday) elif weekdays.count(weekday) == weekdays.count(prefered_weekday.keys()[0]): prefered_weekday[weekday] = weekdays.count(weekday) elif weekdays.count(weekday) > weekdays.count(prefered_weekday.keys()[0]): prefered_weekday = {} prefered_weekday[weekday] = weekdays.count(weekday) else: pass prefered_weekday_string = {} # creates a dictionary with a the weekdays as strings instead of 0-6. for key in prefered_weekday.keys(): prefered_weekday_string[weekday_string_list[key]] = prefered_weekday[key] prefered_weekday_keys = prefered_weekday.keys() prefered_location_keys = prefered_location.keys() prefered_terrain_keys = prefered_terrain.keys() prefered_route_keys = prefered_route.keys() location_image_dictionary = model.location_badges_dictionary terrain_image_dictionary = model.terrain_badges_dictionary route_image_dictionary = model.route_badges_dictionary weekday_image_dictionary = model.weekday_badges_dictionary return render_template("ideal.html", high_distance = average_dist_high_rated_runs, average_distance = average_dist_run, prefered_terrain = prefered_terrain, prefered_route = prefered_route, prefered_location = prefered_location, prefered_weekday = prefered_weekday_string, page = page, len_day = len(prefered_weekday_string), len_location = len(prefered_location), len_terrain = len(prefered_terrain), len_route = len(prefered_route), prefered_weekday_keys=prefered_weekday_keys, prefered_route_keys = prefered_route_keys, prefered_location_keys = prefered_location_keys, prefered_terrain_keys = prefered_terrain_keys, location_images = location_image_dictionary, terrain_images = terrain_image_dictionary, route_images = route_image_dictionary, weekday_images = weekday_image_dictionary)