Added h-index statistic
This commit is contained in:
parent
18ddf5b554
commit
e70481b664
2 changed files with 51 additions and 1 deletions
|
|
@ -185,6 +185,11 @@ def get_players_from_play(play_id: int, session: Session = Depends(get_session))
|
|||
|
||||
return requested_players
|
||||
|
||||
@app.get('/statistics/h_index', response_model=statistic_classes.NumberStatistic)
|
||||
def get_h_index(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||
statistic_to_return = statistic_creator.get_h_index(session, query)
|
||||
return statistic_to_return
|
||||
|
||||
@app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic)
|
||||
def get_amount_of_games(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,52 @@ def get_from_cache(argument_dict: dict):
|
|||
if md5hash in cached_statistics:
|
||||
return md5hash, cached_statistics[md5hash]
|
||||
else:
|
||||
return md5hash, None
|
||||
return md5hash, None
|
||||
|
||||
def get_h_index(session: Session, filtering_query: BoardgameFilterParams) -> statistic_classes.NumberStatistic:
|
||||
|
||||
statistic_name = 'Total h-index'
|
||||
|
||||
md5hash, cached_statistic = get_from_cache({**locals()})
|
||||
|
||||
if cached_statistic != None:
|
||||
return cached_statistic
|
||||
|
||||
owned_collection = data_connection.get_user_owned_collection(session)
|
||||
|
||||
if filtering_query != None:
|
||||
owned_collection = filtering_query.do_filtering(owned_collection)
|
||||
|
||||
#{Amount of plays: how many boardgames that were played this much}
|
||||
amount_of_plays_counter = {}
|
||||
|
||||
for owned_boardgame in owned_collection:
|
||||
times_boardgame_played = len(owned_boardgame.plays)
|
||||
for x in range(1, times_boardgame_played + 1):
|
||||
if x in amount_of_plays_counter:
|
||||
amount_of_plays_counter[x] += 1
|
||||
else:
|
||||
amount_of_plays_counter[x] = 1
|
||||
|
||||
h_index = 0
|
||||
|
||||
for amount_played, games_played_that_amount in amount_of_plays_counter.items():
|
||||
if games_played_that_amount >= amount_played:
|
||||
h_index = amount_played
|
||||
else:
|
||||
break
|
||||
|
||||
statistic_dict = {
|
||||
"name":statistic_name,
|
||||
"result":h_index
|
||||
}
|
||||
|
||||
statistic_to_return = statistic_classes.NumberStatistic.model_validate(statistic_dict)
|
||||
|
||||
cached_statistics[md5hash] = statistic_to_return
|
||||
|
||||
return statistic_to_return
|
||||
|
||||
|
||||
def get_total_owned_games(session: Session, filtering_query: BoardgameFilterParams) -> statistic_classes.NumberStatistic:
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue