def update_element_history_fixtures_route(): logger.info("setting up client") tasks_client = utils.set_up_tasks() logger.info("purging queue") utils.purge_cloud_queue("update-element-history-fixtures", tasks_client) bootstrap_data = element_data.get_bootstrap() elements = element_data.get_elements(bootstrap_data) logger.info("setting up big query client") big_query_client = utils.set_up_bigquery() logger.info("deleting element gameweek history") utils.run_query( "DELETE FROM `footbot-001.fpl.element_gameweeks_2122` WHERE true", big_query_client, ) logger.info("deleting element fixtures") utils.run_query( "DELETE FROM `footbot-001.fpl.element_future_fixtures_2122` WHERE true", big_query_client, ) logger.info("queueing elements") for element in elements: create_update_element_history_fixtures_task(element, tasks_client) logger.info("elements queued") return "elements queued"
def update_element_history_fixtures_worker(element, delete=False): logger.info(f"setting up client for element {element}") client = utils.set_up_bigquery() logger.info(f"getting element {element} gameweek history and fixtures") ( element_history_df, element_fixtures_df, ) = element_data.get_element_history_fixture_dfs(element) if delete: logger.info(f"deleting element {element} gameweek history") utils.run_query( f"DELETE FROM `footbot-001.fpl.element_gameweeks_2122` WHERE element = {element}", client, ) logger.info(f"writing element {element} gameweek history") utils.write_to_table("fpl", "element_gameweeks_2122", element_history_df, client) logger.info(f"done writing element {element} gameweek history") if delete: logger.info(f"deleting element {element} fixtures") utils.run_query( f"DELETE FROM `footbot-001.fpl.element_future_fixtures_2122` WHERE element = {element}", client, ) logger.info(f"writing element {element} fixtures") utils.write_to_table("fpl", "element_future_fixtures_2122", element_fixtures_df, client) logger.info(f"done writing element {element} fixtures")
def update_element_data_route(): logger.info("getting element data") bootstrap_data = element_data.get_bootstrap() element_df = element_data.get_element_df(bootstrap_data) client = utils.set_up_bigquery() logger.info("writing element data") utils.write_to_table("fpl", "element_data_2122", element_df, client) logger.info("done writing element data") return "Updated element data, baby"
def update_predictions_route(): client = utils.set_up_bigquery() predict_df = train_predict.get_predicted_points_df( "./footbot/predictor/sql/train.sql", "./footbot/predictor/sql/predict.sql", client, ) logger.info("writing predictions") utils.write_to_table( "fpl", "element_gameweeks_predictions_2122_v01", predict_df, client, write_disposition="WRITE_TRUNCATE", ) logger.info("done writing predictions") return "predictions updated"
def optimise_entry( entry, total_budget=1000, first_team_factor=0.9, bench_factor=0.1, captain_factor=0.9, vice_factor=0.1, transfer_penalty=4, transfer_limit=15, start_event=1, end_event=38, login=None, password=None, ): """ Select the optimal team and transfers for a specified entry. If credentials are provided, use private entry data. :param entry: Team identifier :param total_budget: Total budget available, i.e. total team value + bank :param first_team_factor: The probability a player on the first team will play :param bench_factor: The probability a player on the bench will play :param captain_factor: The probability the captain will play :param vice_factor: The probability the captain will not play :param transfer_penalty: The cost in points of transferring a player :param transfer_limit: The limit to the number of transfers that can be made :param start_event: Start of event range to optimiser over :param end_event: End of event range to optimiser over :param login: FPL login :param password: FPL password :return: Dictionary of team selection decisions """ logger.info("getting predictions") client = set_up_bigquery() players = run_templated_query( "./footbot/optimiser/sql/optimiser.sql", dict(start_event=start_event, end_event=end_event), client, ).to_dict("records") if login and password: private_data = get_private_entry_data(entry, login, password) existing_squad = [i["element"] for i in private_data["picks"]] team_value = np.sum( [i["selling_price"] for i in private_data["picks"]]) bank = private_data["transfers"]["bank"] total_budget = team_value + bank # update player values with selling prices for players in existing squad for player in players: for pick in private_data["picks"]: if player["element"] == pick["element"]: player["value"] = pick["selling_price"] else: public_data = get_public_entry_data(entry) existing_squad = [i["element"] for i in public_data["picks"]] logger.info("optimising team") ( first_team, bench, captain, vice, transfers, ) = select_team( players, optimise_key="average_points", existing_squad=existing_squad, total_budget=total_budget, first_team_factor=first_team_factor, bench_factor=bench_factor, captain_factor=captain_factor, vice_factor=vice_factor, transfer_penalty=transfer_penalty, transfer_limit=transfer_limit, ) first_team = get_sorted_safe_web_names(first_team, players) bench = get_sorted_safe_web_names(bench, players, is_sorted=False) captain = get_sorted_safe_web_names(captain, players) vice = get_sorted_safe_web_names(vice, players) transfers_in = get_sorted_safe_web_names(transfers["transfers_in"], players) transfers_out = get_sorted_safe_web_names(transfers["transfers_out"], players) return { "first_team": first_team, "bench": bench, "captain": captain, "vice": vice, "transfers_in": transfers_in, "transfers_out": transfers_out, }