33Copyright (c) 2025, All Rights Reserved.
44"""
55
6- from typing import Optional , Union
6+ from datetime import datetime
7+ from typing import List , Optional , Union
8+ from uuid import uuid4
79
810from pydantic import BaseModel , Field
9- from sqlalchemy import Column , DateTime , Integer , String , Text , func
11+ from sqlalchemy import Column , DateTime , Integer , String , Text
12+ from sqlalchemy .sql import func
1013
1114from db .mysql import Base
1215
@@ -17,60 +20,103 @@ class TaskAnalysis(Base):
1720 """
1821
1922 __tablename__ = "test_insights"
20- id = Column (Integer , primary_key = True , index = True )
21- task_id = Column (String (40 ), nullable = False , unique = True )
23+
24+ id = Column (Integer , primary_key = True , autoincrement = True )
25+ task_id = Column (String (36 ), nullable = False , unique = True )
2226 eval_prompt = Column (Text , nullable = False )
2327 analysis_report = Column (Text , nullable = False )
24- status = Column (String (20 ), nullable = False , default = "completed" )
28+ status = Column (
29+ String (20 ), nullable = False , default = "pending"
30+ ) # pending, processing, completed, failed
2531 error_message = Column (Text , nullable = True )
26- created_at = Column (DateTime , server_default = func .now ())
27- updated_at = Column (DateTime , server_default = func .now (), onupdate = func .now ())
32+ created_at = Column (DateTime , nullable = False , server_default = func .now ())
33+ updated_at = Column (
34+ DateTime , nullable = False , server_default = func .now (), onupdate = func .now ()
35+ )
36+
37+
38+ class AnalysisJob (Base ):
39+ """
40+ SQLAlchemy model for tracking background analysis jobs.
41+ """
42+
43+ __tablename__ = "analysis_jobs"
44+
45+ id = Column (String (36 ), primary_key = True , default = lambda : str (uuid4 ()))
46+ task_ids = Column (Text , nullable = False ) # JSON string of task IDs
47+ analysis_type = Column (Integer , nullable = False ) # 0=single, 1=multiple
48+ language = Column (String (10 ), nullable = False , default = "en" )
49+ eval_prompt = Column (Text , nullable = True )
50+ status = Column (
51+ String (20 ), nullable = False , default = "pending"
52+ ) # pending, processing, completed, failed
53+ result_data = Column (Text , nullable = True ) # JSON string of analysis result
54+ error_message = Column (Text , nullable = True )
55+ created_at = Column (DateTime , nullable = False , server_default = func .now ())
56+ updated_at = Column (
57+ DateTime , nullable = False , server_default = func .now (), onupdate = func .now ()
58+ )
2859
2960
3061class AnalysisRequest (BaseModel ):
3162 """
32- Request model for AI analysis.
63+ Request model for AI analysis (single or multiple tasks).
64+ """
65+
66+ task_ids : List [str ] = Field (..., description = "List of task IDs to analyze" )
67+ eval_prompt : Optional [str ] = Field (
68+ None , description = "Custom evaluation prompt for analysis"
69+ )
70+ language : Optional [str ] = Field ("en" , description = "Language for analysis report" )
71+ background : Optional [bool ] = Field (False , description = "Process in background" )
72+
3373
34- Attributes :
35- task_id: The task ID to analyze.
36- language: The language for analysis prompt (en/zh) .
74+ class AnalysisJobRequest ( BaseModel ) :
75+ """
76+ Request model for starting a background analysis job .
3777 """
3878
39- eval_prompt : Optional [str ] = Field (None , description = "Custom evaluation prompt " )
40- language : Optional [str ] = Field (
41- "en" , description = "Language for analysis prompt (en/zh) "
79+ task_ids : List [str ] = Field (... , description = "List of task IDs to analyze " )
80+ eval_prompt : Optional [str ] = Field (
81+ None , description = "Custom evaluation prompt for analysis "
4282 )
83+ language : Optional [str ] = Field ("en" , description = "Language for analysis report" )
4384
4485
4586class AnalysisResponse (BaseModel ):
4687 """
4788 Response model for AI analysis.
48-
49- Attributes:
50- task_id: The task ID.
51- analysis_report: The AI analysis content.
52- status: The analysis status.
53- error_message: Error message if analysis failed.
54- created_at: The creation timestamp.
5589 """
5690
57- task_id : str
91+ task_ids : List [ str ]
5892 analysis_report : str
5993 status : str
6094 error_message : Optional [str ] = None
6195 created_at : str
96+ job_id : Optional [str ] = Field (
97+ None , description = "Background job ID if processed asynchronously"
98+ )
6299
63100
64- class GetAnalysisResponse (BaseModel ):
101+ class AnalysisJobResponse (BaseModel ):
102+ """
103+ Response model for background analysis job status.
65104 """
66- Response model for getting analysis results.
67105
68- Attributes:
69- data: The analysis data.
70- status: The status of the response.
71- error: An error message if the request failed, otherwise None.
106+ job_id : str
107+ task_ids : List [str ]
108+ status : str
109+ result_data : Optional [str ] = None
110+ error_message : Optional [str ] = None
111+ created_at : str
112+ updated_at : str
113+
114+
115+ class GetAnalysisResponse (BaseModel ):
116+ """
117+ Response model for getting AI analysis.
72118 """
73119
74- data : Optional [AnalysisResponse ] = None
120+ data : Union [AnalysisResponse , None ]
75121 status : str
76- error : Union [str , None ]
122+ error : Optional [str ] = None
0 commit comments