|
37 | 37 |
|
38 | 38 | from app.utils.logging_utils import configure_logging |
39 | 39 | from app.api.models.APIResponseModel import ResponseModel, ErrorResponseModel |
40 | | -from app.database.models import Task, TaskStatus, TaskPriority |
| 40 | +from app.database.models import Task, TaskStatus, TaskPriority, QueryTasksOptionalFilter |
41 | 41 |
|
42 | 42 | router = APIRouter() |
43 | 43 |
|
44 | 44 | # 配置日志记录器 |
45 | 45 | logger = configure_logging(name=__name__) |
46 | 46 |
|
47 | 47 |
|
48 | | -# 在后台创建一个转录任务 | Create a transcription task in the background |
| 48 | +# 创建任务 | Create task |
49 | 49 | @router.post( |
50 | 50 | "/task/create", |
51 | 51 | response_model=ResponseModel, |
52 | 52 | summary="上传媒体文件并且创建一个Whisper转录任务在后台处理 | Upload a media file and create a Whisper transcription task to be processed in the background" |
53 | 53 | ) |
54 | | -async def create_transcription_task( |
| 54 | +async def task_create( |
55 | 55 | request: Request, |
56 | 56 | file: UploadFile = File(..., |
57 | 57 | description="媒体文件(支持的格式:音频和视频,如 MP3, WAV, MP4, MKV 等) / Media file (supported formats: audio and video, e.g., MP3, WAV, MP4, MKV)"), |
@@ -203,86 +203,178 @@ async def create_transcription_task( |
203 | 203 | ) |
204 | 204 |
|
205 | 205 |
|
206 | | -# 获取任务状态 | Get task status |
207 | | -@router.get("/tasks/check", |
208 | | - summary="获取任务状态 / Get task status", |
209 | | - response_model=ResponseModel) |
210 | | -async def get_task_status( |
| 206 | +# 查询任务 | Query task |
| 207 | +@router.post("/tasks/query", |
| 208 | + response_model=ResponseModel, |
| 209 | + summary="查询任务 | Query task" |
| 210 | + ) |
| 211 | +async def task_query( |
211 | 212 | request: Request, |
212 | | - task_id: int = Query(description="任务ID / Task ID") |
| 213 | + params: QueryTasksOptionalFilter |
213 | 214 | ): |
214 | 215 | """ |
215 | 216 | # [中文] |
216 | 217 |
|
217 | 218 | ### 用途说明: |
218 | | - - 获取指定任务的状态信息。 |
| 219 | + - 根据多种筛选条件查询任务列表,包括任务状态、优先级、创建时间、语言、引擎名称等信息。 |
| 220 | + - 该接口适用于分页查询,并且通过 `limit` 和 `offset` 参数控制每页显示的记录数,支持客户端逐页加载数据。 |
219 | 221 |
|
220 | 222 | ### 参数说明: |
221 | | - - `task_id` (int): 任务ID。 |
| 223 | + - `status` (TaskStatus): 筛选任务状态: |
| 224 | + - 例如 'queued'(排队中)或 'processing'(处理中)或 'completed'(已完成) 或 'failed'(失败)。 |
| 225 | + - `priority` (TaskPriority): 筛选任务优先级: |
| 226 | + - 例如 'low'、'normal'、'high'。 |
| 227 | + - `created_after` (str): 创建时间的起始时间,格式为 'YYYY-MM-DDTHH:MM:SS',为空时忽略该条件。 |
| 228 | + - `created_before` (str): 创建时间的结束时间,格式为 'YYYY-MM-DDTHH:MM:SS',为空时忽略该条件。 |
| 229 | + - `language` (str): 任务的语言代码,例如 `zh`或'en'。设置为空字符串 `""` 可以查询所有语言的任务。 |
| 230 | + - `engine_name` (str): 引擎名称,例如 'faster_whisper'或'openai_whisper'。 |
| 231 | + - `has_result` (bool): 指定是否要求任务有结果数据。 |
| 232 | + - `has_error` (bool): 指定是否要求任务有错误信息。 |
| 233 | + - `limit` (int): 每页的记录数量,默认值为20,用户可根据需求自定义每页数量。 |
| 234 | + - `offset` (int): 数据分页的起始位置,默认值为0,后续使用响应中的 `next_offset` 值进行下一页查询。 |
222 | 235 |
|
223 | 236 | ### 返回: |
224 | | - - 返回一个包含任务状态信息的响应,包括任务ID、状态、优先级等信息。 |
| 237 | + - `tasks` (list): 包含满足条件的任务列表,每个任务记录包括任务ID、状态、优先级、创建时间等详细信息。 |
| 238 | + - `total_count` (int): 符合条件的任务总数。 |
| 239 | + - `has_more` (bool): 是否还有更多数据。如果为 `True`,则表示存在下一页数据。 |
| 240 | + - `next_offset` (int): 下次分页请求的偏移量。用户可以通过此值构建下一页查询请求。 |
| 241 | +
|
| 242 | + ### 使用示例: |
| 243 | + - 请求示例: |
| 244 | + ```json |
| 245 | + { |
| 246 | + "status": "completed", |
| 247 | + "priority": "high", |
| 248 | + "created_after": "2024-01-01T00:00:00", |
| 249 | + "created_before": "2024-12-31T23:59:59", |
| 250 | + "language": "", |
| 251 | + "engine_name": "faster_whisper", |
| 252 | + "has_result": true, |
| 253 | + "has_error": false, |
| 254 | + "limit": 10, |
| 255 | + "offset": 0 |
| 256 | + } |
| 257 | + ``` |
| 258 | + - 响应示例: |
| 259 | + ```json |
| 260 | + { |
| 261 | + "code": 200, |
| 262 | + "router": "http://localhost/api/tasks/query", |
| 263 | + "params": { ... }, |
| 264 | + "data": { |
| 265 | + "tasks": [ |
| 266 | + { |
| 267 | + "id": 123, |
| 268 | + "status": "completed", |
| 269 | + "priority": "high", |
| 270 | + "created_at": "2024-05-15T12:34:56", |
| 271 | + "language": "en", |
| 272 | + "engine_name": "faster_whisper", |
| 273 | + "result": {...}, |
| 274 | + "error_message": null |
| 275 | + }, |
| 276 | + ... |
| 277 | + ], |
| 278 | + "total_count": 55, |
| 279 | + "has_more": true, |
| 280 | + "next_offset": 10 |
| 281 | + } |
| 282 | + } |
| 283 | + ``` |
225 | 284 |
|
226 | 285 | ### 错误代码说明: |
227 | | -
|
228 | | - - `404`: 任务未找到,可能是任务ID不存在。 |
229 | | - - `500`: 未知错误。 |
| 286 | + - `500`: 未知错误,通常为内部错误。 |
230 | 287 |
|
231 | 288 | # [English] |
232 | 289 |
|
233 | 290 | ### Purpose: |
234 | | - - Get the status information of the specified task. |
| 291 | + - Query the task list based on multiple filtering conditions, including task status, priority, creation time, language, engine name, etc. |
| 292 | + - This endpoint is suitable for paginated queries, and the number of records displayed per page is controlled by the `limit` and `offset` parameters, supporting clients to load data page by page. |
235 | 293 |
|
236 | 294 | ### Parameters: |
237 | | - - `task_id` (int): Task ID. |
| 295 | + - `status` (TaskStatus): Filter task status: |
| 296 | + - e.g., 'queued' (in the queue), 'processing' (processing), 'completed' (completed), or 'failed' (failed). |
| 297 | + - `priority` (TaskPriority): Filter task priority: |
| 298 | + - e.g., 'low', 'normal', 'high'. |
| 299 | + - `created_after` (str): Start time of creation time, format is 'YYYY-MM-DDTHH:MM:SS', ignore this condition when empty. |
| 300 | + - `created_before` (str): End time of creation time, format is 'YYYY-MM-DDTHH:MM:SS', ignore this condition when empty. |
| 301 | + - `language` (str): Language code of the task, e.g., `zh` or `en`. Set to an empty string `""` to query tasks in all languages. |
| 302 | + - `engine_name` (str): Engine name, e.g., 'faster_whisper' or 'openai_whisper'. |
| 303 | + - `has_result` (bool): Specify whether the task requires result data. |
| 304 | + - `has_error` (bool): Specify whether the task requires error information. |
| 305 | + - `limit` (int): Number of records per page, default is 20, users can customize the number of records per page according to their needs. |
| 306 | + - `offset` (int): Starting position of data pagination, default is 0, use the `next_offset` value in the response for the next page query. |
238 | 307 |
|
239 | 308 | ### Returns: |
240 | | - - Returns a response containing task status information, including task ID, status, priority, etc. |
| 309 | + - `tasks` (list): List of tasks that meet the conditions, each task record includes detailed information such as task ID, status, priority, creation time, etc. |
| 310 | + - `total_count` (int): Total number of tasks that meet the conditions. |
| 311 | + - `has_more` (bool): Whether there is more data. If `True`, it means there is more data on the next page. |
| 312 | + - `next_offset` (int): Offset value for the next page request. Users can use this value to construct the next page query request. |
| 313 | +
|
| 314 | + ### Example: |
| 315 | + - Request example: |
| 316 | + ```json |
| 317 | + { |
| 318 | + "status": "completed", |
| 319 | + "priority": "high", |
| 320 | + "created_after": "2024-01-01T00:00:00", |
| 321 | + "created_before": "2024-12-31T23:59:59", |
| 322 | + "language": "", |
| 323 | + "engine_name": "faster_whisper", |
| 324 | + "has_result": true, |
| 325 | + "has_error": false, |
| 326 | + "limit": 10, |
| 327 | + "offset": 0 |
| 328 | + } |
| 329 | + ``` |
| 330 | + - Response example: |
| 331 | + ```json |
| 332 | + { |
| 333 | + "code": 200, |
| 334 | + "router": "http://localhost/api/tasks/query", |
| 335 | + "params": { ... }, |
| 336 | + "data": { |
| 337 | + "tasks": [ |
| 338 | + { |
| 339 | + "id": 123, |
| 340 | + "status": "completed", |
| 341 | + "priority": "high", |
| 342 | + "created_at": "2024-05-15T12:34:56", |
| 343 | + "language": "en", |
| 344 | + "engine_name": "faster_whisper", |
| 345 | + "result": {...}, |
| 346 | + "error_message": null |
| 347 | + }, |
| 348 | + ... |
| 349 | + ], |
| 350 | + "total_count": 55, |
| 351 | + "has_more": true, |
| 352 | + "next_offset": 10 |
| 353 | + } |
| 354 | + } |
| 355 | + ``` |
241 | 356 |
|
242 | 357 | ### Error Code Description: |
243 | | -
|
244 | | - - `404`: Task not found, possibly because the task ID does not exist. |
245 | | - - `500`: Unknown error. |
| 358 | + - `500`: Unknown error, usually an internal error. |
246 | 359 | """ |
247 | | - try: |
248 | | - async with request.app.state.db_manager.get_session() as session: |
249 | | - task_info = await session.get(Task, task_id) |
250 | | - if not task_info: |
251 | | - raise HTTPException( |
252 | | - status_code=status.HTTP_404_NOT_FOUND, |
253 | | - detail=ErrorResponseModel( |
254 | | - code=status.HTTP_404_NOT_FOUND, |
255 | | - message="Task not found.", |
256 | | - router=str(request.url), |
257 | | - params=dict(request.query_params), |
258 | | - ).dict() |
259 | | - ) |
260 | | - return ResponseModel(code=200, |
261 | | - router=str(request.url), |
262 | | - params=dict(request.query_params), |
263 | | - data=task_info.to_dict()) |
264 | 360 |
|
265 | | - except HTTPException as http_error: |
266 | | - raise http_error |
| 361 | + async with request.app.state.db_manager.get_session() as session: |
| 362 | + result = await request.app.state.db_manager.query_tasks(session, params) |
| 363 | + if result is None: |
| 364 | + raise HTTPException(status_code=500, detail="An error occurred while querying tasks.") |
267 | 365 |
|
268 | | - except Exception as e: |
269 | | - logger.error(f"Unexpected error: {str(e)}") |
270 | | - logger.error(f"Traceback: {traceback.format_exc()}") |
271 | | - raise HTTPException( |
272 | | - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
273 | | - detail=ErrorResponseModel( |
274 | | - code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
275 | | - message=f"An unexpected error occurred while retrieving the task status: {str(e)}", |
276 | | - router=str(request.url), |
277 | | - params=dict(request.query_params), |
278 | | - ).dict() |
279 | | - ) |
| 366 | + return ResponseModel( |
| 367 | + code=200, |
| 368 | + router=str(request.url), |
| 369 | + params=params.dict(), |
| 370 | + data=result |
| 371 | + ) |
280 | 372 |
|
281 | 373 |
|
282 | 374 | @router.get("/tasks/result", |
283 | 375 | summary="获取任务结果 / Get task result", |
284 | 376 | response_model=ResponseModel) |
285 | | -async def get_task_result( |
| 377 | +async def task_result( |
286 | 378 | request: Request, |
287 | 379 | task_id: int = Query(description="任务ID / Task ID") |
288 | 380 | ): |
|
0 commit comments