Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apis/web/plugin): stage/resource plugin api add related_scope_count #655

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,9 @@ class ScopePluginConfigListOutputSLZ(serializers.Serializer):
name = serializers.CharField(help_text="插件类型名称")
config = serializers.DictField(help_text="插件配置")
config_id = serializers.IntegerField(help_text="插件配置 id")
related_scope_count = serializers.SerializerMethodField(help_text="插件类型绑定的环境及资源数量")

def get_related_scope_count(self, obj):
related_scope_count = self.context.get("type_related_scope_count", {})
print("the related_scope_count:", related_scope_count)
return related_scope_count.get(obj["code"], {"stage": 0, "resource": 0})
23 changes: 22 additions & 1 deletion src/dashboard/apigateway/apigateway/apis/web/plugin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,27 @@ def get(self, request, *args, **kwargs):


class ScopePluginConfigListApi(generics.ListAPIView, ScopeValidationMixin):
def get_serializer_context(self):
# 需要返回每个 pluginType 对应绑定的环境数量/资源数量
type_related_scope_count = {}
gateway = self.request.gateway
for binding in PluginBinding.objects.filter(gateway=gateway).prefetch_related("config", "config__type").all():
key = binding.config.type.code
if key not in type_related_scope_count:
type_related_scope_count[key] = {
"stage": 0,
"resource": 0,
}

# all
scope_type = binding.scope_type
count = type_related_scope_count[key].get(scope_type, 0)
type_related_scope_count[key][scope_type] = count + 1

return {
"type_related_scope_count": type_related_scope_count,
}

@swagger_auto_schema(
responses={status.HTTP_200_OK: ScopePluginConfigListOutputSLZ(many=True)},
operation_description="获取某个环境或资源绑定的插件列表 (插件类型 + 插件配置)",
Expand Down Expand Up @@ -453,5 +474,5 @@ def get(self, request, *args, **kwargs):
for binding in bindings
]

serializer = ScopePluginConfigListOutputSLZ(data, many=True)
serializer = ScopePluginConfigListOutputSLZ(data, many=True, context=self.get_serializer_context())
return OKJsonResponse(data=serializer.data)
Loading