feat:增加分页功能

This commit is contained in:
2026-09-13 22:21:49 +08:00
parent d1fdf04749
commit 31b3acb226
23 changed files with 179 additions and 71 deletions
+16 -4
View File
@@ -7,6 +7,7 @@ from datetime import datetime
from sqlalchemy import text
from nl2sql.audit import _sanitize
from utils.pagination import normalize_pagination, pagination_result
async def record_job_history(db, result, *, elapsed_ms: float, parameter_summary: dict | None = None) -> None:
@@ -49,9 +50,15 @@ async def record_job_history_safely(db, result, *, elapsed_ms: float, parameter_
return True
async def list_job_history(db, *, page: int = 1, page_size: int = 20, status: str | None = None) -> list[dict]:
async def list_job_history(db, *, page: int = 1, page_size: int = 10, status: str | None = None) -> dict:
"""分页查询任务历史,只返回执行摘要,不返回 detail 明细。"""
page, page_size, offset = normalize_pagination(page, page_size)
conditions = "WHERE (:status IS NULL OR status = :status)"
count_result = await db.execute(
text(f"SELECT COUNT(*) AS total FROM nl2sql_job_history {conditions}"),
{"status": status},
)
total = int(count_result.scalar() or 0)
statement = text(
f"""
SELECT id, job_name, status, attempts, error_type, elapsed_ms, create_time
@@ -65,8 +72,13 @@ async def list_job_history(db, *, page: int = 1, page_size: int = 20, status: st
statement,
{
"status": status,
"limit": max(1, min(page_size, 100)),
"offset": max(0, (page - 1) * page_size),
"limit": page_size,
"offset": offset,
},
)
return [dict(row) for row in result.mappings().all()]
return pagination_result(
[dict(row) for row in result.mappings().all()],
total,
page=page,
page_size=page_size,
)