Spaces:
Running
Running
Update network_graph_generator.py
Browse files- network_graph_generator.py +27 -31
network_graph_generator.py
CHANGED
@@ -16,26 +16,31 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
16 |
dot = graphviz.Graph(
|
17 |
name='NetworkGraph',
|
18 |
format='png',
|
19 |
-
engine='
|
20 |
graph_attr={
|
21 |
-
'
|
22 |
-
'splines': '
|
23 |
'bgcolor': 'white',
|
24 |
'pad': '0.5',
|
25 |
-
'
|
26 |
-
|
27 |
-
node_attr={
|
28 |
-
'fixedsize': 'false'
|
29 |
}
|
30 |
)
|
31 |
|
32 |
base_color = '#19191a'
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
nodes = data.get('nodes', [])
|
36 |
connections = data.get('connections', [])
|
37 |
|
38 |
-
for
|
39 |
node_id = node.get('id')
|
40 |
label = node.get('label')
|
41 |
node_type = node.get('type', 'default')
|
@@ -43,48 +48,39 @@ def generate_network_graph(json_input: str, output_format: str) -> str:
|
|
43 |
if not all([node_id, label]):
|
44 |
raise ValueError(f"Invalid node: {node}")
|
45 |
|
46 |
-
|
47 |
|
48 |
-
if
|
49 |
-
|
|
|
|
|
50 |
else:
|
51 |
-
|
52 |
-
|
53 |
-
base_r = int(base_color_safe[1:3], 16)
|
54 |
-
base_g = int(base_color_safe[3:5], 16)
|
55 |
-
base_b = int(base_color_safe[5:7], 16)
|
56 |
-
|
57 |
-
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
58 |
-
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
59 |
-
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
60 |
-
|
61 |
-
current_r = min(255, current_r)
|
62 |
-
current_g = min(255, current_g)
|
63 |
-
current_b = min(255, current_b)
|
64 |
-
|
65 |
-
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
66 |
-
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
67 |
-
font_size = max(9, 14 - (current_depth * 1))
|
68 |
|
69 |
if node_type == 'server':
|
70 |
shape = 'box'
|
|
|
71 |
elif node_type == 'database':
|
72 |
shape = 'cylinder'
|
|
|
73 |
elif node_type == 'user':
|
74 |
shape = 'ellipse'
|
|
|
75 |
elif node_type == 'service':
|
76 |
shape = 'hexagon'
|
|
|
77 |
else:
|
78 |
shape = 'circle'
|
|
|
79 |
|
80 |
dot.node(
|
81 |
node_id,
|
82 |
label,
|
83 |
shape=shape,
|
84 |
-
style=
|
85 |
fillcolor=node_color,
|
86 |
fontcolor=font_color,
|
87 |
-
fontsize=
|
88 |
)
|
89 |
|
90 |
for connection in connections:
|
|
|
16 |
dot = graphviz.Graph(
|
17 |
name='NetworkGraph',
|
18 |
format='png',
|
19 |
+
engine='dot',
|
20 |
graph_attr={
|
21 |
+
'rankdir': 'TB',
|
22 |
+
'splines': 'ortho',
|
23 |
'bgcolor': 'white',
|
24 |
'pad': '0.5',
|
25 |
+
'nodesep': '1.5',
|
26 |
+
'ranksep': '2.0'
|
|
|
|
|
27 |
}
|
28 |
)
|
29 |
|
30 |
base_color = '#19191a'
|
31 |
+
|
32 |
+
type_colors = {
|
33 |
+
'server': base_color,
|
34 |
+
'service': '#4a90e2',
|
35 |
+
'database': '#a0a0a0',
|
36 |
+
'user': '#f39c12',
|
37 |
+
'default': base_color
|
38 |
+
}
|
39 |
|
40 |
nodes = data.get('nodes', [])
|
41 |
connections = data.get('connections', [])
|
42 |
|
43 |
+
for node in nodes:
|
44 |
node_id = node.get('id')
|
45 |
label = node.get('label')
|
46 |
node_type = node.get('type', 'default')
|
|
|
48 |
if not all([node_id, label]):
|
49 |
raise ValueError(f"Invalid node: {node}")
|
50 |
|
51 |
+
node_color = type_colors.get(node_type, type_colors['default'])
|
52 |
|
53 |
+
if node_type == 'database' or node_color == '#a0a0a0':
|
54 |
+
font_color = 'black'
|
55 |
+
elif node_color == '#f39c12':
|
56 |
+
font_color = 'black'
|
57 |
else:
|
58 |
+
font_color = 'white'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
if node_type == 'server':
|
61 |
shape = 'box'
|
62 |
+
style = 'filled,rounded'
|
63 |
elif node_type == 'database':
|
64 |
shape = 'cylinder'
|
65 |
+
style = 'filled'
|
66 |
elif node_type == 'user':
|
67 |
shape = 'ellipse'
|
68 |
+
style = 'filled'
|
69 |
elif node_type == 'service':
|
70 |
shape = 'hexagon'
|
71 |
+
style = 'filled'
|
72 |
else:
|
73 |
shape = 'circle'
|
74 |
+
style = 'filled'
|
75 |
|
76 |
dot.node(
|
77 |
node_id,
|
78 |
label,
|
79 |
shape=shape,
|
80 |
+
style=style,
|
81 |
fillcolor=node_color,
|
82 |
fontcolor=font_color,
|
83 |
+
fontsize='12'
|
84 |
)
|
85 |
|
86 |
for connection in connections:
|