Skip to content

Commit

Permalink
Add get_edges_under_consolidation Function
Browse files Browse the repository at this point in the history
Useful when synchronizing sub hierarchies between instances

Fixes #915
  • Loading branch information
MariusWirtz committed Jun 6, 2023
1 parent 6105399 commit 0c36ca2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
43 changes: 42 additions & 1 deletion TM1py/Services/ElementService.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def get_elements_dataframe(self, dimension_name: str = None, hierarchy_name: str
skip_consolidations: bool = True, attributes: Iterable[str] = None,
attribute_column_prefix: str = "", skip_parents: bool = False,
level_names: List[str] = None, parent_attribute: str = None,
skip_weights: bool = False, use_blob: bool=False, **kwargs) -> 'pd.DataFrame':
skip_weights: bool = False, use_blob: bool = False, **kwargs) -> 'pd.DataFrame':
"""
:param dimension_name: Name of the dimension. Can be derived from elements MDX
Expand Down Expand Up @@ -700,6 +700,47 @@ def get_leaves_under_consolidation(self, dimension_name: str, hierarchy_name: st
return self.get_members_under_consolidation(dimension_name, hierarchy_name, consolidation, max_depth, True,
**kwargs)

def get_edges_under_consolidation(self, dimension_name: str, hierarchy_name: str, consolidation: str,
max_depth: int = None, **kwargs) -> List[str]:
""" Get all members under a consolidated element
:param dimension_name: name of dimension
:param hierarchy_name: name of hierarchy
:param consolidation: name of consolidated Element
:param max_depth: 99 if not passed
:return:
"""
depth = max_depth - 1 if max_depth else 99
# edges to return
edges = CaseAndSpaceInsensitiveTuplesDict()

# build url
bare_url = "/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements('{}')?"
url = format_url(bare_url, dimension_name, hierarchy_name, consolidation)
for d in range(depth):
if d == 0:
url += "$select=Edges&$expand=Edges($expand=Component("
else:
url += "$select=Edges;$expand=Edges($expand=Component("

url = url[:-1] + ")" * (depth * 2 - 1)

response = self._rest.GET(url, **kwargs)
consolidation_tree = response.json()

# recursive function to parse consolidation_tree
def get_edges(sub_trees):
for sub_tree in sub_trees:
edges[sub_tree["ParentName"], sub_tree["ComponentName"]] = sub_tree["Weight"]

if "Edges" not in sub_tree["Component"]:
return

get_edges(sub_trees=sub_tree["Component"]["Edges"])

get_edges(consolidation_tree["Edges"])
return edges

def get_members_under_consolidation(self, dimension_name: str, hierarchy_name: str, consolidation: str,
max_depth: int = None, leaves_only: bool = False, **kwargs) -> List[str]:
""" Get all members under a consolidated element
Expand Down
21 changes: 21 additions & 0 deletions Tests/ElementService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,27 @@ def test_get_leaves_under_consolidation(self):
for year in self.years:
self.assertIn(year, leaves)

def test_get_edges_under_consolidation(self):
edges = self.tm1.dimensions.hierarchies.elements.get_edges_under_consolidation(
self.dimension_name,
self.hierarchy_name,
"All Consolidations")

self.assertEqual(len(self.years) + 1, len(edges))
self.assertEqual(1, edges["All Consolidations", "Total Years"])
for year in self.years:
self.assertEqual(1, edges["Total Years", year])

def test_get_edges_under_consolidation_max_depth_2(self):
edges = self.tm1.dimensions.hierarchies.elements.get_edges_under_consolidation(
self.dimension_name,
self.hierarchy_name,
"All Consolidations",
max_depth=2)

self.assertEqual(1, len(edges))
self.assertEqual(1, edges["All Consolidations", "Total Years"])

def test_get_members_under_consolidation(self):
leaves = self.tm1.dimensions.hierarchies.elements.get_members_under_consolidation(
self.dimension_name,
Expand Down

0 comments on commit 0c36ca2

Please sign in to comment.