diff --git a/src/api/v2/report.py b/src/api/v2/report.py index e8fe7f6..6b7855b 100644 --- a/src/api/v2/report.py +++ b/src/api/v2/report.py @@ -1,4 +1,3 @@ -import asyncio import logging from fastapi import APIRouter, Depends, status @@ -18,7 +17,12 @@ @router.post("/report", status_code=status.HTTP_201_CREATED, response_model=Ok) -async def post_reports(detections: list[Detection], session=Depends(get_session)): +async def post_reports( + detections: list[Detection], + session=Depends(get_session), +): + global player_cache + report_repo = Report() player_repo = Player(session=session, cache=player_cache) @@ -29,9 +33,7 @@ async def post_reports(detections: list[Detection], session=Depends(get_session) # get unique list of names player_names = list(set([d.reported for d in data] + [d.reporter for d in data])) - players = await asyncio.gather( - *[player_repo.get_or_insert(player_name=p) for p in player_names] - ) + players = [await player_repo.get_or_insert(player_name=p) for p in player_names] players = {p.name: p.id for p in players} _data = [] diff --git a/src/app/repositories/player.py b/src/app/repositories/player.py index 5329be3..350de34 100644 --- a/src/app/repositories/player.py +++ b/src/app/repositories/player.py @@ -69,9 +69,8 @@ async def get_report_score(self, player_names: tuple[str]): func.coalesce(sub_query_alias.c.manual_detect, 0).label("manual_detect"), ) - async with self.session: - result: AsyncResult = await self.session.execute(sql) - await self.session.commit() + result: AsyncResult = await self.session.execute(sql) + await self.session.commit() return tuple(result.mappings()) async def get_feedback_score(self, player_names: list[str]): @@ -95,24 +94,24 @@ async def get_feedback_score(self, player_names: list[str]): fb_subject.confirmed_player, ) - async with self.session: - result: AsyncResult = await self.session.execute(query) - await self.session.commit() + result: AsyncResult = await self.session.execute(query) + await self.session.commit() return tuple(result.mappings()) async def get_prediction(self, player_names: list[str]): query: Select = select(dbPrediction) query = query.select_from(dbPrediction) query = query.where(dbPrediction.name.in_(player_names)) - async with self.session: - result: AsyncResult = await self.session.execute(query) - result = result.scalars().all() + + result: AsyncResult = await self.session.execute(query) + result = result.scalars().all() return jsonable_encoder(result) async def get(self, player_name: str) -> PlayerInDB: player_name = self.sanitize_name(player_name) sql = sqla.select(dbPlayer).where(dbPlayer.name == player_name) + result = await self.session.execute(sql) data = result.scalars().all() @@ -131,7 +130,6 @@ async def get_cache(self, player_name: str) -> PlayerInDB: if isinstance(player, PlayerInDB): await self.cache.put(key=player_name, value=player) - return player async def insert(self, player: PlayerCreate) -> PlayerInDB: