fix(P0-02): 积分榜改为追加快照(append-only) + available_at cutoff
去掉 uq_standings_league_season_team,改为 (league, season, team, available_at) 唯一; 每次采集 INSERT 新行(available_at=now),ON CONFLICT DO NOTHING,不覆盖旧行。 standings_slice(before):DISTINCT ON (team_id) WHERE available_at<=cutoff ORDER available_at DESC;before=None → cutoff=now()。 公开 list_standings 取每队最新可用快照(子查询 max available_at)。 迁移 0023 + 切片/路由/docs 同步;测试 test_p0_standings_cutoff(5/5)。 284 测试全绿。
This commit is contained in:
@@ -286,7 +286,9 @@ async def list_standings(
|
||||
):
|
||||
"""联赛积分榜(只读)。按联赛分组,每张榜按 position 排序。
|
||||
|
||||
season 为空时返回每个联赛最新采集到的赛季榜单(适合前端"查看最新积分榜")。
|
||||
P0-02: standings 为追加快照,公开接口取每队 available_at 最新快照
|
||||
(league_id, season, team_id 上按 available_at 取最新)。
|
||||
season 为空时返回每个联赛最新采集到的赛季榜单。
|
||||
"""
|
||||
# 取每个联赛最新赛季(当 season 为空时)
|
||||
latest_seasons: dict[int, str] = {}
|
||||
@@ -299,9 +301,25 @@ async def list_standings(
|
||||
).all()
|
||||
latest_seasons = {r.league_id: r.latest for r in rows}
|
||||
|
||||
# P0-02: 子查询取每队最新 available_at 快照,再 JOIN 回主表拿完整行 + League
|
||||
latest_per_team = (
|
||||
select(
|
||||
Standing.league_id, Standing.season, Standing.team_id,
|
||||
func.max(Standing.available_at).label("max_available"),
|
||||
)
|
||||
.group_by(Standing.league_id, Standing.season, Standing.team_id)
|
||||
.subquery("latest_per_team")
|
||||
)
|
||||
q = (
|
||||
select(Standing, League)
|
||||
.join(League, League.id == Standing.league_id)
|
||||
.join(
|
||||
latest_per_team,
|
||||
(Standing.league_id == latest_per_team.c.league_id)
|
||||
& (Standing.season == latest_per_team.c.season)
|
||||
& (Standing.team_id == latest_per_team.c.team_id)
|
||||
& (Standing.available_at == latest_per_team.c.max_available),
|
||||
)
|
||||
.order_by(League.name.asc(), Standing.position.asc())
|
||||
)
|
||||
if league:
|
||||
|
||||
Reference in New Issue
Block a user