perf: del domain node

pull/9133/head
feng 2022-11-29 19:14:12 +08:00
parent 52541d1dad
commit dd207016b2
5 changed files with 4 additions and 34 deletions

View File

@ -4,18 +4,11 @@ from django.db import migrations, models
import django.db.models.deletion
# TODO 最后去掉这个迁移
class Migration(migrations.Migration):
dependencies = [
('assets', '0113_alter_accounttemplate_options'),
]
operations = [
migrations.AddField(
model_name='node',
name='domain',
field=models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL,
related_name='nodes', to='assets.domain', verbose_name='Domain'
),
),
]

View File

@ -105,14 +105,6 @@ class Asset(NodesRelationMixin, AbsConnectivity, JMSOrgBaseModel):
def __str__(self):
return '{0.name}({0.address})'.format(self)
def get_domains(self):
from ..domain import Domain
node_ids = self.get_all_nodes(flat=True)
domains = Domain.objects.filter(
Q(nodes__id__in=node_ids) | Q(id=self.domain_id)
).distinct()
return domains
@property
def specific(self):
if not hasattr(self, self.category):

View File

@ -558,10 +558,6 @@ class Node(OrgModelMixin, SomeNodesMixin, FamilyMixin, NodeAssetsMixin):
max_length=64, verbose_name=_("Parent key"), db_index=True, default=''
)
assets_amount = models.IntegerField(default=0)
domain = models.ForeignKey(
"assets.Domain", null=True, blank=True, related_name='nodes',
verbose_name=_("Domain"), on_delete=models.SET_NULL
)
objects = OrgManager.from_queryset(NodeQuerySet)()
is_node = True

View File

@ -14,12 +14,8 @@ from .utils import validate_password_for_ansible, validate_ssh_key
class DomainSerializer(BulkOrgResourceModelSerializer):
node_count = serializers.SerializerMethodField(label=_('Nodes amount'))
asset_count = serializers.SerializerMethodField(label=_('Assets amount'))
gateway_count = serializers.SerializerMethodField(label=_('Gateways count'))
nodes = ObjectRelatedField(
many=True, required=False, queryset=Node.objects, label=_('Node')
)
assets = ObjectRelatedField(
many=True, required=False, queryset=Asset.objects, label=_('Asset')
)
@ -29,17 +25,13 @@ class DomainSerializer(BulkOrgResourceModelSerializer):
fields_mini = ['id', 'name']
fields_small = fields_mini + ['comment']
fields_m2m = ['nodes', 'assets']
read_only_fields = ['node_count', 'asset_count', 'gateway_count', 'date_created']
read_only_fields = ['asset_count', 'gateway_count', 'date_created']
fields = fields_small + fields_m2m + read_only_fields
extra_kwargs = {
'assets': {'required': False, 'label': _('Assets')},
}
@staticmethod
def get_node_count(obj):
return obj.nodes.count()
@staticmethod
def get_asset_count(obj):
return obj.assets.count()

View File

@ -1,7 +1,6 @@
# ~*~ coding: utf-8 ~*~
import json
import os
import random
from collections import defaultdict
from django.utils.translation import gettext as _
@ -118,10 +117,8 @@ class JMSInventory:
host.update(ansible_config)
gateway = None
domains = asset.get_domains()
if domains:
gateways = [i.select_gateway() for i in domains if i.select_gateway()]
gateway = random.choice(gateways) if gateways else None
if asset.domain:
gateway = asset.domain.select_gateway()
if ansible_connection == 'local':
if gateway: