hockey-blast-common-lib 0.1.26__py3-none-any.whl → 0.1.28__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,10 +15,12 @@ class Comment(db.Model):
15
15
  class Division(db.Model):
16
16
  __tablename__ = 'divisions'
17
17
  id = db.Column(db.Integer, primary_key=True)
18
- league_number = db.Column(db.Integer)
19
- season_number = db.Column(db.Integer)
20
- level = db.Column(db.String(100)) # UNIQUE LEVEL NAME
21
- skill_id = db.Column(db.Integer, db.ForeignKey('skills.id')) # SKILL LEVEL
18
+ league_number = db.Column(db.Integer) # TODO: Deprecate usage and remove (get this info through Season->League)
19
+ season_number = db.Column(db.Integer) # TODO: Deprecate usage and remove (get this info from Season by season_id)
20
+ season_id = db.Column(db.Integer, db.ForeignKey('seasons.id'))
21
+ level = db.Column(db.String(100))
22
+ skill_value = db.Column(db.Float)
23
+ skill_propagation_sequence = db.Column(db.Integer, nullable=True, default=-1)
22
24
  org_id = db.Column(db.Integer, db.ForeignKey('organizations.id'), nullable=False)
23
25
  __table_args__ = (
24
26
  db.UniqueConstraint('org_id', 'league_number', 'season_number', 'level', name='_org_league_season_level_uc'),
@@ -159,6 +161,7 @@ class Skill(db.Model):
159
161
  skill_value = db.Column(db.Float) # A number from 0 (NHL) to 100 (pedestrian)
160
162
  level_name = db.Column(db.String(100), unique=True)
161
163
  level_alternative_name = db.Column(db.String(100))
164
+ is_seed = db.Column(db.Boolean, nullable=True, default=False) # New field
162
165
  __table_args__ = (
163
166
  db.UniqueConstraint('org_id', 'level_name', name='_org_level_name_uc'),
164
167
  )
@@ -294,7 +297,8 @@ class Season(db.Model):
294
297
  season_name = db.Column(db.String(100))
295
298
  start_date = db.Column(db.Date)
296
299
  end_date = db.Column(db.Date)
297
- league_number = db.Column(db.Integer)
300
+ league_number = db.Column(db.Integer) # TODO: Deprecate usage and remove (get this info from League by league_id)
301
+ league_id = db.Column(db.Integer, db.ForeignKey('leagues.id'))
298
302
  org_id = db.Column(db.Integer, db.ForeignKey('organizations.id'), nullable=False)
299
303
  __table_args__ = (
300
304
  db.UniqueConstraint('org_id', 'league_number', 'season_number', name='_org_league_season_uc'),
@@ -0,0 +1,183 @@
1
+ import sys
2
+ import os
3
+ from collections import defaultdict
4
+
5
+ # Add the project root directory to the Python path
6
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
7
+
8
+ from hockey_blast_common_lib.models import Game, Division, Skill, Season, League
9
+ from hockey_blast_common_lib.db_connection import create_session
10
+
11
+ def analyze_levels(org):
12
+ session = create_session(org)
13
+
14
+ # Query to get games and their divisions for season_number 33 and 35 with league_number 1
15
+ games_season_33 = session.query(Game.home_team_id, Game.visitor_team_id, Division.level).join(Division, Game.division_id == Division.id).filter(Division.season_number == 33, Division.league_number == 1).all()
16
+ games_season_35 = session.query(Game.home_team_id, Game.visitor_team_id, Division.level).join(Division, Game.division_id == Division.id).filter(Division.season_number == 35, Division.league_number == 1).all()
17
+
18
+ # Dictionary to store levels for each team by season
19
+ team_levels_season_33 = defaultdict(set)
20
+ team_levels_season_35 = defaultdict(set)
21
+
22
+ # Populate the dictionaries
23
+ for home_team_id, visitor_team_id, level in games_season_33:
24
+ team_levels_season_33[home_team_id].add(level)
25
+ team_levels_season_33[visitor_team_id].add(level)
26
+
27
+ for home_team_id, visitor_team_id, level in games_season_35:
28
+ team_levels_season_35[home_team_id].add(level)
29
+ team_levels_season_35[visitor_team_id].add(level)
30
+
31
+ # Dictionary to store level name connections
32
+ level_connections = defaultdict(lambda: defaultdict(int))
33
+
34
+ # Analyze the level name connections
35
+ for team_id in team_levels_season_33:
36
+ if team_id in team_levels_season_35:
37
+ for old_level in team_levels_season_33[team_id]:
38
+ for new_level in team_levels_season_35[team_id]:
39
+ level_connections[new_level][old_level] += 1
40
+
41
+ # Output the results
42
+ for new_level in sorted(level_connections.keys()):
43
+ connections = level_connections[new_level]
44
+ connections_list = sorted(connections.items(), key=lambda x: x[0])
45
+ connections_str = ", ".join([f"{old_level}: {count}" for old_level, count in connections_list])
46
+ print(f"{new_level}: {connections_str}")
47
+
48
+ session.close()
49
+
50
+ def reset_skill_values_in_divisions():
51
+ session = create_session("boss")
52
+
53
+ # Fetch all records from the Division table
54
+ divisions = session.query(Division).all()
55
+
56
+ for division in divisions:
57
+ # Look up the Skill table using the level from Division
58
+ div_level = division.level
59
+
60
+ # Query to find the matching Skill
61
+ skill = session.query(Skill).filter(Skill.org_id == division.org_id, Skill.level_name == div_level).one_or_none()
62
+
63
+ if not skill:
64
+ # If no match found, check each alternative name individually
65
+ skills = session.query(Skill).filter(Skill.org_id == division.org_id).all()
66
+ for s in skills:
67
+ alternative_names = s.level_alternative_name.split(',')
68
+ if div_level in alternative_names:
69
+ skill = s
70
+ break
71
+
72
+ if skill:
73
+ # Assign the skill_value and set skill_propagation_sequence to 0
74
+ division.skill_value = skill.skill_value
75
+ division.skill_propagation_sequence = 0
76
+ print(f"Assigned Skill {skill.skill_value} for Division with name {division.level}")
77
+ else:
78
+ # Assign -1 to skill_value and skill_propagation_sequence
79
+ division.skill_value = -1
80
+ division.skill_propagation_sequence = -1
81
+ print(f"No matching Skill found for Division with name {division.level}, assigned default values")
82
+
83
+ # Commit the changes to the Division
84
+ session.commit()
85
+
86
+ print("Skill values and propagation sequences have been populated into the Division table.")
87
+
88
+ def fill_seed_skills():
89
+ session = create_session("boss")
90
+
91
+ # List of Skill objects based on the provided comments
92
+ skills = [
93
+ Skill(is_seed=True, org_id=1, skill_value=10.0, level_name='Adult Division 1', level_alternative_name='Senior A'),
94
+ Skill(is_seed=True, org_id=1, skill_value=20.0, level_name='Adult Division 2', level_alternative_name='Senior B'),
95
+ Skill(is_seed=True, org_id=1, skill_value=30.0, level_name='Adult Division 3A', level_alternative_name='Senior BB'),
96
+ Skill(is_seed=True, org_id=1, skill_value=35.0, level_name='Adult Division 3B', level_alternative_name='Senior C'),
97
+ Skill(is_seed=True, org_id=1, skill_value=40.0, level_name='Adult Division 4A', level_alternative_name='Senior CC'),
98
+ Skill(is_seed=True, org_id=1, skill_value=45.0, level_name='Adult Division 4B', level_alternative_name='Senior CCC,Senior CCCC'),
99
+ Skill(is_seed=True, org_id=1, skill_value=50.0, level_name='Adult Division 5A', level_alternative_name='Senior D,Senior DD'),
100
+ Skill(is_seed=True, org_id=1, skill_value=55.0, level_name='Adult Division 5B', level_alternative_name='Senior DDD'),
101
+ Skill(is_seed=True, org_id=1, skill_value=60.0, level_name='Adult Division 6A', level_alternative_name='Senior DDDD'),
102
+ Skill(is_seed=True, org_id=1, skill_value=65.0, level_name='Adult Division 6B', level_alternative_name='Senior DDDDD'),
103
+ Skill(is_seed=True, org_id=1, skill_value=70.0, level_name='Adult Division 7A', level_alternative_name='Senior E'),
104
+ Skill(is_seed=True, org_id=1, skill_value=75.0, level_name='Adult Division 7B', level_alternative_name='Senior EE'),
105
+ Skill(is_seed=True, org_id=1, skill_value=80.0, level_name='Adult Division 8', level_alternative_name='Senior EEE'),
106
+ Skill(is_seed=True, org_id=1, skill_value=80.0, level_name='Adult Division 8A', level_alternative_name='Senior EEE'),
107
+ Skill(is_seed=True, org_id=1, skill_value=85.0, level_name='Adult Division 8B', level_alternative_name='Senior EEEE'),
108
+ Skill(is_seed=True, org_id=1, skill_value=90.0, level_name='Adult Division 9', level_alternative_name='Senior EEEEE')
109
+ ]
110
+
111
+ for skill in skills:
112
+ session.add(skill)
113
+ session.commit()
114
+
115
+ print("Seed skills have been populated into the database.")
116
+
117
+ def get_fake_skill(session):
118
+ # Create a special fake Skill with org_id == -1 and skill_value == -1
119
+ fake_skill = session.query(Skill).filter_by(org_id=1, level_name='Fake Skill').first()
120
+ if not fake_skill:
121
+ fake_skill = Skill(
122
+ org_id=1,
123
+ skill_value=-1,
124
+ level_name='Fake Skill',
125
+ level_alternative_name='',
126
+ is_seed=False
127
+ )
128
+ session.add(fake_skill)
129
+ session.commit()
130
+ print("Created special fake Skill record.")
131
+ return fake_skill
132
+
133
+ def assign_fake_skill_to_divisions(session, fake_skill):
134
+ # Assign the special fake Skill to every existing Division
135
+ divisions = session.query(Division).all()
136
+ for division in divisions:
137
+ division.skill_id = fake_skill.id
138
+ session.commit()
139
+ print("Assigned special fake Skill to all Division records.")
140
+
141
+ def delete_all_skills():
142
+ session = create_session("boss")
143
+ fake_skill = get_fake_skill(session)
144
+ assign_fake_skill_to_divisions(session, fake_skill)
145
+ # Delete all Skill records except the fake skill
146
+ session.query(Skill).filter(Skill.id != fake_skill.id).delete(synchronize_session=False)
147
+ session.commit()
148
+ print("All Skill records except the fake skill have been deleted.")
149
+
150
+ def populate_season_ids():
151
+ session = create_session("boss")
152
+ divisions = session.query(Division).all()
153
+ for division in divisions:
154
+ # Find the Season record that matches the season_number
155
+ season = session.query(Season).filter_by(season_number=division.season_number, org_id=division.org_id, league_number=division.league_number).first()
156
+ if season:
157
+ division.season_id = season.id
158
+ print(f"Assigned season_id {season.id} for Division with season_number {division.season_number}")
159
+ else:
160
+ print(f"Season not found for Division with season_number {division.season_number}")
161
+ session.commit()
162
+ print("Season IDs have been populated into the Division table.")
163
+
164
+ def populate_league_ids():
165
+ session = create_session("boss")
166
+ seasons = session.query(Season).all()
167
+ for season in seasons:
168
+ # Find the League record that matches the league_number and org_id
169
+ league = session.query(League).filter_by(league_number=season.league_number, org_id=season.org_id).first()
170
+ if league:
171
+ season.league_id = league.id
172
+ print(f"Assigned league_id {league.id} for Season with league_number {season.league_number}")
173
+ else:
174
+ print(f"League not found for Season with league_number {season.league_number}")
175
+ session.commit()
176
+ print("League IDs have been populated into the Season table.")
177
+
178
+ if __name__ == "__main__":
179
+ # delete_all_skills()
180
+ # fill_seed_skills()
181
+ reset_skill_values_in_divisions()
182
+ #populate_season_ids() # Call the function to populate season_ids
183
+ #populate_league_ids() # Call the new function to populate league_ids
@@ -11,7 +11,7 @@ from hockey_blast_common_lib.stats_models import db
11
11
  from hockey_blast_common_lib.db_connection import get_db_params
12
12
 
13
13
  app = Flask(__name__)
14
- db_params = get_db_params("bosshockey-blast-radonly")
14
+ db_params = get_db_params("boss")
15
15
  db_url = f"postgresql://{db_params['user']}:{db_params['password']}@{db_params['host']}:{db_params['port']}/{db_params['dbname']}"
16
16
  app.config['SQLALCHEMY_DATABASE_URI'] = db_url
17
17
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hockey-blast-common-lib
3
- Version: 0.1.26
3
+ Version: 0.1.28
4
4
  Summary: Common library for shared functionality and DB models
5
5
  Author: Pavel Kletskov
6
6
  Author-email: kletskov@gmail.com
@@ -5,14 +5,15 @@ hockey_blast_common_lib/aggregate_referee_stats.py,sha256=A0PTyEbPUjqfXxlJCDOVio
5
5
  hockey_blast_common_lib/aggregate_skater_stats.py,sha256=37fhgej9trukr8cGaK7DT1HoxBcp95qwsypYCCziqqc,15563
6
6
  hockey_blast_common_lib/db_connection.py,sha256=HvPxDvOj7j5H85RfslGvHVNevfg7mKCd0syJ6NX21mU,1890
7
7
  hockey_blast_common_lib/dump_sample_db.sh,sha256=MHPA-Ciod7wsvAlMbRtXFiyajgnEqU1xR59sJQ9UWR0,738
8
- hockey_blast_common_lib/hockey_blast_sample_backup.sql.gz,sha256=UBbPuU8WzHmxIQScT1uXIbTpjWkkt3gu-FS2LNjBx2A,1033691
9
- hockey_blast_common_lib/models.py,sha256=GqQIGpanmFub97BoYCuaqDov2MxM4LMesUTxZ2edpvw,15393
8
+ hockey_blast_common_lib/hockey_blast_sample_backup.sql.gz,sha256=DcRvR7oyP2gbVU7fYUP8-Z9R79kl0jWt4zgFyNECstk,1033685
9
+ hockey_blast_common_lib/models.py,sha256=H8d58xZqD6bUiWrdhu8xeeDXYUuWyyKNyzWLVm5IerE,15852
10
10
  hockey_blast_common_lib/options.py,sha256=-LtEX8duw5Pl3CSpjFlLM5FPvrZuTAxTfSlDPa7H6mQ,761
11
11
  hockey_blast_common_lib/restore_sample_db.sh,sha256=u2zKazC6vNMULkpYzI64nlneCWaGUtDHPBAU-gWgRbw,1861
12
+ hockey_blast_common_lib/skills_in_divisions.py,sha256=lOVww6ff7LBANmcgFlv1Eh-fvPPq0E9noG8tSfUuIZc,9140
12
13
  hockey_blast_common_lib/stats_models.py,sha256=PI-mL1jmjCHLAvaATxSsjHEn05g9L_reA_YpsITPWjQ,21047
13
14
  hockey_blast_common_lib/utils.py,sha256=odDJWCK0BgbResXeoUzxbVChjaxcXr168ZxbrAw3L_8,3752
14
- hockey_blast_common_lib/wsgi.py,sha256=wl1gf3KtaGSnYbCF5Z2xWwRXNSTQp43HoyhCAKJibI4,750
15
- hockey_blast_common_lib-0.1.26.dist-info/METADATA,sha256=pk2DS7PIU7DyABn3LxU5enJmHSghN_4OyGn8RN-l8e8,318
16
- hockey_blast_common_lib-0.1.26.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
17
- hockey_blast_common_lib-0.1.26.dist-info/top_level.txt,sha256=wIR4LIkE40npoA2QlOdfCYlgFeGbsHR8Z6r0h46Vtgc,24
18
- hockey_blast_common_lib-0.1.26.dist-info/RECORD,,
15
+ hockey_blast_common_lib/wsgi.py,sha256=7LGUzioigviJp-EUhSEaQcd4jBae0mxbkyBscQfZhlc,730
16
+ hockey_blast_common_lib-0.1.28.dist-info/METADATA,sha256=09IAnWyeWgm3Uy9FQSRU99XOSbbFXC9nV22Z3eHeAX8,318
17
+ hockey_blast_common_lib-0.1.28.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
18
+ hockey_blast_common_lib-0.1.28.dist-info/top_level.txt,sha256=wIR4LIkE40npoA2QlOdfCYlgFeGbsHR8Z6r0h46Vtgc,24
19
+ hockey_blast_common_lib-0.1.28.dist-info/RECORD,,