Compute current streaks for statistics

This commit is contained in:
Maximilian Keßler 2023-11-24 13:12:41 +01:00
parent 9ba98a1f47
commit 94d698ad14
Signed by: max
GPG Key ID: BCC5A619923C0BA5
2 changed files with 8 additions and 3 deletions

View File

@ -40,3 +40,6 @@ FORBIDDEN_GAME_OPTIONS = [
, "allOrNothing" , "allOrNothing"
, "detrimentalCharacters" , "detrimentalCharacters"
] ]
# Cache time in secodn
USER_HISTORY_CACHE_TIME = 5

View File

@ -180,12 +180,14 @@ def update_user_statistics():
# row ranging over all games, where we grouped by user id and rating type (Clue Starved/Non-CS currently) # row ranging over all games, where we grouped by user id and rating type (Clue Starved/Non-CS currently)
# Finally, we just wrap the computed data into an insert statement to directly store it in the statistics table # Finally, we just wrap the computed data into an insert statement to directly store it in the statistics table
cur.execute( cur.execute(
"INSERT INTO user_statistics (user_id, variant_type, maximum_streak)" "INSERT INTO user_statistics (user_id, variant_type, maximum_streak, current_streak)"
" (" " ("
" SELECT" " SELECT"
" user_id," " user_id,"
" CASE WHEN clue_starved THEN %s ELSE %s END," " CASE WHEN clue_starved THEN %s ELSE %s END,"
" MAX(streak_length) AS max_streak_length FROM" " MAX(streak_length) AS max_streak_length,"
" (array_agg(streak_length ORDER BY league_id DESC))[1]"
" FROM"
" (" " ("
" SELECT" " SELECT"
" *," " *,"
@ -224,7 +226,7 @@ def update_user_statistics():
" GROUP BY user_id, clue_starved" " GROUP BY user_id, clue_starved"
" )" " )"
"ON CONFLICT (user_id, variant_type) DO UPDATE " "ON CONFLICT (user_id, variant_type) DO UPDATE "
"SET maximum_streak = EXCLUDED.maximum_streak", "SET (maximum_streak, current_streak) = (EXCLUDED.maximum_streak, EXCLUDED.current_streak)",
(utils.get_rating_type(True), utils.get_rating_type(False)) (utils.get_rating_type(True), utils.get_rating_type(False))
) )
conn_manager.get_connection().commit() conn_manager.get_connection().commit()