コード例 #1
0
ファイル: statistics_business.py プロジェクト: zjn0224/mo
class StatisticsBusiness(GeneralBusiness):
    repo = Repo(Statistics)

    @classmethod
    def use_app(cls, user_obj, app_obj, output_json, input_json=None):
        return cls.repo.create_one(
            caller=user_obj,
            entity_type="app",
            app=app_obj,
            action="use",
            datetime=datetime.utcnow(),
            # input_json=input_json,
            # output_json=output_json
        )

    @classmethod
    def action(cls, user_obj, entity_obj, entity_type, action):
        # 用于动态加入 attribute 对象元素
        dict = {entity_type: entity_obj}

        return cls.repo.create_one(caller=user_obj,
                                   entity_type=entity_type,
                                   action=action,
                                   datetime=datetime.utcnow(),
                                   **dict)

    @classmethod
    def get_user_statistics(cls, user_obj, entity_type, action):
        # GeneralBusiness.read()
        return cls.repo.objects(caller=user_obj,
                                entity_type=entity_type,
                                action=action)
コード例 #2
0
ファイル: staging_data_set_repo.py プロジェクト: zjn0224/mo
 def read_by_non_unique_field_without_result(self, field_name, field_value):
     return Repo.read(self, {
         field_name: field_value,
         'type': {
             '$ne': 'result'
         }
     })
コード例 #3
0
ファイル: statistics_business.py プロジェクト: StoneXue/mo
class StatisticsBusiness(GeneralBusiness):
    repo = Repo(Statistics)

    entity = Statistics

    # def create(self, **kwargs):
    #     return Statistics(**kwargs).save()
    #
    # @classmethod
    # def use_app(cls, user_obj, app_obj, output_json, input_json=None):
    #     cls.create( caller=user_obj,
    #             entity_type="app",
    #             app=app_obj,
    #             action="use",
    #             datetime=datetime.utcnow())
    #     # StatisticsBusiness.create()
    #     statistics = Statistics(
    #         caller=user_obj,
    #         entity_type="app",
    #         app=app_obj,
    #         action="use",
    #         datetime=datetime.utcnow(),
    #     )
    #     return statistics.save()

    @classmethod
    def use_app(cls, user_obj, app_obj, output_json, input_json=None):
        return cls.repo.create_one(
            caller=user_obj,
            entity_type="app",
            app=app_obj,
            action="use",
            datetime=datetime.utcnow(),
            # input_json=input_json,
            # output_json=output_json
        )

    @classmethod
    def action(cls, user_obj, entity_obj, entity_type, action):
        # 用于动态加入 attribute 对象元素
        dict = {entity_type: entity_obj}

        return cls.repo.create_one(caller=user_obj,
                                   entity_type=entity_type,
                                   action=action,
                                   datetime=datetime.utcnow(),
                                   **dict)

    @classmethod
    def get_user_statistics(cls, user_obj, entity_type, action):
        # GeneralBusiness.read()
        return cls.repo.objects(caller=user_obj,
                                entity_type=entity_type,
                                action=action)
コード例 #4
0
ファイル: general_business.py プロジェクト: zjn0224/mo
class GeneralBusiness:
    # 实例化后的 instance 走general repo
    repo = Repo(None)
    # class 不走general repo
    __cls = None

    @classmethod
    def get_all(cls):
        project = cls.repo.read()
        return project

    # 另一种方式
    # @classmethod
    # def get_all(cls):
    #     return cls.__cls.objects().order_by('-_id')

    @classmethod
    def read(cls, query=None):
        if query is None:
            query = {}
        return cls.__cls.objects(**query).order_by('-_id')

    @classmethod
    def get_by_id(cls, object_id):
        return cls.repo.read_by_id(object_id=object_id)

    @classmethod
    def read_unique_one(cls, **kwargs):
        return cls.repo.read_unique_one(kwargs)

    @classmethod
    def get_pagination(cls, query, page_no, page_size):
        start = (page_no - 1) * page_size
        end = page_no * page_size
        objects = cls.repo.read(query=query)
        count = objects.count()
        return Objects(objects=objects[start:end],
                       count=count,
                       page_no=page_no,
                       page_size=page_size)

    @classmethod
    def create(cls, obj):
        cls.repo.create(obj)

    @classmethod
    def create_one(cls, **kwargs):
        cls.repo.create_one(**kwargs)

    @classmethod
    @check_auth
    def remove_by_id(cls, object_id, user_ID):
        return cls.repo.delete_by_id(object_id)
コード例 #5
0
ファイル: world_business.py プロジェクト: zjn0224/mo
class WorldBusiness(GeneralBusiness):
    repo = Repo(World)

    # 1. 用户发送
    @classmethod
    def user_send(cls, sender, channel, message):
        create_time = datetime.datetime.utcnow()
        world = World(create_time=create_time,
                      sender=sender,
                      channel=channel,
                      message=message,
                      message_type=MessageType.user)
        return cls.repo.create(world)

    # 2. 系统发送
    @classmethod
    def system_send(cls, channel, message):
        create_time = datetime.datetime.utcnow()
        world = World(create_time=create_time,
                      channel=channel,
                      message=message)
        return cls.repo.create(world)
コード例 #6
0
 def read_by_type_and_private(self, owned_type, is_private):
     return Repo.read(self, {
         'private': is_private,
         '%s__exists' % owned_type: True
     })
コード例 #7
0
ファイル: job_repo.py プロジェクト: zjn0224/mo
 def update_one_by_id_status_and_time(self, job_id, status, updated_time):
     return Repo.update_one_by_id(self, job_id,
                                  {'status': status,
                                   'updated_time': updated_time})
コード例 #8
0
class GeneralBusiness:
    # 实例化后的 instance 走general repo
    repo = Repo(None)
    # class 不走general repo
    entity = None

    @classmethod
    def get_all(cls):
        project = cls.repo.read()
        return project

    # 另一种方式
    # @classmethod
    # def get_all(cls):
    #     return cls.__cls.objects().order_by('-_id')

    @classmethod
    def read(cls, query=None):
        if query is None:
            query = {}
        return cls.entity.objects(**query).order_by('-_id')

    @classmethod
    def get_by_id(cls, object_id):
        return cls.repo.read_by_id(object_id=object_id)

    @classmethod
    def read_unique_one(cls, **kwargs):
        return cls.repo.read_unique_one(kwargs)

    @classmethod
    def get_pagination(cls, query, page_no, page_size):
        start = (page_no - 1) * page_size
        end = page_no * page_size
        objects = cls.repo.read(query=query)
        count = objects.count()
        return Objects(objects=objects[start:end],
                       count=count,
                       page_no=page_no,
                       page_size=page_size)

    @classmethod
    def create(cls, obj):
        return cls.repo.create(obj)

    @classmethod
    def create_one(cls, **kwargs):
        return cls.repo.create_one(**kwargs)

    @classmethod
    @check_auth
    def remove_by_id(cls, object_id, user_ID):
        return cls.repo.delete_by_id(object_id)

    @classmethod
    def get_hot_tag(cls, search_query, object_type, user_request=False):
        if user_request:
            objects = cls.repo.read({'type': object_type})
        else:
            objects = cls.repo.read()
        # objects = entity.objects(type=object_type)
        if search_query:
            tag_freqs = objects(tags__icontains=search_query).item_frequencies(
                'tags', normalize=True)
            top_tags = sorted(tag_freqs.items(),
                              key=itemgetter(1),
                              reverse=True)[:100]
            res = []
            max_number = 5
            number = 0
            top_five = []
            top_five_length = 0
            for i in top_tags:
                if search_query in i[0]:
                    res.append(i)
                    number += 1
                else:
                    if top_five_length < max_number:
                        top_five.append(i)
                        top_five_length += 1
                if number == max_number:
                    return res
            if number < max_number:
                res += top_five[:max_number - number]
            return res
        else:
            tag_freqs = objects().item_frequencies('tags', normalize=True)
            top_tags = sorted(tag_freqs.items(),
                              key=itemgetter(1),
                              reverse=True)[:5]
            return top_tags
コード例 #9
0
 def create_project(cls, *args, **kwargs):
     ProjectBusiness.repo = Repo(project.App)
     return ProjectBusiness.create_project(*args,
                                           status='inactive',
                                           **kwargs)
コード例 #10
0
ファイル: general_business.py プロジェクト: yssAI/tp_project
class GeneralBusiness:
    # 实例化后的 instance 走general repo
    repo = Repo(None)
    # class 不走general repo
    entity = None

    @classmethod
    def get_all(cls):
        project = cls.repo.read()
        return project

    # 另一种方式
    # @classmethod
    # def get_all(cls):
    #     return cls.__cls.objects().order_by('-_id')

    @classmethod
    def read(cls, query=None):
        if query is None:
            query = {}
        return cls.entity.objects(**query).order_by('-_id')

    @classmethod
    def filter(cls, **query):
        return cls.entity.objects(**query).order_by('-_id')

    @classmethod
    def get_by_id(cls, object_id):
        return cls.repo.read_by_id(object_id=object_id)

    @classmethod
    def read_unique_one(cls, **kwargs):
        return cls.repo.read_unique_one(kwargs)

    @classmethod
    def get_pagination(cls, query, page_no, page_size):
        start = (page_no - 1) * page_size
        end = page_no * page_size
        objects = cls.repo.read(query=query)
        count = objects.count()
        return Objects(objects=objects[start:end],
                       count=count,
                       page_no=page_no,
                       page_size=page_size)

    @classmethod
    def create(cls, obj):
        return cls.repo.create(obj)

    @classmethod
    def create_one(cls, **kwargs):
        return cls.repo.create_one(**kwargs)

    @classmethod
    @check_auth
    def remove_by_id(cls, object_id, user_ID):
        return cls.repo.delete_by_id(object_id)

    @classmethod
    def get_hot_tag(cls,
                    search_query,
                    object_type,
                    category=None,
                    user_request=False,
                    status=None):
        max_number = 10
        queryJson = {}
        # 项目先不添加任何筛选条件
        if not user_request:
            objects = cls.repo.read_with_no_query()
        else:
            objects = cls.repo.read()
        if object_type != 'project':
            queryJson['type'] = object_type
        if not user_request:
            queryJson['privacy'] = 'public'
            # queryJson['type'] = object_type
            if object_type == 'module' and category:
                queryJson['category'] = category
        if status:
            queryJson['status'] = status
        if search_query:
            regex = re.compile('.*' + search_query + '.*', re.IGNORECASE)
            queryJson['tags__icontains'] = regex
            objects = objects(**queryJson)

            tag_freqs = cls.my_item_frequencies_map_reduce(
                objects, 'tags', object_type, user_request=user_request)
            top_tags = sorted(((k, v) for k, v in tag_freqs.items()
                               if search_query.lower() in k.lower()),
                              key=itemgetter(1),
                              reverse=True)[:max_number]
            return top_tags
        else:
            objects = objects(**queryJson)
            tag_freqs = cls.my_item_frequencies_map_reduce(
                objects, 'tags', object_type, user_request=user_request)
            # tag_freqs = objects.item_frequencies('tags', normalize=False)
            top_tags = sorted(tag_freqs.items(),
                              key=itemgetter(1),
                              reverse=True)[:5]
            return top_tags

    @classmethod
    def my_item_frequencies_map_reduce(cls,
                                       objects,
                                       field,
                                       project_type,
                                       user_request=False):
        map_func = """
            function() {
                var path = '{{~%(field)s}}'.split('.');
                var field = this;

                for (p in path) {
                    if (typeof field != 'undefined')
                       field = field[path[p]];
                    else
                       break;
                }
                if (field && field.constructor == Array) {
                    field.forEach(function(item) {
                        emit(item, 1);
                    });
                } else if (typeof field != 'undefined') {
                    emit(field, 1);
                } else {
                    emit(null, 1);
                }
            }
        """ % {
            'field': field
        }
        reduce_func = """
            function(key, values) {
                var total = 0;
                var valuesSize = values.length;
                for (var i=0; i < valuesSize; i++) {
                    total += parseInt(values[i], 10);
                }
                return total;
            }
        """
        if user_request:
            table = 'inline' + '_' + 'request_' + project_type
        else:
            table = 'inline' + '_' + project_type
        values = objects.map_reduce(map_func, reduce_func, table)
        frequencies = {}
        for f in values:
            key = f.key
            if isinstance(key, float):
                if int(key) == key:
                    key = int(key)
            frequencies[key] = int(f.value)

        return frequencies

    @classmethod
    def send_email(cls, email, subject, text):
        msg = MIMEMultipart('mixed')
        msg['Subject'] = subject
        msg['From'] = USERNAME + ' <' + USERNAME + '>'
        receiver = email
        msg['To'] = email
        text_plain = MIMEText(text, 'html', 'utf-8')
        msg.attach(text_plain)
        smtp = smtplib.SMTP()
        smtp.connect(SMTP_SERVER)
        smtp.login(USERNAME, PASSWORD)
        smtp.sendmail(SENDER, receiver, msg.as_string())
        smtp.quit()
コード例 #11
0
ファイル: data_set_business.py プロジェクト: zjn0224/mo
class DatasetBusiness(ProjectBusiness, GeneralBusiness):
    repo = Repo(project.Dataset)
コード例 #12
0
 def get_by_user(cls, user):
     courses = Repo(Course).read()
     res = {}
     for course in courses:
         res[course.id] = cls.business.get_by_user(user, course)
     return res
コード例 #13
0
ファイル: module_business.py プロジェクト: StoneXue/mo
class ModuleBusiness(ProjectBusiness):
    repo = Repo(project.Module)

    @classmethod
    def create_project(cls,
                       name,
                       description,
                       user,
                       privacy='private',
                       tags=None,
                       user_token='',
                       type='app',
                       category='model',
                       **kwargs):
        """
        Create a new project

        :param user:
        :param privacy:
        :param category:
        :param name: str
        :param description: str
        :param type: string (app/module/dataset)
        :param tags: list of string
        :param user_token: string
        :return: a new created project object
        """
        if tags is None:
            tags = []
        user_ID = user.user_ID

        # user_path = os.path.join(USER_DIR, user_ID)
        # project_path = os.path.join(USER_DIR, user_ID, name)

        # generate project dir
        project_path = cls.gen_dir(user_ID, name)
        temp_path = cls.gen_dir(user_ID, uuid.uuid4().hex)
        # temp_tool_path = cls.gen_dir(user_ID+'_tool', uuid.uuid4().hex)

        # init git repo
        cls.init_git_repo(user_ID, name)

        # clone to project dir
        repo = cls.clone(user_ID, name, project_path)

        # create template to temp path
        cookiecutter(cat_dict[category],
                     no_input=True,
                     output_dir=temp_path,
                     extra_context={
                         "author_name": user_ID,
                         "module_name": name,
                         "module_type": category,
                         "module_description": description,
                     })

        # copy temp project to project dir and remove temp dir
        # need to keep .git, cannot use cls.copytree_wrapper
        copy_tree(os.path.join(temp_path, name), project_path)
        remove_tree(temp_path)

        # tools_path = os.path.join(project_path, 'dev_cmd_tools')
        # os.makedirs(tools_path)
        # copy_tree(temp_tool_path, tools_path)

        # cookiecutter(
        #     TOOL_REPO,
        #     no_input=True, output_dir=project_path,
        #     extra_context={
        #         "author_name": user_ID,
        #         "module_name": name,
        #         "module_type": category,
        #         "module_description": description,
        #     })

        # add all
        repo.git.add(A=True)
        # initial commit
        repo.index.commit('Initial Commit')
        repo.remote(name='origin').push()
        commit = cls.update_project_commits(repo)
        # auth jupyterhub with user token
        res = cls.auth_hub_user(user_ID, name, user_token)

        # create a new project object
        create_time = datetime.utcnow()
        return cls.repo.create_one(
            name=name,
            description=description,
            create_time=create_time,
            update_time=create_time,
            type=type,
            tags=tags,
            hub_token=res.get('token'),
            path=project_path,
            user=user,
            status='inactive',
            privacy=privacy,
            category=category,
            commits=[commit],
            repo_path=f'http://{GIT_SERVER_IP}/repos/{user_ID}/{name}')

    # @classmethod
    # def get_by_id(cls, project_id, yml=False):
    #     module = ProjectBusiness.get_by_id(project_id)
    #     # TODO 完全加入这个参数后去掉
    #     if module.module_path is None:
    #         user_ID = module.user.user_ID
    #         dir_path = os.path.join(MODULE_DIR, user_ID, module.name)
    #         module.module_path = dir_path
    #         module.save()
    #     if yml and module.module_path:
    #         module.input, module.output = cls.load_module_params(module)
    #     return module

    @staticmethod
    def load_module_params(module, version=DEFAULT_DEPLOY_VERSION):
        if not version:
            if len(module.versions) > 0:
                version = module.versions[-1]
            else:
                version = DEFAULT_DEPLOY_VERSION
        yml_path = os.path.join(module.module_path, version, tail_path)
        if os.path.exists(yml_path):
            with open(yml_path, 'r') as stream:
                obj = yaml.load(stream)
                return {'input': obj.get('input'), 'output': obj.get('output')}
        else:
            return {'input': {}, 'output': {}}

    @classmethod
    def deploy_or_publish(cls,
                          project_id,
                          commit_msg,
                          version=DEFAULT_DEPLOY_VERSION):
        module = cls.get_by_id(project_id)

        module.status = 'deploying'
        module.save()

        # commit module
        cls.commit(project_id, commit_msg, version)

        module.module_path = os.path.join(MODULE_DIR, module.user.user_ID,
                                          module.name)
        dst = os.path.join(module.module_path, version)

        # copy module
        cls.copytree_wrapper(module.path,
                             dst,
                             ignore=shutil.ignore_patterns('.git'))
        # install module env
        subprocess.call(['bash', 'install_venv.sh', os.path.abspath(dst)])

        bind_path = '/home/jovyan/modules'

        # copy compile related files to module src
        # shutil.copy('./setup.py',
        #             os.path.join(module.module_path, version, 'src'))
        # shutil.copy('./compile.sh',
        #             os.path.join(module.module_path, version, 'src'))
        #
        # compile_dir = module.module_path.replace('./server3/lib/modules',
        #                                          bind_path)
        # compile_dir = os.path.join(compile_dir, version, 'src')
        #
        # # start container with the singleuser docker, mount the whole pyserver
        # # compile the main.py and delete compile related files
        # client = docker.from_env()
        # client.containers.run(
        #     "singleuser:latest",
        #     volumes={os.path.abspath(MODULE_DIR):
        #                  {'bind': bind_path, 'mode': 'rw'}},
        #     command=f"/bin/bash -c 'cd {compile_dir} && bash ./compile.sh'")

        # if publish update privacy and version
        if version != DEFAULT_DEPLOY_VERSION:
            module.privacy = 'public'
            module.versions.append(version)

        module.status = 'active'
        module.save()

        return module

    @classmethod
    def run_test(cls, project_id):
        project = cls.get_by_id(project_id)
        result = GDValidation.run_test(project.path, project.name,
                                       project.user.user_ID, project.category)
        failures = [f[1] for f in result.failures]
        return failures
コード例 #14
0
ファイル: user_repo.py プロジェクト: zjn0224/mo
 def delete_by_user_ID(self, user_ID):
     return Repo.delete_unique_one(self, {'user_ID': user_ID})
コード例 #15
0
ファイル: staging_data_set_repo.py プロジェクト: zjn0224/mo
 def read_by_name_and_project(self, name, project):
     return Repo.read_unique_one(self, {'name': name, 'project': project})
コード例 #16
0
ファイル: user_repo.py プロジェクト: zjn0224/mo
 def read_by_user_ID(self, user_ID):
     return Repo.read_unique_one(self, {'user_ID': user_ID})
コード例 #17
0
ファイル: staging_data_set_repo.py プロジェクト: zjn0224/mo
 def update_by_name_and_project(self, name, project, field, value):
     return Repo.update_unique_one(self, {
         'name': name,
         'project': project
     }, {field: value})
コード例 #18
0
class AppBusiness(ProjectBusiness, GeneralBusiness):
    repo = Repo(project.App)
    __cls = project.App
    base_func_path = './functions'

    @classmethod
    def create_project(cls, *args, **kwargs):
        ProjectBusiness.repo = Repo(project.App)
        return ProjectBusiness.create_project(*args,
                                              status='inactive',
                                              **kwargs)

    @classmethod
    def deploy_or_publish(cls,
                          app_id,
                          commit_msg,
                          handler_file_path,
                          version='dev'):
        app = cls.get_by_id(app_id)
        app.status = 'deploying'
        app.save()
        modules = [m.user.user_ID + '/' + m.name for m in app.used_modules]
        if modules is None:
            modules = []

        service_name = '-'.join([app.user.user_ID, app.name, version])
        service_name_no_v = '-'.join([app.user.user_ID, app.name])
        # faas new in functions dir
        call([
            'faas-cli', 'new', service_name, '--lang=python3',
            f'--gateway=http://{DOCKER_IP}:8080'
        ],
             cwd=cls.base_func_path)
        # target path = new path
        func_path = os.path.join(cls.base_func_path, service_name)
        module_dir_path = os.path.join(func_path, 'modules')

        cls.copytree_wrapper(app.path,
                             func_path,
                             ignore=shutil.ignore_patterns('.git'))

        # rename py to handler.py
        handler_file_path = handler_file_path.replace('work', func_path)
        handler_file_path = os.path.join(func_path, handler_file_path)
        handler_file_name = handler_file_path.split('/')[-1]
        handler_dst_path = handler_file_path.replace(handler_file_name,
                                                     'handler.py')
        shutil.copy(handler_file_path, handler_dst_path)

        # change some configurable variable to deploy required
        cls.modify_handler_py(handler_dst_path)

        # copy modules
        for module in modules:
            owner_ID = module.split('/')[0]
            module_path = os.path.join(MODULE_DIR, module)
            module_path_target = os.path.join(module_dir_path, module)
            try:
                os.makedirs(os.path.join(module_dir_path, owner_ID))
            except FileExistsError:
                print('dir exists, no need to create')
            # copy module tree to target path
            print(module_path, module_path_target)
            cls.copytree_wrapper(module_path,
                                 module_path_target,
                                 ignore=shutil.ignore_patterns('.git'))

        # deploy
        call(['faas-cli', 'build', '-f', f'./{service_name}.yml'],
             cwd=cls.base_func_path)
        call(['faas-cli', 'deploy', '-f', f'./{service_name}.yml'],
             cwd=cls.base_func_path)

        cls.commit(app_id, commit_msg, version)

        # when not dev(publish), change the privacy etc
        if version != 'dev':
            app.app_path = os.path.join(cls.base_func_path, service_name_no_v)
            app.privacy = 'public'
            app.versions.append(version)

        app.status = 'active'
        app.save()
        return app

    @staticmethod
    def modify_handler_py(py_path):
        for line in fileinput.input(files=py_path, inplace=1):
            line = re.sub(r"""work_path = ''""",
                          r"""work_path = 'function/'""", line.rstrip())
            print(line)

    @staticmethod
    def load_app_params(app, version=''):
        # TODO remove 'try except' after modules all have versions
        try:
            # if no version provided, get the latest
            if not version:
                version = app.versions[-1]
        except:
            version = ''
        yml_path = os.path.join('-'.join([app.app_path, version]),
                                yaml_tail_path)
        with open(yml_path, 'r') as stream:
            obj = yaml.load(stream)
            return {'input': obj.get('input'), 'output': obj.get('output')}

    @classmethod
    def add_used_module(cls, app_id, used_modules, func):
        app = cls.get_by_id(app_id)
        app_yaml_path = os.path.join(app.path, yaml_tail_path)
        args = {}
        output = {}
        # copy module yaml to app yaml
        for module in used_modules:
            cls.insert_module_env(app, module)
            # copy yaml
            input_args = module.to_mongo()['args']['input'].get(func, {})
            output_args = module.to_mongo()['args']['output'].get(func, {})
            if os.path.isfile(app_yaml_path):
                with open(app_yaml_path, 'r') as stream:
                    # read args
                    obj = yaml.load(stream)
                    args = cls.replace_dup_n_update(obj['input'], input_args,
                                                    module.name)
                    output = cls.update_with_module_name(
                        obj.get('output', {}), output_args, module.name)
            else:
                args = cls.update_with_module_name(args, input_args,
                                                   module.name)
                output = cls.update_with_module_name(output, output_args,
                                                     module.name)
            # write new args
            with open(app_yaml_path, 'w') as stream:
                yaml.dump({
                    'input': args,
                    'output': output
                },
                          stream,
                          default_flow_style=False)
        return cls.repo.add_to_set(app_id, used_modules=used_modules)

    @staticmethod
    def insert_module_env(app, module):
        client = docker.from_env()
        # copy venv
        module_user_ID = module.user.user_ID
        app_user_ID = app.user.user_ID.replace('_', '_5F')
        app_name = app.name.replace('_', '_5F')
        container = client.containers. \
            get(f'jupyter-{app_user_ID}_2B{app_name}')
        container.exec_run([
            '/bin/bash', '/home/jovyan/add_venv.sh',
            f'{module_user_ID}/{module.name}'
        ])

    @classmethod
    def replace_dup_n_update(cls, args, func_args, module_name):
        # find duplicate arg name of module_arg and app_arg and
        # replace the name
        args = cls.replace_dup_name(args, func_args, module_name)
        # edit app args
        return cls.update_with_module_name(args, func_args, module_name)

    @staticmethod
    def replace_dup_name(args, func_args, module_name):
        conv_args = args_converter(args)
        for k, v in func_args.items():
            name = v['name']
            if v['name'] in conv_args:
                key = conv_args[name]['key']
                args[key]['name'] = module_name + '_' + args[key]['name']
        return args

    @staticmethod
    def update_with_module_name(app_args, func_args, module_name):
        for k, v in func_args.items():
            app_args[module_name + '_' + k] = v
        return app_args

    # @classmethod
    # def create_project(cls, name, description, user, privacy='private',
    #                    tags=None, user_token='', type='app'):
    #     pass

    @classmethod
    def list_projects_chat(cls,
                           search_query,
                           page_no=None,
                           page_size=None,
                           default_max_score=0.4,
                           privacy="public"):
        start = (page_no - 1) * page_size
        end = page_no * page_size

        # all_apps = cls.get_all()
        all_apps = cls.repo.read(query={"privacy": privacy})
        # all_apps = cls.read(query={"privacy": privacy})

        #  比对打分
        for app in all_apps:
            name_score = synonyms.compare(search_query, app.name, seg=True)
            description_score = synonyms.compare(search_query,
                                                 app.description,
                                                 seg=True)
            app.score = (name_score + description_score) / 2
        # 筛选掉小于 description_score
        apps = list(
            filter(lambda app: app.score >= default_max_score, all_apps))

        count = len(apps)
        apps = sorted(apps, key=lambda item: -item.score)
        return Objects(objects=apps[start:end],
                       count=count,
                       page_no=page_no,
                       page_size=page_size)

    @classmethod
    def increment_usage_count(cls, api_id):
        app = cls.get_by_id(api_id)
        app.usage_count += 1
        return app.save()
コード例 #19
0
 def read_by_item(self, owned_type, owned_item):
     return Repo.read_unique_one(self, {owned_type: owned_item})
コード例 #20
0
class ModuleBusiness(ProjectBusiness):
    repo = Repo(project.Module)

    @classmethod
    def create_project(cls,
                       name,
                       description,
                       user,
                       privacy='private',
                       tags=None,
                       user_token='',
                       type='app',
                       category='model'):
        """
        Create a new project

        :param name: str
        :param description: str
        :param user_ID: ObjectId
        :param is_private: boolean
        :param type: string (app/module/dataset)
        :param tags: list of string
        :param user_token: string
        :return: a new created project object
        """
        if tags is None:
            tags = []
        user_ID = user.user_ID

        # user_path = os.path.join(USER_DIR, user_ID)
        # project_path = os.path.join(USER_DIR, user_ID, name)

        # generate project dir
        project_path = cls.gen_dir(user_ID, name)
        temp_path = cls.gen_dir(user_ID, uuid.uuid4().hex)

        # init git repo
        cls.init_git_repo(user_ID, name)

        # clone to project dir
        repo = cls.clone(user_ID, name, project_path)

        # create template to temp path
        cookiecutter(cat_dict[category],
                     no_input=True,
                     output_dir=temp_path,
                     extra_context={
                         "author_name": user_ID,
                         "module_name": name,
                         "module_type": category,
                         "module_description": description,
                     })

        # copy temp project to project dir and remove temp dir
        copy_tree(os.path.join(temp_path, name), project_path)
        remove_tree(temp_path)

        # add all
        repo.git.add(A=True)
        # initial commit
        repo.index.commit('Initial Commit')
        repo.remote(name='origin').push()

        # auth jupyterhub with user token
        res = cls.auth_hub_user(user_ID, name, user_token)

        # create a new project object
        create_time = datetime.utcnow()
        return cls.repo.create_one(
            name=name,
            description=description,
            create_time=create_time,
            update_time=create_time,
            type=type,
            tags=tags,
            hub_token=res.get('token'),
            path=project_path,
            user=user,
            privacy=privacy,
            category=category,
            repo_path=f'http://{GIT_SERVER_IP}/repos/{user_ID}/{name}')

    # @classmethod
    # def get_by_id(cls, project_id, yml=False):
    #     module = ProjectBusiness.get_by_id(project_id)
    #     # TODO 完全加入这个参数后去掉
    #     if module.module_path is None:
    #         user_ID = module.user.user_ID
    #         dir_path = os.path.join(MODULE_DIR, user_ID, module.name)
    #         module.module_path = dir_path
    #         module.save()
    #     if yml and module.module_path:
    #         module.input, module.output = cls.load_module_params(module)
    #     return module

    @staticmethod
    def load_module_params(module):
        yml_path = os.path.join(module.module_path, tail_path)
        with open(yml_path, 'r') as stream:
            obj = yaml.load(stream)
            return {'input': obj.get('input'), 'output': obj.get('output')}

    @classmethod
    def publish(cls, project_id):
        module = cls.get_by_id(project_id)
        module.module_path = os.path.join(MODULE_DIR, module.user.user_ID,
                                          module.name)
        module.privacy = 'public'
        module.save()

        dst = module.module_path
        # if dir exists, remove it and copytree, cause copytree will
        #  create the dir
        if os.path.exists(dst):
            shutil.rmtree(dst)
        shutil.copytree(module.path, dst)
        # WORKON_HOME=./ pipenv install vv
        subprocess.call(['bash', 'install_venv.sh', os.path.abspath(dst)])

        return module

    @classmethod
    def run_test(cls, project_id):
        project = cls.get_by_id(project_id)
        result = GDValidation.run_test(project.path, project.name,
                                       project.user.user_ID, project.category)
        failures = [f[1] for f in result.failures]
        return failures
コード例 #21
0
 def delete_by_item(self, owned_item, owned_type):
     return Repo.delete_unique_one(self, {owned_type: owned_item})
コード例 #22
0
# -*- coding: UTF-8 -*-
import uuid
import datetime

from server3.entity.api import Api
from server3.repository.general_repo import Repo
from server3.business import world_business
from server3.entity.world import CHANNEL
api_repo = Repo(Api)


def get(**kwargs):
    return api_repo.read(kwargs)


def add(name, user, **kwargs):
    """
    根据 name, userId 生成url
    未来提供自定义 url
    :param name:
    :type name:
    :param user:
    :type user:
    :param kwargs:
    :type kwargs:
    :return:
    :rtype:
    """
    # message = "{}创建了app{}".format(user.name, name)
    # world_business.system_send(channel=CHANNEL.api, message=message)
    url = "/" + user.user_ID + "/" + name + "/" + uuid.uuid4()
コード例 #23
0
 def read_by_type(self, owned_type):
     return Repo.read(self, {'%s__exists' % owned_type: True})
コード例 #24
0
 def __init__(self, instance):
     Repo.__init__(self, instance)
コード例 #25
0
ファイル: toolkit_repo.py プロジェクト: zjn0224/mo
 def read_by_toolkit_name(self, toolkit_obj):
     return Repo.read_unique_one(self, {'name': toolkit_obj.name})