1313import gevent
1414from bacpypes3 .rdf .core import BACnetNS
1515from fastapi import APIRouter , Depends , File , HTTPException , Request , UploadFile , status
16- from fastapi .responses import FileResponse , JSONResponse
16+ from fastapi .responses import FileResponse , JSONResponse , StreamingResponse
1717from pyvis .network import Network
1818from rdflib import Graph , Literal , Namespace # type: ignore
1919from 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