11import datetime
22import logging
3- from typing import List
4- from sqlalchemy import Column , DateTime , Enum , Integer , String
3+ from typing import List , Optional
4+ from sqlalchemy import DateTime , Enum , Integer , String
55from app .database .db import Base
6- from sqlalchemy .orm import Session
6+ from sqlalchemy .orm import Session , Mapped , mapped_column
77
8- from app .schemas import ProcessingStatusEnum
8+ from app .schemas import ProcessTypeEnum , ProcessingStatusEnum
99
1010
1111logger = logging .getLogger (__name__ )
1212
1313
1414class ProcessingJobRecord (Base ):
15- __tablename__ = 'processing_jobs'
16-
17- id = Column (Integer , primary_key = True , index = True , autoincrement = True )
18- title = Column (String , index = True )
19- label = Column (String , index = True )
20- status = Column (Enum (ProcessingStatusEnum ), index = True )
21- user_id = Column (String , index = True )
22- platform_job_id = Column (String , index = True )
23- parameters = Column (String , index = False )
24- result_link = Column (String , index = False )
25- service_record = Column (String , index = True )
26- created = Column (DateTime , default = datetime .datetime .utcnow , index = True )
27- updated = Column (DateTime , default = datetime .datetime .utcnow , onupdate = datetime .datetime .utcnow , index = True )
28-
29-
30-
31- def save_job_to_db (db_session : Session , job : ProcessingJobRecord ) -> ProcessingJobRecord :
15+ __tablename__ = "processing_jobs"
16+
17+ id : Mapped [int ] = mapped_column (
18+ Integer , primary_key = True , index = True , autoincrement = True
19+ )
20+ title : Mapped [str ] = mapped_column (String , index = True )
21+ label : Mapped [ProcessTypeEnum ] = mapped_column (Enum (ProcessTypeEnum ), index = True )
22+ status : Mapped [ProcessingStatusEnum ] = mapped_column (
23+ Enum (ProcessingStatusEnum ), index = True
24+ )
25+ user_id : Mapped [str ] = mapped_column (String , index = True )
26+ platform_job_id : Mapped [Optional [str ]] = mapped_column (String , index = True )
27+ parameters : Mapped [Optional [str ]] = mapped_column (String , index = False )
28+ result_link : Mapped [Optional [str ]] = mapped_column (String , index = False )
29+ service_record : Mapped [Optional [str ]] = mapped_column (String , index = True )
30+ created : Mapped [datetime .datetime ] = mapped_column (
31+ DateTime , default = datetime .datetime .utcnow , index = True
32+ )
33+ updated : Mapped [datetime .datetime ] = mapped_column (
34+ DateTime ,
35+ default = datetime .datetime .utcnow ,
36+ onupdate = datetime .datetime .utcnow ,
37+ index = True ,
38+ )
39+
40+
41+ def save_job_to_db (
42+ db_session : Session , job : ProcessingJobRecord
43+ ) -> ProcessingJobRecord :
3244 """
3345 Save a processing job record to the database and update the ID of the job.
34-
46+
3547 :param db_session: The database session to use for saving the job.
3648 :param job: The ProcessingJobRecord instance to save.
37- """
49+ """
3850 db_session .add (job )
3951 db_session .commit ()
4052 db_session .refresh (job ) # Refresh to get the ID after commit
41- logger .debug ("Processing job saved with ID: {job.id}" )
53+ logger .debug ("Processing job saved with ID: {job.id}" )
4254 return job
43-
55+
4456
4557def get_jobs_by_user_id (database : Session , user_id : str ) -> List [ProcessingJobRecord ]:
4658 logger .info (f"Retrieving all processing jobs for user { user_id } " )
47- return database .query (ProcessingJobRecord ).filter (ProcessingJobRecord .user_id == user_id ).all ()
59+ return (
60+ database .query (ProcessingJobRecord )
61+ .filter (ProcessingJobRecord .user_id == user_id )
62+ .all ()
63+ )
64+
4865
49- def get_job_by_user_id (database : Session , job_id : int , user_id : str ) -> ProcessingJobRecord :
66+ def get_job_by_user_id (
67+ database : Session , job_id : int , user_id : str
68+ ) -> Optional [ProcessingJobRecord ]:
5069 logger .info (f"Retrieving processing job with ID { job_id } for user { user_id } " )
51- return database .query (ProcessingJobRecord ).filter (ProcessingJobRecord .id == job_id , ProcessingJobRecord .user_id == user_id ).first ()
70+ return (
71+ database .query (ProcessingJobRecord )
72+ .filter (
73+ ProcessingJobRecord .id == job_id , ProcessingJobRecord .user_id == user_id
74+ )
75+ .first ()
76+ )
0 commit comments