2022-03-30 06:30:55 +00:00
|
|
|
from calendar import c
|
2020-05-13 01:54:19 +00:00
|
|
|
import requests
|
2022-01-10 04:19:50 +00:00
|
|
|
import pull
|
|
|
|
import pandas as pd
|
2022-03-24 21:19:47 +00:00
|
|
|
import json
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2022-04-08 20:20:41 +00:00
|
|
|
def pull_new_tba_matches(apikey, competition, last_match):
|
2022-03-28 19:21:23 +00:00
|
|
|
api_key= apikey
|
|
|
|
x=requests.get("https://www.thebluealliance.com/api/v3/event/"+competition+"/matches/simple", headers={"X-TBA-Auth-Key":api_key})
|
2022-03-25 18:39:24 +00:00
|
|
|
json = x.json()
|
2020-05-13 01:54:19 +00:00
|
|
|
out = []
|
2022-03-25 18:39:24 +00:00
|
|
|
for i in json:
|
2022-04-08 20:20:41 +00:00
|
|
|
if i["actual_time"] != None and i["comp_level"] == "qm" and i["match_number"] > last_match :
|
2022-03-28 19:21:23 +00:00
|
|
|
out.append({"match" : i['match_number'], "blue" : list(map(lambda x: int(x[3:]), i['alliances']['blue']['team_keys'])), "red" : list(map(lambda x: int(x[3:]), i['alliances']['red']['team_keys'])), "winner": i["winning_alliance"]})
|
2022-03-28 19:43:24 +00:00
|
|
|
out.sort(key=lambda x: x['match'])
|
2020-05-13 01:54:19 +00:00
|
|
|
return out
|
|
|
|
|
2022-03-24 21:19:47 +00:00
|
|
|
def pull_new_tba_matches_manual(apikey, competition, cutoff):
|
|
|
|
filename = competition+"-wins.json"
|
|
|
|
with open(filename, 'r') as f:
|
2022-03-29 23:43:28 +00:00
|
|
|
data = json.load(f)
|
|
|
|
return data
|
2022-03-24 21:19:47 +00:00
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_team_match_data(client, competition, team_num):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client.data_scouting
|
|
|
|
mdata = db.matchdata
|
|
|
|
out = {}
|
2022-03-28 19:21:23 +00:00
|
|
|
for i in mdata.find({"competition" : competition, "team_scouted": str(team_num)}):
|
2020-05-13 01:54:19 +00:00
|
|
|
out[i['match']] = i['data']
|
|
|
|
return pd.DataFrame(out)
|
|
|
|
|
2022-03-28 19:43:24 +00:00
|
|
|
def clear_metrics(client, competition):
|
|
|
|
db = client.data_processing
|
|
|
|
data = db.team_metrics
|
|
|
|
data.delete_many({competition: competition})
|
|
|
|
return True
|
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_team_pit_data(client, competition, team_num):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client.data_scouting
|
|
|
|
mdata = db.pitdata
|
|
|
|
out = {}
|
2022-03-28 19:21:23 +00:00
|
|
|
return mdata.find_one({"competition" : competition, "team_scouted": str(team_num)})["data"]
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_team_metrics_data(client, competition, team_num):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client.data_processing
|
|
|
|
mdata = db.team_metrics
|
2022-03-30 06:30:55 +00:00
|
|
|
temp = mdata.find_one({"team": team_num})
|
|
|
|
if temp != None:
|
|
|
|
if competition in temp['metrics'].keys():
|
|
|
|
temp = temp['metrics'][competition]
|
|
|
|
else :
|
|
|
|
temp = None
|
|
|
|
else:
|
|
|
|
temp = None
|
|
|
|
return temp
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_match_data_formatted(client, competition):
|
2022-01-10 04:19:50 +00:00
|
|
|
teams_at_comp = pull.get_teams_at_competition(competition)
|
2020-05-13 01:54:19 +00:00
|
|
|
out = {}
|
2022-01-10 04:19:50 +00:00
|
|
|
for team in teams_at_comp:
|
2020-05-20 13:52:38 +00:00
|
|
|
try:
|
2022-03-28 19:21:23 +00:00
|
|
|
out[int(team)] = unkeyify_2l(get_team_match_data(client, competition, team).transpose().to_dict())
|
2020-05-20 13:52:38 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-05-13 01:54:19 +00:00
|
|
|
return out
|
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_metrics_data_formatted(client, competition):
|
2022-01-10 04:19:50 +00:00
|
|
|
teams_at_comp = pull.get_teams_at_competition(competition)
|
2020-05-13 01:54:19 +00:00
|
|
|
out = {}
|
2022-01-10 04:19:50 +00:00
|
|
|
for team in teams_at_comp:
|
2020-05-13 01:54:19 +00:00
|
|
|
try:
|
2022-01-10 04:19:50 +00:00
|
|
|
out[int(team)] = get_team_metrics_data(client, competition, int(team))
|
2020-05-13 01:54:19 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return out
|
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_pit_data_formatted(client, competition):
|
2023-03-17 15:09:48 +00:00
|
|
|
x=requests.get("https://scouting.titanrobotics2022.com/api/fetchAllTeamNicknamesAtCompetition?competition="+competition)
|
2022-02-04 03:24:47 +00:00
|
|
|
x = x.json()
|
|
|
|
x = x['data']
|
|
|
|
x = x.keys()
|
|
|
|
out = {}
|
|
|
|
for i in x:
|
|
|
|
try:
|
|
|
|
out[int(i)] = get_team_pit_data(client, competition, int(i))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return out
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_pit_variable_data(client, competition):
|
2020-05-20 13:52:38 +00:00
|
|
|
db = client.data_processing
|
|
|
|
mdata = db.team_pit
|
|
|
|
out = {}
|
|
|
|
return mdata.find()
|
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_pit_variable_formatted(client, competition):
|
|
|
|
temp = get_pit_variable_data(client, competition)
|
2020-05-20 13:52:38 +00:00
|
|
|
out = {}
|
|
|
|
for i in temp:
|
|
|
|
out[i["variable"]] = i["data"]
|
|
|
|
return out
|
|
|
|
|
2022-03-28 19:21:23 +00:00
|
|
|
def push_team_tests_data(client, competition, team_num, data, dbname = "data_processing", colname = "team_tests"):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client[dbname]
|
|
|
|
mdata = db[colname]
|
2022-03-28 19:21:23 +00:00
|
|
|
mdata.replace_one({"competition" : competition, "team": team_num}, {"_id": competition+str(team_num)+"am", "competition" : competition, "team" : team_num, "data" : data}, True)
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2022-03-28 19:21:23 +00:00
|
|
|
def push_team_metrics_data(client, competition, team_num, data, dbname = "data_processing", colname = "team_metrics"):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client[dbname]
|
|
|
|
mdata = db[colname]
|
2022-03-30 06:30:55 +00:00
|
|
|
mdata.update_one({"team": team_num}, {"$set": {"metrics.{}".format(competition): data}}, upsert=True)
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2022-03-28 19:21:23 +00:00
|
|
|
def push_team_pit_data(client, competition, variable, data, dbname = "data_processing", colname = "team_pit"):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client[dbname]
|
|
|
|
mdata = db[colname]
|
2022-03-28 19:21:23 +00:00
|
|
|
mdata.replace_one({"competition" : competition, "variable": variable}, {"competition" : competition, "variable" : variable, "data" : data}, True)
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def get_analysis_flags(client, flag):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client.data_processing
|
|
|
|
mdata = db.flags
|
2022-04-08 20:40:33 +00:00
|
|
|
return mdata.find_one({"_id": "2022"})
|
2020-05-13 01:54:19 +00:00
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
def set_analysis_flags(client, flag, data):
|
2020-05-13 01:54:19 +00:00
|
|
|
db = client.data_processing
|
|
|
|
mdata = db.flags
|
2022-04-08 20:40:33 +00:00
|
|
|
return mdata.update_one({"_id": "2022"}, {"$set": data})
|
2020-05-20 13:52:38 +00:00
|
|
|
|
|
|
|
def unkeyify_2l(layered_dict):
|
|
|
|
out = {}
|
|
|
|
for i in layered_dict.keys():
|
|
|
|
add = []
|
|
|
|
sortkey = []
|
|
|
|
for j in layered_dict[i].keys():
|
2022-03-28 19:21:23 +00:00
|
|
|
add.append([j,layered_dict[i][j]])
|
|
|
|
add.sort(key = lambda x: x[0])
|
2020-05-20 13:52:38 +00:00
|
|
|
out[i] = list(map(lambda x: x[1], add))
|
2021-08-12 21:53:03 +00:00
|
|
|
return out
|
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def get_previous_time(client):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
previous_time = get_analysis_flags(client, "latest_update")
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
if previous_time == None:
|
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
set_analysis_flags(client, "latest_update", 0)
|
2021-08-12 21:53:03 +00:00
|
|
|
previous_time = 0
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
previous_time = previous_time["latest_update"]
|
|
|
|
|
|
|
|
return previous_time
|
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def set_current_time(client, current_time):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2022-03-28 19:21:23 +00:00
|
|
|
set_analysis_flags(client, "latest_update", {"latest_update":current_time})
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def get_database_config(client):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-23 07:10:01 +00:00
|
|
|
remote_config = get_analysis_flags(client, "config")
|
|
|
|
return remote_config["config"] if remote_config != None else None
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def set_database_config(client, config):
|
|
|
|
|
|
|
|
set_analysis_flags(client, "config", {"config": config})
|
|
|
|
|
|
|
|
def load_match(client, competition):
|
|
|
|
|
|
|
|
return get_match_data_formatted(client, competition)
|
|
|
|
|
|
|
|
def load_metric(client, competition, match, group_name, metrics):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
group = {}
|
|
|
|
|
|
|
|
for team in match[group_name]:
|
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
db_data = get_team_metrics_data(client, competition, team)
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-08-17 21:02:08 +00:00
|
|
|
if db_data == None:
|
2022-03-28 19:21:23 +00:00
|
|
|
gl2 = {"score": metrics["gl2"]["score"], "rd": metrics["gl2"]["rd"], "vol": metrics["gl2"]["vol"]}
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2022-03-29 04:17:25 +00:00
|
|
|
group[team] = {"gl2": gl2}
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
2022-03-30 06:30:55 +00:00
|
|
|
metrics = db_data
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
gl2 = metrics["gl2"]
|
|
|
|
|
2022-03-29 04:17:25 +00:00
|
|
|
group[team] = {"gl2": gl2}
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
return group
|
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def load_pit(client, competition):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
return get_pit_data_formatted(client, competition)
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def push_match(client, competition, results):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
for team in results:
|
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
push_team_tests_data(client, competition, team, results[team])
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def push_metric(client, competition, metric):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
for team in metric:
|
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
push_team_metrics_data(client, competition, team, metric[team])
|
2021-08-12 21:53:03 +00:00
|
|
|
|
2021-09-21 05:14:54 +00:00
|
|
|
def push_pit(client, competition, pit):
|
2021-08-12 21:53:03 +00:00
|
|
|
|
|
|
|
for variable in pit:
|
2022-03-28 19:21:23 +00:00
|
|
|
|
2021-10-11 02:59:49 +00:00
|
|
|
push_team_pit_data(client, competition, variable, pit[variable])
|
2021-10-10 06:15:22 +00:00
|
|
|
|
|
|
|
def check_new_database_matches(client, competition):
|
|
|
|
|
2022-03-25 18:39:24 +00:00
|
|
|
return True
|