Skip to content

Commit

Permalink
Use new tables & columns for stats reads (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui authored Apr 21, 2024
1 parent 3964aee commit 6f14d8f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 64 deletions.
23 changes: 5 additions & 18 deletions app/repositories/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@

from app.common.context import Context

mode_map = {
0: ("users_stats", "std"),
1: ("users_stats", "taiko"),
2: ("users_stats", "ctb"),
3: ("users_stats", "mania"),
4: ("rx_stats", "std"),
5: ("rx_stats", "taiko"),
6: ("rx_stats", "ctb"),
8: ("ap_stats", "std"),
}


class UsersRepo:
READ_PARAMS = """\
`u`.`privileges`, `st`.`country`, `s`.`latest_pp_awarded_{}` AS `latest_pp_awarded`
`u`.`privileges`, `u.`.`country`, `s`.`latest_pp_awarded`
"""

def __init__(self, ctx: Context) -> None:
Expand All @@ -30,15 +19,13 @@ async def fetch_one(
user_id: int,
mode: int,
) -> Mapping[str, Any] | None:
(db_table, mode_name) = mode_map[mode]
read_params = self.READ_PARAMS.format(mode_name)

query = f"""\
SELECT {read_params}
FROM `users` `u` INNER JOIN `{db_table}` `s` INNER JOIN `users_stats` `st` ON `st`.`id` = `s`.`id` ON `u`.`id` = `s`.`id`
WHERE `u`.`id` = :user_id LIMIT 1
SELECT {self.READ_PARAMS}
FROM `users` `u` INNER JOIN `user_stats` `s` ON `u`.`id` = `s`.`user_id`
WHERE `u`.`id` = :user_id AND `s`.`mode` = :mode LIMIT 1
"""
params = {
"user_id": user_id,
"mode": mode,
}
return await self.ctx.db.fetch_one(query, params)
21 changes: 7 additions & 14 deletions app/usecases/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@
from app.models.pp import PPHistory
from app.repositories.pp import PPRepo

mode_map = {
0: ("users_stats", "std"),
1: ("users_stats", "taiko"),
2: ("users_stats", "ctb"),
3: ("users_stats", "mania"),
4: ("rx_stats", "std"),
5: ("rx_stats", "taiko"),
6: ("rx_stats", "ctb"),
8: ("ap_stats", "std"),
}


async def fetch_many(
ctx: Context,
Expand All @@ -40,13 +29,17 @@ async def fetch_current(
user_id: int,
mode: int,
) -> PPCapture | None:
(db_table, mode_name) = mode_map[mode]

params = {
"user_id": user_id,
"mode": mode,
}
current_pp = await ctx.db.fetch_val(
f"SELECT pp_{mode_name} AS pp FROM {db_table} WHERE id = :user_id",
"""
SELECT `pp`
FROM `user_stats`
WHERE `user_id` = :user_id
AND `mode` = :mode
""",
params,
)

Expand Down
11 changes: 0 additions & 11 deletions app/usecases/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
from app.models.user import UserInfo
from app.repositories.user import UsersRepo

mode_map = {
0: ("users_stats", "std"),
1: ("users_stats", "taiko"),
2: ("users_stats", "ctb"),
3: ("users_stats", "mania"),
4: ("rx_stats", "std"),
5: ("rx_stats", "taiko"),
6: ("rx_stats", "ctb"),
8: ("ap_stats", "std"),
}


async def fetch_one(
ctx: Context,
Expand Down
52 changes: 31 additions & 21 deletions app/workers/cronjobs/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@
8: ("autoboard", "std"),
}

db_map = {
0: ("users_stats", "std"),
1: ("users_stats", "taiko"),
2: ("users_stats", "ctb"),
3: ("users_stats", "mania"),
4: ("rx_stats", "std"),
5: ("rx_stats", "taiko"),
6: ("rx_stats", "ctb"),
8: ("ap_stats", "std"),
}


async def fetch_rank(
user_id: int,
Expand Down Expand Up @@ -75,13 +64,17 @@ async def fetch_pp(
user_id: int,
mode: int,
) -> int:
(db_table, mode_name) = db_map[mode]

params = {
"user_id": user_id,
"mode": mode,
}
current_pp = await db.fetch_val(
f"SELECT pp_{mode_name} AS pp FROM {db_table} WHERE id = :user_id",
"""
SELECT `pp`
FROM `user_stats`
WHERE `user_id` = :user_id
AND `mode` = :mode
""",
params,
)

Expand All @@ -97,17 +90,27 @@ async def gather_profile_history(user: Mapping[str, Any]) -> None:

start_time = int(time.time())

for mode in [0, 1, 2, 3, 4, 5, 6, 8]:
(db_table, mode_name) = db_map[mode]
for mode in (0, 1, 2, 3, 4, 5, 6, 8):
latest_pp_awarded = await db.fetch_val(
f"SELECT latest_pp_awarded_{mode_name} FROM {db_table} WHERE id = :user_id",
{"user_id": user_id},
"""
SELECT `latest_pp_awarded`
FROM `user_stats`
WHERE `user_id` = :user_id
AND `mode` = :mode
""",
{"user_id": user_id, "mode": mode},
)
inactive_days = (start_time - latest_pp_awarded) / 60 / 60 / 24

if inactive_days > 60 or not privileges & 1:
ranks = await db.fetch_one(
"SELECT `rank`, `country_rank` FROM `user_profile_history` WHERE `user_id` = :user_id AND `mode` = :mode ORDER BY `captured_at` DESC LIMIT 1",
"""
SELECT `rank`, `country_rank`
FROM `user_profile_history`
WHERE `user_id` = :user_id
AND `mode` = :mode
ORDER BY `captured_at` DESC
""",
{"user_id": user_id, "mode": mode},
)
if not ranks:
Expand All @@ -125,7 +128,11 @@ async def gather_profile_history(user: Mapping[str, Any]) -> None:
continue

await db.execute(
"INSERT INTO `user_profile_history` (`user_id`, `mode`, `pp`, `rank`, `country_rank`, `captured_at`) VALUES (:user_id, :mode, :pp, :rank, :c_rank, :captured_at)",
"""
INSERT INTO `user_profile_history`
(`user_id`, `mode`, `pp`, `rank`, `country_rank`, `captured_at`)
VALUES (:user_id, :mode, :pp, :rank, :c_rank, :captured_at)
""",
{
"user_id": user_id,
"mode": mode,
Expand Down Expand Up @@ -162,7 +169,10 @@ async def async_main() -> int:
await db.connect()

users = await db.fetch_all(
"SELECT u.id, u.privileges, s.country FROM users u INNER JOIN users_stats s ON u.id = s.id",
"""
SELECT `id`, `privileges`, `country`
FROM `users`
""",
)
await asyncio.gather(*[gather_profile_history(user) for user in users])

Expand Down

0 comments on commit 6f14d8f

Please sign in to comment.