fix: Got perm tree 500 (#13533)

Co-authored-by: feng <1304903146@qq.com>
pull/13534/head
fit2bot 2024-06-28 11:39:09 +08:00 committed by GitHub
parent bf56549f01
commit 138ea35620
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 16 deletions

View File

@ -8,11 +8,10 @@ from collections import defaultdict
from django.core.cache import cache from django.core.cache import cache
from django.db import models, transaction from django.db import models, transaction
from django.db.models import Q, Manager from django.db.models import F, Q, Manager
from django.db.transaction import atomic from django.db.transaction import atomic
from django.utils.translation import gettext_lazy as _, gettext from django.utils.translation import gettext_lazy as _, gettext
from common.db.models import output_as_string
from common.utils import get_logger, timeit from common.utils import get_logger, timeit
from common.utils.lock import DistributedLock from common.utils.lock import DistributedLock
from orgs.mixins.models import OrgManager, JMSOrgBaseModel from orgs.mixins.models import OrgManager, JMSOrgBaseModel
@ -354,9 +353,9 @@ class NodeAllAssetsMappingMixin:
t1 = time.time() t1 = time.time()
with tmp_to_org(org_id): with tmp_to_org(org_id):
node_ids_key = Node.objects.annotate( node_ids_key = Node.objects.annotate(
char_id=output_as_string('id') char_id=F('id')
).values_list('char_id', 'key') ).values_list('char_id', 'key')
node_ids_key = [(str(uuid.UUID(node_id)), node_key) for node_id, node_key in node_ids_key] node_ids_key = [(str(node_id), node_key) for node_id, node_key in node_ids_key]
node_id_ancestor_keys_mapping = { node_id_ancestor_keys_mapping = {
node_id: cls.get_node_ancestor_keys(node_key, with_self=True) node_id: cls.get_node_ancestor_keys(node_key, with_self=True)
for node_id, node_key in node_ids_key for node_id, node_key in node_ids_key
@ -364,13 +363,13 @@ class NodeAllAssetsMappingMixin:
# * 直接取出全部. filter(node__org_id=org_id)(大规模下会更慢) # * 直接取出全部. filter(node__org_id=org_id)(大规模下会更慢)
nodes_asset_ids = cls.assets.through.objects.all() \ nodes_asset_ids = cls.assets.through.objects.all() \
.annotate(char_node_id=output_as_string('node_id')) \ .annotate(char_node_id=F('node_id')) \
.annotate(char_asset_id=output_as_string('asset_id')) \ .annotate(char_asset_id=F('asset_id')) \
.values_list('char_node_id', 'char_asset_id') .values_list('char_node_id', 'char_asset_id')
nodeid_assetsid_mapping = defaultdict(set) nodeid_assetsid_mapping = defaultdict(set)
for node_id, asset_id in nodes_asset_ids: for node_id, asset_id in nodes_asset_ids:
node_id, asset_id = str(uuid.UUID(node_id)), str(uuid.UUID(asset_id)) node_id, asset_id = str(node_id), str(asset_id)
nodeid_assetsid_mapping[node_id].add(asset_id) nodeid_assetsid_mapping[node_id].add(asset_id)
t2 = time.time() t2 = time.time()

View File

@ -1,9 +1,9 @@
# ~*~ coding: utf-8 ~*~ # ~*~ coding: utf-8 ~*~
# #
from collections import defaultdict from collections import defaultdict
from uuid import UUID
from common.db.models import output_as_string from django.db.models import F
from common.struct import Stack from common.struct import Stack
from common.utils import get_logger, dict_get_any, is_uuid, get_object_or_none, timeit from common.utils import get_logger, dict_get_any, is_uuid, get_object_or_none, timeit
from common.utils.http import is_true from common.utils.http import is_true
@ -129,12 +129,12 @@ class NodeAssetsUtil:
nodes = list(Node.objects.all()) nodes = list(Node.objects.all())
nodes_assets = Asset.nodes.through.objects.all() \ nodes_assets = Asset.nodes.through.objects.all() \
.annotate(aid=output_as_string('asset_id')) \ .annotate(aid=F('asset_id')) \
.values_list('node__key', 'aid') .values_list('node__key', 'aid')
mapping = defaultdict(set) mapping = defaultdict(set)
for key, asset_id in nodes_assets: for key, asset_id in nodes_assets:
asset_id = str(UUID(asset_id)) asset_id = str(asset_id)
mapping[key].add(asset_id) mapping[key].add(asset_id)
util = cls(nodes, mapping) util = cls(nodes, mapping)

View File

@ -49,6 +49,7 @@ class JMSBaseModel(BaseCreateUpdateModel):
return str(self.id) return str(self.id)
# Mysql PostgreSQL incompatible (ce6e5038a6134fad83aba220e6204cf9 ce6e5038-a613-4fad-83ab-a220e6204cf9)
def output_as_string(field_name): def output_as_string(field_name):
return ExpressionWrapper(F(field_name), output_field=models.CharField()) return ExpressionWrapper(F(field_name), output_field=models.CharField())

View File

@ -1,14 +1,13 @@
import time import time
from collections import defaultdict from collections import defaultdict
from uuid import UUID
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.db import transaction from django.db import transaction
from django.db.models import F
from assets.models import Asset from assets.models import Asset
from assets.utils import NodeAssetsUtil from assets.utils import NodeAssetsUtil
from common.db.models import output_as_string
from common.decorators import merge_delay_run from common.decorators import merge_delay_run
from common.decorators import on_transaction_commit from common.decorators import on_transaction_commit
from common.utils import get_logger from common.utils import get_logger
@ -399,11 +398,11 @@ class UserPermTreeBuildUtil(object):
asset_node_pairs = Asset.nodes.through.objects \ asset_node_pairs = Asset.nodes.through.objects \
.filter(asset_id__in=self.direct_asset_ids) \ .filter(asset_id__in=self.direct_asset_ids) \
.annotate( .annotate(
str_asset_id=output_as_string('asset_id'), str_asset_id=F('asset_id'),
str_node_id=output_as_string('node_id') str_node_id=F('node_id')
).values_list('str_asset_id', 'str_node_id') ).values_list('str_asset_id', 'str_node_id')
asset_node_pairs = [ asset_node_pairs = [
(str(UUID(asset_id)), str(UUID(node_id))) (str(asset_id), str(node_id))
for asset_id, node_id in asset_node_pairs for asset_id, node_id in asset_node_pairs
] ]
return asset_node_pairs return asset_node_pairs