客户端生成记录接口追加分页参数
This commit is contained in:
@@ -5,7 +5,7 @@ from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, UploadFile, File, status
|
||||
from fastapi.responses import RedirectResponse
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.config import settings
|
||||
@@ -17,6 +17,7 @@ from app.schemas.generation import (
|
||||
OptimizeParams,
|
||||
GenerateParams,
|
||||
GenerationRecordOut,
|
||||
GenerationRecordPageListOut,
|
||||
OptimizeResult,
|
||||
UpdatePromptRequest,
|
||||
GenerationType,
|
||||
@@ -90,32 +91,89 @@ def _record_to_out(record: GenerationRecord, project_name: str) -> GenerationRec
|
||||
)
|
||||
|
||||
|
||||
@router.get("", response_model=list[GenerationRecordOut])
|
||||
@router.get("", response_model=GenerationRecordPageListOut)
|
||||
async def list_records(
|
||||
project_id: str | None = Query(None, alias="project_id"),
|
||||
project_id: str | None = Query(
|
||||
None,
|
||||
alias="project_id",
|
||||
description="查询单个项目的生成记录",
|
||||
),
|
||||
status: str | None = Query(
|
||||
None,
|
||||
description="查询状态,可以不传。prompt_optimized:待生成 | generating:生成中 | failed:失败 | completed:成功",
|
||||
examples=["completed"],
|
||||
),
|
||||
page: int = Query(
|
||||
1,
|
||||
ge=1,
|
||||
description="分页页码,从1开始",
|
||||
examples=[1],
|
||||
),
|
||||
page_size: int = Query(
|
||||
10,
|
||||
ge=1,
|
||||
le=100,
|
||||
description="每页返回的生成记录数量,范围 1~100",
|
||||
examples=[10],
|
||||
),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
allowed_statuses = {
|
||||
"prompt_optimized",
|
||||
"generating",
|
||||
"failed",
|
||||
"completed",
|
||||
}
|
||||
|
||||
if status and status not in allowed_statuses:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="状态参数错误,仅支持:prompt_optimized、generating、failed、completed",
|
||||
)
|
||||
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
conditions = [
|
||||
GenerationRecord.user_id == current_user.id,
|
||||
GenerationRecord.deleted_at.is_(None),
|
||||
Project.deleted_at.is_(None),
|
||||
]
|
||||
|
||||
if project_id:
|
||||
conditions.append(GenerationRecord.project_id == project_id)
|
||||
|
||||
if status:
|
||||
conditions.append(GenerationRecord.status == status)
|
||||
|
||||
total_result = await db.execute(
|
||||
select(func.count(GenerationRecord.id))
|
||||
.join(Project, GenerationRecord.project_id == Project.id)
|
||||
.where(*conditions)
|
||||
)
|
||||
total = total_result.scalar_one() or 0
|
||||
|
||||
query = (
|
||||
select(GenerationRecord, Project.name)
|
||||
.join(Project, GenerationRecord.project_id == Project.id)
|
||||
.where(
|
||||
GenerationRecord.user_id == current_user.id,
|
||||
GenerationRecord.deleted_at.is_(None),
|
||||
Project.deleted_at.is_(None),
|
||||
)
|
||||
.where(*conditions)
|
||||
.order_by(GenerationRecord.created_at.desc())
|
||||
.offset(offset)
|
||||
.limit(page_size)
|
||||
)
|
||||
if project_id:
|
||||
query = query.where(GenerationRecord.project_id == project_id)
|
||||
|
||||
result = await db.execute(query)
|
||||
rows = result.all()
|
||||
return [
|
||||
_record_to_out(record, project_name)
|
||||
for record, project_name in rows
|
||||
]
|
||||
|
||||
return {
|
||||
"total": int(total),
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"items": [
|
||||
_record_to_out(record, project_name)
|
||||
for record, project_name in rows
|
||||
],
|
||||
}
|
||||
|
||||
@router.post("/optimize", response_model=OptimizeResult)
|
||||
async def optimize(
|
||||
|
||||
Reference in New Issue
Block a user