Exemple #1
0
    def get(self, request, tenant_name, *args, **kwargs):
        """
        获取某团队下的所有用户
        ---
        parameters:
            - name: tenant_name
              description: 租户名称
              required: true
              type: string
              paramType: path

        """
        result = {}
        try:
            user_list = tenant_service.get_tenant_users(tenant_name)
            list = []
            for user in user_list:
                result_map = {}
                result_map["user_id"] = user.user_id
                result_map["email"] = user.email
                result_map["nick_name"] = user.nick_name
                tenant_list = user_service.get_user_tenants(user.user_id)
                result_map["tenants"] = tenant_list
                list.append(result_map)

            result = generate_result("0000", "success", "查询成功", list=list)
        except Tenants.DoesNotExist as e:
            logger.exception(e)
            result = generate_result("1001", "tenant not exist",
                                     "租户{}不存在".format(tenant_name))
        except Exception as e:
            logger.exception(e)
            result = generate_result("9999", "system error", "系统异常")
        return Response(result)
Exemple #2
0
    def get(self, request, *args, **kwargs):
        """
        获取所有租户信息
        ---
        parameters:
            - name: page_num
              description: 页码
              required: false
              type: string
              paramType: query
            - name: page_size
              description: 每页数量
              required: false
              type: string
              paramType: query

        """
        try:
            page = request.GET.get("page_num", 1)
            page_size = request.GET.get("page_size", 20)
            tenant_list = tenant_service.get_all_tenants()
            tenant_paginator = JuncheePaginator(tenant_list, int(page_size))
            tenants = tenant_paginator.page(int(page))
            tenants_num = Tenants.objects.count()
            allow_num = 9999999

            list = []

            for tenant in tenants:
                tenant_dict = {}
                user_list = tenant_service.get_tenant_users(
                    tenant["tenant_name"])
                tenant_dict["user_num"] = len(user_list)
                tenant_dict.update(tenant)
                list.append(tenant_dict)
            bean = {
                "total_tenant_num": allow_num,
                "cur_tenant_num": tenants_num
            }

            result = generate_result("0000",
                                     "success",
                                     "查询成功",
                                     bean=bean,
                                     list=list,
                                     total=tenant_paginator.count)
        except Exception as e:
            logger.exception(e)
            result = generate_error_result()
        return Response(result)
Exemple #3
0
    def get(self, request, *args, **kwargs):
        """
        获取团队信息
        ---
        parameters:
            - name: page_num
              description: 页码
              required: false
              type: string
              paramType: query
            - name: page_size
              description: 每页数量
              required: false
              type: string
              paramType: query
            - name: enterprise_alias
              description: 企业别名
              required: false
              type: string
              paramType: query
            - name: tenant_alias
              description: 团队别名
              required: false
              type: string
              paramType: query
            - name: tenant_name
              description: 团队名称
              required: false
              type: string
              paramType: query

        """
        try:
            page = request.GET.get("page_num", 1)
            page_size = request.GET.get("page_size", 20)
            enterprise_alias = request.GET.get("enterprise_alias", None)
            tenant_alias = request.GET.get("tenant_alias", None)
            tenant_name = request.GET.get("tenant_name", None)
            enterprise_id = None
            if enterprise_alias:
                enter = enterprise_services.get_enterprise_by_enterprise_alias(
                    enterprise_alias)
                if not enter:
                    return Response(
                        generate_result("0404", "enterprise is not found",
                                        "企业{0}不存在".format(enterprise_alias)))
                enterprise_id = enter.enterprise_id

            if tenant_alias:
                team = console_team_service.get_team_by_team_alias(
                    tenant_alias)
                if not team:
                    return Response(
                        generate_result("0404", "team is not found",
                                        "团队别名{0}不存在".format(tenant_alias)))

            if tenant_name:
                team = console_team_service.get_tenant_by_tenant_name(
                    tenant_name)
                if not team:
                    return Response(
                        generate_result("0404", "team is not found",
                                        "团队名称{0}不存在".format(tenant_name)))

            tenant_list = tenant_service.get_team_by_name_or_alias_or_enter(
                tenant_name, tenant_alias, enterprise_id)
            tenant_paginator = JuncheePaginator(tenant_list, int(page_size))
            tenants = tenant_paginator.page(int(page))

            tenants_num = Tenants.objects.count()
            # 需要license控制,现在没有,默认为一百万
            allow_num = 1000000

            list = []

            for tenant in tenants:
                tenant_dict = {}
                user_list = tenant_service.get_tenant_users(tenant.tenant_name)
                tenant_dict["user_num"] = len(user_list)
                tenant_dict.update(tenant.to_dict())
                list.append(tenant_dict)
            bean = {
                "total_tenant_num": allow_num,
                "cur_tenant_num": tenants_num
            }

            result = generate_result("0000",
                                     "success",
                                     "查询成功",
                                     bean=bean,
                                     list=list,
                                     total=tenant_paginator.count)
        except Exception as e:
            logger.exception(e)
            result = generate_error_result()
        return Response(result)
Exemple #4
0
    def get(self, request, *args, **kwargs):
        """
        获取团队信息
        ---
        parameters:
            - name: page_num
              description: 页码
              required: false
              type: string
              paramType: query
            - name: page_size
              description: 每页数量
              required: false
              type: string
              paramType: query
            - name: enterprise_alias
              description: 企业别名
              required: false
              type: string
              paramType: query
            - name: tenant_alias
              description: 团队别名
              required: false
              type: string
              paramType: query
            - name: tenant_name
              description: 团队名称
              required: false
              type: string
              paramType: query

        """
        try:
            page = int(request.GET.get("page_num", 1))
            page_size = int(request.GET.get("page_size", 10))
            enterprise_alias = request.GET.get("enterprise_alias", None)
            tenant_alias = request.GET.get("tenant_alias", None)
            if enterprise_alias:
                enter = enterprise_services.get_enterprise_by_enterprise_alias(enterprise_alias)
                if not enter:
                    return Response(
                        generate_result("0404", "enterprise is not found", "企业{0}不存在".format(enterprise_alias)))
            list1 = []
            tenants_num = Tenants.objects.count()
            start = (page-1)*10
            remaining_num = tenants_num - (page-1)*10
            end = 10
            if remaining_num < page_size:
                end = remaining_num
            # 通过别名来搜索团队(排序分页)
            if tenant_alias:
                cursor = connection.cursor()
                cursor.execute(
                    "select t.tenant_name, t.tenant_alias,t.region,t.limit_memory,t.enterprise_id, t.tenant_id,u.nick_name as creater,count(s.ID) as num from tenant_info t LEFT JOIN tenant_service s on t.tenant_id=s.tenant_id,user_info u where t.creater=u.user_id and t.tenant_alias LIKE '%{0}%' group by tenant_id order by num desc LIMIT {1},{2};".format(
                        tenant_alias, start, end))
                tenant_tuples = cursor.fetchall()
            else:
                cursor = connection.cursor()
                cursor.execute(
                    "select t.tenant_name, t.tenant_alias,t.region,t.limit_memory,t.enterprise_id, t.tenant_id,u.nick_name as creater,count(s.ID) as num from tenant_info t LEFT JOIN tenant_service s on t.tenant_id=s.tenant_id,user_info u where t.creater=u.user_id group by tenant_id order by num desc LIMIT {0},{1};".format(
                        start, end))
                tenant_tuples = cursor.fetchall()
            try:
                # 查询所有团队有哪些数据中心
                region_list = []
                for tenant in tenant_tuples:
                    tenant_id = tenant[5]
                    tenant_region_list = tenant_service.get_all_tenant_region_by_tenant_id(tenant_id)
                    if len(tenant_region_list) != 0:
                        for tenant_region in tenant_region_list:
                            if not tenant_region.region_name in region_list:
                                region_list.append(tenant_region.region_name)
            except Exception as e:
                logger.exception(e)
                result = generate_result("1111", "2.faild", "{0}".format(e.message))
                return Response(result)
            try:
                resources_dicts = {}
                run_app_num_dicts = {}
                for region_name in region_list:
                    time1 = datetime.datetime.now()
                    logger.debug('````````````11111`````````````````{0}'.format(time1))
                    try:
                        region_obj = region_repo.get_region_by_region_name(region_name)
                        if not region_obj:
                            continue
                        tenant_name_list = []
                        # 循环查询哪些团队开通了该数据中心,将团队名放进列表中
                        for tenant in tenant_tuples:
                            tenant_region_list = tenant_service.get_all_tenant_region_by_tenant_id(tenant[5])
                            for tenant_regions in tenant_region_list:
                                tenant_region_name = tenant_regions.region_name
                                if tenant_region_name == region_name:
                                    tenant_name_list.append(tenant[0])
                                else:
                                    continue
                        # 获取数据中心下每个团队的使用资源和运行的应用数量
                        time2 = datetime.datetime.now()
                        logger.debug('```````````222222``````````````````{0}'.format(time2))
                        res, body = http_client.get_tenant_limit_memory(region_obj, json.dumps({"tenant_name": tenant_name_list}))
                        logger.debug("======111===={0}".format(body["list"]))
                        if int(res.status) >= 400:
                            continue
                        if not body.get("list"):
                            continue
                        tenant_resources_list = body.get("list")
                        time3 = datetime.datetime.now()
                        logger.debug('`````````````33333````````````````{0}'.format(time3))

                        tenant_resources_dict = {}
                        for tenant_resources in tenant_resources_list:
                            run_app_num = tenant_resources["service_running_num"]
                            for tenant in tenant_tuples:
                                tenant_id = tenant[5]
                                if tenant_id == tenant_resources["tenant_id"]:
                                    if tenant_id not in run_app_num_dicts:
                                        run_app_num_dicts[tenant_id] = {"run_app_num": [run_app_num]}
                                    else:
                                        run_app_num_dicts[tenant_id]["run_app_num"].append(run_app_num)
                            tenant_resources_dict[tenant_resources["tenant_id"]] = tenant_resources

                        # tenant_resources_dict = {id:{}, id:{}}

                        for tenant in tenant_tuples:
                            tenant_region = {}
                            tenant_id = tenant[5]
                            if tenant_id in tenant_resources_dict:
                                # tenant_region["name1"] = {"cpu_total":0, "cpu_use":0}
                                tenant_region[region_obj.region_alias] = tenant_resources_dict[tenant_id]
                                if tenant_id not in resources_dicts:
                                    resources_dicts[tenant_id] = {"resources": tenant_region}
                                else:
                                    resources_dicts[tenant_id]["resources"].update(tenant_region)
                        time4 = datetime.datetime.now()
                        logger.debug('``````````````4444```````````````{0}'.format(time4))
                    except Exception as e:
                        logger.exception(e)
                        continue
            except Exception as e:
                logger.exception(e)
                result = generate_result("1111", "2.6-faild", "{0}".format(e.message))
                return Response(result)

            for tenant in tenant_tuples:
                tenant_info = {}
                # 为每个团队拼接信息
                tenant_id = tenant[5]
                for key in run_app_num_dicts:
                    if key == tenant_id:
                        tenant_info["run_app_num"] = run_app_num_dicts[key]["run_app_num"]
                for key in resources_dicts:
                    if key == tenant_id:
                        tenant_info["resources"] = resources_dicts[key]["resources"]
                tenant_info["total_app"] = tenant[7]
                user_list = tenant_service.get_tenant_users(tenant[0])
                tenant_info["user_num"] = len(user_list)
                tenant_info["tenant_creater"] = tenant[6]
                tenant_info["tenant_alias"] = tenant[1]
                tenant_info["tenant_name"] = tenant[0]
                tenant_info["region"] = tenant[2]
                tenant_info["tenant_id"] = tenant[5]
                tenant_info["limit_memory"] = tenant[3]
                tenant_info["enterprise_id"] = tenant[4]
                list1.append(tenant_info)
            # 需要license控制,现在没有,默认为一百万
            allow_num = 1000000
            bean = {"tenants_num": tenants_num, "allow_num": allow_num}
            result = generate_result(
                "0000", "success", "查询成功", bean=bean, list=list1, total=tenants_num
            )
            return Response(result)
        except Exception as e:
            result = generate_result("1111", "4.faild", "{0}".format(e.message))
            return Response(result)