Spaces:
Running
Running
Update network_graph_generator.py
Browse files- network_graph_generator.py +186 -21
network_graph_generator.py
CHANGED
@@ -1,9 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import graphviz
|
2 |
import json
|
3 |
from tempfile import NamedTemporaryFile
|
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 +185,7 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
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 +202,12 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
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 +222,23 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
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 +270,11 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
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:
|
|
|
1 |
+
# import graphviz
|
2 |
+
# import json
|
3 |
+
# from tempfile import NamedTemporaryFile
|
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"
|
10 |
+
|
11 |
+
# data = json.loads(json_input)
|
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',
|
19 |
+
# engine='neato',
|
20 |
+
# graph_attr={
|
21 |
+
# 'overlap': 'false',
|
22 |
+
# 'splines': 'true',
|
23 |
+
# 'bgcolor': 'white',
|
24 |
+
# 'pad': '0.5',
|
25 |
+
# 'layout': 'neato'
|
26 |
+
# },
|
27 |
+
# node_attr={
|
28 |
+
# 'fixedsize': 'false'
|
29 |
+
# }
|
30 |
+
# )
|
31 |
+
|
32 |
+
# base_color = '#19191a'
|
33 |
+
|
34 |
+
# type_colors = {
|
35 |
+
# 'server': base_color,
|
36 |
+
# 'service': '#4a90e2',
|
37 |
+
# 'database': '#a0a0a0',
|
38 |
+
# 'user': '#f39c12',
|
39 |
+
# 'default': base_color
|
40 |
+
# }
|
41 |
+
|
42 |
+
# nodes = data.get('nodes', [])
|
43 |
+
# connections = data.get('connections', [])
|
44 |
+
|
45 |
+
# for node in nodes:
|
46 |
+
# node_id = node.get('id')
|
47 |
+
# label = node.get('label')
|
48 |
+
# node_type = node.get('type', 'default')
|
49 |
+
|
50 |
+
# if not all([node_id, label]):
|
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,
|
80 |
+
# label,
|
81 |
+
# shape=shape,
|
82 |
+
# style=style,
|
83 |
+
# fillcolor=node_color,
|
84 |
+
# fontcolor=font_color,
|
85 |
+
# fontsize='12'
|
86 |
+
# )
|
87 |
+
|
88 |
+
# for connection in connections:
|
89 |
+
# from_node = connection.get('from')
|
90 |
+
# to_node = connection.get('to')
|
91 |
+
# label = connection.get('label', '')
|
92 |
+
# weight = connection.get('weight', 1)
|
93 |
+
|
94 |
+
# if not all([from_node, to_node]):
|
95 |
+
# raise ValueError(f"Invalid connection: {connection}")
|
96 |
+
|
97 |
+
# penwidth = str(max(1, min(5, weight)))
|
98 |
+
|
99 |
+
# dot.edge(
|
100 |
+
# from_node,
|
101 |
+
# to_node,
|
102 |
+
# label=label,
|
103 |
+
# color='#4a4a4a',
|
104 |
+
# fontcolor='#4a4a4a',
|
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:
|
116 |
+
# return f"Error: {str(e)}"
|
117 |
+
|
118 |
import graphviz
|
119 |
import json
|
120 |
from tempfile import NamedTemporaryFile
|
121 |
import os
|
122 |
|
123 |
def generate_network_graph(json_input: str, output_format: str) -> str:
|
124 |
+
"""
|
125 |
+
Generates a network graph from JSON input.
|
126 |
+
|
127 |
+
Args:
|
128 |
+
json_input (str): A JSON string describing the network graph structure.
|
129 |
+
It must follow the Expected JSON Format Example below.
|
130 |
+
|
131 |
+
Expected JSON Format Example:
|
132 |
+
{
|
133 |
+
"nodes": [
|
134 |
+
{
|
135 |
+
"id": "server1",
|
136 |
+
"label": "Web Server",
|
137 |
+
"type": "server"
|
138 |
+
},
|
139 |
+
{
|
140 |
+
"id": "db1",
|
141 |
+
"label": "Database",
|
142 |
+
"type": "database"
|
143 |
+
},
|
144 |
+
{
|
145 |
+
"id": "user1",
|
146 |
+
"label": "User",
|
147 |
+
"type": "user"
|
148 |
+
},
|
149 |
+
{
|
150 |
+
"id": "service1",
|
151 |
+
"label": "API Service",
|
152 |
+
"type": "service"
|
153 |
+
}
|
154 |
+
],
|
155 |
+
"connections": [
|
156 |
+
{
|
157 |
+
"from": "user1",
|
158 |
+
"to": "server1",
|
159 |
+
"label": "HTTP Request",
|
160 |
+
"weight": 2
|
161 |
+
},
|
162 |
+
{
|
163 |
+
"from": "server1",
|
164 |
+
"to": "service1",
|
165 |
+
"label": "API Call",
|
166 |
+
"weight": 3
|
167 |
+
},
|
168 |
+
{
|
169 |
+
"from": "service1",
|
170 |
+
"to": "db1",
|
171 |
+
"label": "Query",
|
172 |
+
"weight": 1
|
173 |
+
}
|
174 |
+
]
|
175 |
+
}
|
176 |
+
|
177 |
+
Returns:
|
178 |
+
str: The filepath to the generated PNG image file.
|
179 |
+
"""
|
180 |
try:
|
181 |
if not json_input.strip():
|
182 |
return "Error: Empty input"
|
|
|
185 |
|
186 |
if 'nodes' not in data or 'connections' not in data:
|
187 |
raise ValueError("Missing required fields: nodes or connections")
|
188 |
+
|
189 |
dot = graphviz.Graph(
|
190 |
name='NetworkGraph',
|
191 |
format='png',
|
|
|
202 |
}
|
203 |
)
|
204 |
|
|
|
|
|
205 |
type_colors = {
|
206 |
+
'server': '#BEBEBE',
|
207 |
+
'service': '#B8D4F1',
|
208 |
+
'database': '#A8E6CF',
|
209 |
+
'user': '#FFF9C4',
|
210 |
+
'default': '#BEBEBE'
|
211 |
}
|
212 |
|
213 |
nodes = data.get('nodes', [])
|
|
|
222 |
raise ValueError(f"Invalid node: {node}")
|
223 |
|
224 |
node_color = type_colors.get(node_type, type_colors['default'])
|
225 |
+
font_color = 'black'
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
if node_type == 'server':
|
228 |
shape = 'box'
|
229 |
style = 'filled,rounded'
|
230 |
elif node_type == 'database':
|
231 |
shape = 'cylinder'
|
232 |
+
style = 'filled,rounded'
|
233 |
elif node_type == 'user':
|
234 |
shape = 'ellipse'
|
235 |
+
style = 'filled,rounded'
|
236 |
elif node_type == 'service':
|
237 |
shape = 'hexagon'
|
238 |
+
style = 'filled,rounded'
|
239 |
else:
|
240 |
shape = 'circle'
|
241 |
+
style = 'filled,rounded'
|
242 |
|
243 |
dot.node(
|
244 |
node_id,
|
|
|
270 |
fontsize='10',
|
271 |
penwidth=penwidth
|
272 |
)
|
273 |
+
|
274 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
275 |
dot.render(tmp.name, format=output_format, cleanup=True)
|
276 |
return f"{tmp.name}.{output_format}"
|
277 |
+
|
278 |
except json.JSONDecodeError:
|
279 |
return "Error: Invalid JSON format"
|
280 |
except Exception as e:
|