compact DB queries
This commit is contained in:
parent
cc08f032e3
commit
bb3be235d9
1 changed files with 27 additions and 40 deletions
45
src/stats.py
45
src/stats.py
|
@ -85,7 +85,8 @@ def update_user_statistics():
|
||||||
# Note that this will immediately be changed by the next query in case it is nonzero,
|
# Note that this will immediately be changed by the next query in case it is nonzero,
|
||||||
# so the zero value never shows up in the database if it was nonzero before.
|
# so the zero value never shows up in the database if it was nonzero before.
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO user_statistics (user_id, variant_type, total_game_moves, games_played, games_won, current_streak)"
|
"INSERT INTO user_statistics"
|
||||||
|
" (user_id, variant_type, total_game_moves, games_played, games_won, current_streak)"
|
||||||
" ("
|
" ("
|
||||||
" SELECT id, %s, 0, 0, 0, 0 FROM users"
|
" SELECT id, %s, 0, 0, 0, 0 FROM users"
|
||||||
" )"
|
" )"
|
||||||
|
@ -97,43 +98,29 @@ def update_user_statistics():
|
||||||
(rating_type,)
|
(rating_type,)
|
||||||
)
|
)
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO user_statistics (user_id, variant_type, total_game_moves)"
|
"INSERT INTO user_statistics (user_id, variant_type, total_game_moves, games_played, games_won)"
|
||||||
" ("
|
" ("
|
||||||
" SELECT users.id, %s, SUM(games.num_turns) FROM users "
|
" SELECT"
|
||||||
|
" users.id,"
|
||||||
|
" CASE WHEN clue_starved THEN %s ELSE %s END,"
|
||||||
|
" SUM(games.num_turns),"
|
||||||
|
" COUNT(*),"
|
||||||
|
" COUNT(*) FILTER ( WHERE variants.num_suits * 5 = games.score )"
|
||||||
|
"FROM users"
|
||||||
" INNER JOIN game_participants "
|
" INNER JOIN game_participants "
|
||||||
" ON game_participants.user_id = users.id "
|
" ON game_participants.user_id = users.id "
|
||||||
" INNER JOIN games "
|
" INNER JOIN games "
|
||||||
" ON game_participants.game_id = games.id "
|
" ON game_participants.game_id = games.id "
|
||||||
" INNER JOIN variants"
|
" INNER JOIN variants"
|
||||||
" ON variants.id = games.variant_id "
|
" ON variants.id = games.variant_id "
|
||||||
" WHERE variants.clue_starved = %s"
|
" GROUP BY users.id, clue_starved "
|
||||||
" GROUP BY users.id "
|
|
||||||
" ) "
|
" ) "
|
||||||
"ON CONFLICT (user_id, variant_type) DO UPDATE "
|
"ON CONFLICT (user_id, variant_type) DO UPDATE "
|
||||||
"SET total_game_moves = EXCLUDED.total_game_moves",
|
"SET"
|
||||||
(rating_type, clue_starved)
|
" (total_game_moves, games_played, games_won)"
|
||||||
)
|
" ="
|
||||||
cur.execute(
|
" (EXCLUDED.total_game_moves, EXCLUDED.games_played, EXCLUDED.games_won)",
|
||||||
"INSERT INTO user_statistics (user_id, variant_type, games_played, games_won)"
|
(utils.get_rating_type(True), utils.get_rating_type(False))
|
||||||
" ("
|
|
||||||
" SELECT"
|
|
||||||
" users.id,"
|
|
||||||
" %s,"
|
|
||||||
" COUNT(games.id),"
|
|
||||||
" COUNT(games.id) FILTER ( WHERE variants.num_suits * 5 = games.score )"
|
|
||||||
" FROM users "
|
|
||||||
" INNER JOIN game_participants "
|
|
||||||
" ON game_participants.user_id = users.id "
|
|
||||||
" INNER JOIN games "
|
|
||||||
" ON game_participants.game_id = games.id "
|
|
||||||
" INNER JOIN variants "
|
|
||||||
" ON variants.id = games.variant_id "
|
|
||||||
" WHERE variants.clue_starved = %s"
|
|
||||||
" GROUP BY users.id"
|
|
||||||
" )"
|
|
||||||
"ON CONFLICT (user_id, variant_type) DO UPDATE "
|
|
||||||
"SET (games_played, games_won) = (EXCLUDED.games_played, EXCLUDED.games_won)",
|
|
||||||
(rating_type, clue_starved)
|
|
||||||
)
|
)
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO user_statistics (user_id, variant_type, current_streak)"
|
"INSERT INTO user_statistics (user_id, variant_type, current_streak)"
|
||||||
|
|
Loading…
Reference in a new issue