修复统计数据 API 中的数据库查询方法,优化性能和准确性

This commit is contained in:
DrSmoothl
2026-03-07 21:18:36 +08:00
parent fe5cab9c41
commit 4565bd94b4

View File

@@ -134,7 +134,7 @@ async def _get_summary_statistics(start_time: datetime, end_time: datetime) -> S
func.sum(col(ModelUsage.total_tokens)).label("total_tokens"), func.sum(col(ModelUsage.total_tokens)).label("total_tokens"),
func.avg(col(ModelUsage.time_cost)).label("avg_response_time"), func.avg(col(ModelUsage.time_cost)).label("avg_response_time"),
).where(col(ModelUsage.timestamp) >= start_time, col(ModelUsage.timestamp) <= end_time) ).where(col(ModelUsage.timestamp) >= start_time, col(ModelUsage.timestamp) <= end_time)
result = session.execute(statement).first() result = session.exec(statement).first()
if result: if result:
total_requests, total_cost, total_tokens, avg_response_time = result total_requests, total_cost, total_tokens, avg_response_time = result
@@ -151,7 +151,7 @@ async def _get_summary_statistics(start_time: datetime, end_time: datetime) -> S
col(OnlineTime.end_timestamp) >= start_time, col(OnlineTime.end_timestamp) >= start_time,
) )
) )
online_records = session.execute(statement).scalars().all() online_records = session.exec(statement).all()
for record in online_records: for record in online_records:
start = max(record.start_timestamp, start_time) start = max(record.start_timestamp, start_time)
@@ -165,7 +165,7 @@ async def _get_summary_statistics(start_time: datetime, end_time: datetime) -> S
col(Messages.timestamp) >= start_time, col(Messages.timestamp) >= start_time,
col(Messages.timestamp) <= end_time, col(Messages.timestamp) <= end_time,
) )
total_messages = session.execute(statement).scalar() total_messages = session.exec(statement).one()
summary.total_messages = int(total_messages or 0) summary.total_messages = int(total_messages or 0)
# 统计回复数量 # 统计回复数量
@@ -175,7 +175,7 @@ async def _get_summary_statistics(start_time: datetime, end_time: datetime) -> S
col(Messages.timestamp) <= end_time, col(Messages.timestamp) <= end_time,
col(Messages.reply_to).is_not(None), col(Messages.reply_to).is_not(None),
) )
total_replies = session.execute(statement).scalar() total_replies = session.exec(statement).one()
summary.total_replies = int(total_replies or 0) summary.total_replies = int(total_replies or 0)
# 计算派生指标 # 计算派生指标
@@ -198,7 +198,7 @@ async def _get_model_statistics(start_time: datetime) -> list[ModelStatistics]:
) )
with get_db_session() as session: with get_db_session() as session:
rows = session.execute(statement).all() rows = session.exec(statement).all()
aggregates: dict[str, dict[str, float | int]] = {} aggregates: dict[str, dict[str, float | int]] = {}
for record in rows: for record in rows:
@@ -257,7 +257,7 @@ async def _get_hourly_statistics(start_time: datetime, end_time: datetime) -> li
) )
with get_db_session() as session: with get_db_session() as session:
rows = session.execute(statement).all() rows = session.exec(statement).all()
# 转换为字典以快速查找 # 转换为字典以快速查找
data_dict = {row[0]: row for row in rows} data_dict = {row[0]: row for row in rows}
@@ -300,7 +300,7 @@ async def _get_daily_statistics(start_time: datetime, end_time: datetime) -> lis
) )
with get_db_session() as session: with get_db_session() as session:
rows = session.execute(statement).all() rows = session.exec(statement).all()
# 转换为字典 # 转换为字典
data_dict = {row[0]: row for row in rows} data_dict = {row[0]: row for row in rows}
@@ -331,7 +331,7 @@ async def _get_recent_activity(limit: int = 10) -> list[dict[str, Any]]:
"""获取最近活动""" """获取最近活动"""
with get_db_session() as session: with get_db_session() as session:
statement = select(ModelUsage).order_by(desc(col(ModelUsage.timestamp))).limit(limit) statement = select(ModelUsage).order_by(desc(col(ModelUsage.timestamp))).limit(limit)
records = session.execute(statement).scalars().all() records = session.exec(statement).all()
activities = [] activities = []
for record in records: for record in records: