-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_connections_data.py
More file actions
122 lines (110 loc) · 4.25 KB
/
generate_connections_data.py
File metadata and controls
122 lines (110 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import util
from sqlalchemy import Column, Integer, String, Float, Boolean, create_engine, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from typing import Tuple, List, Dict
from settings import Settings
from db_setup import GraphTeamInfoV4, TrueSkillTeamV1, TrueSkillMatchV1
import requests
import re
import json
Base = declarative_base()
settings = Settings()
engine = create_engine(settings.database_address)
Base.metadata.create_all(engine)
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
def get_team_info(team_number: int) -> Dict:
session = DBSession()
row = session.query(GraphTeamInfoV4).filter(GraphTeamInfoV4.team_number == team_number).first()
if row is None:
print(f'Querying TBA for Team Info: {team_number}')
url = f'https://www.thebluealliance.com/api/v2/team/frc{team_number}'
headers = {
'X-TBA-App-Id': settings.app_id
}
response = requests.get(url, headers=headers).json()
session.add(GraphTeamInfoV4(
team_number=team_number,
region=response['region'],
nickname=response['nickname']
))
session.commit()
session.close()
return {
'region': response['region'],
'nickname': response['nickname']
}
else:
session.close()
return {
'region': row.region,
'nickname': row.nickname
}
def generate_nodes_and_edges_filtered(filter_team: int, instance_thickness=False):
session = DBSession()
nodes = {}
connections = {}
for match in session.query(TrueSkillMatchV1).filter(TrueSkillMatchV1.blue_1 == filter_team or TrueSkillMatchV1.blue_2 == filter_team or TrueSkillMatchV1.blue_3 == filter_team or TrueSkillMatchV1.red_1 == filter_team or TrueSkillMatchV1.red_2 == filter_team or TrueSkillMatchV1.red_3 == filter_team).all(): # type: TrueSkillMatchV1
for team in {match.blue_1, match.blue_2, match.blue_3, match.red_1, match.red_2, match.red_3}: # type: int
if (team in nodes) is False:
team_trueskill_info = session.query(TrueSkillTeamV1).filter(TrueSkillTeamV1.team_number == team).first() # TrueSkillTeamV1
team_info = get_team_info(team)
nodes[team] = {
'id': team,
'label': str(team),
'title': f'{team_info["nickname"]}<br>Region: {team_info["region"]}',
'value': team_trueskill_info.mu,
'group': team_info['region']
}
for team_2nd_loop in {match.blue_1, match.blue_2, match.blue_3, match.red_1, match.red_2, match.red_3}:
if team != team_2nd_loop:
connections[team] = connections.get(team, {})
connections[team][team_2nd_loop] = connections[team].get(team_2nd_loop, 0) + 1
edges = []
for team_number, value in connections.items():
for other_team, instances in value.items():
edges.append({
'from': team_number,
'to': other_team,
'width': instances if instance_thickness else 1
})
session.close()
return [node for team, node in nodes.items()], edges
def generate_nodes_and_edges(instance_thickness=False):
session = DBSession()
nodes = {}
connections = {}
for match in session.query(TrueSkillMatchV1).all(): # type: TrueSkillMatchV1
for team in {match.blue_1, match.blue_2, match.blue_3, match.red_1, match.red_2, match.red_3}: # type: int
if (team in nodes) is False:
team_trueskill_info = session.query(TrueSkillTeamV1).filter(TrueSkillTeamV1.team_number == team).first() # TrueSkillTeamV1
team_info = get_team_info(team)
nodes[team] = {
'id': team,
'label': str(team),
'title': f'{team_info["nickname"]}<br>Region: {team_info["region"]}',
'value': team_trueskill_info.mu,
'group': team_info['region']
}
for team_2nd_loop in {match.blue_1, match.blue_2, match.blue_3, match.red_1, match.red_2, match.red_3}:
if team != team_2nd_loop:
connections[team] = connections.get(team, {})
connections[team][team_2nd_loop] = connections[team].get(team_2nd_loop, 0) + 1
edges = []
for team_number, value in connections.items():
for other_team, instances in value.items():
edges.append({
'from': team_number,
'to': other_team,
'width': instances if instance_thickness else 1
})
session.close()
return [node for team, node in nodes.items()], edges
nodes_, edges_ = generate_nodes_and_edges_filtered(5687)
data = {
'nodes': nodes_,
'edges': edges_
}
with open('connections/data.json', 'w') as fp:
json.dump(data, fp)