后台概览页面修改
This commit is contained in:
@@ -34,6 +34,9 @@ from app.schemas.admin import (
|
||||
SystemConfigOut,
|
||||
AdminUserOut,
|
||||
AdminStatsOut,
|
||||
DailyCreditOut,
|
||||
TeamCreditOut,
|
||||
ModelUsageOut,
|
||||
CreateUserRequest,
|
||||
UpdateMenusRequest,
|
||||
ResetPasswordRequest,
|
||||
@@ -1853,6 +1856,66 @@ async def get_stats(
|
||||
)
|
||||
)).scalar() or 0
|
||||
|
||||
# ── 每日各模块积分消耗(按日期+模块分组)
|
||||
daily_credits_rows = (await db.execute(
|
||||
select(
|
||||
func.to_char(CreditRecord.created_at, 'YYYY-MM-DD').label('date'),
|
||||
func.coalesce(CreditRecord.source_module, 'other').label('module'),
|
||||
func.coalesce(func.sum(func.abs(CreditRecord.amount)), 0).label('credits'),
|
||||
)
|
||||
.where(
|
||||
CreditRecord.type == "consume",
|
||||
CreditRecord.created_at >= date_start,
|
||||
CreditRecord.created_at <= date_end,
|
||||
)
|
||||
.group_by(func.to_char(CreditRecord.created_at, 'YYYY-MM-DD'), func.coalesce(CreditRecord.source_module, 'other'))
|
||||
.order_by(func.to_char(CreditRecord.created_at, 'YYYY-MM-DD'))
|
||||
)).all()
|
||||
daily_credits_by_module = [
|
||||
DailyCreditOut(date=row.date, module=row.module, credits=float(row.credits or 0))
|
||||
for row in daily_credits_rows
|
||||
]
|
||||
|
||||
# ── 各团队积分消耗(有团队 vs 无团队,使用流水中的团队快照)
|
||||
team_credit_rows = (await db.execute(
|
||||
select(
|
||||
func.coalesce(CreditRecord.team_name_snapshot, '未分配团队').label('team_name'),
|
||||
CreditRecord.team_id_snapshot.label('team_id'),
|
||||
func.coalesce(func.sum(func.abs(CreditRecord.amount)), 0).label('credits'),
|
||||
)
|
||||
.where(
|
||||
CreditRecord.type == "consume",
|
||||
CreditRecord.created_at >= date_start,
|
||||
CreditRecord.created_at <= date_end,
|
||||
)
|
||||
.group_by(CreditRecord.team_id_snapshot, CreditRecord.team_name_snapshot)
|
||||
.order_by(func.coalesce(func.sum(func.abs(CreditRecord.amount)), 0).desc())
|
||||
)).all()
|
||||
credits_by_team = [
|
||||
TeamCreditOut(team_name=row.team_name, team_id=row.team_id, credits=float(row.credits or 0))
|
||||
for row in team_credit_rows
|
||||
]
|
||||
|
||||
# ── 各模型使用次数(通过 engine 快照字段统计)
|
||||
model_usage_rows = (await db.execute(
|
||||
select(
|
||||
func.coalesce(CreditRecord.engine_name, '未知').label('model_name'),
|
||||
func.coalesce(CreditRecord.engine_provider, 'unknown').label('provider'),
|
||||
func.count(CreditRecord.id).label('count'),
|
||||
)
|
||||
.where(
|
||||
CreditRecord.type == "consume",
|
||||
CreditRecord.created_at >= date_start,
|
||||
CreditRecord.created_at <= date_end,
|
||||
)
|
||||
.group_by(CreditRecord.engine_name, CreditRecord.engine_provider)
|
||||
.order_by(func.count(CreditRecord.id).desc())
|
||||
)).all()
|
||||
model_usage = [
|
||||
ModelUsageOut(model_name=row.model_name, provider=row.provider, count=int(row.count or 0))
|
||||
for row in model_usage_rows
|
||||
]
|
||||
|
||||
return AdminStatsOut(
|
||||
total_users=total_users,
|
||||
total_projects=total_projects,
|
||||
@@ -1868,6 +1931,9 @@ async def get_stats(
|
||||
last_period_records=last_period_records,
|
||||
last_period_revenue=float(last_period_revenue),
|
||||
last_period_credits_consumed=float(last_period_credits_consumed),
|
||||
daily_credits_by_module=daily_credits_by_module,
|
||||
credits_by_team=credits_by_team,
|
||||
model_usage=model_usage,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user