Skip to content

Commit

Permalink
Migrate Neo4j population to seperate function
Browse files Browse the repository at this point in the history
  • Loading branch information
john681611 committed Sep 14, 2023
1 parent dc81559 commit d93b3c6
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions application/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,34 @@ def instance(self):
def __init__(sel):
raise ValueError("NEO_DB is a singleton, please call instance() instead")

@classmethod
def populate_DB(self, session) -> nx.Graph:
graph = nx.DiGraph()
for il in session.query(InternalLinks).all():
group = session.query(CRE).filter(CRE.id == il.group).first()
if not group:
logger.error(f"CRE {il.group} does not exist?")
self.add_cre(group)

cre = session.query(CRE).filter(CRE.id == il.cre).first()
if not cre:
logger.error(f"CRE {il.cre} does not exist?")
self.add_cre(cre)

self.link_CRE_to_CRE(il.group, il.cre, il.type)

for lnk in session.query(Links).all():
node = session.query(Node).filter(Node.id == lnk.node).first()
if not node:
logger.error(f"Node {lnk.node} does not exist?")
self.add_dbnode(node)

cre = session.query(CRE).filter(CRE.id == lnk.cre).first()
self.add_cre(cre)

self.link_CRE_to_Node(lnk.cre, lnk.node, lnk.type)
return graph

@classmethod
def add_cre(self, dbcre: CRE):
if not self.connected:
Expand All @@ -208,6 +236,7 @@ def add_cre(self, dbcre: CRE):
def add_dbnode(self, dbnode: Node):
if not self.connected:
return
# TODO: Add diffrent Node types
self.driver.execute_query(
"MERGE (n:Node {id: $nid, name: $name, section: $section, section_id: $section_id, subsection: $subsection, tags: $tags, version: $version, description: $description, ntype: $ntype})",
nid=dbnode.id,
Expand Down Expand Up @@ -363,15 +392,13 @@ def standards(self):

class CRE_Graph:
graph: nx.Graph = None
neo_db: NEO_DB = None
__instance = None

@classmethod
def instance(cls, session, neo_db: NEO_DB):
def instance(cls, session):
if cls.__instance is None:
cls.__instance = cls.__new__(cls)
cls.neo_db = neo_db
# cls.graph = cls.load_cre_graph(session) # TODO: Call this in a seperate statup mode
cls.graph = cls.load_cre_graph(session)
return cls.__instance

def __init__(sel):
Expand All @@ -386,7 +413,6 @@ def add_node(self, *args, **kwargs):
@classmethod
def add_cre(cls, dbcre: CRE, graph: nx.DiGraph) -> nx.DiGraph:
if dbcre:
cls.neo_db.add_cre(dbcre)
graph.add_node(
f"CRE: {dbcre.id}", internal_id=dbcre.id, external_id=dbcre.external_id
)
Expand All @@ -397,7 +423,6 @@ def add_cre(cls, dbcre: CRE, graph: nx.DiGraph) -> nx.DiGraph:
@classmethod
def add_dbnode(cls, dbnode: Node, graph: nx.DiGraph) -> nx.DiGraph:
if dbnode:
cls.neo_db.add_dbnode(dbnode)
# coma separated tags

graph.add_node(
Expand Down Expand Up @@ -426,7 +451,6 @@ def load_cre_graph(cls, session) -> nx.Graph:
graph = cls.add_cre(dbcre=cre, graph=graph)

graph.add_edge(f"CRE: {il.group}", f"CRE: {il.cre}", ltype=il.type)
cls.neo_db.link_CRE_to_CRE(il.group, il.cre, il.type)

for lnk in session.query(Links).all():
node = session.query(Node).filter(Node.id == lnk.node).first()
Expand All @@ -438,8 +462,8 @@ def load_cre_graph(cls, session) -> nx.Graph:
graph = cls.add_cre(dbcre=cre, graph=graph)

graph.add_edge(f"CRE: {lnk.cre}", f"Node: {str(lnk.node)}", ltype=lnk.type)
cls.neo_db.link_CRE_to_Node(lnk.cre, lnk.node, lnk.type)
return graph



class Node_collection:
Expand All @@ -449,8 +473,8 @@ class Node_collection:

def __init__(self) -> None:
if not os.environ.get("NO_LOAD_GRAPH"):
self.neo_db = NEO_DB.instance()
self.graph = CRE_Graph.instance(sqla.session, self.neo_db)
self.graph = CRE_Graph.instance(sqla.session)
self.neo_db = NEO_DB.instance()
self.session = sqla.session

def __get_external_links(self) -> List[Tuple[CRE, Node, str]]:
Expand Down

0 comments on commit d93b3c6

Please sign in to comment.