Fix bug: User ratings not retrieved correctly

This commit is contained in:
Maximilian Keßler 2023-11-23 16:19:10 +01:00
parent f0b658f290
commit 89a38784ed
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -47,15 +47,29 @@ def get_current_user_ratings(user_ids: List[int], rating_type: int) -> Dict[int,
user_ids + [rating_type]
)
base_ratings = cur.fetchall()
# This query is a bit tricky:
# The subclause transforms the user_ratings table into the same table (with lesse columns), except that we now
# group entries corresponding to the same (user_id, type) and replace all of them with just the maximum league id
# Then we can do an inner join with this specific table, where we join again on (user_id, type), but now also
# require that the league id matches the max_league_id column from the subclause-generated table.
# Since an inner join only returns rows that match both tables, this will act as a filter on the initial table,
# even though we do not retrieve any values from the subclause-table
cur.execute("SELECT user_ratings.user_id, value_after FROM user_ratings "
"INNER JOIN ("
" SELECT user_id, type, MAX(league_id) AS max_league_id"
" FROM user_ratings "
" GROUP BY (user_id, type)"
" ) AS latest_user_ratings "
" ON user_ratings.league_id = latest_user_ratings.max_league_id "
"WHERE user_ratings.user_id IN ({}) AND user_ratings.type = %s".format(", ".join("%s" for _ in user_ids)),
user_ids + [rating_type]
" ON"
" user_ratings.league_id = latest_user_ratings.max_league_id"
" AND user_ratings.user_id = latest_user_ratings.user_id"
" AND user_ratings.type = latest_user_ratings.type "
"WHERE "
" user_ratings.user_id IN ({})"
" AND user_ratings.type = %s"
.format(", ".join("%s" for _ in user_ids))
, user_ids + [rating_type]
)
current_ratings = cur.fetchall()
@ -70,13 +84,17 @@ def get_current_user_ratings(user_ids: List[int], rating_type: int) -> Dict[int,
def get_current_variant_rating(variant_id: int, player_count: int) -> float:
cur = conn_manager.get_new_cursor()
# Again, this query is tricky. For explanation, see the corresponding query for the user ratings
cur.execute("SELECT value_after FROM variant_ratings "
"INNER JOIN ("
" SELECT variant_id, player_count, MAX(league_id) AS max_league_id"
" FROM variant_ratings "
" GROUP BY (variant_id, player_count)"
" ) AS latest_variant_ratings "
" ON variant_ratings.league_id = latest_variant_ratings.max_league_id "
" ON"
" variant_ratings.league_id = latest_variant_ratings.max_league_id "
" AND variant_ratings.variant_id = latest_variant_ratings.variant_id "
" AND variant_ratings.player_count = latest_variant_ratings.player_count "
"WHERE variant_ratings.variant_id = %s AND variant_ratings.player_count = %s",
(variant_id, player_count)
)