diff --git a/web/backend/routers/tasks.py b/web/backend/routers/tasks.py index 2a0e732..02df263 100644 --- a/web/backend/routers/tasks.py +++ b/web/backend/routers/tasks.py @@ -112,6 +112,29 @@ async def get_task( return task +@router.delete("/{task_id}") +async def delete_task( + task_id: str, + current_user: dict = Depends(get_current_user), +): + """Delete a single task by ID.""" + loop = asyncio.get_event_loop() + deleted = await loop.run_in_executor(None, lambda: db_schema.delete_task(task_id)) + if not deleted: + raise HTTPException(status_code=404, detail="任务不存在") + return {"message": "已删除"} + + +@router.delete("") +async def clear_all_tasks( + current_user: dict = Depends(get_current_user), +): + """Clear all task history records.""" + loop = asyncio.get_event_loop() + count = await loop.run_in_executor(None, db_schema.clear_task_history) + return {"message": f"已清除 {count} 条记录", "count": count} + + @router.post("/{task_id}/retry") async def retry_task( task_id: str, diff --git a/web/backend/services/db_schema.py b/web/backend/services/db_schema.py index ab1bd14..b1aa62f 100644 --- a/web/backend/services/db_schema.py +++ b/web/backend/services/db_schema.py @@ -333,6 +333,28 @@ def query_task_stats() -> dict: conn.close() +def delete_task(task_id: str) -> bool: + """Delete a single task by ID. Returns True if deleted.""" + conn = sqlite3.connect(_db_path) + try: + cur = conn.execute("DELETE FROM task_history WHERE id = ?", (task_id,)) + conn.commit() + return cur.rowcount > 0 + finally: + conn.close() + + +def clear_task_history() -> int: + """Delete all task history records. Returns number of deleted rows.""" + conn = sqlite3.connect(_db_path) + try: + cur = conn.execute("DELETE FROM task_history") + conn.commit() + return cur.rowcount + finally: + conn.close() + + # --------------------------------------------------------------------------- # Query functions — File metadata # --------------------------------------------------------------------------- diff --git a/web/frontend/src/views/Tasks.vue b/web/frontend/src/views/Tasks.vue index fb60752..684f29c 100644 --- a/web/frontend/src/views/Tasks.vue +++ b/web/frontend/src/views/Tasks.vue @@ -62,6 +62,7 @@ 刷新 + 清除全部 @@ -88,10 +89,11 @@ {{ formatTime(row.created_at) }} - + @@ -137,7 +139,7 @@