Skip to content
Merged

1140 #1164

Changes from 1 commit
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
159 changes: 93 additions & 66 deletions src/sempy_labs/tom/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,17 @@ def clear_extended_properties(self, object):

object.ExtendedProperties.Clear()

def _valid_perspective_objects():

import Microsoft.AnalysisServices.Tabular as TOM

return [
TOM.ObjectType.Table,
TOM.ObjectType.Column,
TOM.ObjectType.Measure,
TOM.ObjectType.Hierarchy,
]
Comment thread
m-kovalsky marked this conversation as resolved.
Outdated

def in_perspective(
self,
object: Union["TOM.Table", "TOM.Column", "TOM.Measure", "TOM.Hierarchy"],
Expand All @@ -1634,17 +1645,12 @@ def in_perspective(
"""
import Microsoft.AnalysisServices.Tabular as TOM

validObjects = [
TOM.ObjectType.Table,
TOM.ObjectType.Column,
TOM.ObjectType.Measure,
TOM.ObjectType.Hierarchy,
]
valid_objects = self._valid_perspective_objects()
objectType = object.ObjectType

if objectType not in validObjects:
if objectType not in valid_objects:
raise ValueError(
f"{icons.red_dot} Only the following object types are valid for perspectives: {validObjects}."
f"{icons.red_dot} Only the following object types are valid for perspectives: {valid_objects}."
)

object.Model.Perspectives[perspective_name]
Expand Down Expand Up @@ -1690,17 +1696,12 @@ def add_to_perspective(
"""
import Microsoft.AnalysisServices.Tabular as TOM

validObjects = [
TOM.ObjectType.Table,
TOM.ObjectType.Column,
TOM.ObjectType.Measure,
TOM.ObjectType.Hierarchy,
]
valid_objects = self._valid_perspective_objects()
objectType = object.ObjectType

if objectType not in validObjects:
if objectType not in valid_objects:
raise ValueError(
f"{icons.red_dot} Only the following object types are valid for perspectives: {validObjects}."
f"{icons.red_dot} Only the following object types are valid for perspectives: {valid_objects}."
)

if any(p.Name == perspective_name for p in self.model.Perspectives):
Expand All @@ -1710,13 +1711,28 @@ def add_to_perspective(
f"{icons.red_dot} The '{perspective_name}' perspective does not exist."
)

# Ignore if object is already in the perspective
if self.in_perspective(object=object, perspective_name=perspective_name):
return

# Tables
if objectType == TOM.ObjectType.Table:
pt = TOM.PerspectiveTable()
if include_all:
pt.IncludeAll = True
pt.Table = object
object.Model.Perspectives[perspective_name].PerspectiveTables.Add(pt)
elif objectType == TOM.ObjectType.Column:
else:
# Add the table if it is not in the perspective
if not self.in_perspective(
object=object.Parent, perspective_name=perspective_name
):
pt = TOM.PerspectiveTable()
pt.Table = object.Parent
object.Model.Perspectives[perspective_name].PerspectiveTables.Add(pt)

# Columns, Measures, Hierarchies
if objectType == TOM.ObjectType.Column:
pc = TOM.PerspectiveColumn()
pc.Column = object
object.Model.Perspectives[perspective_name].PerspectiveTables[
Expand Down Expand Up @@ -1752,24 +1768,23 @@ def remove_from_perspective(
"""
import Microsoft.AnalysisServices.Tabular as TOM

validObjects = [
TOM.ObjectType.Table,
TOM.ObjectType.Column,
TOM.ObjectType.Measure,
TOM.ObjectType.Hierarchy,
]
valid_objects = self._valid_perspective_objects()
objectType = object.ObjectType

if objectType not in validObjects:
if objectType not in valid_objects:
raise ValueError(
f"{icons.red_dot} Only the following object types are valid for perspectives: {validObjects}."
f"{icons.red_dot} Only the following object types are valid for perspectives: {valid_objects}."
)

if not any(p.Name == perspective_name for p in self.model.Perspectives):
raise ValueError(
f"{icons.red_dot} The '{perspective_name}' perspective does not exist."
)

# Ignore if the object is already not in the perspective
if not self.in_perspective(object=object, perspective_name=perspective_name):
return

if objectType == TOM.ObjectType.Table:
pt = object.Model.Perspectives[perspective_name].PerspectiveTables[
object.Name
Expand Down Expand Up @@ -1803,6 +1818,18 @@ def remove_from_perspective(
object.Parent.Name
].PerspectiveHierarchies.Remove(ph)

def _valid_translation_objects():

import Microsoft.AnalysisServices.Tabular as TOM

return [
TOM.ObjectType.Table,
TOM.ObjectType.Column,
TOM.ObjectType.Measure,
TOM.ObjectType.Hierarchy,
TOM.ObjectType.Level,
]
Comment thread
m-kovalsky marked this conversation as resolved.
Outdated

def set_translation(
self,
object: Union[
Expand Down Expand Up @@ -1832,17 +1859,11 @@ def set_translation(

property = property.title()

validObjects = [
TOM.ObjectType.Table,
TOM.ObjectType.Column,
TOM.ObjectType.Measure,
TOM.ObjectType.Hierarchy,
TOM.ObjectType.Level,
]
valid_objects = self._valid_translation_objects()

if object.ObjectType not in validObjects:
if object.ObjectType not in valid_objects:
raise ValueError(
f"{icons.red_dot} Translations can only be set to {validObjects}."
f"{icons.red_dot} Translations can only be set to {valid_objects}."
)

mapping = {
Expand Down Expand Up @@ -5068,20 +5089,25 @@ def _reduce_model(self, perspective_name: str):
)
]

tables = dfP[dfP["Object Type"] == "Table"]["Table Name"].tolist()
measures = dfP[dfP["Object Type"] == "Measure"]["Object Name"].tolist()
columns = dfP[dfP["Object Type"] == "Column"][["Table Name", "Object Name"]]
cols = [
f"'{row[0]}'[{row[1]}]"
for row in columns.itertuples(index=False, name=None)
]
hierarchies = dfP[dfP["Object Type"] == "Hierarchy"][
["Table Name", "Object Name"]
]
hier = [
f"'{row[0]}'[{row[1]}]"
for row in hierarchies.itertuples(index=False, name=None)
]
for p in tom.model.Perspectives:
Comment thread
m-kovalsky marked this conversation as resolved.
Outdated
if p.Name == perspective_name:
tables = [pt.Name for pt in p.PerspectiveTables]
columns = [
f"'{pt.Name}'[{pc.Name}]"
for pt in p.PerspectiveTables
for pc in pt.PerspectiveColumns
]
measures = [
pm.Name
for pt in p.PerspectiveTables
for pc in pt.PerspectiveMeasures
]
hierarchies = [
f"'{pt.Name}'[{ph.Name}]"
for pt in p.PerspectiveTables
for pc in pt.PerspectiveHierarchies
Comment thread
m-kovalsky marked this conversation as resolved.
Outdated
]

filt = dep_filt[
(dep_filt["Object Type"].isin(["Rows Allowed", "Calc Item"]))
| (dep_filt["Object Type"] == "Measure")
Expand All @@ -5094,7 +5120,7 @@ def _reduce_model(self, perspective_name: str):
dep_filt.apply(
lambda row: f"'{row['Table Name']}'[{row['Object Name']}]",
axis=1,
).isin(cols)
).isin(columns)
)
)
| (
Expand All @@ -5103,25 +5129,24 @@ def _reduce_model(self, perspective_name: str):
dep_filt.apply(
lambda row: f"'{row['Table Name']}'[{row['Object Name']}]",
axis=1,
).isin(hier)
).isin(hierarchies)
)
)
]

result_df = pd.DataFrame(columns=["Table Name", "Object Name", "Object Type"])

def add_to_result(table_name, object_name, object_type, dataframe):

new_data = {
"Table Name": table_name,
"Object Name": object_name,
"Object Type": object_type,
}
def add_to_result(table_name, object_name, object_type, rows):

return pd.concat(
[dataframe, pd.DataFrame(new_data, index=[0])], ignore_index=True
return rows.append(
{
"Table Name": table_name,
"Object Name": object_name,
"Object Type": object_type,
}
)
Comment thread
m-kovalsky marked this conversation as resolved.
Outdated

rows = []
for _, r in filt.iterrows():
added = False
obj_type = r["Referenced Object Type"]
Expand Down Expand Up @@ -5155,7 +5180,7 @@ def add_to_result(table_name, object_name, object_type, dataframe):
)
added = True
if added:
result_df = add_to_result(table_name, object_name, obj_type, result_df)
rows = add_to_result(table_name, object_name, obj_type, rows)

# Reduce model...

Expand All @@ -5179,8 +5204,8 @@ def add_to_result(table_name, object_name, object_type, dataframe):
object=r.FromColumn, perspective_name=perspective_name
)

result_df = add_to_result(
r.FromTable.Name, r.FromColumn.Name, "Column", result_df
rows = add_to_result(
r.FromTable.Name, r.FromColumn.Name, "Column", rows
)
if not self.in_perspective(r.ToColumn, perspective_name=perspective_name):
table_name = r.ToTable.Name
Expand All @@ -5189,9 +5214,7 @@ def add_to_result(table_name, object_name, object_type, dataframe):
object=r.ToColumn, perspective_name=perspective_name
)

result_df = add_to_result(
r.ToTable.Name, r.ToColumn.Name, "Column", result_df
)
rows = add_to_result(r.ToTable.Name, r.ToColumn.Name, "Column", rows)

# Remove objects not in the perspective
for t in self.model.Tables:
Expand All @@ -5208,7 +5231,11 @@ def add_to_result(table_name, object_name, object_type, dataframe):
self.remove_object(object=obj)

# Return the objects added to the perspective based on dependencies
return result_df.drop_duplicates()
if rows:
result_df = pd.DataFrame(rows)
result_df.drop_duplicates(inplace=True)

return result_df

def convert_direct_lake_to_import(
self,
Expand Down
Loading