perf: 修改 sftp 的一些处理

pull/11106/head
ibuler 1 year ago committed by Bryan
parent 114645732a
commit 3b6c2fc0c0

@ -2,7 +2,7 @@ from typing import List
from rest_framework.request import Request from rest_framework.request import Request
from assets.models import Node, PlatformProtocol, Protocol from assets.models import Node, Protocol
from assets.utils import get_node_from_request, is_query_node_all_assets from assets.utils import get_node_from_request, is_query_node_all_assets
from common.utils import lazyproperty, timeit from common.utils import lazyproperty, timeit
@ -42,7 +42,7 @@ class SerializeToTreeNodeMixin:
'name': _name(node), 'name': _name(node),
'title': _name(node), 'title': _name(node),
'pId': node.parent_key, 'pId': node.parent_key,
'isParent': True, 'isParent': node.assets_amount > 0,
'open': _open(node), 'open': _open(node),
'meta': { 'meta': {
'data': { 'data': {
@ -70,25 +70,18 @@ class SerializeToTreeNodeMixin:
@timeit @timeit
def serialize_assets(self, assets, node_key=None, pid=None): def serialize_assets(self, assets, node_key=None, pid=None):
sftp_enabled_platform = PlatformProtocol.objects \
.filter(name='ssh', setting__sftp_enabled=True) \
.values_list('platform', flat=True) \
.distinct()
if node_key is None: if node_key is None:
get_pid = lambda asset: getattr(asset, 'parent_key', '') get_pid = lambda asset: getattr(asset, 'parent_key', '')
else: else:
get_pid = lambda asset: node_key get_pid = lambda asset: node_key
ssh_asset_ids = [ sftp_asset_ids = Protocol.objects.filter(name='sftp') \
str(i) for i in .values_list('asset_id', flat=True)
Protocol.objects.filter(name='ssh').values_list('asset_id', flat=True) sftp_asset_ids = list(sftp_asset_ids)
]
data = [ data = [
{ {
'id': str(asset.id), 'id': str(asset.id),
'name': asset.name, 'name': asset.name,
'title': 'title': f'{asset.address}\n{asset.comment}',
f'{asset.address}\n{asset.comment}'
if asset.comment else asset.address,
'pId': pid or get_pid(asset), 'pId': pid or get_pid(asset),
'isParent': False, 'isParent': False,
'open': False, 'open': False,
@ -99,8 +92,7 @@ class SerializeToTreeNodeMixin:
'data': { 'data': {
'platform_type': asset.platform.type, 'platform_type': asset.platform.type,
'org_name': asset.org_name, 'org_name': asset.org_name,
'sftp': (asset.platform_id in sftp_enabled_platform) \ 'sftp': asset.id in sftp_asset_ids,
and (str(asset.id) in ssh_asset_ids),
'name': asset.name, 'name': asset.name,
'address': asset.address 'address': asset.address
}, },

Loading…
Cancel
Save