This commit is contained in:
Andrey Tikhonov 2025-02-24 15:38:19 +01:00
parent 7794c6cfcb
commit 9c3e7f2c5d
2 changed files with 5 additions and 54 deletions

View File

@ -434,21 +434,17 @@ class PowerOutletViewSet(PathEndpointMixin, NetBoxModelViewSet):
class InterfaceViewSet(PathEndpointMixin, NetBoxModelViewSet): class InterfaceViewSet(PathEndpointMixin, NetBoxModelViewSet):
queryset = Interface.objects.prefetch_related( queryset = Interface.objects.prefetch_related(
# '_path',
# 'cable__terminations',
GenericPrefetch( GenericPrefetch(
"cable__terminations__termination", "cable__terminations__termination",
[ [
Interface.objects.select_related("device", "cable"), Interface.objects.select_related("device", "cable"),
], ],
), ),
Prefetch( GenericPrefetch(
"_path", "_path__path_objects",
CablePath.objects.prefetch_related( [
GenericPrefetch("path_objects", [ Interface.objects.select_related("device", "cable"),
Interface.objects.select_related("device"), ],
]),
)
), ),
'l2vpn_terminations', # Referenced by InterfaceSerializer.l2vpn_termination 'l2vpn_terminations', # Referenced by InterfaceSerializer.l2vpn_termination
'ip_addresses', # Referenced by Interface.count_ipaddresses() 'ip_addresses', # Referenced by Interface.count_ipaddresses()

View File

@ -490,15 +490,6 @@ class CablePath(models.Model):
ct_id, _ = decompile_path_node(self.path[-1][0]) ct_id, _ = decompile_path_node(self.path[-1][0])
return ObjectType.objects.get_for_id(ct_id) return ObjectType.objects.get_for_id(ct_id)
@property
def path_objects_old(self):
"""
Cache and return the complete path as lists of objects, derived from their annotation within the path.
"""
if not hasattr(self, '_path_objects'):
self._path_objects = self._get_path()
return self._path_objects
@property @property
def _path_decompiled(self): def _path_decompiled(self):
res = [] res = []
@ -747,42 +738,6 @@ class CablePath(models.Model):
self.delete() self.delete()
retrace.alters_data = True retrace.alters_data = True
def _get_path(self):
"""
Return the path as a list of prefetched objects.
"""
# Compile a list of IDs to prefetch for each type of model in the path
to_prefetch = defaultdict(list)
for node in self._nodes:
ct_id, object_id = decompile_path_node(node)
to_prefetch[ct_id].append(object_id)
# Prefetch path objects using one query per model type. Prefetch related devices where appropriate.
prefetched = {}
for ct_id, object_ids in to_prefetch.items():
model_class = ObjectType.objects.get_for_id(ct_id).model_class()
queryset = model_class.objects.filter(pk__in=object_ids)
if hasattr(model_class, 'device'):
queryset = queryset.prefetch_related('device')
prefetched[ct_id] = {
obj.id: obj for obj in queryset
}
# Replicate the path using the prefetched objects.
path = []
for step in self.path:
nodes = []
for node in step:
ct_id, object_id = decompile_path_node(node)
try:
nodes.append(prefetched[ct_id][object_id])
except KeyError:
# Ignore stale (deleted) object IDs
pass
path.append(nodes)
return path
def get_cable_ids(self): def get_cable_ids(self):
""" """
Return all Cable IDs within the path. Return all Cable IDs within the path.