From 457021040a668a2a4e20c98868412347e8e1010a Mon Sep 17 00:00:00 2001 From: Bai Date: Thu, 25 Jul 2024 18:59:06 +0800 Subject: [PATCH] perf: Modifying the label matching logic of an AppletHost (random) --- apps/assets/models/asset/common.py | 2 +- apps/terminal/models/applet/applet.py | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/apps/assets/models/asset/common.py b/apps/assets/models/asset/common.py index 9676c3db0..6f7df504d 100644 --- a/apps/assets/models/asset/common.py +++ b/apps/assets/models/asset/common.py @@ -173,7 +173,7 @@ class Asset(NodesRelationMixin, LabeledMixin, AbsConnectivity, JSONFilterMixin, def get_labels(self): from labels.models import Label, LabeledResource - res_type = ContentType.objects.get_for_model(self.__class__) + res_type = ContentType.objects.get_for_model(self.__class__.label_model()) label_ids = LabeledResource.objects.filter(res_type=res_type, res_id=self.id) \ .values_list('label_id', flat=True) return Label.objects.filter(id__in=label_ids) diff --git a/apps/terminal/models/applet/applet.py b/apps/terminal/models/applet/applet.py index 7ec80279d..8b91c7b5f 100644 --- a/apps/terminal/models/applet/applet.py +++ b/apps/terminal/models/applet/applet.py @@ -171,12 +171,13 @@ class Applet(JMSBaseModel): if not hosts: return None - spec_label = asset.labels.filter(label__name__in=['AppletHost', '发布机']).first() - if spec_label and spec_label.label: - label_value = spec_label.label.value - matched = [host for host in hosts if host.name == label_value] - if matched: - return random.choice(matched) + spec_label_values = asset.get_labels().filter( + name__in=['AppletHost', '发布机'] + ).values_list('value', flat=True) + host_matched = [host for host in hosts if host.name in spec_label_values] + if host_matched: + return random.choice(host_matched) + hosts = [h for h in hosts if h.auto_create_accounts] prefer_key = self.host_prefer_key_tpl.format(user.id)