def get_group_app_metadata(self, group_id, tenant): all_data = dict() compose_group_info = compose_repo.get_group_compose_by_group_id( group_id) compose_service_relation = None if compose_group_info: compose_service_relation = compose_relation_repo.get_compose_service_relation_by_compose_id( compose_group_info.compose_id) group_info = group_repo.get_group_by_id(group_id) service_group_relations = group_service_relation_repo.get_services_by_group( group_id) service_ids = [sgr.service_id for sgr in service_group_relations] services = service_repo.get_services_by_service_ids(*service_ids) all_data["compose_group_info"] = compose_group_info.to_dict( ) if compose_group_info else None all_data["compose_service_relation"] = [ relation.to_dict() for relation in compose_service_relation ] if compose_service_relation else None all_data["group_info"] = group_info.to_dict() all_data["service_group_relation"] = [ sgr.to_dict() for sgr in service_group_relations ] apps = [] total_memory = 0 for service in services: if service.service_source == "third_party" or service.create_status != "complete": continue total_memory += service.min_memory * service.min_node app_info = self.get_service_details(tenant, service) apps.append(app_info) all_data["apps"] = apps return total_memory, json.dumps(all_data)
def __get_current_app_and_version(self): """ :return: app object app_version object """ from console.services.market_app_service import market_app_service group_id = service_group_relation_repo.get_group_id_by_service( self.service) service_ids = group_service_relation_repo.get_services_by_group( group_id).values_list("service_id", flat=True) versions = service_source_repo.get_service_sources( self.tenant.tenant_id, service_ids).exclude(version=None).values_list("version", flat=True) sorted_versions = sorted( versions, key=lambda x: map(lambda y: int(filter(str.isdigit, str(y))), x.split("."))) current_version = sorted_versions[-1] if not self.install_from_cloud: app, app_version = rainbond_app_repo.get_rainbond_app_and_version( self.tenant.enterprise_id, self.service_source.group_key, current_version) else: app, app_version = market_app_service.get_app_from_cloud( self.tenant, self.service_source.group_key, current_version) self.market_id = app.market_id if app_version: self.template = json.loads(app_version.app_template) self.current_app = app self.current_version = app_version
def get_enterprise_app_component_list(self, app_id, page=1, page_size=10): group_relation_services = group_service_relation_repo.get_services_by_group(app_id) if not group_relation_services: return [], 0 service_ids = group_relation_services.values_list("service_id", flat=True) services = service_repo.get_service_by_service_ids(service_ids) return services[(page - 1) * page_size:page * page_size], services.count()
def get_group_app_metadata(self, group_id, tenant): all_data = dict() compose_group_info = compose_repo.get_group_compose_by_group_id( group_id) compose_service_relation = None if compose_group_info: compose_service_relation = compose_relation_repo.get_compose_service_relation_by_compose_id( compose_group_info.compose_id) group_info = group_repo.get_group_by_id(group_id) service_group_relations = group_service_relation_repo.get_services_by_group( group_id) service_ids = [sgr.service_id for sgr in service_group_relations] services = service_repo.get_services_by_service_ids(*service_ids) all_data["compose_group_info"] = compose_group_info.to_dict( ) if compose_group_info else None all_data["compose_service_relation"] = [ relation.to_dict() for relation in compose_service_relation ] if compose_service_relation else None all_data["group_info"] = group_info.to_dict() all_data["service_group_relation"] = [ sgr.to_dict() for sgr in service_group_relations ] apps = [] for service in services: app_info = self.get_service_details(tenant, service) apps.append(app_info) all_data["apps"] = apps return json.dumps(all_data)
def get_rainbond_services(self, group_id, group_key, upgrade_group_id=None): """获取云市应用下的所有组件""" gsr = group_service_relation_repo.get_services_by_group(group_id) service_ids = gsr.values_list('service_id', flat=True) components = service_repo.get_services_by_service_ids_and_group_key(group_key, service_ids) if upgrade_group_id: return components.filter(tenant_service_group_id=upgrade_group_id) return components
def get_group_app_metadata(self, group_id, tenant): all_data = dict() compose_group_info = compose_repo.get_group_compose_by_group_id(group_id) compose_service_relation = None if compose_group_info: compose_service_relation = compose_relation_repo.get_compose_service_relation_by_compose_id( compose_group_info.compose_id) group_info = group_repo.get_group_by_id(group_id) service_group_relations = group_service_relation_repo.get_services_by_group(group_id) service_ids = [sgr.service_id for sgr in service_group_relations] services = service_repo.get_services_by_service_ids(service_ids) all_data["compose_group_info"] = compose_group_info.to_dict() if compose_group_info else None all_data["compose_service_relation"] = [ relation.to_dict() for relation in compose_service_relation] if compose_service_relation else None all_data["group_info"] = group_info.to_dict() all_data["service_group_relation"] = [sgr.to_dict() for sgr in service_group_relations] apps = [] total_memory = 0 plugin_ids = [] for service in services: if service.service_source == "third_party" or service.create_status != "complete": continue total_memory += service.min_memory * service.min_node app_info, pids = self.get_service_details(tenant, service) plugin_ids.extend(pids) apps.append(app_info) all_data["apps"] = apps # plugin plugins = [] plugin_build_versions = [] plugin_config_groups = [] plugin_config_items = [] for plugin_id in plugin_ids: plugin = plugin_repo.get_plugin_by_plugin_id(tenant.tenant_id, plugin_id) if plugin is None: continue plugins.append(plugin.to_dict()) bv = build_version_repo.get_last_ok_one(plugin_id, tenant.tenant_id) if bv is None: continue plugin_build_versions.append(bv.to_dict()) pcgs = plugin_config_group_repo.list_by_plugin_id(plugin_id) if pcgs: plugin_config_groups.extend([p.to_dict() for p in pcgs]) pcis = plugin_config_items_repo.list_by_plugin_id(plugin_id) if pcis: plugin_config_items.extend([p.to_dict() for p in pcis]) all_data["plugin_info"] = {} all_data["plugin_info"]["plugins"] = plugins all_data["plugin_info"]["plugin_build_versions"] = plugin_build_versions all_data["plugin_info"]["plugin_config_groups"] = plugin_config_groups all_data["plugin_info"]["plugin_config_items"] = plugin_config_items return total_memory, json.dumps(all_data)
def get(self, request, team_id, region_name, app_id, *args, **kwargs): is_outer = request.GET.get("is_outer", False) if is_outer == "true": is_outer = True data = [] start = request.GET.get("start") end = request.GET.get("end") step = request.GET.get("step", 60) if not start or not end: raise ServiceHandleException(msg="params error", msg_show="缺少query参数") services_relation = group_service_relation_repo.get_services_by_group(self.app.ID) service_ids = services_relation.values_list('service_id', flat=True) if service_ids: services = service_repo.get_services_by_service_ids(service_ids).exclude(service_source="third_party") for service in services: is_outer_service = True has_plugin = False service_abled_plugins = app_plugin_service.get_service_abled_plugin(service) for plugin in service_abled_plugins: if plugin.category == PluginCategoryConstants.PERFORMANCE_ANALYSIS: has_plugin = True if is_outer: is_outer_service = False tenant_service_ports = port_service.get_service_ports(service) for service_port in tenant_service_ports: if service_port.is_outer_service: is_outer_service = True break if has_plugin and is_outer_service: dt = { "service_id": service.service_id, "service_cname": service.service_cname, "service_alias": service.service_alias, "monitors": [] } for k, v in list(monitor_query_range_items.items()): monitor = {"monitor_item": k} body = {} try: res, body = region_api.get_query_range_data(self.region_name, self.team.tenant_name, v % (service.service_id, start, end, step)) except Exception as e: logger.debug(e) if body.get("data"): if body["data"]["result"]: result_list = [] for result in body["data"]["result"]: result["value"] = [str(value) for value in result["value"]] result_list.append(result) body["data"]["result"] = result_list monitor.update(body) dt["monitors"].append(monitor) data.append(dt) serializers = ComponentMonitorSerializers(data=data, many=True) serializers.is_valid(raise_exception=True) return Response(serializers.data, status=200)
def get_group_services_by_id(self, app_id): services = group_service_relation_repo.get_services_by_group(app_id) if not services: return [] service_ids = [service.service_id for service in services] cp_service_ids = copy.copy(service_ids) for service_id in cp_service_ids: service_obj = service_repo.get_service_by_service_id(service_id) if service_obj and service_obj.service_source == "third_party": service_ids.remove(service_id) return service_ids
def get_service_by_service_key_and_group_id(self, service_key, group_id): rst = None services = group_service_relation_repo.get_services_by_group(group_id) if not services: return rst service_ids = [service.service_id for service in services] services = service_repo.get_services_by_service_ids(service_ids) if not services: return rst for service in services: if service.service_key == service_key: rst = service break return rst
def initial(self, request, *args, **kwargs): super(TeamAppServiceAPIView, self).initial(request, *args, **kwargs) service_id = kwargs.get("service_id") self.service = TenantServiceInfo.objects.filter( tenant_id=self.team.tenant_id, service_region=self.region_name, service_id=service_id).first() if not self.service: self.service = TenantServiceInfo.objects.filter( tenant_id=self.team.tenant_id, service_region=self.region_name, service_alias=service_id).first() if not self.service: raise ServiceHandleException(msg_show="组件不存在", msg="no found component", status_code=404) gsr = group_service_relation_repo.get_services_by_group(self.app.ID) if gsr: service_ids = gsr.values_list("service_id", flat=True) if self.service.service_id not in service_ids: raise ServiceHandleException(msg_show="组件不属于指定应用", msg="component not belong to this app", status_code=404)
def get_group_services_by_id(self, group_id): service_group = group_repo.get_group_by_id(group_id) if not service_group: return None, None tenant = team_repo.get_team_by_team_id(service_group.tenant_id) services = group_service_relation_repo.get_services_by_group(group_id) if not services: return tenant, [] service_ids = [service.service_id for service in services] cp_service_ids = copy.copy(service_ids) for service_id in cp_service_ids: service_obj = service_repo.get_service_by_service_id(service_id) if service_obj: if service_obj.service_source == "third_party": service_ids.remove(service_id) return tenant, service_ids
def __get_current_app_and_version(self): """ :return: app object app_version object """ group_id = service_group_relation_repo.get_group_id_by_service(self.service) service_ids = group_service_relation_repo.get_services_by_group(group_id).values_list("service_id", flat=True) service_sources = service_source_repo.get_service_sources(self.tenant.tenant_id, service_ids) versions = service_sources.exclude(version=None).values_list("version", flat=True) if versions: def foobar(y): try: s = filter(str.isdigit, str(y)) return int(s) except ValueError: # compatible with old version like 'RELEASE.2018-04-19T2' return -1 sorted_versions = sorted(versions, key=lambda x: map(foobar, x.split("."))) current_version = sorted_versions[-1] current_version_source = service_sources.filter(version=current_version).first() else: current_version = None current_version_source = None if not self.install_from_cloud: app, app_version = rainbond_app_repo.get_rainbond_app_and_version(self.tenant.enterprise_id, self.service_source.group_key, current_version) else: app, app_version = app_market_service.cloud_app_model_to_db_model(self.market, self.service_source.group_key, current_version) if app_version: self.template = json.loads(app_version.app_template) self.current_app = app self.current_version = app_version if current_version_source: self.service_source.create_time = current_version_source.create_time
def get_services_by_group_id(self, group_id): return group_service_relation_repo.get_services_by_group(group_id)
def get_app_service_count(self, group_id): services = group_service_relation_repo.get_services_by_group(group_id) if not services: return 0 return len(services)
def get_group_service_sources(self, group_id): """查询某一应用下的组件源信息""" gsr = group_service_relation_repo.get_services_by_group(group_id) service_ids = gsr.values_list('service_id', flat=True) return service_source_repo.get_service_sources_by_service_ids( service_ids)
def get_rainbond_services(self, group_id, group_key): """获取云市应用下的所有组件""" gsr = group_service_relation_repo.get_services_by_group(group_id) service_ids = gsr.values_list('service_id', flat=True) return service_repo.get_services_by_service_ids_and_group_key( group_key, service_ids)
def get_group_services(self, group_id): """查询某一应用下的组件""" gsr = group_service_relation_repo.get_services_by_group(group_id) service_ids = [gs.service_id for gs in gsr] services = service_repo.get_services_by_service_ids(service_ids) return services
def get(self, request, *args, **kwargs): """ 总览 团队应用信息 """ try: page = int(request.GET.get("page", 1)) page_size = int(request.GET.get("page_size", 10)) groups = group_repo.get_tenant_region_groups( self.team.tenant_id, self.response_region) total = len(groups) app_num_dict = {"total": total} start = (page - 1) * page_size end = page * page_size app_list = [] if groups: for group in groups: app_dict = dict() app_dict["group_name"] = group.group_name app_dict["group_id"] = group.ID # 分享记录和备份记录 share_record_num = share_repo.get_app_share_record_count_by_groupid( group_id=group.ID) app_dict["share_record_num"] = share_record_num backup_records = backup_record_repo.get_group_backup_records( self.team.tenant_id, self.response_region, group.ID) backup_record_num = len(backup_records) app_dict["backup_record_num"] = backup_record_num # 服务数量记录 services = group_service_relation_repo.get_services_by_group( group.ID) services_num = len(services) app_dict["services_num"] = services_num run_service_num = 0 if services: service_ids = [] for service in services: service_ids.append(service.service_id) status_list = base_service.status_multi_service( region=self.response_region, tenant_name=self.team_name, service_ids=service_ids, enterprise_id=self.team.enterprise_id) for status in status_list: if status["status"] in [ "running", "upgrade", "starting", "some_abnormal" ]: run_service_num += 1 app_dict["run_service_num"] = run_service_num app_list.append(app_dict) # 排序 app_list.sort(key=lambda x: x['run_service_num'], reverse=True) apps_list = app_list[start:end] result = general_message(200, "success", "查询成功", list=apps_list, bean=app_num_dict) return Response(result, status=200) except Exception as e: logger.exception(e) result = error_message(e.message) return Response(result, status=500)
def list_components_by_upgrade_group_id(group_id, upgrade_group_id): gsr = group_service_relation_repo.get_services_by_group(group_id) service_ids = gsr.values_list('service_id', flat=True) components = service_repo.list_by_ids(service_ids) return components.filter(tenant_service_group_id=upgrade_group_id)
def get_component_and_resource_by_group_ids(self, app_id, group_ids): gsr = group_service_relation_repo.get_services_by_group(app_id) components = service_repo.get_services_by_service_group_ids(gsr.values_list('service_id', flat=True), group_ids) service_ids = components.values_list('service_id', flat=True) return components, service_source_repo.get_service_sources_by_service_ids(service_ids)