From da66420eaa589af323c57f0fae7572556026b74a Mon Sep 17 00:00:00 2001 From: MingxuanGame Date: Fri, 22 Aug 2025 11:16:08 +0000 Subject: [PATCH] refactor(recalculate): make it a tool --- main.py | 7 ------- {app/service => tools}/recalculate.py | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 10 deletions(-) rename {app/service => tools}/recalculate.py (96%) diff --git a/main.py b/main.py index c6e414b..cecbc36 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,7 @@ from __future__ import annotations -import asyncio from contextlib import asynccontextmanager from datetime import datetime -import os from pathlib import Path from app.config import settings @@ -38,7 +36,6 @@ from app.service.init_geoip import init_geoip from app.service.load_achievements import load_achievements from app.service.online_status_maintenance import schedule_online_status_maintenance from app.service.osu_rx_statistics import create_rx_statistics -from app.service.recalculate import recalculate from app.service.redis_message_system import redis_message_system from app.service.stats_scheduler import start_stats_scheduler, stop_stats_scheduler from app.utils import bg_tasks @@ -188,10 +185,6 @@ if settings.osu_web_client_secret == "your_osu_web_client_secret_here": if __name__ == "__main__": import uvicorn - if os.environ.get("RECALCULATE", "false").lower() == "true": - asyncio.run(recalculate()) - exit() - uvicorn.run( "main:app", host=settings.host, diff --git a/app/service/recalculate.py b/tools/recalculate.py similarity index 96% rename from app/service/recalculate.py rename to tools/recalculate.py index 0112bd2..bd897e8 100644 --- a/app/service/recalculate.py +++ b/tools/recalculate.py @@ -2,13 +2,17 @@ from __future__ import annotations import asyncio import math +import os +import sys + +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from app.calculator import ( + calculate_pp, calculate_score_to_level, calculate_weighted_acc, calculate_weighted_pp, clamp, - pre_fetch_and_calculate_pp, ) from app.config import settings from app.const import BANCHOBOT_ID @@ -64,6 +68,7 @@ async def recalculate(): await session.commit() logger.success(f"Recalculated for mode: {mode}, total users: {len(statistics_list)}") + await engine.dispose() async def _recalculate_pp( @@ -99,8 +104,8 @@ async def _recalculate_pp( score.pp = 0 return try: - pp = await pre_fetch_and_calculate_pp(score, beatmap_id, session, redis, fetcher) - score.pp = pp + beatmap_raw = await fetcher.get_or_fetch_beatmap_raw(redis, beatmap_id) + pp = await calculate_pp(score, beatmap_raw, session) if pp == 0: return if score.beatmap_id not in prev or prev[score.beatmap_id].pp < pp: @@ -280,3 +285,7 @@ async def _recalculate_statistics(statistics: UserStatistics, session: AsyncSess case Rank.A: statistics.grade_a -= 1 statistics.level_current = calculate_score_to_level(statistics.total_score) + + +if __name__ == "__main__": + asyncio.run(recalculate())