From 0a35be510187e6eaf46811399d2b5b6f8f36e798 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 21 Jul 2026 18:15:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A1=A5=E5=85=A8=E6=89=80=E6=9C=89j2?= =?UTF-8?q?=5F*=E5=92=8Capi=5Fmodel=5Fperf/api=5Fprovider=5Froi=E7=9A=84tr?= =?UTF-8?q?y/except,=E5=BD=BB=E5=BA=95=E6=B6=88=E9=99=A4None=E8=BF=94?= =?UTF-8?q?=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sage_datamart/init.py | 203 ++++++++++++++++++++++++++---------------- 1 file changed, 124 insertions(+), 79 deletions(-) diff --git a/sage_datamart/init.py b/sage_datamart/init.py index ccde5e6..bb25044 100644 --- a/sage_datamart/init.py +++ b/sage_datamart/init.py @@ -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 ──