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