refactor: dedupe scheduler logging/runtime, centralize SystemSetting access, fix rankings N+1
Behavior-preserving cleanup (345 tests pass, ruff clean):
- scheduler: replace 62 inline logger.x(json.dumps({...})) calls with a
_log_event helper, and collapse 11 identical _job_runtime dicts into an
_idle_runtime() factory over _JOB_NAMES.
- settings: add app/services/settings_store.py (get_setting/get_value/get_map/
upsert_setting) and route ~13 hand-rolled SystemSetting queries + two
identical _settings_map helpers through it.
- scoring.get_rankings: collapse the per-ticker N+1 (3-4 queries + a commit each)
into 2 bulk reads + a single conditional commit; drop the redundant re-fetch.
Lazy recompute-on-read is preserved. Adds first tests for get_rankings.
Net ~ -245 lines across the touched modules.
This commit is contained in:
@@ -18,9 +18,8 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.config import settings
|
||||
from app.providers.alpaca import AlpacaOHLCVProvider
|
||||
from app.services import settings_store
|
||||
from app.services.admin_service import update_setting
|
||||
from app.models.settings import SystemSetting
|
||||
from sqlalchemy import select
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -105,8 +104,7 @@ async def update_market_regime(db: AsyncSession) -> dict:
|
||||
|
||||
async def get_market_regime(db: AsyncSession) -> dict:
|
||||
"""Return the cached regime (computed by the daily job)."""
|
||||
result = await db.execute(select(SystemSetting).where(SystemSetting.key == KEY_REGIME))
|
||||
setting = result.scalar_one_or_none()
|
||||
setting = await settings_store.get_setting(db, KEY_REGIME)
|
||||
if setting is None:
|
||||
return {"label": "unknown", "benchmark": BENCHMARK, "reason": "not computed yet"}
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user