diff --git a/src/classes/statistic_classes.py b/src/classes/statistic_classes.py index 7399c0b..11d3418 100644 --- a/src/classes/statistic_classes.py +++ b/src/classes/statistic_classes.py @@ -1,15 +1,21 @@ from pydantic import BaseModel -from typing import Union +from typing import Union, Dict +from datetime import date from src.classes import boardgame_classes -class NumberStatistic(BaseModel): +class StatisticBase(BaseModel): name: str + +class NumberStatistic(StatisticBase): result: float -class GameOrderStatistic(BaseModel): - name: str +class GameOrderStatistic(StatisticBase): result: list[Union[ boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion, boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion, - boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]] \ No newline at end of file + boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]] + + +class TimeLineStatistic(StatisticBase): + result: Dict[date, int] \ No newline at end of file diff --git a/src/main.py b/src/main.py index faa14a5..669f281 100644 --- a/src/main.py +++ b/src/main.py @@ -1,4 +1,5 @@ from typing import Union +from datetime import date, timedelta from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware @@ -76,6 +77,32 @@ def get_amount_of_games(): return statistic_to_return +@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic) +def get_amount_of_games_over_time(): + + def daterange(start_date: date, end_date: date): + days = int((end_date - start_date).days) + for n in range(days): + yield start_date + timedelta(n) + + games_in_owned_collection = data_connection.get_user_owned_collection() + games_in_owned_collection.sort(key=lambda x: x.acquisition_date) + + timeline_dict = {} + + for current_date in daterange(games_in_owned_collection[0].acquisition_date, date.today()): + games_in_collection_at_date = list(filter(lambda game: game.acquisition_date <= current_date, games_in_owned_collection)) + timeline_dict[current_date] = len(games_in_collection_at_date) + + statistic_dict = { + "name":"Amount of games in owned collection over time", + "result":timeline_dict + } + + statistic_to_return = statistic_classes.TimeLineStatistic(**statistic_dict) + + return statistic_to_return + @app.get('/statistics/most_expensive_games', response_model=statistic_classes.GameOrderStatistic) def get_most_expensive_game(top_amount: int = 10):