refactor(recalculate): make it a tool

This commit is contained in:
MingxuanGame
2025-08-22 11:16:08 +00:00
parent 545fc9e0c6
commit da66420eaa
2 changed files with 12 additions and 10 deletions

View File

@@ -1,9 +1,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from datetime import datetime from datetime import datetime
import os
from pathlib import Path from pathlib import Path
from app.config import settings 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.load_achievements import load_achievements
from app.service.online_status_maintenance import schedule_online_status_maintenance from app.service.online_status_maintenance import schedule_online_status_maintenance
from app.service.osu_rx_statistics import create_rx_statistics 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.redis_message_system import redis_message_system
from app.service.stats_scheduler import start_stats_scheduler, stop_stats_scheduler from app.service.stats_scheduler import start_stats_scheduler, stop_stats_scheduler
from app.utils import bg_tasks 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__": if __name__ == "__main__":
import uvicorn import uvicorn
if os.environ.get("RECALCULATE", "false").lower() == "true":
asyncio.run(recalculate())
exit()
uvicorn.run( uvicorn.run(
"main:app", "main:app",
host=settings.host, host=settings.host,

View File

@@ -2,13 +2,17 @@ from __future__ import annotations
import asyncio import asyncio
import math import math
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from app.calculator import ( from app.calculator import (
calculate_pp,
calculate_score_to_level, calculate_score_to_level,
calculate_weighted_acc, calculate_weighted_acc,
calculate_weighted_pp, calculate_weighted_pp,
clamp, clamp,
pre_fetch_and_calculate_pp,
) )
from app.config import settings from app.config import settings
from app.const import BANCHOBOT_ID from app.const import BANCHOBOT_ID
@@ -64,6 +68,7 @@ async def recalculate():
await session.commit() await session.commit()
logger.success(f"Recalculated for mode: {mode}, total users: {len(statistics_list)}") logger.success(f"Recalculated for mode: {mode}, total users: {len(statistics_list)}")
await engine.dispose()
async def _recalculate_pp( async def _recalculate_pp(
@@ -99,8 +104,8 @@ async def _recalculate_pp(
score.pp = 0 score.pp = 0
return return
try: try:
pp = await pre_fetch_and_calculate_pp(score, beatmap_id, session, redis, fetcher) beatmap_raw = await fetcher.get_or_fetch_beatmap_raw(redis, beatmap_id)
score.pp = pp pp = await calculate_pp(score, beatmap_raw, session)
if pp == 0: if pp == 0:
return return
if score.beatmap_id not in prev or prev[score.beatmap_id].pp < pp: 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: case Rank.A:
statistics.grade_a -= 1 statistics.grade_a -= 1
statistics.level_current = calculate_score_to_level(statistics.total_score) statistics.level_current = calculate_score_to_level(statistics.total_score)
if __name__ == "__main__":
asyncio.run(recalculate())