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