diff --git a/routes/admin_observability_routes.py b/routes/admin_observability_routes.py index 4fa609f..c44e733 100644 --- a/routes/admin_observability_routes.py +++ b/routes/admin_observability_routes.py @@ -1575,6 +1575,17 @@ def budget_dashboard(): 'cost': float(r[2] or 0), }) + # Phase 55 S-3: 當月各 provider cost 分布(給圓餅圖用) + provider_cost_month = session.execute( + sa_text(""" + SELECT provider, COALESCE(SUM(cost_usd), 0) AS cost + FROM ai_calls + WHERE called_at >= :ms AND cost_usd > 0 + GROUP BY provider ORDER BY cost DESC + """), + {'ms': month_start}, + ).fetchall() + # Phase 47 K-3: top 5 cost-burning caller (當月) top_cost_callers = session.execute( sa_text(""" @@ -1643,6 +1654,10 @@ def budget_dashboard(): } for r in top_cost_callers ], + provider_cost_month=[ + {'provider': r[0], 'cost': float(r[1] or 0)} + for r in provider_cost_month + ], price_rec_7d=[ { 'strategy': r[0], 'count': int(r[1] or 0), @@ -1656,6 +1671,7 @@ def budget_dashboard(): return render_template('admin/budget.html', active_page='obs_budget', rows=[], budget_strategies=[], cost_trend_30d=[], top_cost_callers=[], price_rec_7d=[], + provider_cost_month=[], error=f'查詢失敗: {type(e).__name__}: {str(e)[:200]}') finally: session.close() diff --git a/templates/admin/budget.html b/templates/admin/budget.html index 0691a71..6262fb4 100644 --- a/templates/admin/budget.html +++ b/templates/admin/budget.html @@ -99,6 +99,44 @@ + + {% if provider_cost_month %} +
| 供應商 | 成本 | 佔比 |
|---|---|---|
{{ p.provider }} |
+ ${{ "%.4f"|format(p.cost) }} | +{{ "%.1f"|format(p.cost / total_pc * 100) }}% | +
| 總計 | +${{ "%.2f"|format(total_pc) }} | +100% | +