From 8e675c207d6654f964549e4bb033634d577868f3 Mon Sep 17 00:00:00 2001 From: Admin Date: Sat, 24 Jan 2026 18:19:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20/api/stats/daily?= =?UTF-8?q?=20=E6=AF=8F=E6=97=A5=E7=BB=9F=E8=AE=A1=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/routers/stats.py | 41 +++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/backend/app/routers/stats.py b/backend/app/routers/stats.py index 7d50965..3df2177 100644 --- a/backend/app/routers/stats.py +++ b/backend/app/routers/stats.py @@ -7,7 +7,7 @@ from sqlalchemy import func from ..database import get_db from ..config import get_settings -from ..models.stats import AICallEvent +from ..models.stats import AICallEvent, TenantUsageDaily from ..schemas.stats import AICallEventCreate, AICallEventResponse, BatchReportRequest from ..services.auth import decode_token @@ -131,3 +131,42 @@ async def get_stats_trend( current += timedelta(days=1) return {"trend": trend} + + +@router.get("/daily") +async def get_daily_stats( + tenant_id: Optional[str] = None, + app_code: Optional[str] = None, + start_date: Optional[str] = None, + end_date: Optional[str] = None, + db: Session = Depends(get_db), + user = Depends(get_current_user_optional) +): + """获取每日统计数据""" + query = db.query(TenantUsageDaily) + + if tenant_id: + query = query.filter(TenantUsageDaily.tenant_id == tenant_id) + if app_code: + query = query.filter(TenantUsageDaily.app_code == app_code) + if start_date: + query = query.filter(TenantUsageDaily.stat_date >= start_date) + if end_date: + query = query.filter(TenantUsageDaily.stat_date <= end_date) + + results = query.order_by(TenantUsageDaily.stat_date.desc()).all() + + return { + "items": [ + { + "id": r.id, + "tenant_id": r.tenant_id, + "app_code": r.app_code, + "stat_date": str(r.stat_date) if r.stat_date else None, + "ai_calls": r.ai_calls or 0, + "ai_tokens": r.ai_tokens or 0, + "ai_cost": float(r.ai_cost) if r.ai_cost else 0.0 + } + for r in results + ] + }