Spaces:
Running
Running
File size: 10,038 Bytes
e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 de18aa6 e1b03e0 248b059 e1b03e0 248b059 e1b03e0 248b059 8a73bd1 248b059 8a73bd1 bcd6fa4 248b059 bcd6fa4 de18aa6 bcd6fa4 8a73bd1 de18aa6 bcd6fa4 248b059 ba12590 e1b03e0 ba12590 248b059 ba12590 248b059 ba12590 e1b03e0 248b059 ba12590 248b059 e1b03e0 248b059 e1b03e0 248b059 e1b03e0 248b059 e1b03e0 248b059 ba12590 248b059 ba12590 248b059 bcd6fa4 de18aa6 bcd6fa4 248b059 e1b03e0 248b059 e1b03e0 248b059 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# import graphviz
# import json
# from tempfile import NamedTemporaryFile
# import os
# def generate_network_graph(json_input: str, output_format: str) -> str:
# """
# Generates a network graph from JSON input.
# Args:
# json_input (str): A JSON string describing the network graph structure.
# It must follow the Expected JSON Format Example below.
# Expected JSON Format Example:
# {
# "nodes": [
# {
# "id": "server1",
# "label": "Web Server",
# "type": "server"
# },
# {
# "id": "db1",
# "label": "Database",
# "type": "database"
# },
# {
# "id": "user1",
# "label": "User",
# "type": "user"
# },
# {
# "id": "service1",
# "label": "API Service",
# "type": "service"
# }
# ],
# "connections": [
# {
# "from": "user1",
# "to": "server1",
# "label": "HTTP Request",
# "weight": 2
# },
# {
# "from": "server1",
# "to": "service1",
# "label": "API Call",
# "weight": 3
# },
# {
# "from": "service1",
# "to": "db1",
# "label": "Query",
# "weight": 1
# }
# ]
# }
# Returns:
# str: The filepath to the generated PNG image file.
# """
# try:
# if not json_input.strip():
# return "Error: Empty input"
# data = json.loads(json_input)
# if 'nodes' not in data or 'connections' not in data:
# raise ValueError("Missing required fields: nodes or connections")
# dot = graphviz.Graph(
# name='NetworkGraph',
# format='png',
# engine='neato',
# graph_attr={
# 'overlap': 'false',
# 'splines': 'true',
# 'bgcolor': 'white',
# 'pad': '0.5',
# 'layout': 'neato'
# },
# node_attr={
# 'fixedsize': 'false'
# }
# )
# type_colors = {
# 'server': '#BEBEBE',
# 'service': '#B8D4F1',
# 'database': '#A8E6CF',
# 'user': '#FFF9C4',
# 'default': '#BEBEBE'
# }
# nodes = data.get('nodes', [])
# connections = data.get('connections', [])
# for node in nodes:
# node_id = node.get('id')
# label = node.get('label')
# node_type = node.get('type', 'default')
# if not all([node_id, label]):
# raise ValueError(f"Invalid node: {node}")
# node_color = type_colors.get(node_type, type_colors['default'])
# font_color = 'black'
# if node_type == 'server':
# shape = 'box'
# style = 'filled,rounded'
# elif node_type == 'database':
# shape = 'cylinder'
# style = 'filled,rounded'
# elif node_type == 'user':
# shape = 'ellipse'
# style = 'filled,rounded'
# elif node_type == 'service':
# shape = 'hexagon'
# style = 'filled,rounded'
# else:
# shape = 'circle'
# style = 'filled,rounded'
# dot.node(
# node_id,
# label,
# shape=shape,
# style=style,
# fillcolor=node_color,
# fontcolor=font_color,
# fontsize='12'
# )
# for connection in connections:
# from_node = connection.get('from')
# to_node = connection.get('to')
# label = connection.get('label', '')
# weight = connection.get('weight', 1)
# if not all([from_node, to_node]):
# raise ValueError(f"Invalid connection: {connection}")
# penwidth = str(max(1, min(5, weight)))
# dot.edge(
# from_node,
# to_node,
# label=label,
# color='#4a4a4a',
# fontcolor='#4a4a4a',
# fontsize='10',
# penwidth=penwidth
# )
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
# dot.render(tmp.name, format=output_format, cleanup=True)
# return f"{tmp.name}.{output_format}"
# except json.JSONDecodeError:
# return "Error: Invalid JSON format"
# except Exception as e:
# return f"Error: {str(e)}"
import graphviz
import json
from tempfile import NamedTemporaryFile
import os
def generate_network_graph(json_input: str, output_format: str) -> str:
"""
Generates a network graph from JSON input.
Args:
json_input (str): A JSON string describing the network graph structure.
It must follow the Expected JSON Format Example below.
Expected JSON Format Example:
{
"nodes": [
{
"id": "server1",
"label": "Web Server",
"type": "server"
},
{
"id": "db1",
"label": "Database",
"type": "database"
},
{
"id": "user1",
"label": "User",
"type": "user"
},
{
"id": "service1",
"label": "API Service",
"type": "service"
}
],
"connections": [
{
"from": "user1",
"to": "server1",
"label": "HTTP Request",
"weight": 2
},
{
"from": "server1",
"to": "service1",
"label": "API Call",
"weight": 3
},
{
"from": "service1",
"to": "db1",
"label": "Query",
"weight": 1
}
]
}
Returns:
str: The filepath to the generated PNG image file.
"""
try:
if not json_input.strip():
return "Error: Empty input"
data = json.loads(json_input)
if 'nodes' not in data or 'connections' not in data:
raise ValueError("Missing required fields: nodes or connections")
dot = graphviz.Graph(
name='NetworkGraph',
format='png',
engine='neato',
graph_attr={
'overlap': 'false',
'splines': 'curved',
'bgcolor': 'white',
'pad': '1.0',
'layout': 'neato',
'sep': '+20',
'esep': '+10',
'nodesep': '1.5',
'concentrate': 'false',
'maxiter': '1000'
},
node_attr={
'fixedsize': 'false'
},
edge_attr={
'labeldistance': '3.0',
'labelangle': '15',
'labelfloat': 'true'
}
)
type_colors = {
'server': '#BEBEBE',
'service': '#B8D4F1',
'database': '#A8E6CF',
'user': '#FFF9C4',
'default': '#BEBEBE'
}
nodes = data.get('nodes', [])
connections = data.get('connections', [])
for node in nodes:
node_id = node.get('id')
label = node.get('label')
node_type = node.get('type', 'default')
if not all([node_id, label]):
raise ValueError(f"Invalid node: {node}")
node_color = type_colors.get(node_type, type_colors['default'])
font_color = 'black'
if node_type == 'server':
shape = 'box'
style = 'filled,rounded'
elif node_type == 'database':
shape = 'cylinder'
style = 'filled,rounded'
elif node_type == 'user':
shape = 'ellipse'
style = 'filled,rounded'
elif node_type == 'service':
shape = 'hexagon'
style = 'filled,rounded'
else:
shape = 'circle'
style = 'filled,rounded'
dot.node(
node_id,
label,
shape=shape,
style=style,
fillcolor=node_color,
fontcolor=font_color,
fontsize='12'
)
for connection in connections:
from_node = connection.get('from')
to_node = connection.get('to')
label = connection.get('label', '')
weight = connection.get('weight', 1)
if not all([from_node, to_node]):
raise ValueError(f"Invalid connection: {connection}")
penwidth = str(max(1, min(5, weight)))
dot.edge(
from_node,
to_node,
label=label,
color='#4a4a4a',
fontcolor='#4a4a4a',
fontsize='8',
penwidth=penwidth,
labeldistance='2.5',
labelangle='10',
labelfloat='true'
)
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
dot.render(tmp.name, format=output_format, cleanup=True)
return f"{tmp.name}.{output_format}"
except json.JSONDecodeError:
return "Error: Invalid JSON format"
except Exception as e:
return f"Error: {str(e)}" |