From f8800b8303a188c2a7c62ce8f4f38b5e0f99e397 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 2 Oct 2020 21:39:55 -0400 Subject: [PATCH] Optimize console/power/interface connection lists --- netbox/dcim/tables.py | 57 ++++++++++++++++++++++--------------------- netbox/dcim/views.py | 42 ++++++++++++------------------- 2 files changed, 45 insertions(+), 54 deletions(-) diff --git a/netbox/dcim/tables.py b/netbox/dcim/tables.py index 7ab08eae4..57cd0b6eb 100644 --- a/netbox/dcim/tables.py +++ b/netbox/dcim/tables.py @@ -67,8 +67,8 @@ INTERFACE_TAGGED_VLANS = """ {% endfor %} """ -CONNECTION_STATUS = """ -{{ record.get_connection_status_display }} +PATH_STATUS = """ +{% if value %}Connected{% else %}Not Connected{% endif %} """ @@ -813,13 +813,13 @@ class CableTable(BaseTable): class ConsoleConnectionTable(BaseTable): console_server = tables.Column( - accessor=Accessor('path__destination__device'), + accessor=Accessor('_path__destination__device'), orderable=False, linkify=True, verbose_name='Console Server' ) console_server_port = tables.Column( - accessor=Accessor('path__destination'), + accessor=Accessor('_path__destination'), orderable=False, linkify=True, verbose_name='Port' @@ -831,27 +831,28 @@ class ConsoleConnectionTable(BaseTable): linkify=True, verbose_name='Console Port' ) - connection_status = tables.TemplateColumn( - accessor=Accessor('path__is_connected'), - orderable=False, - template_code=CONNECTION_STATUS, - verbose_name='Status' + path_status = tables.TemplateColumn( + accessor=Accessor('_path__is_connected'), + template_code=PATH_STATUS, + verbose_name='Path Status' ) + add_prefetch = False + class Meta(BaseTable.Meta): model = ConsolePort - fields = ('console_server', 'console_server_port', 'device', 'name', 'connection_status') + fields = ('console_server', 'console_server_port', 'device', 'name', 'path_status') class PowerConnectionTable(BaseTable): pdu = tables.Column( - accessor=Accessor('path__destination__device'), + accessor=Accessor('_path__destination__device'), orderable=False, linkify=True, verbose_name='PDU' ) outlet = tables.Column( - accessor=Accessor('path__destination'), + accessor=Accessor('_path__destination'), orderable=False, linkify=True, verbose_name='Outlet' @@ -863,16 +864,17 @@ class PowerConnectionTable(BaseTable): linkify=True, verbose_name='Power Port' ) - connection_status = tables.TemplateColumn( - accessor=Accessor('path__is_connected'), - orderable=False, - template_code=CONNECTION_STATUS, - verbose_name='Status' + path_status = tables.TemplateColumn( + accessor=Accessor('_path__is_connected'), + template_code=PATH_STATUS, + verbose_name='Path Status' ) + add_prefetch = False + class Meta(BaseTable.Meta): model = PowerPort - fields = ('pdu', 'outlet', 'device', 'name', 'connection_status') + fields = ('pdu', 'outlet', 'device', 'name', 'path_status') class InterfaceConnectionTable(BaseTable): @@ -887,29 +889,28 @@ class InterfaceConnectionTable(BaseTable): verbose_name='Interface A' ) device_b = tables.Column( - accessor=Accessor('path__destination__device'), + accessor=Accessor('_path__destination__device'), orderable=False, linkify=True, verbose_name='Device B' ) interface_b = tables.Column( - accessor=Accessor('path__destination'), + accessor=Accessor('_path__destination'), orderable=False, linkify=True, verbose_name='Interface B' ) - connection_status = tables.TemplateColumn( - accessor=Accessor('path__is_connected'), - orderable=False, - template_code=CONNECTION_STATUS, - verbose_name='Status' + path_status = tables.TemplateColumn( + accessor=Accessor('_path__is_connected'), + template_code=PATH_STATUS, + verbose_name='Path Status' ) + add_prefetch = False + class Meta(BaseTable.Meta): model = Interface - fields = ( - 'device_a', 'interface_a', 'device_b', 'interface_b', 'connection_status', - ) + fields = ('device_a', 'interface_a', 'device_b', 'interface_b', 'path_status') # diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index ac30461b0..87bd38309 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -2079,12 +2079,8 @@ class CableBulkDeleteView(BulkDeleteView): class ConsoleConnectionsListView(ObjectListView): queryset = ConsolePort.objects.prefetch_related( - 'device', 'connected_endpoint__device' - ).filter( - connected_endpoint__isnull=False - ).order_by( - 'cable', 'connected_endpoint__device__name', 'connected_endpoint__name' - ) + 'device', '_path__destination__device' + ).filter(_path__isnull=False).order_by('device') filterset = filters.ConsoleConnectionFilterSet filterset_form = forms.ConsoleConnectionFilterForm table = tables.ConsoleConnectionTable @@ -2097,11 +2093,11 @@ class ConsoleConnectionsListView(ObjectListView): ] for obj in self.queryset: csv = csv_format([ - obj.connected_endpoint.device.identifier if obj.connected_endpoint else None, - obj.connected_endpoint.name if obj.connected_endpoint else None, + obj._path.destination.device.identifier if obj._path.destination else None, + obj._path.destination.name if obj._path.destination else None, obj.device.identifier, obj.name, - obj.get_connection_status_display(), + 'Connected' if obj._path.is_connected else 'Not Connected', ]) csv_data.append(csv) @@ -2110,12 +2106,8 @@ class ConsoleConnectionsListView(ObjectListView): class PowerConnectionsListView(ObjectListView): queryset = PowerPort.objects.prefetch_related( - 'device', '_connected_poweroutlet__device' - ).filter( - _connected_poweroutlet__isnull=False - ).order_by( - 'cable', '_connected_poweroutlet__device__name', '_connected_poweroutlet__name' - ) + 'device', '_path__destination__device' + ).filter(_path__isnull=False).order_by('device') filterset = filters.PowerConnectionFilterSet filterset_form = forms.PowerConnectionFilterForm table = tables.PowerConnectionTable @@ -2128,11 +2120,11 @@ class PowerConnectionsListView(ObjectListView): ] for obj in self.queryset: csv = csv_format([ - obj.connected_endpoint.device.identifier if obj.connected_endpoint else None, - obj.connected_endpoint.name if obj.connected_endpoint else None, + obj._path.destination.device.identifier if obj._path.destination else None, + obj._path.destination.name if obj._path.destination else None, obj.device.identifier, obj.name, - obj.get_connection_status_display(), + 'Connected' if obj._path.is_connected else 'Not Connected', ]) csv_data.append(csv) @@ -2141,14 +2133,12 @@ class PowerConnectionsListView(ObjectListView): class InterfaceConnectionsListView(ObjectListView): queryset = Interface.objects.prefetch_related( - 'device', 'cable', '_connected_interface__device' + 'device', '_path__destination__device' ).filter( # Avoid duplicate connections by only selecting the lower PK in a connected pair - _connected_interface__isnull=False, + _path__isnull=False, pk__lt=F('_connected_interface') - ).order_by( - 'device' - ) + ).order_by('device') filterset = filters.InterfaceConnectionFilterSet filterset_form = forms.InterfaceConnectionFilterForm table = tables.InterfaceConnectionTable @@ -2163,11 +2153,11 @@ class InterfaceConnectionsListView(ObjectListView): ] for obj in self.queryset: csv = csv_format([ - obj.connected_endpoint.device.identifier if obj.connected_endpoint else None, - obj.connected_endpoint.name if obj.connected_endpoint else None, + obj._path.destination.device.identifier if obj._path.destination else None, + obj._path.destination.name if obj._path.destination else None, obj.device.identifier, obj.name, - obj.get_connection_status_display(), + 'Connected' if obj._path.is_connected else 'Not Connected', ]) csv_data.append(csv)