Skip to content

Commit 3afb413

Browse files
committed
Fix EPA schema mismatches and Decode scoring
1 parent 7a348b2 commit 3afb413

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

packages/epa/src/breakdown.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class SeasonBreakdown:
8585
name="Decode",
8686
auto_columns=["auto_points"],
8787
dc_columns=["dc_points"],
88-
endgame_columns=["eg_points"],
88+
endgame_columns=[],
89+
has_endgame=False,
8990
score_sd=35.0,
9091
score_mean=100.0,
9192
),

packages/epa/src/calculate.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ def get_score_stats(db: Session, season: int) -> tuple[float, float]:
2424
Reads from the dynamic match_score_YYYY tables managed by TypeORM.
2525
"""
2626
table = f"match_score_{season}"
27+
breakdown = get_breakdown(season)
2728

2829
try:
2930
result = db.execute(
30-
text(f"SELECT total_points FROM {table} WHERE total_points IS NOT NULL")
31+
text(
32+
f"SELECT {breakdown.total_column} "
33+
f"FROM {table} WHERE {breakdown.total_column} IS NOT NULL"
34+
)
3135
)
3236
scores = [row[0] for row in result if row[0] is not None]
3337
except Exception:
38+
db.rollback()
3439
return 100.0, 30.0
3540

3641
if not scores:
37-
breakdown = get_breakdown(season)
3842
return breakdown.score_mean, breakdown.score_sd
3943

4044
return float(np.mean(scores)), max(1.0, float(np.std(scores)))
@@ -43,10 +47,22 @@ def get_score_stats(db: Session, season: int) -> tuple[float, float]:
4347
def _get_match_scores(db: Session, season: int, event_code: str, match_id: str) -> dict:
4448
"""Fetch match scores from dynamic match_score_YYYY table."""
4549
table = f"match_score_{season}"
50+
breakdown = get_breakdown(season)
51+
52+
def sum_expr(columns: list[str]) -> str:
53+
if not columns:
54+
return "0"
55+
return " + ".join(f"COALESCE({column}, 0)" for column in columns)
56+
4657
try:
4758
result = db.execute(
4859
text(
49-
f"SELECT alliance, total_points, total_points_np, auto_points, dc_points, eg_points "
60+
f"SELECT alliance, "
61+
f"{breakdown.total_column} AS total_points, "
62+
f"{(breakdown.total_np_column or breakdown.total_column)} AS total_points_np, "
63+
f"{sum_expr(breakdown.auto_columns)} AS auto_points, "
64+
f"{sum_expr(breakdown.dc_columns)} AS dc_points, "
65+
f"{sum_expr(breakdown.endgame_columns)} AS eg_points "
5066
f"FROM {table} WHERE season = :season AND event_code = :event_code AND match_id = :match_id"
5167
),
5268
{"season": season, "event_code": event_code, "match_id": match_id},
@@ -63,6 +79,7 @@ def _get_match_scores(db: Session, season: int, event_code: str, match_id: str)
6379
}
6480
return scores
6581
except Exception:
82+
db.rollback()
6683
return {}
6784

6885

@@ -105,11 +122,13 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
105122
Match.event_code == event.code,
106123
Match.has_been_played == True,
107124
)
108-
.order_by(Match.tournament_level, Match.match_num)
125+
.order_by(Match.tournament_level, Match.series, Match.id)
109126
.all()
110127
)
111128

112129
for match in matches:
130+
match_key = str(match.id)
131+
113132
# Get team participation
114133
tmps = (
115134
db.query(TeamMatchParticipation)
@@ -170,7 +189,7 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
170189
EpaTeamMatch.team_number == team_num,
171190
EpaTeamMatch.season == season,
172191
EpaTeamMatch.event_code == event.code,
173-
EpaTeamMatch.match_id == match.id,
192+
EpaTeamMatch.match_id == match_key,
174193
)
175194
.first()
176195
)
@@ -179,7 +198,7 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
179198
team_number=team_num,
180199
season=season,
181200
event_code=event.code,
182-
match_id=match.id,
201+
match_id=match_key,
183202
alliance=alliance,
184203
)
185204
db.add(etm)
@@ -223,7 +242,7 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
223242
EpaTeamMatch.team_number == team_num,
224243
EpaTeamMatch.season == season,
225244
EpaTeamMatch.event_code == event.code,
226-
EpaTeamMatch.match_id == match.id,
245+
EpaTeamMatch.match_id == match_key,
227246
)
228247
.first()
229248
)

packages/epa/src/db/readonly.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
names and column names match the SnakeNamingStrategy used by TypeORM.
66
"""
77

8-
from sqlalchemy import Boolean, Column, DateTime, Float, Integer, String, Text
8+
from sqlalchemy import Boolean, Column, DateTime, Integer, String
99
from sqlalchemy.orm import declarative_base
1010

1111
ReadBase = declarative_base()
@@ -46,15 +46,18 @@ class Match(ReadBase):
4646

4747
__tablename__ = "match"
4848

49-
season = Column(Integer, primary_key=True)
49+
season = Column("event_season", Integer, primary_key=True)
5050
event_code = Column(String, primary_key=True)
51-
id = Column(String, primary_key=True)
51+
id = Column(Integer, primary_key=True)
5252
tournament_level = Column(String, nullable=True)
5353
series = Column(Integer, nullable=True)
54-
match_num = Column(Integer, nullable=True)
5554
has_been_played = Column(Boolean, default=False)
56-
scheduled_start_time = Column(String, nullable=True)
57-
actual_start_time = Column(String, nullable=True)
55+
scheduled_start_time = Column(DateTime, nullable=True)
56+
actual_start_time = Column(DateTime, nullable=True)
57+
58+
@property
59+
def match_num(self) -> int:
60+
return self.id % 1000
5861

5962

6063
class TeamMatchParticipation(ReadBase):

0 commit comments

Comments
 (0)