fix: 补全所有j2_*和api_model_perf/api_provider_roi的try/except,彻底消除None返回

This commit is contained in:
yumoqing 2026-07-21 18:15:03 +08:00
parent e0399c4178
commit 0a35be5101

View File

@ -48,35 +48,41 @@ async def _resolve_view(request):
# ── 性能指标 API ──
async def api_model_perf(sor, params_kw=None):
model = params_kw.get('model') if params_kw else None
stat_date = params_kw.get('date') if params_kw else None
sql = "SELECT * FROM dm_model_perf_daily WHERE 1=1"
ns = {}
if stat_date:
sql += " AND stat_date=${stat_date}$"
ns['stat_date'] = stat_date
if model:
sql += " AND model=${model}$"
ns['model'] = model
sql += " ORDER BY total_calls DESC LIMIT 50"
rows = await sor.sqlExe(sql, ns)
return [dict(r) for r in rows]
try:
model = params_kw.get('model') if params_kw else None
stat_date = params_kw.get('date') if params_kw else None
sql = "SELECT * FROM dm_model_perf_daily WHERE 1=1"
ns = {}
if stat_date:
sql += " AND stat_date=${stat_date}$"
ns['stat_date'] = stat_date
if model:
sql += " AND model=${model}$"
ns['model'] = model
sql += " ORDER BY total_calls DESC LIMIT 50"
rows = await sor.sqlExe(sql, ns)
return [dict(r) for r in rows]
except Exception:
return []
async def api_provider_roi(sor, params_kw=None):
model = params_kw.get('model') if params_kw else None
stat_date = params_kw.get('date') if params_kw else None
sql = "SELECT * FROM dm_provider_cost_daily WHERE 1=1"
ns = {}
if stat_date:
sql += " AND stat_date=${stat_date}$"
ns['stat_date'] = stat_date
if model:
sql += " AND model=${model}$"
ns['model'] = model
sql += " ORDER BY total_amount DESC LIMIT 50"
rows = await sor.sqlExe(sql, ns)
return [dict(r) for r in rows]
try:
model = params_kw.get('model') if params_kw else None
stat_date = params_kw.get('date') if params_kw else None
sql = "SELECT * FROM dm_provider_cost_daily WHERE 1=1"
ns = {}
if stat_date:
sql += " AND stat_date=${stat_date}$"
ns['stat_date'] = stat_date
if model:
sql += " AND model=${model}$"
ns['model'] = model
sql += " ORDER BY total_amount DESC LIMIT 50"
rows = await sor.sqlExe(sql, ns)
return [dict(r) for r in rows]
except Exception:
return []
# ── 运营指标 API ──
@ -214,97 +220,136 @@ async def api_distributor_customers(request):
# ── Jinja2 模板函数(实时,角色感知)──
async def j2_realtime_calls(request):
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_realtime_calls(sor, today, scope, org_filter)
try:
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_realtime_calls(sor, today, scope, org_filter)
except Exception:
return 0
async def j2_realtime_amount(request):
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_realtime_amount(sor, today, scope, org_filter)
try:
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_realtime_amount(sor, today, scope, org_filter)
except Exception:
return 0
async def j2_total_users(request):
env = request._run_ns
scope, org_filter = await _resolve_view(request)
async with get_sor_context(env, MODULE_NAME) as sor:
if scope == DISTRIBUTOR:
return await get_distributor_customers_count(sor, org_filter)
return await get_total_users_count(sor, scope, org_filter)
try:
env = request._run_ns
scope, org_filter = await _resolve_view(request)
async with get_sor_context(env, MODULE_NAME) as sor:
if scope == DISTRIBUTOR:
return await get_distributor_customers_count(sor, org_filter)
return await get_total_users_count(sor, scope, org_filter)
except Exception:
return 0
async def j2_active_today(request):
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
if scope == DISTRIBUTOR:
return await get_distributor_active_customers(sor, today, org_filter)
return await get_realtime_active_users(sor, today, scope, org_filter)
try:
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
if scope == DISTRIBUTOR:
return await get_distributor_active_customers(sor, today, org_filter)
return await get_realtime_active_users(sor, today, scope, org_filter)
except Exception:
return 0
async def j2_online_users(request):
env = request._run_ns
return await get_online_users(env)
try:
env = request._run_ns
return await get_online_users(env)
except Exception:
return 0
async def j2_scoped_success_rate(request):
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_scoped_success_rate(sor, today, scope, org_filter)
try:
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_scoped_success_rate(sor, today, scope, org_filter)
except Exception:
return 0
async def j2_scoped_fail_count(request):
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_scoped_fail_count(sor, today, scope, org_filter)
try:
env = request._run_ns
scope, org_filter = await _resolve_view(request)
today = env.curDateString()
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_scoped_fail_count(sor, today, scope, org_filter)
except Exception:
return 0
# ── 向后兼容 Jinja2 函数 ──
async def j2_today_usage(request):
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_today_usage(env, sor)
try:
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_today_usage(env, sor)
except Exception:
return 0
async def j2_today_amount(request):
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_today_amount(env, sor)
try:
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_today_amount(env, sor)
except Exception:
return 0
async def j2_active_users(request):
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_active_users(env, sor)
try:
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_active_users(env, sor)
except Exception:
return 0
async def j2_fail_count(request):
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_fail_count(env, sor)
try:
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_fail_count(env, sor)
except Exception:
return 0
async def j2_success_rate(request):
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_success_rate(env, sor)
try:
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_success_rate(env, sor)
except Exception:
return 0
async def j2_avg_ttft(request):
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_avg_ttft(env, sor)
try:
env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor:
return await get_avg_ttft(env, sor)
except Exception:
return 0
# ── Cron ETL ──