From 5a0d8e307d22748d98bde01c03f5ff11ee7771c0 Mon Sep 17 00:00:00 2001 From: Daniel Bremer Date: Tue, 7 May 2024 17:10:31 +0200 Subject: [PATCH] optimize coordinate generation precedence --- netbox_topology_views/views.py | 39 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/netbox_topology_views/views.py b/netbox_topology_views/views.py index f9db2a7..ae7d2fb 100644 --- a/netbox_topology_views/views.py +++ b/netbox_topology_views/views.py @@ -205,25 +205,11 @@ def create_node( group = get_object_or_404(CoordinateGroup, pk=group_id) node["physics"] = not disable_physics - if not disable_physics: - # Coords must be set even if no coords have been stored. Otherwise nodes with coords - # will not be placed correctly by vis-network. - node["x"] = 0 - node["y"] = 0 - else: - # draw devices clustered by their rack neighbours - # for center coordinates of a rack, use the rack name as seed (if available) - random.seed(device.rack.name if hasattr(device, "rack") else device.name) - # set the upper size of the graph - plot_size = int(nnodes / 12) + 1 - base_coords_x = random.randint(-plot_size * nnodes, plot_size * nnodes) - base_coords_y = random.randint(-plot_size * nnodes, plot_size * nnodes) - # make seed unique for devices for spread - if hasattr(device, "rack"): - random.seed(device.name) - # spread devices around the rack center coordinates - node["x"] = base_coords_x + random.randint(-2 * nnodes, 2 * nnodes) - node["y"] = base_coords_y + random.randint(-2 * nnodes, 2 * nnodes) + + # Coords must be set even if no coords have been stored. Otherwise nodes with coords + # will not be placed correctly by vis-network. + node["x"] = 0 + node["y"] = 0 if model_class.objects.filter(group=group, device=device.pk).values('x') and model_class.objects.filter(group=group, device=device.pk).values('y'): # Coordinates data for the device exists in Coordinates Group. Let's assign them node["x"] = model_class.objects.get(group=group, device=device.pk).x @@ -238,6 +224,21 @@ def create_node( node["x"] = int(cords[0]) node["y"] = int(cords[1]) node["physics"] = False + elif disable_physics: + # draw devices clustered by their rack neighbours + # for center coordinates of a rack, use the rack name as seed (if available) + random.seed(device.rack.name if hasattr(device, "rack") else device.name) + # set the upper size of the graph + plot_size = int(nnodes / 12) + 1 + base_coords_x = random.randint(-plot_size * nnodes, plot_size * nnodes) + base_coords_y = random.randint(-plot_size * nnodes, plot_size * nnodes) + # make seed unique for devices for spread + if hasattr(device, "rack"): + random.seed(device.name) + # spread devices around the rack center coordinates + node["x"] = base_coords_x + random.randint(-2 * nnodes, 2 * nnodes) + node["y"] = base_coords_y + random.randint(-2 * nnodes, 2 * nnodes) + dev_title = " %s
" % (node_content) node["title"] = dev_title