From 195d240a873ba29afca4a19c49b72f2dc7443396 Mon Sep 17 00:00:00 2001 From: julius Date: Mon, 24 Mar 2025 14:18:40 +0100 Subject: [PATCH] feat: use better query --- cutt/analysis.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/cutt/analysis.py b/cutt/analysis.py index 439c5f0..5bf7362 100644 --- a/cutt/analysis.py +++ b/cutt/analysis.py @@ -6,7 +6,7 @@ from fastapi.responses import JSONResponse from pydantic import BaseModel, Field from sqlmodel import Session, func, select from sqlmodel.sql.expression import SelectOfScalar -from cutt.db import Chemistry, MVPRanking, Player, Team, engine +from cutt.db import Chemistry, MVPRanking, Player, PlayerTeamLink, Team, engine import networkx as nx import numpy as np import matplotlib @@ -63,8 +63,12 @@ def graph_json( edges = [] player_map = {} with Session(engine) as session: - statement = select(Team).where(Team.id == request.team_id) - players = [t.players for t in session.exec(statement)][0] + players = session.exec( + select(P) + .join(PlayerTeamLink) + .join(Team) + .where(Team.id == request.team_id, P.disabled == False) + ).all() if not players: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) for p in players: @@ -227,8 +231,12 @@ def mvp( ): ranks = dict() with Session(engine) as session: - statement = select(Team).where(Team.id == request.team_id) - players = [t.players for t in session.exec(statement)][0] + players = session.exec( + select(P) + .join(PlayerTeamLink) + .join(Team) + .where(Team.id == request.team_id, P.disabled == False) + ).all() if not players: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) player_map = {p.id: p.display_name for p in players} @@ -268,8 +276,12 @@ async def turnout( ): player_map = {} with Session(engine) as session: - statement = select(Team).where(Team.id == request.team_id) - players = [t.players for t in session.exec(statement)][0] + players = session.exec( + select(P) + .join(PlayerTeamLink) + .join(Team) + .where(Team.id == request.team_id, P.disabled == False) + ).all() if not players: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) for p in players: