Example #1
0
class Execution(db.Model):
    """Execution Model"""
    id = db.Column(db.GUID(),
                   default=uuid.uuid4,
                   primary_key=True,
                   autoincrement=False)
    start_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    end_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    status = db.Column(db.String(10), default='PENDING')
    progress = db.Column(db.Integer(), default=0)
    params = db.Column(JSONB, default={})
    results = db.Column(JSONB, default={})
    logs = db.relationship('ExecutionLog',
                           backref=db.backref('execution'),
                           cascade='all, delete-orphan',
                           lazy='dynamic')
    script_id = db.Column(db.GUID(), db.ForeignKey('script.id'))
    user_id = db.Column(db.GUID(), db.ForeignKey('user.id'))

    def __init__(self, script_id, params, user_id):
        self.script_id = script_id
        self.params = params
        self.user_id = user_id

    def __repr__(self):
        return '<Execution %r>' % self.id

    def serialize(self, include=None):
        """Return object data in easily serializeable format"""
        include = include if include else []
        end_date_formatted = None
        if self.end_date:
            end_date_formatted = self.end_date.isoformat()
        execution = {
            'id': self.id,
            'script_id': self.script_id,
            'user_id': self.user_id,
            'start_date': self.start_date.isoformat(),
            'end_date': end_date_formatted,
            'status': self.status,
            'progress': self.progress,
            'params': self.params,
            'results': self.results,
        }
        if 'logs' in include:
            execution['logs'] = self.serialize_logs
        if 'user' in include:
            execution['user'] = self.user.serialize()
        if 'script' in include:
            execution['script'] = self.script.serialize()
        return execution

    @property
    def serialize_logs(self):
        """Serialize Logs"""
        return [item.serialize() for item in self.logs]
class ExecutionLog(db.Model):
    """ExecutionLog Model"""
    __tablename__ = 'execution_log'
    id = db.Column(db.Integer(), primary_key=True)
    text = db.Column(db.Text())
    level = db.Column(db.String(80), nullable=False, default='DEBUG')
    register_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    execution_id = db.Column(db.GUID(), db.ForeignKey('execution.id'))

    def __init__(self, text, level, execution_id):
        self.text = text
        self.level = level
        self.execution_id = execution_id

    def __repr__(self):
        return '<ExecutionLog %r>' % self.id

    def serialize(self):
        """Return object data in easily serializeable format"""
        return {
            'id': self.id,
            'text': self.text,
            'level': self.level,
            'register_date': self.register_date.isoformat(),
            'execution_id': self.execution_id
        }
Example #3
0
class Script(db.Model):
    """Script Model"""
    id = db.Column(db.GUID(), default=uuid.uuid4, primary_key=True, autoincrement=False)
    name = db.Column(db.String(120), nullable=False)
    slug = db.Column(db.String(80), unique=True, nullable=False)
    description = db.Column(db.Text(), default='')
    created_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    user_id = db.Column(db.GUID(), db.ForeignKey('user.id'))
    status = db.Column(db.String(80), nullable=False, default='PENDING')
    logs = db.relationship('ScriptLog',
                           backref=db.backref('script'),
                           cascade='all, delete-orphan',
                           lazy='dynamic')
    executions = db.relationship('Execution',
                                 backref=db.backref('script'),
                                 cascade='all, delete-orphan',
                                 lazy='dynamic')
    public = db.Column(db.Boolean(), default=False, nullable=False)

    def __init__(self, name, slug, user_id):
        self.name = name
        self.slug = slug
        self.user_id = user_id

    def __repr__(self):
        return '<Script %r>' % self.name

    def serialize(self, include=None):
        """Return object data in easily serializeable format"""
        include = include if include else []
        script = {
            'id': self.id,
            'name': self.name,
            'slug': self.slug,
            'description': self.description,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
            'user_id': self.user_id,
            'status': self.status,
            'public': self.public or False
        }
        if 'logs' in include:
            script['logs'] = self.serialize_logs
        if 'user' in include:
            script['user'] = self.user.serialize()
        if 'executions' in include:
            script['executions'] = self.serialize_executions
        return script

    @property
    def serialize_logs(self):
        """Serialize Logs"""
        return [item.serialize() for item in self.logs]

    @property
    def serialize_executions(self):
        """Serialize Logs"""
        return [item.serialize() for item in self.executions]
Example #4
0
class ScriptLog(db.Model):
    """ScriptLog Model"""
    __tablename__ = 'script_log'
    id = db.Column(db.Integer(), primary_key=True)
    text = db.Column(db.Text())
    register_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    script_id = db.Column(db.GUID(), db.ForeignKey('script.id'))

    def __init__(self, text, script_id):
        self.text = text
        self.script_id = script_id

    def __repr__(self):
        return '<ScriptLog %r>' % self.id

    def serialize(self):
        """Return object data in easily serializeable format"""
        return {
            'id': self.id,
            'text': self.text,
            'register_date': self.register_date.isoformat(),
            'script_id': self.script_id
        }