Added most bought from designers statistics
This commit is contained in:
parent
9cd614e3a0
commit
4965b48e9e
4 changed files with 41 additions and 3 deletions
|
|
@ -2,7 +2,7 @@ from pydantic import BaseModel
|
|||
from typing import Union, Dict
|
||||
from datetime import date
|
||||
|
||||
from src.classes import boardgame_classes
|
||||
from src.classes import boardgame_classes, people_classes
|
||||
|
||||
class StatisticBase(BaseModel):
|
||||
name: str
|
||||
|
|
@ -24,6 +24,13 @@ class GamesStatistic(StatisticBase):
|
|||
class TimeLineStatistic(StatisticBase):
|
||||
result: Dict[Union[date, int], Union[int, float]]
|
||||
|
||||
model_config = {
|
||||
'validate_assignment':True
|
||||
}
|
||||
|
||||
class PeopleStatistic(StatisticBase):
|
||||
result: Union[list[people_classes.DesignerPublic], list[people_classes.ArtistPublic]]
|
||||
|
||||
model_config = {
|
||||
'validate_assignment':True
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ def get_collection(query: BoardgameFilterParams = Depends(), session: Session =
|
|||
|
||||
@app.get('/designers', response_model=list[people_classes.DesignerPublic])
|
||||
def get_designers(session: Session = Depends(get_session)):
|
||||
to_return_designers = data_connection.get_designers(session)
|
||||
to_return_designers = data_connection.get_all_designers(session)
|
||||
return to_return_designers
|
||||
|
||||
@app.get("/wishlist", response_model=list[boardgame_classes.BoardGamePublicNoPlays])
|
||||
|
|
@ -235,3 +235,9 @@ def get_winrate_over_time(player_name: str | None = None, day_step: int = 1, ses
|
|||
|
||||
return statistic_to_return
|
||||
|
||||
|
||||
@app.get('/statistics/most_bought_designers', response_model=statistic_classes.PeopleStatistic)
|
||||
def get_most_bought_from_designers(top_amount: int = 10, session=Depends(get_session)):
|
||||
statistics_to_return = statistic_creator.get_most_bought_designers(session, top_amount)
|
||||
|
||||
return statistics_to_return
|
||||
|
|
@ -134,7 +134,7 @@ def get_player(player_name: str, session: Session) -> player_classes.Player:
|
|||
player_from_db = db_connection.get_player(player_name.title(), session)
|
||||
return player_from_db
|
||||
|
||||
def get_designers(session: Session) -> list[people_classes.Designer]:
|
||||
def get_all_designers(session: Session) -> list[people_classes.Designer]:
|
||||
designers_from_db = db_connection.get_all_designers(session)
|
||||
return designers_from_db
|
||||
|
||||
|
|
|
|||
|
|
@ -390,3 +390,28 @@ def get_winrate_over_time(session: Session, player_name: str | None = None, day_
|
|||
return dict_to_return
|
||||
|
||||
|
||||
def get_most_bought_designers(session: Session, top_amount: int= 10) -> statistic_classes.PeopleStatistic:
|
||||
statistic_name = 'Designer most bought from'
|
||||
|
||||
md5hash, cached_statistic = get_from_cache({**locals()})
|
||||
|
||||
if cached_statistic != None:
|
||||
return cached_statistic
|
||||
|
||||
all_designers = data_connection.get_all_designers(session)
|
||||
|
||||
all_designers.sort(key=lambda x: len(x.designed_boardgames), reverse=True)
|
||||
|
||||
top_bought_designers = all_designers[0:top_amount]
|
||||
|
||||
|
||||
statistic_dict = {
|
||||
'name': statistic_name,
|
||||
'result': top_bought_designers
|
||||
}
|
||||
|
||||
statistic_to_return = statistic_classes.PeopleStatistic.model_validate(statistic_dict)
|
||||
|
||||
cached_statistics[md5hash] = statistic_to_return
|
||||
|
||||
return statistic_to_return
|
||||
Loading…
Reference in a new issue