Skip to content

Commit

Permalink
fix(program): fix bug in program node (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
thanh-nguyen-dang authored Jun 3, 2020
1 parent 309e9e9 commit 61a0b46
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
7 changes: 4 additions & 3 deletions tube/etl/indexers/base/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_src_name(props):
lst = [tuple(p.split(":")) if ":" in p else tuple([p, p]) for p in props]
return lst

def create_prop_from_json(self, doc_name, p, node_label=None, index=None):
def create_prop_from_json(self, doc_name, p, node_label=None, index=None, is_additional=False):
value_mappings = p.get("value_mappings", [])
src = p["src"] if "src" in p else p["name"]
fn = p.get("fn")
Expand All @@ -112,17 +112,18 @@ def create_prop_from_json(self, doc_name, p, node_label=None, index=None):
src_index=index,
fn=fn,
prop_type=prop_type,
is_additional=is_additional
)
return prop

def create_props_from_json(
self, doc_name, props_in_json, node_label=None, index=None
self, doc_name, props_in_json, node_label=None, index=None, is_additional=False
):
res = []
for p in props_in_json:
res.append(
self.create_prop_from_json(
doc_name, p, node_label=node_label, index=index
doc_name, p, node_label=node_label, index=index, is_additional=is_additional
)
)
return res
4 changes: 4 additions & 0 deletions tube/etl/indexers/base/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def adding_prop(
src_index=None,
fn=None,
prop_type=None,
is_additional=False,
):
if doc_name not in PropFactory.prop_by_names:
PropFactory.prop_by_names[doc_name] = {}
Expand All @@ -45,6 +46,7 @@ def adding_prop(
src_index,
fn,
prop_type,
is_additional=is_additional
)
PropFactory.list_props.append(prop)
PropFactory.prop_by_names.get(doc_name)[name] = prop
Expand Down Expand Up @@ -87,6 +89,7 @@ def __init__(
src_index=None,
fn=None,
prop_type=None,
is_additional=False
):
self.id = id
self.name = name
Expand All @@ -96,6 +99,7 @@ def __init__(
self.value_mappings = [] if value_mappings is None else value_mappings
self.fn = fn
self.type = prop_type
self.is_additional = is_additional

def __hash__(self):
return self.id
Expand Down
42 changes: 27 additions & 15 deletions tube/etl/indexers/injection/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,41 @@ def get_edges_having_data(self):

def get_props_for_nodes(self):
prop_nodes = {}
roots = {}
for (k, v) in self.mapping.get("injecting_props", {}).items():
if k == "project" and "project_code" not in [p.get("name") for p in v.get("props")]:
v.get("props").append({"name": PROJECT_CODE, "src": "code"})
prop_nodes[k] = CollectingNode(
k,
get_node_table_name(self.model, k),
props=self.create_props_from_json(
self.doc_type, v.get("props"), node_label=k
),
)
if k != "program":
prop_nodes[k] = CollectingNode(
k,
get_node_table_name(self.model, k),
props=self.create_props_from_json(
self.doc_type, v.get("props"), node_label=k
),
)
else:
node_props = v.get("props")
node_props.append({"name": "program_name", "src": "name"})
roots[k] = RootNode(
k,
get_node_table_name(self.model, k),
self.create_props_from_json(
self.doc_type,
node_props,
node_label=k, is_additional=True
),
)
if "project" not in prop_nodes.keys():
prop_nodes["project"] = CollectingNode(
"project",
get_node_table_name(self.model, "project"),
props=self.create_props_from_json(
self.doc_type,
[{"name": PROJECT_CODE, "src": "code"}],
node_label="project",
node_label="project", is_additional=True
),
)
return prop_nodes
return prop_nodes, roots

def get_collecting_nodes(self):
def selected_category_comparer(dictionary, x):
Expand All @@ -144,9 +158,9 @@ def selected_category_comparer(dictionary, x):
for l in leaves:
self.leaves.add(LeafNode(l, get_node_table_name(self.model, l)))

nodes_with_props = self.get_props_for_nodes()
nodes_with_props, roots = self.get_props_for_nodes()
self.collectors, self.roots = self.create_tree_from_generated_edges(
flat_paths, nodes_with_props
flat_paths, nodes_with_props, roots
)

self.update_level()
Expand Down Expand Up @@ -204,13 +218,12 @@ def add_root_node(self, child, roots, segment):
self.create_props_from_json(
self.doc_type,
[{"name": "program_name", "src": "name"}],
node_label=root_name,
node_label=root_name, is_additional=True
),
)
)
child.add_parent(top_node.name, edge_up_tbl)
top_node.add_child(child)

roots[root_name] = top_node

def add_collecting_node(self, child, collectors, fst):
Expand All @@ -231,9 +244,8 @@ def add_collecting_node(self, child, collectors, fst):
collectors[parent_name] = collecting_node
return collecting_node

def create_tree_from_generated_edges(self, flat_paths, nodes_with_props):
def create_tree_from_generated_edges(self, flat_paths, nodes_with_props, roots):
collectors = nodes_with_props
roots = {}
checking_set = set(self.generated_edges)
for p in flat_paths:
segments = list(p.path)
Expand Down

0 comments on commit 61a0b46

Please sign in to comment.