fix: 修复剩余审查问题
- ingest.py: 异常不再暴露客户端,改为通用错误消息+日志记录 - bzzoiro.py: 修复 normalize 重复调用,改为一次遍历缓存结果 - understat.py: 使用 Repository 替代原始 SQL
This commit is contained in:
+13
-13
@@ -86,6 +86,8 @@ class UnderstatSource:
|
||||
|
||||
注意: 本方法不控制事务(commit/rollback),由调用方通过 UnitOfWork 控制。
|
||||
"""
|
||||
from src.db.repositories import LeagueRepository, MatchRepository, TeamRepository
|
||||
|
||||
result = {"updated": 0, "skipped": 0, "unmatched": 0, "errors": []}
|
||||
|
||||
try:
|
||||
@@ -95,9 +97,13 @@ class UnderstatSource:
|
||||
result["errors"].append(f"fetch failed: {e}")
|
||||
return result
|
||||
|
||||
# 使用 Repository
|
||||
league_repo = LeagueRepository(db)
|
||||
team_repo = TeamRepository(db)
|
||||
match_repo = MatchRepository(db)
|
||||
|
||||
# 查联赛
|
||||
stmt = select(League).where(League.code == league)
|
||||
league_obj = (await db.execute(stmt)).scalar_one_or_none()
|
||||
league_obj = await league_repo.get_by_code(league)
|
||||
if league_obj is None:
|
||||
result["errors"].append(f"league {league} not found in DB")
|
||||
return result
|
||||
@@ -114,22 +120,16 @@ class UnderstatSource:
|
||||
result["errors"].append(f"normalize: {e}")
|
||||
continue
|
||||
|
||||
# 匹配已有 Match(天级) - 直接查询
|
||||
home_team = (await db.execute(select(Team).where(Team.name == nm.home_team))).scalar_one_or_none()
|
||||
away_team = (await db.execute(select(Team).where(Team.name == nm.away_team))).scalar_one_or_none()
|
||||
# 匹配已有 Match(天级) - 使用 Repository
|
||||
home_team = await team_repo.get_by_name(nm.home_team)
|
||||
away_team = await team_repo.get_by_name(nm.away_team)
|
||||
if home_team is None or away_team is None:
|
||||
result["unmatched"] += 1
|
||||
continue
|
||||
|
||||
date_only = nm.date.date() if hasattr(nm.date, "date") else nm.date
|
||||
stmt = (
|
||||
select(Match)
|
||||
.where(Match.league_id == league_obj.id)
|
||||
.where(Match.home_team_id == home_team.id)
|
||||
.where(Match.away_team_id == away_team.id)
|
||||
.where(func.date(Match.match_date) == date_only)
|
||||
existing = await match_repo.find_by_teams_and_date(
|
||||
league_obj.id, home_team.id, away_team.id, nm.date
|
||||
)
|
||||
existing = (await db.execute(stmt)).scalar_one_or_none()
|
||||
if existing is None:
|
||||
result["unmatched"] += 1
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user