Skip to content

Commit a23d625

Browse files
committed
fix: enhance KenPom data collection with unique team keys
Updated the `collect_kenpom_fanmatch` function to generate unique team keys based on matchup data or fallback to a synthetic key. This change ensures proper handling of FanMatch data in the team-metrics table, preventing row collapse under the UNIQUE constraint.
1 parent facd30b commit a23d625

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

odds/src/odds_pipeline/collect_kenpom.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,23 @@ def collect_kenpom_fanmatch(*, game_date: date) -> int:
120120

121121
with connect(settings.database_url) as conn:
122122
with conn.cursor() as cur:
123-
for r in records:
123+
for idx, r in enumerate(records):
124124
# KenPom seasons use the end-year convention for college hoops.
125125
# Example: games in Nov/Dec 2025 belong to the 2026 season.
126126
season = game_date.year + 1 if game_date.month >= 7 else game_date.year
127127

128-
# FanMatch rows may have matchup/team columns; store under team='__fanmatch__'
128+
# FanMatch is conceptually game-level data, but we store it in the
129+
# team-metrics table. Use a per-row key so the UNIQUE(season, team,
130+
# metric_type, collected_at) constraint does not collapse rows.
131+
# Prefer a matchup-like column if present; otherwise fall back to
132+
# a synthetic stable row key.
133+
matchup_key = None
134+
for candidate in ("Matchup", "matchup", "Game", "game"):
135+
if candidate in r and r[candidate]:
136+
matchup_key = str(r[candidate])
137+
break
138+
team_key = matchup_key or f"__fanmatch_row_{idx}"
139+
129140
cur.execute(
130141
"""
131142
INSERT INTO raw_kenpom_team_metrics (
@@ -137,7 +148,7 @@ def collect_kenpom_fanmatch(*, game_date: date) -> int:
137148
""",
138149
{
139150
"season": int(season),
140-
"team": "__fanmatch__",
151+
"team": team_key,
141152
"metric_type": "fanmatch",
142153
"collected_at": collected_at,
143154
"raw": json.dumps(r),

0 commit comments

Comments
 (0)