Source code for lib.sedna.service.server.knowledgeBase.model

# Copyright 2021 The KubeEdge Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from sqlalchemy import Column, String, Integer, Boolean, Text
from sqlalchemy import DateTime, Float, SmallInteger, ForeignKey, func
from sqlalchemy.orm import relationship

from .database import Base, engine

__all__ = ('TaskGrp', 'Tasks', 'TaskModel', 'TaskRelation', 'Samples',
           'TaskSample', 'get_or_create', 'engine', 'init_db')


[docs]class TaskGrp(Base): """Task groups"""
[docs] __tablename__ = 'll_task_grp'
[docs] id = Column(Integer, primary_key=True, index=True, autoincrement=True)
[docs] name = Column(String(100), unique=True, nullable=False, comment='task group name')
[docs] deploy = Column(Boolean(create_constraint=False), default=False)
[docs] sample_num = Column(Integer, default=0, comment='int of sample number')
[docs] task_num = Column(Integer, default=0, comment='int of task number')
[docs]class Tasks(Base): """Task table"""
[docs] __tablename__ = 'll_tasks'
[docs] id = Column(Integer, primary_key=True, index=True, autoincrement=True)
[docs] name = Column(String(100), unique=True, nullable=False, comment='task name')
[docs] task_attr = Column(Text, default="{}", comment='task attribute, json')
[docs] created_at = Column(DateTime, server_default=func.now(), comment='task create time')
[docs] updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='task update time')
[docs] def __repr__(self): return self.name
[docs]class TaskModel(Base): """model belong tasks"""
[docs] __tablename__ = 'll_task_models'
[docs] id = Column(Integer, primary_key=True, index=True, autoincrement=True)
[docs] task_id = Column(Integer, ForeignKey('ll_task_grp.id'), index=True, nullable=False)
[docs] task = relationship('TaskGrp')
[docs] model_url = Column(Text, default="", comment='model save url/path')
[docs] is_current = Column(Boolean(create_constraint=False), default=False)
[docs] created_at = Column(DateTime, server_default=func.now(), comment='model create time')
[docs]class TaskRelation(Base): """ relation between two tasks"""
[docs] __tablename__ = 'll_task_relation'
[docs] id = Column(Integer, primary_key=True, index=True, autoincrement=True)
[docs] grp_id = Column(Integer, ForeignKey('ll_task_grp.id'), index=True, nullable=False)
[docs] grp = relationship('TaskGrp')
[docs] task_id = Column(Integer, ForeignKey('ll_tasks.id'), index=True, nullable=False)
[docs] task = relationship('Tasks')
[docs] transfer_radio = Column(Float, default=0.0, comment="task transfer radio")
[docs]class Samples(Base): """ Sample storage """
[docs] __tablename__ = 'll_samples'
[docs] id = Column(Integer, primary_key=True, index=True, autoincrement=True)
[docs] data_url = Column(Text, default="", comment='dataset save url/path')
[docs] descr = Column(Text, default="", comment='dataset description')
[docs] data_type = Column( SmallInteger, default=0, index=True, comment='type of dataset, 0: train, 1: evaluation, 2: hard sample')
[docs] updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='dataset update time')
[docs] sample_num = Column(Integer, default=0, comment='int of sample number')
[docs]class TaskSample(Base): """ Sample of tasks"""
[docs] __tablename__ = 'll_task_sample'
[docs] id = Column(Integer, primary_key=True, index=True, autoincrement=True)
[docs] sample_id = Column(Integer, ForeignKey( 'll_samples.id'), index=True, nullable=False)
[docs] sample = relationship('Samples')
[docs] task_id = Column(Integer, ForeignKey('ll_tasks.id'), index=True, nullable=False)
[docs] task = relationship('Tasks')
[docs]def get_or_create(session, model, **kwargs): instance = session.query(model).filter_by(**kwargs).first() if instance: return instance, False else: instance = model(**kwargs) return instance, True
[docs]def init_db(): Base.metadata.create_all(bind=engine) TaskGrp.__table__.create(bind=engine, checkfirst=True) Tasks.__table__.create(bind=engine, checkfirst=True) TaskModel.__table__.create(bind=engine, checkfirst=True) TaskRelation.__table__.create(bind=engine, checkfirst=True) Samples.__table__.create(bind=engine, checkfirst=True) TaskSample.__table__.create(bind=engine, checkfirst=True)