perf: Modifying the label matching logic of an AppletHost (random)

pull/13811/head
Bai 2024-07-25 18:59:06 +08:00 committed by 老广
parent d7c26cab7d
commit 1816d52d21
2 changed files with 8 additions and 7 deletions

View File

@ -174,7 +174,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)

View File

@ -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)