perf: asset type xpack (#9218)

Co-authored-by: feng <1304903146@qq.com>
pull/9219/head
fit2bot 2022-12-19 11:35:50 +08:00 committed by GitHub
parent 675a41013e
commit 69b16e4754
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 9 deletions

View File

@ -1,10 +1,10 @@
from jumpserver.utils import has_valid_xpack_license
from common.drf.api import JMSModelViewSet from common.drf.api import JMSModelViewSet
from common.drf.serializers import GroupedChoiceSerializer from common.drf.serializers import GroupedChoiceSerializer
from assets.models import Platform from assets.models import Platform
from assets.const import AllTypes
from assets.serializers import PlatformSerializer from assets.serializers import PlatformSerializer
__all__ = ['AssetPlatformViewSet'] __all__ = ['AssetPlatformViewSet']
@ -22,6 +22,11 @@ class AssetPlatformViewSet(JMSModelViewSet):
'ops_methods': 'assets.view_platform' 'ops_methods': 'assets.view_platform'
} }
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.filter(type__in=AllTypes.get_types())
return queryset
def get_object(self): def get_object(self):
pk = self.kwargs.get('pk', '') pk = self.kwargs.get('pk', '')
if pk.isnumeric(): if pk.isnumeric():

View File

@ -1,5 +1,6 @@
from django.db.models import TextChoices from django.db.models import TextChoices
from jumpserver.utils import has_valid_xpack_license
from .protocol import Protocol from .protocol import Protocol
@ -53,3 +54,25 @@ class BaseType(TextChoices):
@classmethod @classmethod
def internal_platforms(cls): def internal_platforms(cls):
raise NotImplementedError raise NotImplementedError
@classmethod
def get_community_types(cls):
raise NotImplementedError
@classmethod
def get_types(cls):
tps = [tp for tp in cls]
if not has_valid_xpack_license():
tps = cls.get_community_types()
return tps
@classmethod
def get_choices(cls):
tps = cls.get_types()
cls_choices = cls.choices
return [
choice for choice in cls_choices
if choice[0] in tps
]

View File

@ -49,3 +49,7 @@ class CloudTypes(BaseType):
cls.PRIVATE: [{'name': 'Vmware-vSphere'}], cls.PRIVATE: [{'name': 'Vmware-vSphere'}],
cls.K8S: [{'name': 'Kubernetes'}], cls.K8S: [{'name': 'Kubernetes'}],
} }
@classmethod
def get_community_types(cls):
return [cls.K8S]

View File

@ -1,4 +1,3 @@
from .base import BaseType from .base import BaseType
@ -62,3 +61,8 @@ class DatabaseTypes(BaseType):
cls.REDIS: [{'name': 'Redis'}], cls.REDIS: [{'name': 'Redis'}],
} }
@classmethod
def get_community_types(cls):
return [
cls.MYSQL, cls.MARIADB, cls.MONGODB, cls.REDIS
]

View File

@ -52,3 +52,7 @@ class DeviceTypes(BaseType):
cls.ROUTER: [], cls.ROUTER: [],
cls.FIREWALL: [] cls.FIREWALL: []
} }
@classmethod
def get_community_types(cls):
return []

View File

@ -106,3 +106,9 @@ class HostTypes(BaseType):
], ],
cls.OTHER_HOST: [] cls.OTHER_HOST: []
} }
@classmethod
def get_community_types(cls) -> list:
return [
cls.LINUX, cls.UNIX, cls.WINDOWS, cls.OTHER_HOST
]

View File

@ -62,14 +62,18 @@ class AllTypes(ChoicesMixin):
@classmethod @classmethod
def types(cls, with_constraints=True): def types(cls, with_constraints=True):
types = [] types = []
for category, tps in cls.category_types(): for category, type_cls in cls.category_types():
tps = type_cls.get_types()
types.extend([cls.serialize_type(category, tp, with_constraints) for tp in tps]) types.extend([cls.serialize_type(category, tp, with_constraints) for tp in tps])
return types return types
@classmethod @classmethod
def categories(cls, with_constraints=True): def categories(cls, with_constraints=True):
categories = [] categories = []
for category, tps in cls.category_types(): for category, type_cls in cls.category_types():
tps = type_cls.get_types()
if not tps:
continue
category_data = { category_data = {
'value': category.value, 'value': category.value,
'label': category.label, 'label': category.label,
@ -121,6 +125,13 @@ class AllTypes(ChoicesMixin):
(Category.CLOUD, CloudTypes) (Category.CLOUD, CloudTypes)
) )
@classmethod
def get_types(cls):
tps = []
for i in dict(cls.category_types()).values():
tps.extend(i.get_types())
return tps
@staticmethod @staticmethod
def choice_to_node(choice, pid, opened=True, is_parent=True, meta=None): def choice_to_node(choice, pid, opened=True, is_parent=True, meta=None):
node = TreeNode(**{ node = TreeNode(**{
@ -168,14 +179,15 @@ class AllTypes(ChoicesMixin):
root = TreeNode(id='ROOT', name='所有类型', title='所有类型', open=True, isParent=True) root = TreeNode(id='ROOT', name='所有类型', title='所有类型', open=True, isParent=True)
nodes = [root] nodes = [root]
for category, types in cls.category_types(): for category, type_cls in cls.category_types():
meta = {'type': 'category', 'category': category.value} meta = {'type': 'category', 'category': category.value}
category_node = cls.choice_to_node(category, 'ROOT', meta=meta) category_node = cls.choice_to_node(category, 'ROOT', meta=meta)
category_count = category_type_mapper.get(category, 0) category_count = category_type_mapper.get(category, 0)
category_node.name += f'({category_count})' category_node.name += f'({category_count})'
nodes.append(category_node) nodes.append(category_node)
for tp in types: tps = type_cls.get_types()
for tp in tps:
meta = {'type': 'type', 'category': category.value, '_type': tp.value} meta = {'type': 'type', 'category': category.value, '_type': tp.value}
tp_node = cls.choice_to_node(tp, category_node.id, opened=False, meta=meta) tp_node = cls.choice_to_node(tp, category_node.id, opened=False, meta=meta)
tp_count = category_type_mapper.get(category + '_' + tp, 0) tp_count = category_type_mapper.get(category + '_' + tp, 0)

View File

@ -44,3 +44,7 @@ class WebTypes(BaseType):
{'name': 'Website'}, {'name': 'Website'},
], ],
} }
@classmethod
def get_community_types(cls):
return []