import json from common.db.base import execute, fetchall async def upsert_market_status(analysis_run_id: str, analysis_type: str, status: str) -> None: await execute( "INSERT INTO market_analysis (analysis_run_id, analysis_type, status)" " VALUES (%s, %s, %s)" " ON DUPLICATE KEY UPDATE status = VALUES(status)", (analysis_run_id, analysis_type, status), ) async def upsert_market_result(analysis_run_id: str, analysis_type: str, data: dict) -> None: await execute( "INSERT INTO market_analysis (analysis_run_id, analysis_type, status, data)" " VALUES (%s, %s, 'done', %s)" " ON DUPLICATE KEY UPDATE status = 'done', data = VALUES(data)", (analysis_run_id, analysis_type, json.dumps(data, ensure_ascii=False)), ) async def select_market(analysis_run_id: str) -> dict: rows = await fetchall( "SELECT analysis_type, data FROM market_analysis WHERE analysis_run_id = %s AND status = 'done'", (analysis_run_id,), ) return { row["analysis_type"]: json.loads(row["data"]) if isinstance(row["data"], str) else row["data"] for row in rows }