<s> from __future__ import absolute_import <EOL> from datetime import datetime <EOL> from sqlalchemy import Column , DateTime , ForeignKey , Integer , String <EOL> from sqlalchemy . schema import Index <EOL> from freight . config import db <EOL> from freight . db . types . json import JSONEncodedDict <EOL> class TaskStatus ( object ) : <EOL> unknown = <NUM_LIT:0> <EOL> pending = <NUM_LIT:1> <EOL> in_progress = <NUM_LIT:2> <EOL> finished = <NUM_LIT:3> <EOL> failed = <NUM_LIT:4> <EOL> cancelled = <NUM_LIT:5> <EOL> @ classmethod <EOL> def get_label ( cls , status ) : <EOL> return STATUS_LABELS [ status ] <EOL> @ classmethod <EOL> def label_to_id ( cls , label ) : <EOL> return STATUS_LABELS_REV [ label ] <EOL> STATUS_LABELS = { <EOL> TaskStatus . unknown : '<STR_LIT>' , <EOL> TaskStatus . pending : '<STR_LIT>' , <EOL> TaskStatus . in_progress : '<STR_LIT>' , <EOL> TaskStatus . finished : '<STR_LIT>' , <EOL> TaskStatus . failed : '<STR_LIT>' , <EOL> TaskStatus . cancelled : '<STR_LIT>' , <EOL> } <EOL> STATUS_LABELS_REV = { <EOL> v : k for k , v in STATUS_LABELS . items ( ) <EOL> } <EOL> class Task ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> id = Column ( Integer , primary_key = True ) <EOL> app_id = Column ( Integer , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , <EOL> nullable = False ) <EOL> user_id = Column ( Integer , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , <EOL> nullable = False ) <EOL> ref = Column ( String ( <NUM_LIT> ) , nullable = False ) <EOL> sha = Column ( String ( <NUM_LIT> ) ) <EOL> provider = Column ( String ( <NUM_LIT:64> ) , nullable = False ) <EOL> status = Column ( Integer , nullable = False ) <EOL> params = Column ( JSONEncodedDict , nullable = True ) <EOL> data = Column ( JSONEncodedDict ) <EOL> date_created = Column ( DateTime , default = datetime . utcnow , nullable = False ) <EOL> date_started = Column ( DateTime ) <EOL> date_finished = Column ( DateTime ) <EOL> @ property <EOL> def was_forced ( self ) : <EOL> return self . data . get ( '<STR_LIT>' , False ) <EOL> @ property <EOL> def checks ( self ) : <EOL> return self . data . get ( '<STR_LIT>' , [ ] ) <EOL> @ property <EOL> def notifiers ( self ) : <EOL> return self . data . get ( '<STR_LIT>' , [ ] ) <EOL> @ property <EOL> def provider_config ( self ) : <EOL> return self . data . get ( '<STR_LIT>' , { } ) <EOL> @ property <EOL> def status_label ( self ) : <EOL> return STATUS_LABELS . get ( self . status , '<STR_LIT>' ) <EOL> @ property <EOL> def duration ( self ) : <EOL> if not ( self . date_finished and self . date_started ) : <EOL> return <EOL> return float ( '<STR_LIT>' % ( self . date_finished - self . date_started ) . total_seconds ( ) ) </s>
