Spaces:
Running
Running
Update network_graph_generator.py
Browse files- network_graph_generator.py +83 -25
network_graph_generator.py
CHANGED
@@ -4,6 +4,62 @@
|
|
4 |
# import os
|
5 |
|
6 |
# def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# try:
|
8 |
# if not json_input.strip():
|
9 |
# return "Error: Empty input"
|
@@ -12,7 +68,7 @@
|
|
12 |
|
13 |
# if 'nodes' not in data or 'connections' not in data:
|
14 |
# raise ValueError("Missing required fields: nodes or connections")
|
15 |
-
|
16 |
# dot = graphviz.Graph(
|
17 |
# name='NetworkGraph',
|
18 |
# format='png',
|
@@ -29,14 +85,12 @@
|
|
29 |
# }
|
30 |
# )
|
31 |
|
32 |
-
# base_color = '#19191a'
|
33 |
-
|
34 |
# type_colors = {
|
35 |
-
# 'server':
|
36 |
-
# 'service': '#
|
37 |
-
# 'database': '#
|
38 |
-
# 'user': '#
|
39 |
-
# 'default':
|
40 |
# }
|
41 |
|
42 |
# nodes = data.get('nodes', [])
|
@@ -51,29 +105,23 @@
|
|
51 |
# raise ValueError(f"Invalid node: {node}")
|
52 |
|
53 |
# node_color = type_colors.get(node_type, type_colors['default'])
|
54 |
-
|
55 |
-
# if node_type == 'database' or node_color == '#a0a0a0':
|
56 |
-
# font_color = 'black'
|
57 |
-
# elif node_color == '#f39c12':
|
58 |
-
# font_color = 'black'
|
59 |
-
# else:
|
60 |
-
# font_color = 'white'
|
61 |
|
62 |
# if node_type == 'server':
|
63 |
# shape = 'box'
|
64 |
# style = 'filled,rounded'
|
65 |
# elif node_type == 'database':
|
66 |
# shape = 'cylinder'
|
67 |
-
# style = 'filled'
|
68 |
# elif node_type == 'user':
|
69 |
# shape = 'ellipse'
|
70 |
-
# style = 'filled'
|
71 |
# elif node_type == 'service':
|
72 |
# shape = 'hexagon'
|
73 |
-
# style = 'filled'
|
74 |
# else:
|
75 |
# shape = 'circle'
|
76 |
-
# style = 'filled'
|
77 |
|
78 |
# dot.node(
|
79 |
# node_id,
|
@@ -105,11 +153,11 @@
|
|
105 |
# fontsize='10',
|
106 |
# penwidth=penwidth
|
107 |
# )
|
108 |
-
|
109 |
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
110 |
# dot.render(tmp.name, format=output_format, cleanup=True)
|
111 |
# return f"{tmp.name}.{output_format}"
|
112 |
-
|
113 |
# except json.JSONDecodeError:
|
114 |
# return "Error: Invalid JSON format"
|
115 |
# except Exception as e:
|
@@ -194,11 +242,19 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
194 |
'overlap': 'false',
|
195 |
'splines': 'true',
|
196 |
'bgcolor': 'white',
|
197 |
-
'pad': '0.
|
198 |
-
'layout': 'neato'
|
|
|
|
|
|
|
|
|
199 |
},
|
200 |
node_attr={
|
201 |
'fixedsize': 'false'
|
|
|
|
|
|
|
|
|
202 |
}
|
203 |
)
|
204 |
|
@@ -267,8 +323,10 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
267 |
label=label,
|
268 |
color='#4a4a4a',
|
269 |
fontcolor='#4a4a4a',
|
270 |
-
fontsize='
|
271 |
-
penwidth=penwidth
|
|
|
|
|
272 |
)
|
273 |
|
274 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
|
|
4 |
# import os
|
5 |
|
6 |
# def generate_network_graph(json_input: str, output_format: str) -> str:
|
7 |
+
# """
|
8 |
+
# Generates a network graph from JSON input.
|
9 |
+
|
10 |
+
# Args:
|
11 |
+
# json_input (str): A JSON string describing the network graph structure.
|
12 |
+
# It must follow the Expected JSON Format Example below.
|
13 |
+
|
14 |
+
# Expected JSON Format Example:
|
15 |
+
# {
|
16 |
+
# "nodes": [
|
17 |
+
# {
|
18 |
+
# "id": "server1",
|
19 |
+
# "label": "Web Server",
|
20 |
+
# "type": "server"
|
21 |
+
# },
|
22 |
+
# {
|
23 |
+
# "id": "db1",
|
24 |
+
# "label": "Database",
|
25 |
+
# "type": "database"
|
26 |
+
# },
|
27 |
+
# {
|
28 |
+
# "id": "user1",
|
29 |
+
# "label": "User",
|
30 |
+
# "type": "user"
|
31 |
+
# },
|
32 |
+
# {
|
33 |
+
# "id": "service1",
|
34 |
+
# "label": "API Service",
|
35 |
+
# "type": "service"
|
36 |
+
# }
|
37 |
+
# ],
|
38 |
+
# "connections": [
|
39 |
+
# {
|
40 |
+
# "from": "user1",
|
41 |
+
# "to": "server1",
|
42 |
+
# "label": "HTTP Request",
|
43 |
+
# "weight": 2
|
44 |
+
# },
|
45 |
+
# {
|
46 |
+
# "from": "server1",
|
47 |
+
# "to": "service1",
|
48 |
+
# "label": "API Call",
|
49 |
+
# "weight": 3
|
50 |
+
# },
|
51 |
+
# {
|
52 |
+
# "from": "service1",
|
53 |
+
# "to": "db1",
|
54 |
+
# "label": "Query",
|
55 |
+
# "weight": 1
|
56 |
+
# }
|
57 |
+
# ]
|
58 |
+
# }
|
59 |
+
|
60 |
+
# Returns:
|
61 |
+
# str: The filepath to the generated PNG image file.
|
62 |
+
# """
|
63 |
# try:
|
64 |
# if not json_input.strip():
|
65 |
# return "Error: Empty input"
|
|
|
68 |
|
69 |
# if 'nodes' not in data or 'connections' not in data:
|
70 |
# raise ValueError("Missing required fields: nodes or connections")
|
71 |
+
|
72 |
# dot = graphviz.Graph(
|
73 |
# name='NetworkGraph',
|
74 |
# format='png',
|
|
|
85 |
# }
|
86 |
# )
|
87 |
|
|
|
|
|
88 |
# type_colors = {
|
89 |
+
# 'server': '#BEBEBE',
|
90 |
+
# 'service': '#B8D4F1',
|
91 |
+
# 'database': '#A8E6CF',
|
92 |
+
# 'user': '#FFF9C4',
|
93 |
+
# 'default': '#BEBEBE'
|
94 |
# }
|
95 |
|
96 |
# nodes = data.get('nodes', [])
|
|
|
105 |
# raise ValueError(f"Invalid node: {node}")
|
106 |
|
107 |
# node_color = type_colors.get(node_type, type_colors['default'])
|
108 |
+
# font_color = 'black'
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# if node_type == 'server':
|
111 |
# shape = 'box'
|
112 |
# style = 'filled,rounded'
|
113 |
# elif node_type == 'database':
|
114 |
# shape = 'cylinder'
|
115 |
+
# style = 'filled,rounded'
|
116 |
# elif node_type == 'user':
|
117 |
# shape = 'ellipse'
|
118 |
+
# style = 'filled,rounded'
|
119 |
# elif node_type == 'service':
|
120 |
# shape = 'hexagon'
|
121 |
+
# style = 'filled,rounded'
|
122 |
# else:
|
123 |
# shape = 'circle'
|
124 |
+
# style = 'filled,rounded'
|
125 |
|
126 |
# dot.node(
|
127 |
# node_id,
|
|
|
153 |
# fontsize='10',
|
154 |
# penwidth=penwidth
|
155 |
# )
|
156 |
+
|
157 |
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
158 |
# dot.render(tmp.name, format=output_format, cleanup=True)
|
159 |
# return f"{tmp.name}.{output_format}"
|
160 |
+
|
161 |
# except json.JSONDecodeError:
|
162 |
# return "Error: Invalid JSON format"
|
163 |
# except Exception as e:
|
|
|
242 |
'overlap': 'false',
|
243 |
'splines': 'true',
|
244 |
'bgcolor': 'white',
|
245 |
+
'pad': '0.8',
|
246 |
+
'layout': 'neato',
|
247 |
+
'sep': '+10',
|
248 |
+
'esep': '+5',
|
249 |
+
'nodesep': '1.0',
|
250 |
+
'concentrate': 'true'
|
251 |
},
|
252 |
node_attr={
|
253 |
'fixedsize': 'false'
|
254 |
+
},
|
255 |
+
edge_attr={
|
256 |
+
'labeldistance': '2.0',
|
257 |
+
'labelangle': '0'
|
258 |
}
|
259 |
)
|
260 |
|
|
|
323 |
label=label,
|
324 |
color='#4a4a4a',
|
325 |
fontcolor='#4a4a4a',
|
326 |
+
fontsize='9',
|
327 |
+
penwidth=penwidth,
|
328 |
+
labeldistance='1.5',
|
329 |
+
labelangle='0'
|
330 |
)
|
331 |
|
332 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|