Skip to content

Commit

Permalink
resolve: solve some problems
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 committed Dec 28, 2024
1 parent 7b0f8bc commit 59055e4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 46 deletions.
26 changes: 6 additions & 20 deletions src/bk-user/bkuser/apis/open_v3/serializers/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
from typing import List

from django.conf import settings
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from bkuser.apps.tenant.constants import TenantStatus
from bkuser.biz.tenant import TenantUserHandler
from bkuser.common.serializers import StringArrayField


class TenantListOutputSLZ(serializers.Serializer):
Expand All @@ -34,24 +32,12 @@ class Meta:


class TenantUserDisplayNameListInputSLZ(serializers.Serializer):
bk_usernames = serializers.CharField(help_text="蓝鲸唯一标识,多个用逗号分隔")

def validate_bk_usernames(self, bk_usernames: str) -> List[str]:
# 判断个数
if len(bk_usernames.split(",")) > settings.BK_USERNAME_BATCH_QUERY_DISPLAY_NAME_LIMIT:
raise ValidationError(
_("待查询的 bk_username 个数不能超过 %s") % settings.BK_USERNAME_BATCH_QUERY_DISPLAY_NAME_LIMIT
)

return bk_usernames.split(",")

class Meta:
ref_name = "open_v3.DisplayNameListInputSLZ"
bk_usernames = StringArrayField(help_text="蓝鲸唯一标识,多个使用逗号分隔", max_items=50)


class TenantUserDisplayNameListOutputSLZ(serializers.Serializer):
bk_username = serializers.CharField(help_text="蓝鲸唯一标识", source="id")
display_name = serializers.CharField(help_text="用户展示名称", source="data_source_user.full_name")
display_name = serializers.SerializerMethodField(help_text="用户展示名称")

class Meta:
ref_name = "open_v3.DisplayNameListOutputSLZ"
def get_display_name(self, obj) -> str:
return TenantUserHandler.generate_tenant_user_display_name(obj)
15 changes: 11 additions & 4 deletions src/bk-user/bkuser/apis/open_v3/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
from django.urls import path
from django.urls import include, path

from . import views

urlpatterns = [
path("tenants/", views.TenantListApi.as_view(), name="open_v3.tenant.list"),
path(
"tenant/users/display_name/",
views.TenantUserDisplayNameListApi.as_view(),
name="open_v3.tenant_user.display_name.list",
"tenant/",
include(
[
path(
"users/-/display_name/",
views.TenantUserDisplayNameListApi.as_view(),
name="open_v3.tenant_user.display_name.list",
)
]
),
),
]
18 changes: 6 additions & 12 deletions src/bk-user/bkuser/apis/open_v3/views/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,28 @@ def get(self, request, *args, **kwargs):
class TenantUserDisplayNameListApi(OpenApiCommonMixin, generics.ListAPIView):
"""
批量根据用户 bk_username 获取用户展示名
Note:性能较高,只查询所需字段,后续开发 DisplayName 支持表达式配置时添加 Cache 方案
TODO:性能较高,只查询所需字段,后续开发 DisplayName 支持表达式配置时添加 Cache 方案
"""

serializer_class = TenantUserDisplayNameListOutputSLZ

pagination_class = None

serializer_class = TenantUserDisplayNameListOutputSLZ

def get_queryset(self):
slz = TenantUserDisplayNameListInputSLZ(data=self.request.query_params)
slz.is_valid(raise_exception=True)
data = slz.validated_data

tenant_users = (
return (
TenantUser.objects.filter(id__in=data["bk_usernames"])
.select_related("data_source_user")
.only("id", "data_source_user__full_name")
)

# 可能存在部分用户的 bk_username 非法,无法查询到对应的 display_name
if tenant_users.count() != len(data["bk_usernames"]):
logger.info("%d users' display_name not found", len(data["bk_usernames"]) - tenant_users.count())

return tenant_users

@swagger_auto_schema(
tags=["open_v3.tenant"],
operation_id="list_display_name",
operation_description="批量获取用户展示名",
operation_id="batch_query_user_display_name",
operation_description="批量查询用户展示名",
responses={status.HTTP_200_OK: TenantUserDisplayNameListOutputSLZ(many=True)},
)
def get(self, request, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,4 @@ def _build_file_handler(log_path: Path, filename: str, format: str) -> Dict:
ORGANIZATION_BATCH_OPERATION_API_LIMIT = env.int("ORGANIZATION_BATCH_OPERATION_API_LIMIT", 100)

# 限制 bk_username 批量查询 display_name 的数量上限,避免性能问题
BK_USERNAME_BATCH_QUERY_DISPLAY_NAME_LIMIT = env.int("BK_USERNAME_BATCH_QUERY_DISPLAY_NAME_LIMIT", 50)
BATCH_QUERY_USER_DISPLAY_NAME_BY_BK_USERNAME_LIMIT = env.int("BATCH_QUERY_USER_DISPLAY_NAME_BY_BK_USERNAME_LIMIT", 50)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Description

Batch query tenant user's display_name
Batch query user's display_name

### Parameters

Expand Down Expand Up @@ -56,7 +56,7 @@ bk_usernames=7idwx3b7nzk6xigs,0wngfim3uzhadh1w
{
"error": {
"code": "INVALID_ARGUMENT",
"message": "Arguments Validation Failed: bk_usernames: Length cannot exceed 50"
"message": "Arguments Validation Failed: bk_usernames: This field must contain at most 50 objects."
}
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 描述

批量查询租户用户展示名
批量查询用户展示名

### 输入参数

Expand Down Expand Up @@ -56,7 +56,7 @@ bk_usernames=7idwx3b7nzk6xigs,0wngfim3uzhadh1w
{
"error": {
"code": "INVALID_ARGUMENT",
"message": "参数校验不通过: bk_usernames: 长度不能超过 50"
"message": "参数校验不通过: bk_usernames: 至多包含 50 个对象。"
}
}
```
10 changes: 5 additions & 5 deletions src/bk-user/support-files/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ paths:
resourcePermissionRequired: false
descriptionEn: Query the list of tenants

/api/v3/open/tenant/users/display_name/:
/api/v3/open/tenant/users/-/display_name/:
get:
operationId: list_display_name
description: 批量查询租户用户展示名
operationId: batch_query_user_display_name
description: 批量查询用户展示名
tags: []
responses:
default:
Expand All @@ -47,12 +47,12 @@ paths:
backend:
name: default
method: get
path: /api/v3/open/tenant/users/display_name/
path: /api/v3/open/tenant/users/-/display_name/
matchSubpath: false
timeout: 0
pluginConfigs: []
authConfig:
userVerifiedRequired: false
appVerifiedRequired: true
resourcePermissionRequired: false
descriptionEn: Batch query tenant user's display_name
descriptionEn: Batch query user's display_name

0 comments on commit 59055e4

Please sign in to comment.