feat: 添加 /api/stats/daily 每日统计接口
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -7,7 +7,7 @@ from sqlalchemy import func
|
|||||||
|
|
||||||
from ..database import get_db
|
from ..database import get_db
|
||||||
from ..config import get_settings
|
from ..config import get_settings
|
||||||
from ..models.stats import AICallEvent
|
from ..models.stats import AICallEvent, TenantUsageDaily
|
||||||
from ..schemas.stats import AICallEventCreate, AICallEventResponse, BatchReportRequest
|
from ..schemas.stats import AICallEventCreate, AICallEventResponse, BatchReportRequest
|
||||||
from ..services.auth import decode_token
|
from ..services.auth import decode_token
|
||||||
|
|
||||||
@@ -131,3 +131,42 @@ async def get_stats_trend(
|
|||||||
current += timedelta(days=1)
|
current += timedelta(days=1)
|
||||||
|
|
||||||
return {"trend": trend}
|
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
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user