feat: schedule periodic reports asynchronously
This commit is contained in:
@@ -295,7 +295,7 @@ def generate_user_report(
|
||||
if payload.reportType not in ("weekly", "monthly", "stage"):
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="不支持的报告类型")
|
||||
try:
|
||||
report = PeriodicReportService.generate_for_user(
|
||||
report = PeriodicReportService.enqueue_for_user(
|
||||
db,
|
||||
user=user,
|
||||
report_type=payload.reportType, # type: ignore[arg-type]
|
||||
@@ -305,9 +305,10 @@ def generate_user_report(
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
OperationLogService.write(db, admin_id=current_admin.id, module="user_report", action="generate", target_id=report.id)
|
||||
OperationLogService.write(db, admin_id=current_admin.id, module="user_report", action="enqueue", target_id=report.id)
|
||||
db.commit()
|
||||
return api_success(periodic_report_dict(report))
|
||||
db.refresh(report)
|
||||
return api_success(periodic_report_dict(report), message="报告任务已进入后台队列")
|
||||
|
||||
|
||||
@router.put("/user/{user_id}")
|
||||
|
||||
@@ -62,5 +62,10 @@ def periodic_reports(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> dict:
|
||||
reports = PeriodicReportService.list_user_reports(db, user_id=current_user.id, limit=max(1, min(limit, 50)))
|
||||
reports = PeriodicReportService.list_user_reports(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
limit=max(1, min(limit, 50)),
|
||||
statuses=("success",),
|
||||
)
|
||||
return api_success([periodic_report_dict(item) for item in reports])
|
||||
|
||||
Reference in New Issue
Block a user