feat(knowledge): add batch synchronization
This commit is contained in:
@@ -20,8 +20,9 @@ from app.models.knowledge import (
|
||||
KnowledgeVersion,
|
||||
)
|
||||
from app.schemas.knowledge import (
|
||||
KnowledgeLifecycleRequest,
|
||||
KnowledgeBatchMetadataUpdateRequest,
|
||||
KnowledgeBatchSyncRequest,
|
||||
KnowledgeLifecycleRequest,
|
||||
KnowledgeMetadataUpdateRequest,
|
||||
KnowledgeStatusRequest,
|
||||
)
|
||||
@@ -180,6 +181,79 @@ def sync_jobs(
|
||||
return api_success([_job_dict(item) for item in rows])
|
||||
|
||||
|
||||
@router.post("/knowledge/batch/sync")
|
||||
async def batch_sync(
|
||||
payload: KnowledgeBatchSyncRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: Admin = Depends(get_current_admin),
|
||||
) -> dict:
|
||||
_require_super_admin(current_admin)
|
||||
knowledge_ids = list(dict.fromkeys(payload.knowledgeIds))
|
||||
rows = {
|
||||
item.id: item
|
||||
for item in db.scalars(select(Knowledge).where(Knowledge.id.in_(knowledge_ids))).all()
|
||||
}
|
||||
results: list[dict] = []
|
||||
for knowledge_id in knowledge_ids:
|
||||
knowledge = rows.get(knowledge_id)
|
||||
if knowledge is None:
|
||||
results.append({"knowledgeId": knowledge_id, "ok": False, "error": "知识库不存在"})
|
||||
continue
|
||||
if knowledge.lifecycle_status == "archived":
|
||||
results.append(
|
||||
{
|
||||
"knowledgeId": knowledge_id,
|
||||
"name": knowledge.name,
|
||||
"ok": False,
|
||||
"error": "归档知识库不能同步",
|
||||
}
|
||||
)
|
||||
continue
|
||||
try:
|
||||
job = await KnowledgePipelineService.synchronize(
|
||||
db, knowledge, admin_id=current_admin.id
|
||||
)
|
||||
OperationLogService.write(
|
||||
db,
|
||||
admin_id=current_admin.id,
|
||||
module="knowledge",
|
||||
action="batch_sync",
|
||||
target_id=knowledge.id,
|
||||
)
|
||||
db.commit()
|
||||
results.append(
|
||||
{
|
||||
"knowledgeId": knowledge.id,
|
||||
"name": knowledge.name,
|
||||
"ok": True,
|
||||
"status": job.status,
|
||||
"job": _job_dict(job),
|
||||
}
|
||||
)
|
||||
except Exception as exc:
|
||||
OperationLogService.write(
|
||||
db,
|
||||
admin_id=current_admin.id,
|
||||
module="knowledge",
|
||||
action="batch_sync",
|
||||
target_id=knowledge.id,
|
||||
result="FAILED",
|
||||
)
|
||||
db.commit()
|
||||
results.append(
|
||||
{
|
||||
"knowledgeId": knowledge.id,
|
||||
"name": knowledge.name,
|
||||
"ok": False,
|
||||
"error": str(exc),
|
||||
}
|
||||
)
|
||||
success = sum(1 for item in results if item["ok"])
|
||||
return api_success(
|
||||
{"total": len(results), "success": success, "failed": len(results) - success, "results": results}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/knowledge/{knowledge_id}/sync")
|
||||
async def synchronize(
|
||||
knowledge_id: int,
|
||||
|
||||
Reference in New Issue
Block a user