forked from VA602AA-master/VASTKnowledgeGraphVisualization
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatasets.py
More file actions
90 lines (78 loc) · 2.95 KB
/
Copy pathdatasets.py
File metadata and controls
90 lines (78 loc) · 2.95 KB
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
"""
Built-in dataset handlers.
`builtin_summary(name)` powers the onboarding picker (eager structural flags
without a follow-up /schema/ call). `load_builtin(name)` materializes a
built-in graph to disk and returns the `graph_id` — the endpoint in main.py
is a thin wrapper around it.
"""
import json
import networkx as nx
from fastapi import HTTPException
from registry import builtin_summary_cache, graph_path, register_graph
from schema import BUILTIN_DATASETS, compute_schema
def builtin_summary(name: str):
"""
Lazy-built, cached summary of a built-in dataset. Drives the onboarding
picker so it can show structural flags without a follow-up /schema/ call.
Loader can fail if its disk asset (CSV/gzip) is missing — surfaced as a
503 rather than a generic 500 so the client can show "this built-in is
unavailable" instead of "server error".
"""
cached = builtin_summary_cache.get(name)
if cached is not None:
return cached
loader_fn, _ = BUILTIN_DATASETS[name]
try:
graph = loader_fn()
except FileNotFoundError as e:
raise HTTPException(
status_code=503,
detail=f"Built-in dataset '{name}' asset missing: {e}",
)
schema = compute_schema(graph, name=name.replace('_', ' ').title())
summary = {
'nodes': schema['nodes'],
'edges': schema['edges'],
'directed': schema['directed'],
'weighted': schema['weighted'],
'multigraph': schema['multigraph'],
'bipartite': schema.get('bipartite', False),
'node_types': len(schema['node_types']),
'edge_types': len(schema['edge_types']),
}
builtin_summary_cache[name] = summary
return summary
def list_builtin_payload():
"""Build the /datasets/ response (built-in list with eager summaries)."""
return {
"datasets": [
{"name": name, "description": desc, **builtin_summary(name)}
for name, (_, desc) in BUILTIN_DATASETS.items()
]
}
def load_builtin_payload(name: str):
"""
Materialize a built-in graph to disk, register it, return the graph_id.
Raises HTTPException(404) if the name is unknown.
Note: kickoff() of the centrality precompute is the caller's job — the
handler in main.py drains in-flight tasks first.
"""
if name not in BUILTIN_DATASETS:
raise HTTPException(
status_code=404,
detail=f"Unknown dataset '{name}'. Available: {list(BUILTIN_DATASETS)}",
)
loader_fn, _ = BUILTIN_DATASETS[name]
try:
graph = loader_fn()
except FileNotFoundError as e:
raise HTTPException(
status_code=503,
detail=f"Built-in dataset '{name}' asset missing: {e}",
)
graph_id = f"builtin_{name}"
file_path = graph_path(graph_id)
with open(file_path, "w") as f:
json.dump(nx.node_link_data(graph), f)
register_graph(graph_id, file_path, name.replace('_', ' ').title())
return graph_id