Created timeline statistic

This commit is contained in:
Yarne Coppens 2024-08-11 22:52:15 +02:00
parent e7b9c4d0c1
commit 0abb778536
2 changed files with 38 additions and 5 deletions

View file

@ -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]]
class TimeLineStatistic(StatisticBase):
result: Dict[date, int]

View file

@ -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):