Skip to content

Commit f3b9985

Browse files
author
Daniel Bremer
committed
optimize coordinate generation precedence
1 parent ab5a7f4 commit f3b9985

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

netbox_topology_views/views.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -205,25 +205,11 @@ def create_node(
205205
group = get_object_or_404(CoordinateGroup, pk=group_id)
206206

207207
node["physics"] = not disable_physics
208-
if not disable_physics:
209-
# Coords must be set even if no coords have been stored. Otherwise nodes with coords
210-
# will not be placed correctly by vis-network.
211-
node["x"] = 0
212-
node["y"] = 0
213-
else:
214-
# draw devices clustered by their rack neighbours
215-
# for center coordinates of a rack, use the rack name as seed (if available)
216-
random.seed(device.rack.name if hasattr(device, "rack") else device.name)
217-
# set the upper size of the graph
218-
plot_size = int(nnodes / 12) + 1
219-
base_coords_x = random.randint(-plot_size * nnodes, plot_size * nnodes)
220-
base_coords_y = random.randint(-plot_size * nnodes, plot_size * nnodes)
221-
# make seed unique for devices for spread
222-
if hasattr(device, "rack"):
223-
random.seed(device.name)
224-
# spread devices around the rack center coordinates
225-
node["x"] = base_coords_x + random.randint(-2 * nnodes, 2 * nnodes)
226-
node["y"] = base_coords_y + random.randint(-2 * nnodes, 2 * nnodes)
208+
209+
# Coords must be set even if no coords have been stored. Otherwise nodes with coords
210+
# will not be placed correctly by vis-network.
211+
node["x"] = 0
212+
node["y"] = 0
227213
if model_class.objects.filter(group=group, device=device.pk).values('x') and model_class.objects.filter(group=group, device=device.pk).values('y'):
228214
# Coordinates data for the device exists in Coordinates Group. Let's assign them
229215
node["x"] = model_class.objects.get(group=group, device=device.pk).x
@@ -238,6 +224,21 @@ def create_node(
238224
node["x"] = int(cords[0])
239225
node["y"] = int(cords[1])
240226
node["physics"] = False
227+
elif disable_physics:
228+
# draw devices clustered by their rack neighbours
229+
# for center coordinates of a rack, use the rack name as seed (if available)
230+
random.seed(device.rack.name if hasattr(device, "rack") else device.name)
231+
# set the upper size of the graph
232+
plot_size = int(nnodes / 12) + 1
233+
base_coords_x = random.randint(-plot_size * nnodes, plot_size * nnodes)
234+
base_coords_y = random.randint(-plot_size * nnodes, plot_size * nnodes)
235+
# make seed unique for devices for spread
236+
if hasattr(device, "rack"):
237+
random.seed(device.name)
238+
# spread devices around the rack center coordinates
239+
node["x"] = base_coords_x + random.randint(-2 * nnodes, 2 * nnodes)
240+
node["y"] = base_coords_y + random.randint(-2 * nnodes, 2 * nnodes)
241+
241242

242243
dev_title = "<table><tbody> %s</tbody></table>" % (node_content)
243244
node["title"] = dev_title

0 commit comments

Comments
 (0)