class Image(db.Model): """Image class""" __tablename__ = "image" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(LowerCaseText, unique=True, nullable=False) title = db.Column(db.Text) description = db.Column(db.Text, nullable=True) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) environments_based_on_this = db.relationship('Environment', lazy=True, backref='base_image') created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) def __init__(self): super(Image, self).__init__() def __repr__(self): """Display when printing a image object""" return "<ID: {}, description: {}>".format(self.id, self.desription) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class User(db.Model): """User class""" __tablename__ = "user" id = db.Column(db.Integer, autoincrement=True, primary_key=True) first_name = db.Column(db.Text, nullable=True) last_name = db.Column(db.Text, nullable=True) username = db.Column(LowerCaseText, unique=True, index=True, nullable=False) email = db.Column(LowerCaseText, unique=True, index=True, nullable=False) password_hash = db.Column(db.Text, nullable=False) created_images = db.relationship('Image', lazy=True, backref='creator') created_environments = db.relationship('Environment', lazy=True, backref='creator') created_ml_models = db.relationship('MLModel', lazy=True, backref='creator') created_features = db.relationship('Feature', lazy=True, backref='creator') created_feature_sets = db.relationship('FeatureSet', lazy=True, backref='creator') data_requests = db.relationship('DataRequest', lazy='subquery', secondary='user_data_request') # accessible_evironments = db.relationship('Environment', lazy='subquery', secondary='user_environment_access') created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) def __init__(self): super(User, self).__init__() def __repr__(self): """Display when printing a user object""" return "<ID: {}, Name: {} {}, username: {}, e-mail: {}>".format( self.id, self.first_name, self.last_name, self.username, self.email) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns} def hash_password(self, password): self.password_hash = pwd_context.encrypt(password) def verify_password(self, password): return pwd_context.verify(password, self.password_hash)
class MLModel(db.Model): """Image class""" __tablename__ = "ml_model" id = db.Column(db.Integer, autoincrement=True, primary_key=True) environment_id = db.Column(db.Integer, db.ForeignKey('environment.id'), nullable=False) ml_model_name = db.Column(db.Text, nullable=False) name = db.Column(LowerCaseText, nullable=False) description = db.Column(db.Text, nullable=True) condition_refcode = db.Column(db.Text, nullable=True) condition_name = db.Column(db.Text, nullable=True) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) feature_set_id = db.Column(db.Integer, db.ForeignKey('feature_set.id'), nullable=True) def __init__(self): super(MLModel, self).__init__() def __repr__(self): """Display when printing a image object""" return "<ID: {}, name: {}, description: {}>".format( self.id, self.name, self.desription) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class FeatureSet(db.Model): """Feature Set Class""" __tablename__ = "feature_set" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.Text) description = db.Column(db.Text) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) features = db.relationship('Feature', lazy='subquery', secondary='feature_feature_set') ml_models = db.relationship('MLModel', lazy='select', cascade='delete, delete-orphan', backref='feature_set') def __init__(self): super(FeatureSet, self).__init__() def __repr__(self): """Display when printing a feature set object""" return "<ID: {}, Name: {}, description: {}>".format( self.id, self.name, self.description) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Feature(db.Model): """Feature Class""" __tablename__ = "feature" id = db.Column(db.Integer, autoincrement=True, primary_key=True) resource = db.Column(db.Text, nullable=False) parameter_name = db.Column(db.Text, nullable=False) value = db.Column(db.Text, nullable=False) name = db.Column(db.Text) output_value_path = db.Column(db.Text, nullable=True) description = db.Column(db.Text) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) feature_sets = db.relationship('FeatureSet', lazy=True, secondary='feature_feature_set') def __init__(self): super(Feature, self).__init__() def __repr__(self): """Display when printing a feature object""" return "<ID: {}, Name: {}, description: {}>".format( self.id, self.name, self.description) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Environment(db.Model): """Environment Class""" __tablename__ = "environment" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.Text, nullable=False) container_id = db.Column(db.Text, nullable=False) container_name = db.Column(db.Text, nullable=False) status = db.Column(LowerCaseText, nullable=False) jupyter_port = db.Column(db.Text, nullable=False) jupyter_token = db.Column(db.Text, nullable=False) jupyter_url = None description = db.Column(db.Text) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # authorized_users = db.relationship('User', lazy='subquery', secondary='user_environment_access') image_id = db.Column(db.Integer, db.ForeignKey('image.id'), nullable=False) ml_models = db.relationship('MLModel', lazy='select', cascade='delete, delete-orphan', backref='environment') created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) def __init__(self): super(Environment, self).__init__() def __repr__(self): """Display when printing a environment object""" return "<ID: {}, Name: {}, description: {}>".format( self.id, self.name, self.description) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns} def handle_jupyter_data(self): if is_admin_user() or g.user.id == self.creator_id: self.set_jupyter_url() else: self.hide_jupyter_data() def start_jupyter(self): # wait for container api to be up and running wait_for_it(self.container_name, 5000) # start jupyter notebook and get jupyter token resp = requests.post('http://' + self.container_name + ':5000/jupyter').json() self.jupyter_token = str(resp['jupyter_token']) self.status = Environment.Status.running.value def set_jupyter_url(self): # TODO: read host address from os host = 'localhost' self.jupyter_url = host + ':' + self.jupyter_port + '/?token=' + self.jupyter_token def hide_jupyter_data(self): self.jupyter_port = None self.jupyter_token = None self.jupyter_url = None def get_data_directory(self): return '/ketos/environments_data/' + self.container_name class Status(Enum): running = 'running' stopped = 'stopped'