mirror of https://github.com/jumpserver/jumpserver
				
				
				
			perf: filter gateway with new params
							parent
							
								
									26420b78f8
								
							
						
					
					
						commit
						59c87483e6
					
				| 
						 | 
					@ -4,7 +4,6 @@ from django.utils.translation import gettext_noop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from accounts.const import AutomationTypes
 | 
					from accounts.const import AutomationTypes
 | 
				
			||||||
from accounts.tasks.common import quickstart_automation_by_snapshot
 | 
					from accounts.tasks.common import quickstart_automation_by_snapshot
 | 
				
			||||||
from assets.const import GATEWAY_NAME
 | 
					 | 
				
			||||||
from common.utils import get_logger
 | 
					from common.utils import get_logger
 | 
				
			||||||
from orgs.utils import org_aware_func
 | 
					from orgs.utils import org_aware_func
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -32,13 +31,13 @@ def verify_accounts_connectivity_util(accounts, task_name):
 | 
				
			||||||
    asset_ids = [a.asset_id for a in accounts]
 | 
					    asset_ids = [a.asset_id for a in accounts]
 | 
				
			||||||
    assets = Asset.objects.filter(id__in=asset_ids)
 | 
					    assets = Asset.objects.filter(id__in=asset_ids)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    gateways = assets.filter(platform__name=GATEWAY_NAME)
 | 
					    gateways = assets.gateways()
 | 
				
			||||||
    verify_connectivity_util(
 | 
					    verify_connectivity_util(
 | 
				
			||||||
        gateways, AutomationTypes.verify_gateway_account,
 | 
					        gateways, AutomationTypes.verify_gateway_account,
 | 
				
			||||||
        accounts, task_name
 | 
					        accounts, task_name
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    common_assets = assets.exclude(platform__name=GATEWAY_NAME)
 | 
					    common_assets = assets.gateways(0)
 | 
				
			||||||
    verify_connectivity_util(
 | 
					    verify_connectivity_util(
 | 
				
			||||||
        common_assets, AutomationTypes.verify_account,
 | 
					        common_assets, AutomationTypes.verify_account,
 | 
				
			||||||
        accounts, task_name
 | 
					        accounts, task_name
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,10 +2,10 @@
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
from collections import defaultdict
 | 
					from collections import defaultdict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import django_filters
 | 
					 | 
				
			||||||
from django.conf import settings
 | 
					from django.conf import settings
 | 
				
			||||||
from django.shortcuts import get_object_or_404
 | 
					from django.shortcuts import get_object_or_404
 | 
				
			||||||
from django.utils.translation import gettext as _
 | 
					from django.utils.translation import gettext as _
 | 
				
			||||||
 | 
					from django_filters import rest_framework as drf_filters
 | 
				
			||||||
from rest_framework import status
 | 
					from rest_framework import status
 | 
				
			||||||
from rest_framework.decorators import action
 | 
					from rest_framework.decorators import action
 | 
				
			||||||
from rest_framework.response import Response
 | 
					from rest_framework.response import Response
 | 
				
			||||||
| 
						 | 
					@ -33,31 +33,32 @@ __all__ = [
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AssetFilterSet(BaseFilterSet):
 | 
					class AssetFilterSet(BaseFilterSet):
 | 
				
			||||||
    platform = django_filters.CharFilter(method='filter_platform')
 | 
					    platform = drf_filters.CharFilter(method='filter_platform')
 | 
				
			||||||
    exclude_platform = django_filters.CharFilter(field_name="platform__name", lookup_expr='exact', exclude=True)
 | 
					    is_gateway = drf_filters.BooleanFilter(method='filter_is_gateway')
 | 
				
			||||||
    domain = django_filters.CharFilter(method='filter_domain')
 | 
					    exclude_platform = drf_filters.CharFilter(field_name="platform__name", lookup_expr='exact', exclude=True)
 | 
				
			||||||
    type = django_filters.CharFilter(field_name="platform__type", lookup_expr="exact")
 | 
					    domain = drf_filters.CharFilter(method='filter_domain')
 | 
				
			||||||
    category = django_filters.CharFilter(field_name="platform__category", lookup_expr="exact")
 | 
					    type = drf_filters.CharFilter(field_name="platform__type", lookup_expr="exact")
 | 
				
			||||||
    protocols = django_filters.CharFilter(method='filter_protocols')
 | 
					    category = drf_filters.CharFilter(field_name="platform__category", lookup_expr="exact")
 | 
				
			||||||
    domain_enabled = django_filters.BooleanFilter(
 | 
					    protocols = drf_filters.CharFilter(method='filter_protocols')
 | 
				
			||||||
 | 
					    domain_enabled = drf_filters.BooleanFilter(
 | 
				
			||||||
        field_name="platform__domain_enabled", lookup_expr="exact"
 | 
					        field_name="platform__domain_enabled", lookup_expr="exact"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    ping_enabled = django_filters.BooleanFilter(
 | 
					    ping_enabled = drf_filters.BooleanFilter(
 | 
				
			||||||
        field_name="platform__automation__ping_enabled", lookup_expr="exact"
 | 
					        field_name="platform__automation__ping_enabled", lookup_expr="exact"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    gather_facts_enabled = django_filters.BooleanFilter(
 | 
					    gather_facts_enabled = drf_filters.BooleanFilter(
 | 
				
			||||||
        field_name="platform__automation__gather_facts_enabled", lookup_expr="exact"
 | 
					        field_name="platform__automation__gather_facts_enabled", lookup_expr="exact"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    change_secret_enabled = django_filters.BooleanFilter(
 | 
					    change_secret_enabled = drf_filters.BooleanFilter(
 | 
				
			||||||
        field_name="platform__automation__change_secret_enabled", lookup_expr="exact"
 | 
					        field_name="platform__automation__change_secret_enabled", lookup_expr="exact"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    push_account_enabled = django_filters.BooleanFilter(
 | 
					    push_account_enabled = drf_filters.BooleanFilter(
 | 
				
			||||||
        field_name="platform__automation__push_account_enabled", lookup_expr="exact"
 | 
					        field_name="platform__automation__push_account_enabled", lookup_expr="exact"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    verify_account_enabled = django_filters.BooleanFilter(
 | 
					    verify_account_enabled = drf_filters.BooleanFilter(
 | 
				
			||||||
        field_name="platform__automation__verify_account_enabled", lookup_expr="exact"
 | 
					        field_name="platform__automation__verify_account_enabled", lookup_expr="exact"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    gather_accounts_enabled = django_filters.BooleanFilter(
 | 
					    gather_accounts_enabled = drf_filters.BooleanFilter(
 | 
				
			||||||
        field_name="platform__automation__gather_accounts_enabled", lookup_expr="exact"
 | 
					        field_name="platform__automation__gather_accounts_enabled", lookup_expr="exact"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -77,6 +78,11 @@ class AssetFilterSet(BaseFilterSet):
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            return queryset.filter(platform__name=value)
 | 
					            return queryset.filter(platform__name=value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def filter_is_gateway(queryset, name, value):
 | 
				
			||||||
 | 
					        queryset = queryset.gateways(value)
 | 
				
			||||||
 | 
					        return queryset
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @staticmethod
 | 
				
			||||||
    def filter_domain(queryset, name, value):
 | 
					    def filter_domain(queryset, name, value):
 | 
				
			||||||
        if is_uuid(value):
 | 
					        if is_uuid(value):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,6 +38,13 @@ class AssetQuerySet(models.QuerySet):
 | 
				
			||||||
    def valid(self):
 | 
					    def valid(self):
 | 
				
			||||||
        return self.active()
 | 
					        return self.active()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def gateways(self, is_gateway=1):
 | 
				
			||||||
 | 
					        kwargs = {'platform__name__startswith': 'Gateway'}
 | 
				
			||||||
 | 
					        if is_gateway:
 | 
				
			||||||
 | 
					            return self.filter(**kwargs)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            return self.exclude(**kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def has_protocol(self, name):
 | 
					    def has_protocol(self, name):
 | 
				
			||||||
        return self.filter(protocols__contains=name)
 | 
					        return self.filter(protocols__contains=name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,7 +31,7 @@ class Domain(LabeledMixin, JMSOrgBaseModel):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @lazyproperty
 | 
					    @lazyproperty
 | 
				
			||||||
    def assets_amount(self):
 | 
					    def assets_amount(self):
 | 
				
			||||||
        return self.assets.exclude(platform__name='Gateway').count()
 | 
					        return self.assets.gateways(0).count()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def random_gateway(self):
 | 
					    def random_gateway(self):
 | 
				
			||||||
        gateways = [gw for gw in self.active_gateways if gw.is_connective]
 | 
					        gateways = [gw for gw in self.active_gateways if gw.is_connective]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -33,10 +33,6 @@ class Gateway(Host):
 | 
				
			||||||
        proxy = True
 | 
					        proxy = True
 | 
				
			||||||
        verbose_name = _("Gateway")
 | 
					        verbose_name = _("Gateway")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def save(self, *args, **kwargs):
 | 
					 | 
				
			||||||
        self.platform = self.default_platform()
 | 
					 | 
				
			||||||
        return super().save(*args, **kwargs)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def default_platform(cls):
 | 
					    def default_platform(cls):
 | 
				
			||||||
        return Platform.objects.get(name=GATEWAY_NAME, internal=True)
 | 
					        return Platform.objects.get(name=GATEWAY_NAME, internal=True)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -68,7 +68,7 @@ class DomainListSerializer(DomainSerializer):
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def setup_eager_loading(cls, queryset):
 | 
					    def setup_eager_loading(cls, queryset):
 | 
				
			||||||
        queryset = queryset.annotate(
 | 
					        queryset = queryset.annotate(
 | 
				
			||||||
            assets_amount=Count('assets', filter=~Q(assets__platform__name='Gateway'), distinct=True),
 | 
					            assets_amount=Count('assets', filter=~Q(assets__platform__name__startswith='Gateway'), distinct=True),
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        return queryset
 | 
					        return queryset
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue