From 4965b48e9e8be94faa1ece2800768b8972b71a6b Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Sun, 25 Aug 2024 20:32:32 +0200 Subject: [PATCH] Added most bought from designers statistics --- src/classes/statistic_classes.py | 9 ++++++++- src/main.py | 8 +++++++- src/modules/data_connection.py | 2 +- src/modules/statistic_creator.py | 25 +++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/classes/statistic_classes.py b/src/classes/statistic_classes.py index d6bfc03..8a55883 100644 --- a/src/classes/statistic_classes.py +++ b/src/classes/statistic_classes.py @@ -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 } \ No newline at end of file diff --git a/src/main.py b/src/main.py index f09cc89..d071264 100644 --- a/src/main.py +++ b/src/main.py @@ -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 \ No newline at end of file diff --git a/src/modules/data_connection.py b/src/modules/data_connection.py index de32318..fcf8ce4 100644 --- a/src/modules/data_connection.py +++ b/src/modules/data_connection.py @@ -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 diff --git a/src/modules/statistic_creator.py b/src/modules/statistic_creator.py index 205a2ac..3a07f9b 100644 --- a/src/modules/statistic_creator.py +++ b/src/modules/statistic_creator.py @@ -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 \ No newline at end of file