Skip to content

Commit a412355

Browse files
authored
fix broken csv route (#56)
1 parent 6fda3e9 commit a412355

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Types of changes:
2323

2424
- Moved Changelog to its own file and followed format as stated in [Keep a Changelog]
2525

26+
### Fixed
27+
28+
- Fixed Fastapi csv route
29+
2630
## [0.1.1] - 2025-05-30
2731

2832
### Fixed

Grasshopper/grasshopper/api.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import gevent
1414
from bacpypes3.rdf.core import BACnetNS
1515
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile, status
16-
from fastapi.responses import FileResponse, JSONResponse
16+
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
1717
from pyvis.network import Network
1818
from rdflib import Graph, Literal, Namespace # type: ignore
1919
from rdflib.compare import graph_diff, to_isomorphic
@@ -756,21 +756,27 @@ async def export_csv(ttl_filename: str, request: Request):
756756
g.parse(ttl_filepath, format="ttl")
757757
nx_graph, node_data, edge_data = build_networkx_graph(g)
758758

759+
BACnetEdgeType_subnets = [
760+
BACnetEdgeType.BACNET_ROUTER_ON_SUBNET.value,
761+
BACnetEdgeType.DEVICE_ON_SUBNET.value,
762+
BACnetEdgeType.BBMD_BROADCAST_DOMAIN.value,
763+
]
764+
BACnetEdgeType_networks = [
765+
BACnetEdgeType.DEVICE_ON_NETWORK.value,
766+
]
767+
759768
for u, v, attr in nx_graph.edges(data=True):
760769
edge_label = attr.get("triples", [])[0][1] if "triples" in attr else None
761770
if edge_label:
762-
if "device-on-network" in edge_label:
763-
if "router" in str(u):
764-
node_data[str(u)]["subnet"] = "/".join(str(v).split("/")[-2:])
765-
else:
766-
node_data[str(u)]["network-id"] = [str(v).split("/")[-1]]
767-
if "router-to-network" in edge_label:
771+
if any(subnet in edge_label for subnet in BACnetEdgeType_subnets):
772+
node_data[str(u)]["subnet"] = "/".join(str(v).split("/")[-2:])
773+
elif any(network in edge_label for network in BACnetEdgeType_networks):
768774
if "network-id" in node_data[str(u)]:
769775
node_data[str(u)]["network-id"].append(str(v).split("/")[-1])
770776
else:
771777
node_data[str(u)]["network-id"] = [str(v).split("/")[-1]]
772778

773-
output_str = StringIO()
779+
output_str = StringIO(newline="")
774780
writer = csv.writer(output_str)
775781

776782
# Write header
@@ -793,7 +799,7 @@ async def export_csv(ttl_filename: str, request: Request):
793799
)
794800

795801
# Return as a downloadable CSV file
796-
response = JSONResponse(content=output_str.getvalue())
802+
response = StreamingResponse(content=output_str.getvalue())
797803
response.headers["Content-Disposition"] = f"attachment; filename={ttl_filename}.csv"
798804
response.headers["Content-Type"] = "text/csv"
799805

0 commit comments

Comments
 (0)