-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
90 lines (62 loc) · 1.96 KB
/
model.py
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
# Initialiaze django
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rekuest.settings")
django.setup()
# Import the models
from facade import models
from pydantic import BaseModel, Field
## Build dependency graph
x = models.Template.objects.get(id=8)
class InvalidNode(BaseModel):
id: int
initial_hash: str
class NodeNode(BaseModel):
id: int
node_id: str
class TemplateNode(BaseModel):
id: int
template_id: str
class DependencyEdge(BaseModel):
id: int
source: str
target: str
optional: bool
Node = NodeNode | InvalidNode | TemplateNode
class Graph(BaseModel):
nodes: list[Node]
edges: list[DependencyEdge]
def build_graph_recursive(
template_id: int, edges: list[DependencyEdge], nodes: list[Node]
) -> None:
template = models.Template.objects.get(id=template_id)
nodes.append(TemplateNode(id=template.id, template_id=template.id))
for dep in template.dependencies.all():
if dep.node is not None:
nodes.append(NodeNode(id=dep.node.id, node_id=dep.node.id))
edges.append(
DependencyEdge(
id=dep.id,
source=dep.node.hash,
target=template.node.hash,
optional=dep.optional,
)
)
for template in dep.node.templates.all():
build_graph_recursive(template.id, edges, nodes)
else:
nodes.append(InvalidNode(id=dep.id, initial_hash=dep.initial_hash))
edges.append(
DependencyEdge(
id=dep.id,
source=dep.initial_hash,
target=template.node.hash,
optional=dep.optional,
)
)
def build_graph(template_id: int) -> Graph:
nodes = []
edges = []
build_graph_recursive(template_id, edges, nodes)
return Graph(nodes=nodes, edges=edges)
x = build_graph(8)