Skip to content

Commit

Permalink
performance improvements & added name
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Apr 20, 2024
1 parent e2bdaa2 commit d5d3dd4
Show file tree
Hide file tree
Showing 4 changed files with 347 additions and 342 deletions.
1 change: 1 addition & 0 deletions src/api/v2/highscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async def get_highscore_latest_v2(
history=False,
)

logger.info(data[0])
for d in data:
scraper_id = d.pop("scraper_id")
d["Player_id"] = d.pop("player_id")
Expand Down
162 changes: 82 additions & 80 deletions src/app/repositories/scraper_data.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
from fastapi.encoders import jsonable_encoder
from sqlalchemy.ext.asyncio import AsyncResult, AsyncSession
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import Select

from src.app.repositories.abstract_repo import AbstractAPI
from src.core.database.models import Player, ScraperData, ScraperDataLatest


class ScraperDataRepo(AbstractAPI):
def __init__(self, session: AsyncSession) -> None:
super().__init__()
self.session = session

async def insert(self, id):
raise NotImplementedError

async def select(
self,
player_name: str,
player_id: int,
label_id: int,
many: bool,
limit: int,
history: bool = False,
) -> list[dict]:
table = (
aliased(ScraperData, name="sd")
if history
else aliased(ScraperDataLatest, name="sdl")
)
player = aliased(Player, name="pl")

sql = Select(table)
sql = sql.join(player, table.player_id == player.id)

if player_id:
if many:
sql = sql.where(table.player_id >= player_id)
else:
sql = sql.where(table.player_id == player_id)

if player_name:
sql = sql.where(player.name == player_name)

if label_id:
sql = sql.where(player.label_id == label_id)

sql = sql.order_by(player.id.asc())
sql = sql.limit(limit)

async with self.session:
result: AsyncResult = await self.session.execute(sql)
result = result.scalars().all()
return jsonable_encoder(result)

async def select_history(self, player_name: str, player_id: int, many: bool):
table = ScraperData
sql = Select(table)

if player_id:
if many:
sql = sql.where(table.player_id >= player_id)
else:
sql = sql.where(table.player_id == player_id)

if player_name:
sql = sql.join(Player, table.player_id == Player.id)
sql = sql.where(Player.name == player_name)

async with self.session:
result: AsyncResult = await self.session.execute(sql)
result = result.scalars().all()
return jsonable_encoder(result)

async def update(self):
raise NotImplementedError

async def delete(self):
raise NotImplementedError
from fastapi.encoders import jsonable_encoder
from sqlalchemy.ext.asyncio import AsyncResult, AsyncSession
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import Select

from src.app.repositories.abstract_repo import AbstractAPI
from src.core.database.models import Player, ScraperData, ScraperDataLatest


class ScraperDataRepo(AbstractAPI):
def __init__(self, session: AsyncSession) -> None:
super().__init__()
self.session = session

async def insert(self, id):
raise NotImplementedError

async def select(
self,
player_name: str,
player_id: int,
label_id: int,
many: bool,
limit: int,
history: bool = False,
) -> list[dict]:
table = (
aliased(ScraperData, name="sd")
if history
else aliased(ScraperDataLatest, name="sdl")
)
player = aliased(Player, name="pl")

sql = Select(player.name, table)
sql = sql.join(player, table.player_id == player.id)

if player_id:
if many:
sql = sql.where(table.player_id >= player_id)
else:
sql = sql.where(table.player_id == player_id)

if player_name:
sql = sql.where(player.name == player_name)

if label_id:
sql = sql.where(player.label_id == label_id)

sql = sql.order_by(table.player_id.asc())
# sql = sql.order_by(player.id.asc()) # not performant
sql = sql.limit(limit)

async with self.session:
result: AsyncResult = await self.session.execute(sql)
result = result.fetchall()
data = [{"name": name, **jsonable_encoder(r)} for name, r in result]
return data

async def select_history(self, player_name: str, player_id: int, many: bool):
table = ScraperData
sql = Select(table)

if player_id:
if many:
sql = sql.where(table.player_id >= player_id)
else:
sql = sql.where(table.player_id == player_id)

if player_name:
sql = sql.join(Player, table.player_id == Player.id)
sql = sql.where(Player.name == player_name)

async with self.session:
result: AsyncResult = await self.session.execute(sql)
result = result.scalars().all()
return jsonable_encoder(result)

async def update(self):
raise NotImplementedError

async def delete(self):
raise NotImplementedError
1 change: 1 addition & 0 deletions src/app/views/response/highscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PlayerHiscoreData(BaseModel):
id: Optional[int] = None
timestamp: datetime = None
ts_date: Optional[date] = None
name: str
Player_id: int
total: int = 0
attack: int = 0
Expand Down
Loading

0 comments on commit d5d3dd4

Please sign in to comment.