Ejemplo n.º 1
0
class Dataset(db.Model):
    __tablename__ = "datasets"

    id = db.Column(db.Integer, primary_key=True)
    bucket_name = db.Column(db.String(255))
    folder_name = db.Column(db.String(255))
    object_name = db.Column(db.String(255))
    file_name = db.Column(db.String(255))

    project_id = db.Column(db.Integer, db.ForeignKey("projects.id"), nullable=False)
    project = db.relationship("Project")

    def __init__(self, bucket_name, folder_name, object_name, file_name, project_id):
        self.bucket_name = bucket_name
        self.folder_name = folder_name
        self.object_name = object_name
        self.file_name = file_name
        self.project_id = project_id

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def get_data_path(self):
        return f"s3://{self.bucket_name}/{self.folder_name}/train"

    @classmethod
    def find_data_by_id(cls, data_id):
        return cls.query.filter_by(id=data_id).first()
Ejemplo n.º 2
0
class Project(db.Model):
    __tablename__ = "projects"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    location_name = db.Column(
        db.String(255))  # This denotes the project location name in s3 bucket
    description = db.Column(db.String(255))
    type = db.Column(db.String(255))
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)

    def __init__(self, name, description, type, user_id):
        self.name = name
        self.description = description
        self.type = type
        self.user_id = user_id
        self.generate_location_name()

    def generate_location_name(self):
        # Generate a unique uuid string
        self.location_name = uuid4().hex

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def belongs_to_user(self, user_id):
        return self.user_id == user_id

    def json(self):
        return {
            "id": self.id,
            "name": self.name,
            "location_name": self.location_name,
            "description": self.description,
            "type": self.type
        }

    @classmethod
    def find_project_with_id(cls, project_id):
        return cls.query.filter_by(id=project_id).first()

    @classmethod
    def find_project_from_user(cls, user_id):
        return cls.query.filter_by(user_id=user_id).all()

    def is_classification_problem(self):
        return self.type == "classification"

    def is_regression_problem(self):
        return self.type == "regression"
Ejemplo n.º 3
0
class NotebookModel(db.Model):
    __tablename__ = "notebooks"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    status = db.Column(db.String(20))
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)

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

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.remove(self)
        db.session.commit()

    def json(self):
        return {
            "id": self.id,
            "name": self.name,
            "status": self.status,
            "user_id": self.user_id
        }

    def update_status(self, status):
        self.status = status
        self.save()

    @classmethod
    def find_by_id(cls, id):
        return cls.query.filter_by(id=id).first()

    @classmethod
    def find_by_user_id(cls, user_id):
        return cls.query.filter_by(user_id=user_id).all()

    @classmethod
    def find_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    def is_in_terminal_state(self):
        return self.status in ["Failed", "InService", "Stopped"]
Ejemplo n.º 4
0
class UserModel(db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(255), nullable=False)
    email = db.Column(db.String(255), nullable=False)
    tokens = db.Column(db.Integer, default=0)
    _password = db.Column(db.String(255), nullable=False)

    @hybrid_property
    def password(self):
        return self._password

    @password.setter
    def password(self, plaintext):
        self._password = bcrypt.generate_password_hash(plaintext).decode(
            'utf-8')

    def save(self):
        db.session.add(self)
        db.session.commit()

    def increase_tokens(self, tokens):
        self.tokens += tokens
        self.save()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def check_password(self, plaintext):
        return bcrypt.check_password_hash(self._password, plaintext)

    def json(self):
        return {
            "email": self.email,
            "username": self.username,
            "tokens": self.tokens
        }

    @classmethod
    def find_user_with_id(cls, user_id):
        return cls.query.filter_by(id=user_id).first()

    @classmethod
    def find_user_with_email(cls, email):
        return cls.query.filter_by(email=email).first()
Ejemplo n.º 5
0
class QuizModel(database.Model):
    __tablename__ = 'quizzes'

    quiz_id = database.Column(database.Integer, primary_key=True, nullable=False)
    test_id = database.Column(database.Integer, database.ForeignKey('tests.test_id'), nullable=False)
    order_num = database.Column(database.Integer, nullable=False)
    credit_value = database.Column(database.Integer, nullable=False)
    gained_credit = database.Column(database.Integer)
    quiz_type = database.Column(database.String)
    text_body = database.Column(database.String)
    path_to_attachment = database.Column(database.String)
    title = database.Column(database.String(100))
    instructions = database.Column(database.String)

    test = database.relationship('TestModel')
    answers = database.relationship('AnswerModel', lazy='dynamic')

    def __init__(self, quiz_id, test_id, order_num, credit_value, gained_credit, quiz_type, text_body, path_to_attachment, title, instructions):
        self.quiz_id = quiz_id
        self.test_id = test_id
        self.order_num = order_num
        self.credit_value = credit_value
        self.gained_credit = gained_credit
        self.quiz_type = quiz_type # 'type' is a key word in Python
        self.text_body = text_body
        self.path_to_attachment = path_to_attachment
        self.title = title
        self.instructions = instructions
    
    def json(self, getCorrectAnswers=False):
        return {
            'quiz_id': self.quiz_id,
            'test_id': self.test_id,
            'order_num': self.order_num,
            'credit_value': self.credit_value,
            'gained_credit': self.gained_credit,
            'quiz_type': self.quiz_type,
            'text_body': self.text_body,
            'path_to_attachment': self.path_to_attachment,
            'title': self.title,
            'instructions': self.instructions,
            'answers': [a.json(getCorrectAnswers=getCorrectAnswers) for a in self.answers.all()]
        }

    def save_to_database(self):
        database.session.add(self)
        database.session.commit()

    def delete_from_database(self):
        database.session.delete(self)
        database.session.commit()

    def get_correct_answer(self):
        return self.answers.filter_by(quiz_id=self.quiz_id, is_correct=True).first()

    @classmethod
    def find_by_id(cls, quiz_id):
        return cls.query.filter_by(quiz_id=quiz_id).first()
Ejemplo n.º 6
0
class UserModel(database.Model):
    __tablename__ = 'users'

    id = database.Column(database.Integer, primary_key=True)
    username = database.Column(database.String(80))
    password = database.Column(database.String(80))
    puzzles = database.relationship('PuzzleModel', lazy='dynamic')

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def to_json(self):
        return {'id': self.id, 'username': self.username}

    def to_json_score(self):
        lst = [p.to_json().get('score') for p in self.puzzles.all()]
        return {
            'id': self.id,
            'username': self.username,
            'scores': lst,
            'total_score': sum(lst)  # reduce(add, lst, 0)  # fold_left
        }

    @classmethod
    def find_by_username(cls, username):
        return cls.query.filter_by(username=username).first()

    @classmethod
    def find_by_id(cls, user_id):
        return cls.query.filter_by(id=user_id).first()

    def save_to_db(self):
        database.session.add(self)
        database.session.commit()

    def update_to_db(self, pwd):
        self.password = pwd
        database.session.commit()

    def delete_from_db(self):
        database.session.delete(self)
        database.session.commit()
Ejemplo n.º 7
0
class PaymentModel(db.Model):
    __tablename__ = "payments"
    id = db.Column(db.Integer, primary_key=True)
    type = db.Column(db.String(20), nullable=False)
    price = db.Column(db.Float, nullable=False)
    amount = db.Column(db.Integer, nullable=False)

    @classmethod
    def find_by_id(cls, id):
        return cls.query.filter_by(id=id).first()
Ejemplo n.º 8
0
class DeploymentModel(db.Model):
    __tablename__ = "deployments"
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer,
                           db.ForeignKey("projects.id"),
                           nullable=False)
    endpoint_name = db.Column(db.String(255), nullable=False)
    status = db.Column(db.String(255), default="Creating")

    def __init__(self, project_id, endpoint_name):
        self.project_id = project_id
        self.endpoint_name = endpoint_name
        self.status = "Creating"

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def json(self):
        return {
            "id": self.id,
            "project_id": self.project_id,
            "endpoint_name": self.endpoint_name,
            "status": self.status
        }

    def update_status(self, status):
        self.status = status
        self.save()

    @classmethod
    def find_by_project_id(cls, project_id):
        return cls.query.filter_by(project_id=project_id).first()

    @classmethod
    def if_a_deployment_exist(cls, project_id):
        return cls.query.filter_by(project_id=project_id).first() != None
Ejemplo n.º 9
0
class TopicModel(database.Model):
    __tablename__ = 'topics'

    topic_id = database.Column(database.Integer,
                               primary_key=True,
                               nullable=False)
    is_unlocked = database.Column(database.Boolean, nullable=False)
    name = database.Column(database.String(50), nullable=False)
    needed_credit = database.Column(database.Integer)

    tests = database.relationship('TestModel', lazy='dynamic')
    formativeAssessments = database.relationship('FormativeAssessmentModel',
                                                 lazy='dynamic')

    def __init__(self, topic_id, is_unlocked, name, needed_credit):
        self.topic_id = topic_id
        self.is_unlocked = is_unlocked
        self.name = name
        self.needed_credit = needed_credit

    def json(self):
        return {
            'topic_id':
            self.topic_id,
            'is_unlocked':
            self.is_unlocked,
            'name':
            self.name,
            'needed_credit':
            self.needed_credit,
            'tests': [t.json() for t in self.tests.all()],
            'formative_assessments':
            [fa.json() for fa in self.formativeAssessments.all()]
        }

    def save_to_database(self):
        database.session.add(self)
        database.session.commit()

    def delete_from_database(self):
        database.session.delete(self)
        database.session.commit()

    @classmethod
    def find_by_id(cls, topic_id):
        return cls.query.filter_by(topic_id=topic_id).first()

    @classmethod
    def get_all(cls):
        return cls.query.all()
Ejemplo n.º 10
0
class UserModel(database.Model):
    __tablename__ = 'user'

    first_name = database.Column(database.String(60))
    last_name = database.Column(database.String(60))
    date_of_birth = database.Column(database.String(10))
    address = database.Column(database.String(100))
    contact_no = database.Column(database.Integer)
    email = database.Column(database.String(80))
    qualification = database.Column(database.String(80))
    gender = database.Column(database.String(80))
    salary = database.Column(database.Integer)
    pan_no = database.Column(database.String(80))
    type_of_employer = database.Column(database.String(100))
    name_of_employer = database.Column(database.String(100))
    user_id = database.Column(database.String(100), primary_key=True)
    password = database.Column(database.String(100))

    def __init__(self, first_name, last_name, date_of_birth, address, contact_no, email, qualification, gender, salary,
                 pan_no, type_of_employer, name_of_employer, user_id, password):
        self.first_name = first_name
        self.last_name = last_name
        self.date_of_birth = date_of_birth
        self.address = address
        self.contact_no = contact_no
        self.email = email
        self.qualification = qualification
        self.gender = gender
        self.salary = salary
        self.pan_no = pan_no
        self.type_of_employer = type_of_employer
        self.name_of_employer = name_of_employer
        self.user_id = user_id
        self.password = password

    def json(self):
        return {'first_name': self.first_name,
                'last_name': self.last_name,
                'date_of_birth': self.date_of_birth,
                'address': self.address,
                'contact_no': self.contact_no,
                'email': self.email,
                'qualification': self.qualification,
                'gender': self.gender,
                'salary': self.salary,
                'pan_no': self.pan_no,
                'type_of_employer': self.type_of_employer,
                'name_of_employer': self.name_of_employer,
                'user_id': self.user_id,
                'password': self.password
                }

    @classmethod
    def find_by_email(cls, email):
        return cls.query.filter_by(email=email).first()

    @classmethod
    def find_by_password(cls, password):
        return cls.query.filter_by(password=password).first()

    @classmethod
    def find_by_login(cls, user_id, password):
        return cls.query.filter_by(user_id=user_id, password=password).first()

    @classmethod
    def find_by_user_id(cls, user_id):
        return cls.query.filter_by(user_id=user_id).first()

    def save_to_database(self):
        database.session.add(self)
        database.session.commit()

    def delete_from_database(self):
        database.session.delete(self)
        database.session.commit()

    def generate_auth_token(self, expiration=600):
        s = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
        return s.dumps({'id': self.id})

    @staticmethod
    def verify_auth_token(token):
        app = Flask(__name__)
        s = Serializer(app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except SignatureExpired:
            return None  # valid token, but expired
        except BadSignature:
            return None  # invalid token
        user = UserModel.query.get(data['id'])
        return user
Ejemplo n.º 11
0
class FormativeAssessmentModel(database.Model):
    __tablename__ = 'formativeAssessments'

    fa_id = database.Column(database.Integer, primary_key=True, nullable=False)
    topic_id = database.Column(database.Integer,
                               database.ForeignKey('topics.topic_id'))
    is_unlocked = database.Column(database.Boolean, nullable=False)
    max_credit = database.Column(database.Integer)
    order_num = database.Column(database.Integer, nullable=False)
    gained_credit = database.Column(database.Integer)
    answer = database.Column(database.String)
    pass_credit = database.Column(database.Integer)
    instructions = database.Column(database.String)
    title = database.Column(database.String(100), nullable=False)
    path_to_attachment = database.Column(database.String)
    deadline = database.Column(database.String)
    reviewer_comment = database.Column(database.String)
    is_marked = database.Column(database.Boolean)

    topic = database.relationship('TopicModel')

    def __init__(self, fa_id, topic_id, is_unlocked, order_num, max_credit,
                 gained_credit, answer, pass_credit, instructions, title,
                 path_to_attachment, deadline, reviewer_comment, is_marked):
        self.fa_id = fa_id
        self.topic_id = topic_id
        self.is_unlocked = is_unlocked
        self.max_credit = max_credit
        self.order_num = order_num
        self.gained_credit = gained_credit
        self.answer = answer
        self.pass_credit = pass_credit
        self.instructions = instructions
        self.title = title
        self.path_to_attachment = path_to_attachment
        self.deadline = deadline
        self.reviewer_comment = reviewer_comment
        self.is_marked = is_marked

    def json(self):
        return {
            'fa_id': self.fa_id,
            'topic_id': self.topic_id,
            'is_unlocked': self.is_unlocked,
            'max_credit': self.max_credit,
            'order_num': self.order_num,
            'gained_credit': self.gained_credit,
            'answer': self.answer,
            'pass_credit': self.pass_credit,
            'instructions': self.instructions,
            'title': self.title,
            'path_to_attachment': self.path_to_attachment,
            'deadline': self.deadline,
            'reviewer_comment': self.reviewer_comment,
            'is_marked': self.is_marked
        }

    def save_to_database(self):
        database.session.add(self)
        database.session.commit()

    def delete_from_database(self, query, args):
        database.session.delete(self)
        database.session.commit()

    @classmethod
    def find_by_id(cls, fa_id):
        return cls.query.filter_by(fa_id=fa_id).first()

    @classmethod
    def get_all(cls):
        return cls.query.all()

    @classmethod
    def get_all_for_topic(cls, topic_id):
        return cls.query.filter_by(topic_id=topic_id).all()
Ejemplo n.º 12
0
class ClassficationProblem(db.Model):
    _tablename_ = "classifications"

    id = db.Column(db.Integer, primary_key=True)
    job_name = db.Column(db.String(255))
    algorithm_name = db.Column(db.String(255))

    train_accuracy = db.Column(db.Float)
    test_accuracy = db.Column(db.Float)
    train_f1 = db.Column(db.Float)
    test_f1 = db.Column(db.Float)

    project_id = db.Column(db.Integer,
                           db.ForeignKey("projects.id"),
                           nullable=False)
    project = db.relationship("Project")

    def __init__(self, job_name, algorithm_name, project_id):
        self.job_name = job_name
        self.algorithm_name = algorithm_name
        self.project_id = project_id

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def json(self):
        return {
            "id": self.id,
            "job_name": self.job_name,
            "algorithm_name": self.algorithm_name,
            "train_accuracy": self.train_accuracy,
            "test_accuracy": self.test_accuracy,
            "train_f1": self.train_f1,
            "test_f1": self.test_f1,
            "project_id": self.project_id
        }

    @classmethod
    def map_solution_to_solution_id(cls, solutions):
        return [solution.id for solution in solutions]

    @classmethod
    def find_solution_with_id(cls, solution_id):
        return cls.query.filter_by(id=solution_id).first()

    @classmethod
    def find_solutions_of_project(cls, project_id):
        solutions = cls.query.filter_by(project_id=project_id).order_by(
            ClassficationProblem.id.desc()).all()
        return cls.map_solution_to_solution_id(solutions)

    @classmethod
    def find_best_solution_of_project(cls, project_id):
        best_solution = cls.query.filter_by(project_id=project_id).filter(
            cls.test_accuracy != None).order_by(
                cls.test_accuracy.desc()).first()
        return best_solution

    def if_belongs_to(self, project_id):
        return self.project_id == project_id

    def analytics_filled(self):
        return self.train_accuracy != None

    def update_analytics(self, analytics):
        self.train_accuracy = analytics["train:accuracy"]
        self.test_accuracy = analytics["test:accuracy"]
        self.train_f1 = analytics["train:f1"]
        self.test_f1 = analytics["test:f1"]
        self.save()
Ejemplo n.º 13
0
class PolicyModel(database.Model):
    __tablename__ = 'policy_table'

    policy_name = database.Column(database.String(80))
    start_date = database.Column(database.String(80))
    end_date = database.Column(database.String(80))
    duration_in_years = database.Column(database.String(80))
    company_name = database.Column(database.String(100))
    initial_deposit = database.Column(database.Integer)
    policy_type = database.Column(database.String(80))
    user_type = database.Column(database.String(80))
    terms_per_year = database.Column(database.Integer)
    term_amount = database.Column(database.Integer)
    interest = database.Column(database.Float)
    maturity_amount = database.Column(database.Float)
    policy_id = database.Column(database.String(120), primary_key=True)

    def __init__(self, policy_name, start_date, end_date, duration_in_years, company_name, initial_deposit, policy_type,
                 user_type, terms_per_year, term_amount,
                 interest, maturity_amount, policy_id):
        self.policy_name = policy_name
        self.start_date = start_date
        self.end_date = end_date
        self.duration_in_years = duration_in_years
        self.company_name = company_name
        self.initial_deposit = initial_deposit
        self.policy_type = policy_type
        self.user_type = user_type
        self.terms_per_year = terms_per_year
        self.term_amount = term_amount
        self.interest = interest
        self.maturity_amount = maturity_amount
        self.policy_id = policy_id

    def json(self):
        return {

            'policy_name': self.policy_name,
            'start_date': self.start_date,
            'end_date': self.end_date,
            'duration_in_years': self.duration_in_years,
            'company_name': self.company_name,
            'initial_deposit': self.initial_deposit,
            'policy_type': self.policy_type,
            'user_type': self.user_type,
            'terms_per_year': self.terms_per_year,
            'term_amount': self.term_amount,
            'interest': self.interest,
            'maturity_amount': self.maturity_amount,
            'policy_id': self.policy_id

        }

    @classmethod
    def find_by_policy_name(cls, policy_name):
        return cls.query.filter_by(policy_name=policy_name).first()

    @classmethod
    def find_by_company_name(cls, company_name):
        return cls.query.filter_by(company_name=company_name).first()

    @classmethod
    def find_by_policy_id(cls, policy_id):
        return cls.query.filter_by(policy_id=policy_id).first()

    @classmethod
    def find_by_policy_type(cls, policy_type):
        return cls.query.filter_by(policy_type=policy_type).get()

    @classmethod
    def find_by_years(cls, duration_in_years):
        return cls.query.filter_by(duration_in_years=duration_in_years).get()

    def save_to_database(self):
        database.session.add(self)
        database.session.commit()

    def delete_to_database(self):
        database.session.delete(self)
        database.session.commit()