<s> import uuid <EOL> from datetime import datetime <EOL> from sqlalchemy import Column , String , DateTime , ForeignKey <EOL> from sqlalchemy . orm import relationship , backref <EOL> from sqlalchemy . schema import Index <EOL> from changes . config import db <EOL> from changes . constants import Status , Result <EOL> from changes . db . utils import model_repr <EOL> from changes . db . types . enum import Enum <EOL> from changes . db . types . guid import GUID <EOL> from changes . db . types . json import JSONEncodedDict <EOL> class FutureJobStep ( object ) : <EOL> def __init__ ( self , label , commands = None , data = None ) : <EOL> self . label = label <EOL> self . commands = commands or [ ] <EOL> self . data = data or { } <EOL> def as_jobstep ( self , jobphase ) : <EOL> return JobStep ( <EOL> job_id = jobphase . job_id , <EOL> phase = jobphase , <EOL> phase_id = jobphase . id , <EOL> project_id = jobphase . project_id , <EOL> label = self . label , <EOL> status = Status . queued , <EOL> data = self . data , <EOL> ) <EOL> class JobStep ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> Index ( '<STR_LIT>' , '<STR_LIT:status>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> id = Column ( GUID , primary_key = True , default = uuid . uuid4 ) <EOL> job_id = Column ( GUID , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , nullable = False ) <EOL> phase_id = Column ( GUID , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , nullable = False ) <EOL> project_id = Column ( GUID , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , nullable = False ) <EOL> label = Column ( String ( <NUM_LIT> ) , nullable = False ) <EOL> status = Column ( Enum ( Status ) , nullable = False , default = Status . unknown ) <EOL> result = Column ( Enum ( Result ) , nullable = False , default = Result . unknown ) <EOL> node_id = Column ( GUID , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) ) <EOL> replacement_id = Column ( GUID , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , unique = True ) <EOL> cluster = Column ( String ( <NUM_LIT> ) , nullable = True ) <EOL> date_started = Column ( DateTime ) <EOL> date_finished = Column ( DateTime ) <EOL> date_created = Column ( DateTime , default = datetime . utcnow ) <EOL> last_heartbeat = Column ( DateTime ) <EOL> data = Column ( JSONEncodedDict ) <EOL> job = relationship ( '<STR_LIT>' ) <EOL> project = relationship ( '<STR_LIT>' ) <EOL> node = relationship ( '<STR_LIT>' ) <EOL> phase = relationship ( '<STR_LIT>' , backref = backref ( '<STR_LIT>' , order_by = '<STR_LIT>' ) ) <EOL> __repr__ = model_repr ( '<STR_LIT:label>' ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( JobStep , self ) . __init__ ( ** kwargs ) <EOL> if self . id is None : <EOL> self . id = uuid . uuid4 ( ) <EOL> if self . result is None : <EOL> self . result = Result . unknown <EOL> if self . status is None : <EOL> self . status = Status . unknown <EOL> if self . date_created is None : <EOL> self . date_created = datetime . utcnow ( ) <EOL> if self . data is None : <EOL> self . data = { } <EOL> @ property <EOL> def duration ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . date_started and self . date_finished : <EOL> duration = ( self . date_finished - self . date_started ) . total_seconds ( ) * <NUM_LIT:1000> <EOL> else : <EOL> duration = None <EOL> return duration </s>
