fix: 所有API函数加try/except防止get_sor_context异常返回None

This commit is contained in:
yumoqing 2026-07-21 18:05:11 +08:00
parent abc457d547
commit e0399c4178

View File

@ -82,103 +82,133 @@ async def api_provider_roi(sor, params_kw=None):
# ── 运营指标 API ── # ── 运营指标 API ──
async def api_stats(request): async def api_stats(request):
env = request._run_ns try:
async with get_sor_context(env, MODULE_NAME) as sor: env = request._run_ns
return { async with get_sor_context(env, MODULE_NAME) as sor:
'today_usage': await get_today_usage(env, sor), return {
'today_amount': await get_today_amount(env, sor), 'today_usage': await get_today_usage(env, sor),
'success_rate': await get_success_rate(env, sor), 'today_amount': await get_today_amount(env, sor),
'fail_count': await get_fail_count(env, sor), 'success_rate': await get_success_rate(env, sor),
'active_users': await get_active_users(env, sor), 'fail_count': await get_fail_count(env, sor),
} 'active_users': await get_active_users(env, sor),
}
except Exception:
return {}
async def api_top_models(request): async def api_top_models(request):
env = request._run_ns try:
async with get_sor_context(env, MODULE_NAME) as sor: env = request._run_ns
return await get_top_models(env, sor) async with get_sor_context(env, MODULE_NAME) as sor:
return await get_top_models(env, sor)
except Exception:
return []
async def api_top_providers(request): async def api_top_providers(request):
env = request._run_ns try:
async with get_sor_context(env, MODULE_NAME) as sor: env = request._run_ns
return await get_top_providers(env, sor) async with get_sor_context(env, MODULE_NAME) as sor:
return await get_top_providers(env, sor)
except Exception:
return []
async def api_top_users(request): async def api_top_users(request):
env = request._run_ns try:
async with get_sor_context(env, MODULE_NAME) as sor: env = request._run_ns
return await get_top_users(env, sor) async with get_sor_context(env, MODULE_NAME) as sor:
return await get_top_users(env, sor)
except Exception:
return []
async def api_customer_models(request): async def api_customer_models(request):
env = request._run_ns try:
userorgid = await env.get_userorgid() env = request._run_ns
async with get_sor_context(env, MODULE_NAME) as sor: userorgid = await env.get_userorgid()
return { async with get_sor_context(env, MODULE_NAME) as sor:
'daily': await get_customer_daily_models(env, sor, userorgid), return {
'monthly': await get_customer_monthly_models(env, sor, userorgid), 'daily': await get_customer_daily_models(env, sor, userorgid),
} 'monthly': await get_customer_monthly_models(env, sor, userorgid),
}
except Exception:
return {'daily': [], 'monthly': []}
async def api_daily_trend(request): async def api_daily_trend(request):
env = request._run_ns try:
async with get_sor_context(env, MODULE_NAME) as sor: env = request._run_ns
return await get_daily_trend(env, sor) async with get_sor_context(env, MODULE_NAME) as sor:
return await get_daily_trend(env, sor)
except Exception:
return []
async def api_hourly_concurrency(request): async def api_hourly_concurrency(request):
env = request._run_ns try:
async with get_sor_context(env, MODULE_NAME) as sor: env = request._run_ns
return await get_hourly_concurrency(env, sor) async with get_sor_context(env, MODULE_NAME) as sor:
return await get_hourly_concurrency(env, sor)
except Exception:
return []
async def api_currency_stats(request): async def api_currency_stats(request):
env = request._run_ns try:
async with get_sor_context(env, MODULE_NAME) as sor: env = request._run_ns
return await get_currency_breakdown(env, sor) async with get_sor_context(env, MODULE_NAME) as sor:
return await get_currency_breakdown(env, sor)
except Exception:
return []
# ── 实时指标 API角色感知── # ── 实时指标 API角色感知──
async def api_realtime_cards(request): async def api_realtime_cards(request):
"""实时统计卡片 — /sage_datamart/api/realtime_cards.dspy""" """实时统计卡片 — /sage_datamart/api/realtime_cards.dspy"""
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 { if scope == DISTRIBUTOR:
'scope': scope, return {
'calls': await get_realtime_calls(sor, today, scope, org_filter), 'scope': scope,
'amount': await get_realtime_amount(sor, today, scope, org_filter), 'calls': await get_realtime_calls(sor, today, scope, org_filter),
'total_users': await get_distributor_customers_count(sor, org_filter), 'amount': await get_realtime_amount(sor, today, scope, org_filter),
'active_customers': await get_distributor_active_customers(sor, today, org_filter), 'total_users': await get_distributor_customers_count(sor, org_filter),
'online_users': await get_online_users(env), 'active_customers': await get_distributor_active_customers(sor, today, org_filter),
'success_rate': await get_scoped_success_rate(sor, today, scope, org_filter), 'online_users': await get_online_users(env),
'fail_count': await get_scoped_fail_count(sor, today, scope, org_filter), 'success_rate': await get_scoped_success_rate(sor, today, scope, org_filter),
} 'fail_count': await get_scoped_fail_count(sor, today, scope, org_filter),
else: }
return { else:
'scope': scope, return {
'calls': await get_realtime_calls(sor, today, scope, org_filter), 'scope': scope,
'amount': await get_realtime_amount(sor, today, scope, org_filter), 'calls': await get_realtime_calls(sor, today, scope, org_filter),
'total_users': await get_total_users_count(sor, scope, org_filter), 'amount': await get_realtime_amount(sor, today, scope, org_filter),
'active_users': await get_realtime_active_users(sor, today, scope, org_filter), 'total_users': await get_total_users_count(sor, scope, org_filter),
'online_users': await get_online_users(env), 'active_users': await get_realtime_active_users(sor, today, scope, org_filter),
'success_rate': await get_scoped_success_rate(sor, today, scope, org_filter), 'online_users': await get_online_users(env),
'fail_count': await get_scoped_fail_count(sor, today, scope, org_filter), 'success_rate': await get_scoped_success_rate(sor, today, scope, org_filter),
} 'fail_count': await get_scoped_fail_count(sor, today, scope, org_filter),
}
except Exception:
return {}
# ── 分销商专用 API ── # ── 分销商专用 API ──
async def api_distributor_customers(request): async def api_distributor_customers(request):
"""分销商客户排行 — /sage_datamart/api/distributor_customers.dspy""" """分销商客户排行 — /sage_datamart/api/distributor_customers.dspy"""
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)
return await get_distributor_top_customers(sor, org_filter) async with get_sor_context(env, MODULE_NAME) as sor:
return await get_distributor_top_customers(sor, org_filter)
except Exception:
return []
# ── Jinja2 模板函数(实时,角色感知)── # ── Jinja2 模板函数(实时,角色感知)──