def list_apps(cls, search_query=None, page_no=1, page_size=5, tag=None, choose_type=None): if tag: # 通过标签筛选 if not isinstance(tag, list): tag = tag.split(',') apps = AppBusiness.get_objects(search_query=None, has_version=True, privacy='public', tags=tag, page_size=page_size, page_no=page_no) apps, total_count = apps.objects, apps.count # AppBusiness.read({'tags': tag, 'privacy': 'public'}) apps = cls._filter_apps(apps, 'top_rates') elif choose_type: # 其他 apps = AppBusiness.get_objects(search_query=None, has_version=True, privacy='public', page_size=page_size, page_no=page_no) apps, total_count = apps.objects, apps.count # 切换 tab 键 apps = cls._filter_apps(apps, choose_type) else: # search_query apps = AppBusiness.get_objects(search_query=search_query, has_version=True, privacy='public', page_size=page_size, page_no=page_no) apps, total_count = apps.objects, apps.count apps = cls._filter_apps(apps, 'top_rates') # 只需要返回最后一个版本 for app in apps: path = app.path + '/OVERVIEW.md' if os.path.exists(path): with open(path, 'r') as file: app.overview = file.read() app.versions = app.versions[-1] app.average_rate = app.avg_rate app.call_num = app.call app.user_ID = app.user.user_ID app.user_name = app.user.username app.avatarV = app.user.avatarV app.avatar_url = app.user.avatar_url app.args = AppBusiness.load_app_params(app, app.versions[-1]) return apps, total_count
def insert_envs(cls, user_ID, app_name): """ copy used modules and datasets to jl container when start :param user_ID: :param app_name: :return: """ user = UserBusiness.get_by_user_ID(user_ID) app = AppBusiness.read_unique_one(name=app_name, user=user) for used_module in app.used_modules: AppBusiness.insert_module_env(app, used_module.module, used_module.version)
def add_rating(cls, **kwargs): # 取出UserId 和 App Id # print(kwargs) user = UserBusiness.get_by_user_ID(kwargs['user_ID']) app = AppBusiness.get_by_id(kwargs['app']) if user.user_ID == app.user.user_ID: return [] rate = AppRatingBusiness.app_rating_repo.read({ 'app': app, 'user': user }) # print(rate) if len(rate) > 0: rate = rate[0] # 进行更新内容, 但是不会添加新的记录 del kwargs['user_ID'] del kwargs['app'] # rate.update(**kwargs) # rate.save() # return cls._add_rate_info(rate) # print(rate.id, kwargs) return cls.update_rating(rate.id, **kwargs) else: kwargs['create_time'] = datetime.datetime.utcnow() kwargs['user'] = user kwargs['app'] = app rate = AppRatingBusiness.add_rating(**kwargs) return cls._add_rate_info(rate)
def check_task_completion(cls, project_id, type, check_type): lt = cls.business.get_by_project(project_id, type) if lt.project_type: project = lt[lt.project_type] # job tut check job finished if check_type == 'job' and lt.level == 1 and lt.num == 4: for job in project.jobs: if job.status == JobStatus.COMPLETE or \ job.status == JobStatus.FAILED: lt.finish_task_step() return lt if check_type == 'deploy': # app tut check app run if lt.level == 1 and lt.num == 1: for version in [*project.versions, DEFAULT_DEPLOY_VERSION]: _, output_json, status, _ = AppBusiness.run_app( project.id, {}, version) # print(status, version, output_json) if status == 'success': lt.finish_task_step() return lt else: # other projects just deployed is done lt.finish_task_step() return lt raise Exception('task checking failed')
def get_single_app(cls, app_id): app = AppBusiness.get_by_id(app_id) path = app.path + '/OVERVIEW.md' if os.path.exists(path): with open(path, 'r') as file: app.overview = file.read() app.versions = [app.versions[-1]] app.average_rate = app.avg_rate app.call_num = app.call app.user_ID = app.user.user_ID app.user_name = app.user.username app.avatarV = app.user.avatarV app.args = AppBusiness.load_app_params(app, app.versions[-1]) app.avatar_url = app.user.avatar_url app.view_num = EventBusiness.get_number({'app': app, 'action': "view"}) return app
def related_app(cls, app_id, user_ID=None, page_size=7): app = AppBusiness.get_by_id(app_id) tags = app.tags related_apps = AppBusiness.get_objects(tags=tags, has_version=True, privacy='public') related_apps, _ = related_apps.objects, related_apps.count related_ids = [str(a.id) for a in related_apps] # 拷贝一份 back_apps = [app for app in related_apps] # 删除重复 if app_id in related_ids: index = related_ids.index(app_id) del back_apps[index] if user_ID: self_app, _ = cls.self_apps(user_ID) # 拼接, 去重 for a in self_app: if str( a.id ) not in related_ids and 'tutorial' not in a.display_name.lower( ): back_apps.append(a) related_ids.append(str(a.id)) # 判断是否小于七个 if len(related_apps) < page_size: # 判断当前个数, 寻找剩余数量 supplement_apps = AppBusiness.get_objects(page_size=page_size, has_version=True, privacy='public').objects for a in supplement_apps: if str( a.id ) not in related_ids and 'tutorial' not in a.display_name.lower( ): back_apps.append(a) # 添加数据 for app in back_apps: path = app.path + '/OVERVIEW.md' if os.path.exists(path): with open(path, 'r') as file: app.overview = file.read() app.versions = app.versions[-1] app.average_rate = app.avg_rate app.call_num = app.call app.avatar_url = app.user.avatar_url return back_apps, page_size
def get_rate_by_app_and_user(cls, user_ID, app_id): user = UserBusiness.get_by_user_ID(user_ID) app = AppBusiness.get_by_id(app_id) rate = AppRatingBusiness.app_rating_repo.read({ 'app': app, 'user': user }) if len(rate) > 0: rate = rate[0] return cls._add_rate_info(rate) return []
def run_app(cls, app_id, input_json, user_ID, version): """ :param app_id: app id :param input_json: :param user_ID: :param version: :return: :rtype: """ app = AppBusiness.get_by_id(project_id=app_id) url = '-'.join([app.user.user_ID, app.name, version]) domin = f"http://{DOCKER_IP}:8080/function/" url = domin + url payload = json.dumps(input_json) headers = { 'content-type': "application/json", } response = requests.request("POST", url, data=payload, headers=headers) pattern = re.compile(r'STRHEAD(.+?)STREND', flags=re.DOTALL) results = pattern.findall(response.text) print(results, 111) try: output_json = json.loads(results[0]) except IndexError as e: try: errors = cls.business.get_service_logs(app, version) except IndexError as e: output_json = { 'errors': ['Service is down please deploy again!'] } else: output_json = {'errors': errors} # output_json = response.json() # 成功调用后 在新的collection存一笔 user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID) # 筛选 input_json StatisticsBusiness.use_app( user_obj=user_obj, app_obj=app, output_json=output_json # input_json=input_json, # output_json=output_json ) return output_json
def self_apps(cls, user_ID): apps = AppBusiness.get_objects( user=UserBusiness.get_by_user_ID(user_ID), has_version=True) apps, total_count = apps.objects, apps.count for app in apps: path = app.path + '/OVERVIEW.md' if os.path.exists(path): with open(path, 'r') as file: app.overview = file.read() app.versions = app.versions[-1] app.average_rate = app.avg_rate app.call_num = app.call app.view_num = EventBusiness.get_number({ app.type: app, 'action': "view" }) return apps, total_count
def list_tags(cls): # get all deployed # 'versions': {'$gt': 1}, 'privacy': 'public' apps = AppBusiness.read({'privacy': 'public'}) apps = apps(versions__0__exists=True) back_tags = [] for app in apps: tags = app.tags back_tags.extend([tag.id for tag in tags]) back_tags = dict(Counter(back_tags)) back_tags = sorted(back_tags.items(), key=lambda item: item[1], reverse=True) return [{ 'class': class_name, 'num': num } for class_name, num in back_tags]
def get_chat_api_list(): """ 带搜索关键字的, :return: :rtype: """ # user_ID = get_jwt_identity() page_no = int(request.args.get('page_no', 1)) page_size = int(request.args.get('page_size', 5)) search_query = request.args.get('search_query', None) default_max_score = float(request.args.get('max_score', 0.1)) privacy = request.args.get('privacy') if not search_query: return jsonify({'response': 'no search_query arg'}), 400 try: api_list = AppBusiness.list_projects_chat( search_query, page_no=page_no, page_size=page_size, default_max_score=default_max_score, privacy=privacy) except Warning as e: return jsonify({ "response": [], "message": e.args[0]["hint_message"] }), 200 except Error as e: return jsonify({"message": e.args[0]["hint_message"]}), 404 else: objects = json_utility.me_obj_list_to_json_list(api_list.objects) return jsonify({ "response": { "objects": objects, "count": api_list.count, "page_no": api_list.page_no, "page_size": api_list.page_size, } }), 200
def run_app(cls, app_id, input_json, user_ID): """ :param app_id: app id :type app_id: ObjectId :param input_json: :type input_json: :param user_ID: :type user_ID: :return: :rtype: """ app = AppBusiness.get_by_id(project_id=app_id) url = app.user.user_ID + "-" + app.name domin = f"http://{DOCKER_IP}:8080/function/" url = domin + url payload = json.dumps(input_json) headers = { 'content-type': "application/json", } response = requests.request("POST", url, data=payload, headers=headers) print(response.text) pattern = re.compile(r'STRHEAD(.+?)STREND', flags=re.DOTALL) results = pattern.findall(response.text) output_json = json.loads(results[0]) print(output_json) # output_json = response.json() # 成功调用后 在新的collection存一笔 user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID) # 筛选 input_json StatisticsBusiness.use_app( user_obj=user_obj, app_obj=app, output_json=output_json # input_json=input_json, # output_json=output_json ) return output_json
def insert_envs(cls, user_ID, app_name): user = UserBusiness.get_by_user_ID(user_ID) app = AppBusiness.read_unique_one(name=app_name, user=user) for module in app.used_modules: AppBusiness.insert_module_env(app, module)
def add_used_module(cls, app_id, used_modules, func): used_modules = [ModuleBusiness.get_by_id(mid) for mid in used_modules] for module in used_modules: module.args = ModuleBusiness.load_module_params( module) return AppBusiness.add_used_module(app_id, used_modules, func)
def nb_to_script(app_id): data = request.get_json() optimise = data.get('optimise') nb_path = data.get('nb_path') AppBusiness.nb_to_script(app_id, nb_path, optimise) return jsonify({"response": 1})
def get_action_entity(cls, app_id, **kwargs): app = AppBusiness.get_by_id(app_id) return AppBusiness.get_action_entity(app, **kwargs)
def get_call_num(cls, app_id): app = AppBusiness.get_by_id(app_id) return app.call