mirror of https://github.com/jumpserver/jumpserver
perf: Modifying the label matching logic of an AppletHost (random)
parent
4887b21d35
commit
457021040a
|
@ -173,7 +173,7 @@ class Asset(NodesRelationMixin, LabeledMixin, AbsConnectivity, JSONFilterMixin,
|
||||||
|
|
||||||
def get_labels(self):
|
def get_labels(self):
|
||||||
from labels.models import Label, LabeledResource
|
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) \
|
label_ids = LabeledResource.objects.filter(res_type=res_type, res_id=self.id) \
|
||||||
.values_list('label_id', flat=True)
|
.values_list('label_id', flat=True)
|
||||||
return Label.objects.filter(id__in=label_ids)
|
return Label.objects.filter(id__in=label_ids)
|
||||||
|
|
|
@ -171,12 +171,13 @@ class Applet(JMSBaseModel):
|
||||||
if not hosts:
|
if not hosts:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
spec_label = asset.labels.filter(label__name__in=['AppletHost', '发布机']).first()
|
spec_label_values = asset.get_labels().filter(
|
||||||
if spec_label and spec_label.label:
|
name__in=['AppletHost', '发布机']
|
||||||
label_value = spec_label.label.value
|
).values_list('value', flat=True)
|
||||||
matched = [host for host in hosts if host.name == label_value]
|
host_matched = [host for host in hosts if host.name in spec_label_values]
|
||||||
if matched:
|
if host_matched:
|
||||||
return random.choice(matched)
|
return random.choice(host_matched)
|
||||||
|
|
||||||
|
|
||||||
hosts = [h for h in hosts if h.auto_create_accounts]
|
hosts = [h for h in hosts if h.auto_create_accounts]
|
||||||
prefer_key = self.host_prefer_key_tpl.format(user.id)
|
prefer_key = self.host_prefer_key_tpl.format(user.id)
|
||||||
|
|
Loading…
Reference in New Issue