Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/get values #1020

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions TM1py/Services/CellService.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import List, Union, Dict, Iterable, Tuple, Optional

import ijson
from mdxpy import MdxHierarchySet, MdxBuilder, Member
from mdxpy import MdxHierarchySet, MdxBuilder, Member, MdxTuple
from requests import Response

from TM1py.Exceptions.Exceptions import TM1pyException, TM1pyWritePartialFailureException, TM1pyWriteFailureException, \
Expand Down Expand Up @@ -253,6 +253,51 @@ def get_value(self, cube_name: str, elements: Union[str, Iterable] = None, dimen
cellset = dict(self.execute_mdx(mdx=mdx, sandbox_name=sandbox_name, **kwargs))
return next(iter(cellset.values()))["Value"]

def get_values(self, cube_name: str, element_sets: Iterable[Iterable[str]] = None, dimensions: List[str] = None,
sandbox_name: str = None, element_separator: str = ",", hierarchy_separator: str = "&&",
hierarchy_element_separator: str = "::", **kwargs) -> List:
""" Returns list of cube values from specified coordinates list. will be in same order as original list

:param cube_name: Name of the cube
:param element_sets: Set of coordinates where each element is provided in the correct dimension order.
[('2024', 'Actual', 'London', 'P02), ('2024', 'Forecast', 'Berlin', 'P03)]
:param dimensions: Dimension names in correct order
:param sandbox_name: str
:param element_separator: Alternative separator for the element selections
:param hierarchy_separator: Alternative separator for multiple hierarchies
:param hierarchy_element_separator: Alternative separator between hierarchy name and element name
:return:
"""

if not dimensions:
dimensions = self.get_dimension_names_for_writing(cube_name=cube_name)

q = MdxBuilder.from_cube(cube_name)

for elements in element_sets:
members = []
element_selections = elements.split(element_separator)
for dimension_name, element_selection in zip(dimensions, element_selections):
if hierarchy_separator not in element_selection:
if hierarchy_element_separator in element_selection:
hierarchy_name, element_name = element_selection.split(hierarchy_element_separator)
else:
hierarchy_name = dimension_name
element_name = element_selection

member = Member.of(dimension_name, hierarchy_name, element_name)
members.append(member)
else:
for element_selection_part in element_selection.split(hierarchy_separator):
hierarchy_name, element_name = element_selection_part.split(hierarchy_element_separator)
member = Member.of(dimension_name, hierarchy_name, element_name)
members.append(member)

q.add_member_tuple_to_columns(MdxTuple(members))

# Execute MDX
return self.execute_mdx_values(mdx=q.to_mdx(), sandbox_name=sandbox_name, **kwargs)

def _compose_odata_tuple_from_string(self, cube_name: str,
element_string: str,
dimensions: Iterable[str] = None,
Expand Down Expand Up @@ -2270,7 +2315,8 @@ async def _exec_mdx_dataframe_async():

@require_pandas
def execute_mdx_dataframe_shaped(self, mdx: str, sandbox_name: str = None, display_attribute: bool = False,
use_iterative_json: bool = False, use_blob: bool = False, mdx_headers: bool=False,
use_iterative_json: bool = False, use_blob: bool = False,
mdx_headers: bool = False,
**kwargs) -> 'pd.DataFrame':
""" Retrieves data from cube in the shape of the query.
Dimensions on rows can be stacked. One dimension must be placed on columns. Title selections are ignored.
Expand Down Expand Up @@ -3753,7 +3799,7 @@ def extract_cellset_dataframe(
@require_pandas
def extract_cellset_dataframe_shaped(self, cellset_id: str, sandbox_name: str = None,
display_attribute: bool = False, infer_dtype: bool = False,
mdx_headers: bool=False, **kwargs) -> 'pd.DataFrame':
mdx_headers: bool = False, **kwargs) -> 'pd.DataFrame':
""" Retrieves data from cellset in the shape of the query.
Dimensions on rows can be stacked. One dimension must be placed on columns. Title selections are ignored.

Expand Down
19 changes: 18 additions & 1 deletion Tests/CellService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4273,10 +4273,27 @@ def test_get_value(self):
def test_get_value_other_hierarchy_in_attribute_cube(self):
value = self.tm1.cells.get_value(
cube_name='}ElementAttributes_' + self.dimension_with_hierarchies_name,
element_string=f'Hierarchy2::Cons1,ea2')
elements=f'Hierarchy2::Cons1,ea2')

self.assertEqual('ABC', value)

def test_get_values(self):
values = self.tm1.cells.get_values(
cube_name=self.cube_name,
element_sets=["Element1|Element1|Element1", "Element1|Element2|Element3", "Element2|Element2|Element2"],
dimnesions=self.dimension_names,
element_separator="|")

self.assertEqual([1, None, 1], values)

def test_get_values_other_hierarchy_in_attribute_cube(self):
values = self.tm1.cells.get_values(
cube_name='}ElementAttributes_' + self.dimension_with_hierarchies_name,
element_sets=[f'Hierarchy2¦Cons1,ea2'],
hierarchy_element_separator='¦')

self.assertEqual(['ABC'], values)

def test_trace_cell_calculation_no_depth_iterable(self):
result = self.tm1.cells.trace_cell_calculation(
cube_name=self.cube_with_rules_name,
Expand Down