Cleaned up debugging logs in CablePath

This commit is contained in:
Jeremy Stretch 2025-12-01 09:53:09 -05:00
parent 665f91f6b3
commit 5597664b74

View File

@ -1,4 +1,5 @@
import itertools import itertools
import logging
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -30,6 +31,8 @@ __all__ = (
'CableTermination', 'CableTermination',
) )
logger = logging.getLogger(__name__)
trace_paths = Signal() trace_paths = Signal()
@ -666,14 +669,13 @@ class CablePath(models.Model):
is_active = True is_active = True
is_split = False is_split = False
DEBUG = False logger.debug(f'Tracing cable path from {terminations}...')
segment = 0 segment = 0
while terminations: while terminations:
segment += 1 segment += 1
if DEBUG: logger.debug(f'[Path segment #{segment}] Position stack: {position_stack}')
print(f'[#{segment}] Position stack: {position_stack}') logger.debug(f'[Path segment #{segment}] Local terminations: {terminations}')
print(f'[#{segment}] Local terminations: {terminations}')
# Terminations must all be of the same type # Terminations must all be of the same type
if not all(isinstance(t, type(terminations[0])) for t in terminations[1:]): if not all(isinstance(t, type(terminations[0])) for t in terminations[1:]):
@ -707,8 +709,7 @@ class CablePath(models.Model):
links = list(dict.fromkeys( links = list(dict.fromkeys(
termination.link for termination in terminations if termination.link is not None termination.link for termination in terminations if termination.link is not None
)) ))
if DEBUG: logger.debug(f'[Path segment #{segment}] Links: {links}')
print(f'[#{segment}] Links: {links}')
if len(links) == 0: if len(links) == 0:
if len(path) == 1: if len(path) == 1:
# If this is the start of the path and no link exists, return None # If this is the start of the path and no link exists, return None
@ -771,12 +772,13 @@ class CablePath(models.Model):
link.interface_b if link.interface_a is terminations[0] else link.interface_a for link in links link.interface_b if link.interface_a is terminations[0] else link.interface_a for link in links
] ]
if DEBUG: logger.debug(f'[Path segment #{segment}] Remote terminations: {remote_terminations}')
print(f'[#{segment}] Remote terminations: {remote_terminations}')
# Remote Terminations must all be of the same type, otherwise return a split path # Remote Terminations must all be of the same type, otherwise return a split path
if not all(isinstance(t, type(remote_terminations[0])) for t in remote_terminations[1:]): if not all(isinstance(t, type(remote_terminations[0])) for t in remote_terminations[1:]):
is_complete = False is_complete = False
is_split = True is_split = True
logger.debug('Remote termination types differ; aborting trace.')
break break
# Step 7: Record the far-end termination object(s) # Step 7: Record the far-end termination object(s)
@ -798,6 +800,10 @@ class CablePath(models.Model):
port_mappings = PortMapping.objects.filter(q_filter) port_mappings = PortMapping.objects.filter(q_filter)
elif remote_terminations[0].positions > 1: elif remote_terminations[0].positions > 1:
is_split = True is_split = True
logger.debug(
'Encountered front port mapped to multiple rear ports but position stack is empty; aborting '
'trace.'
)
break break
else: else:
port_mappings = PortMapping.objects.filter(front_port__in=remote_terminations) port_mappings = PortMapping.objects.filter(front_port__in=remote_terminations)
@ -819,6 +825,10 @@ class CablePath(models.Model):
port_mappings = PortMapping.objects.filter(q_filter) port_mappings = PortMapping.objects.filter(q_filter)
elif remote_terminations[0].positions > 1: elif remote_terminations[0].positions > 1:
is_split = True is_split = True
logger.debug(
'Encountered rear port mapped to multiple front ports but position stack is empty; aborting '
'trace.'
)
break break
else: else:
port_mappings = PortMapping.objects.filter(rear_port__in=remote_terminations) port_mappings = PortMapping.objects.filter(rear_port__in=remote_terminations)
@ -876,6 +886,7 @@ class CablePath(models.Model):
# Unsupported topology, mark as split and exit # Unsupported topology, mark as split and exit
is_complete = False is_complete = False
is_split = True is_split = True
logger.warning('Encountered an unsupported topology; aborting trace.')
break break
return cls( return cls(