Skip to content

Commit

Permalink
Mark link as inactive in KML based on devices (#349)
Browse files Browse the repository at this point in the history
* Mark link as inactive based on devices

* update test
  • Loading branch information
WillNilges authored Apr 26, 2024
1 parent 07a4bc1 commit 1c3fda9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/meshapi/tests/test_kml_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def test_kml_data(self):
s3_building, sn3_install, sn3, sn3_omni = create_building_install_node_and_device(fake_member, 713)
brian_building, brian_install, brian, brian_omni = create_building_install_node_and_device(fake_member, 3)
random_building, random_install, random, random_omni = create_building_install_node_and_device(fake_member, 123)
dead_building, dead_install, dead, dead_omni = create_building_install_node_and_device(fake_member, 888)
dead_omni.status = Device.DeviceStatus.INACTIVE
dead_omni.save()

links.append(
Link(
Expand Down Expand Up @@ -128,6 +131,16 @@ def test_kml_data(self):
)
)

# Should show up as inactive because dead_omni is inactive
links.append(
Link(
from_device=dead_omni,
to_device=random_omni,
status=Link.LinkStatus.ACTIVE,
type=Link.LinkType.STANDARD,
)
)

for link in links:
link.save()

Expand All @@ -141,7 +154,7 @@ def test_kml_data(self):
assert len(kml_tree[0]) == 6 # 4 styles and 2 folders
assert len(kml_tree[0][4]) == 3 # "Active" and "Inactive" node folders + 1 for "name" tag
assert len(kml_tree[0][4][1]) == 7 # 5 borough folders and "Other" + 1 for "name" tag
assert len(kml_tree[0][4][1][6]) == 13 # 6 installs and 6 NNs, all in the "Other" folder + 1 for "name" tag
assert len(kml_tree[0][4][1][6]) == 15 # 7 installs and 7 NNs, all in the "Other" folder + 1 for "name" tag
assert len(kml_tree[0][5]) == 3 # "Active" and "Inactive" link folders + 1 for "name" tag
assert len(kml_tree[0][5][1]) == 5 # 4 active links + 1 for "name" tag
assert len(kml_tree[0][5][2]) == 2 # 1 inactive links + 1 for "name" tag
assert len(kml_tree[0][5][2]) == 3 # 1 inactive links + 1 for "name" tag
22 changes: 18 additions & 4 deletions src/meshapi/views/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from rest_framework.views import APIView

from meshapi.models import Install, Link
from meshapi.models.devices.device import Device
from meshapi.models.node import Node

KML_CONTENT_TYPE = "application/vnd.google-earth.kml+xml"
KML_CONTENT_TYPE_WITH_CHARSET = f"{KML_CONTENT_TYPE}; charset=utf-8"
Expand Down Expand Up @@ -208,9 +210,21 @@ def get(self, request: HttpRequest) -> HttpResponse:
.annotate(highest_altitude=Greatest("from_device__altitude", "to_device__altitude"))
.order_by(F("highest_altitude").asc(nulls_first=True))
):
# Logic to decide if this link should show up as active or not
mark_active: bool = (
# Link must be active
link.status == Link.LinkStatus.ACTIVE
# And the devices
and link.from_device.status == Device.DeviceStatus.ACTIVE
and link.to_device.status == Device.DeviceStatus.ACTIVE
# And the device's nodes
and link.from_device.node.status == Node.NodeStatus.ACTIVE
and link.to_device.node.status == Node.NodeStatus.ACTIVE
)
node_label: str = f"{str(link.from_device.node)}-{str(link.to_device.node)}"
placemark = kml.Placemark(
name=f"Links-{link.id}",
style_url=styles.StyleUrl(url="#red_line" if link.status == Link.LinkStatus.ACTIVE else "#grey_line"),
style_url=styles.StyleUrl(url="#red_line" if mark_active else "#grey_line"),
kml_geometry=geometry.LineString(
geometry=LineString(
[
Expand All @@ -235,8 +249,8 @@ def get(self, request: HttpRequest) -> HttpResponse:
to_identifier = link.to_device.node.network_number

extended_data = {
"name": f"Links-{link.id}",
"stroke": ACTIVE_COLOR if link.status == Link.LinkStatus.ACTIVE else INACTIVE_COLOR,
"name": f"Links-{link.id}-{node_label}",
"stroke": ACTIVE_COLOR if mark_active else INACTIVE_COLOR,
"fill": "#000000",
"fill-opacity": "0",
"from": str(from_identifier),
Expand All @@ -249,7 +263,7 @@ def get(self, request: HttpRequest) -> HttpResponse:
elements=[Data(name=key, value=val) for key, val in extended_data.items()]
)

if link.status == Link.LinkStatus.ACTIVE:
if mark_active:
active_links_folder.append(placemark)
else:
inactive_links_folder.append(placemark)
Expand Down

0 comments on commit 1c3fda9

Please sign in to comment.