Skip to content

Commit b985f9f

Browse files
committed
Merge branch 'm-kovalsky/bpausedatasetid'
2 parents cd700ab + 6c38897 commit b985f9f

File tree

9 files changed

+193
-133
lines changed

9 files changed

+193
-133
lines changed

src/sempy_labs/_helper_functions.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,34 @@ def resolve_report_name(report_id: UUID, workspace: Optional[str] = None) -> str
160160
return obj
161161

162162

163-
def resolve_dataset_id(dataset: str, workspace: Optional[str] = None) -> UUID:
163+
def resolve_dataset_name_and_id(
164+
dataset: str | UUID, workspace: Optional[str] = None
165+
) -> Tuple[str, UUID]:
166+
167+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
168+
169+
if _is_valid_uuid(dataset):
170+
dataset_id = dataset
171+
dataset_name = fabric.resolve_item_name(
172+
item_id=dataset_id, type="SemanticModel", workspace=workspace_id
173+
)
174+
else:
175+
dataset_name = dataset
176+
dataset_id = fabric.resolve_item_id(
177+
item_name=dataset, type="SemanticModel", workspace=workspace_id
178+
)
179+
180+
return dataset_name, dataset_id
181+
182+
183+
def resolve_dataset_id(dataset: str | UUID, workspace: Optional[str] = None) -> UUID:
164184
"""
165185
Obtains the ID of the semantic model.
166186
167187
Parameters
168188
----------
169-
dataset : str
170-
The name of the semantic model.
189+
dataset : str | UUID
190+
The name or ID of the semantic model.
171191
workspace : str, default=None
172192
The Fabric workspace name.
173193
Defaults to None which resolves to the workspace of the attached lakehouse
@@ -179,15 +199,14 @@ def resolve_dataset_id(dataset: str, workspace: Optional[str] = None) -> UUID:
179199
The ID of the semantic model.
180200
"""
181201

182-
if workspace is None:
183-
workspace_id = fabric.get_workspace_id()
184-
workspace = fabric.resolve_workspace_name(workspace_id)
185-
186-
obj = fabric.resolve_item_id(
187-
item_name=dataset, type="SemanticModel", workspace=workspace
188-
)
202+
if _is_valid_uuid(dataset):
203+
dataset_id = dataset
204+
else:
205+
dataset_id = fabric.resolve_item_id(
206+
item_name=dataset, type="SemanticModel", workspace=workspace
207+
)
189208

190-
return obj
209+
return dataset_id
191210

192211

193212
def resolve_dataset_name(dataset_id: UUID, workspace: Optional[str] = None) -> str:
@@ -1167,20 +1186,20 @@ def _make_list_unique(my_list):
11671186

11681187
def _get_partition_map(dataset: str, workspace: Optional[str] = None) -> pd.DataFrame:
11691188

1170-
if workspace is None:
1171-
workspace = fabric.resolve_workspace_name()
1189+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
1190+
(dataset_name, dataset_id) = resolve_dataset_name_and_id(dataset, workspace_id)
11721191

11731192
partitions = fabric.evaluate_dax(
1174-
dataset=dataset,
1175-
workspace=workspace,
1193+
dataset=dataset_id,
1194+
workspace=workspace_id,
11761195
dax_string="""
11771196
select [ID] AS [PartitionID], [TableID], [Name] AS [PartitionName] from $system.tmschema_partitions
11781197
""",
11791198
)
11801199

11811200
tables = fabric.evaluate_dax(
1182-
dataset=dataset,
1183-
workspace=workspace,
1201+
dataset=dataset_id,
1202+
workspace=workspace_id,
11841203
dax_string="""
11851204
select [ID] AS [TableID], [Name] AS [TableName] from $system.tmschema_tables
11861205
""",

src/sempy_labs/_list_functions.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@
77
pagination,
88
resolve_item_type,
99
format_dax_object_name,
10+
resolve_dataset_name_and_id,
1011
)
1112
import pandas as pd
1213
from typing import Optional
1314
import sempy_labs._icons as icons
1415
from sempy.fabric.exceptions import FabricHTTPException
16+
from uuid import UUID
1517

1618

1719
def get_object_level_security(
18-
dataset: str, workspace: Optional[str] = None
20+
dataset: str | UUID, workspace: Optional[str] = None
1921
) -> pd.DataFrame:
2022
"""
2123
Shows the object level security for the semantic model.
2224
2325
Parameters
2426
----------
25-
dataset : str
26-
Name of the semantic model.
27+
dataset : str | UUID
28+
Name or ID of the semantic model.
2729
workspace : str, default=None
2830
The Fabric workspace name.
2931
Defaults to None which resolves to the workspace of the attached lakehouse
@@ -37,12 +39,13 @@ def get_object_level_security(
3739

3840
from sempy_labs.tom import connect_semantic_model
3941

40-
workspace = fabric.resolve_workspace_name(workspace)
42+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
43+
(dataset_name, dataset_id) = resolve_dataset_name_and_id(dataset, workspace_id)
4144

4245
df = pd.DataFrame(columns=["Role Name", "Object Type", "Table Name", "Object Name"])
4346

4447
with connect_semantic_model(
45-
dataset=dataset, readonly=True, workspace=workspace
48+
dataset=dataset_id, readonly=True, workspace=workspace_id
4649
) as tom:
4750

4851
for r in tom.model.Roles:
@@ -82,15 +85,15 @@ def get_object_level_security(
8285

8386

8487
def list_tables(
85-
dataset: str, workspace: Optional[str] = None, extended: bool = False
88+
dataset: str | UUID, workspace: Optional[str] = None, extended: bool = False
8689
) -> pd.DataFrame:
8790
"""
8891
Shows a semantic model's tables and their properties.
8992
9093
Parameters
9194
----------
92-
dataset : str
93-
Name of the semantic model.
95+
dataset : str | UUID
96+
Name or ID of the semantic model.
9497
workspace : str, default=None
9598
The Fabric workspace name.
9699
Defaults to None which resolves to the workspace of the attached lakehouse
@@ -106,7 +109,8 @@ def list_tables(
106109

107110
from sempy_labs.tom import connect_semantic_model
108111

109-
workspace = fabric.resolve_workspace_name(workspace)
112+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
113+
(dataset_name, dataset_id) = resolve_dataset_name_and_id(dataset, workspace_id)
110114

111115
df = pd.DataFrame(
112116
columns=[
@@ -121,20 +125,20 @@ def list_tables(
121125
)
122126

123127
with connect_semantic_model(
124-
dataset=dataset, workspace=workspace, readonly=True
128+
dataset=dataset_id, workspace=workspace_id, readonly=True
125129
) as tom:
126130
if extended:
127131
dict_df = fabric.evaluate_dax(
128-
dataset=dataset,
129-
workspace=workspace,
132+
dataset=dataset_id,
133+
workspace=workspace_id,
130134
dax_string="""
131135
EVALUATE SELECTCOLUMNS(FILTER(INFO.STORAGETABLECOLUMNS(), [COLUMN_TYPE] = "BASIC_DATA"),[DIMENSION_NAME],[DICTIONARY_SIZE])
132136
""",
133137
)
134138
dict_sum = dict_df.groupby("[DIMENSION_NAME]")["[DICTIONARY_SIZE]"].sum()
135139
data = fabric.evaluate_dax(
136-
dataset=dataset,
137-
workspace=workspace,
140+
dataset=dataset_id,
141+
workspace=workspace_id,
138142
dax_string="""EVALUATE SELECTCOLUMNS(INFO.STORAGETABLECOLUMNSEGMENTS(),[TABLE_ID],[DIMENSION_NAME],[USED_SIZE])""",
139143
)
140144
data_sum = (
@@ -162,8 +166,8 @@ def list_tables(
162166
.sum()
163167
)
164168
rc = fabric.evaluate_dax(
165-
dataset=dataset,
166-
workspace=workspace,
169+
dataset=dataset_id,
170+
workspace=workspace_id,
167171
dax_string="""
168172
SELECT [DIMENSION_NAME],[ROWS_COUNT] FROM $SYSTEM.DISCOVER_STORAGE_TABLES
169173
WHERE RIGHT ( LEFT ( TABLE_ID, 2 ), 1 ) <> '$'
@@ -850,15 +854,15 @@ def update_item(
850854

851855

852856
def list_relationships(
853-
dataset: str, workspace: Optional[str] = None, extended: bool = False
857+
dataset: str | UUID, workspace: Optional[str] = None, extended: bool = False
854858
) -> pd.DataFrame:
855859
"""
856860
Shows a semantic model's relationships and their properties.
857861
858862
Parameters
859863
----------
860-
dataset: str
861-
Name of the semantic model.
864+
dataset: str | UUID
865+
Name or UUID of the semantic model.
862866
workspace : str, default=None
863867
The Fabric workspace name.
864868
Defaults to None which resolves to the workspace of the attached lakehouse
@@ -872,17 +876,18 @@ def list_relationships(
872876
A pandas dataframe showing the object level security for the semantic model.
873877
"""
874878

875-
workspace = fabric.resolve_workspace_name(workspace)
879+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
880+
(dataset_name, dataset_id) = resolve_dataset_name_and_id(dataset, workspace_id)
876881

877-
dfR = fabric.list_relationships(dataset=dataset, workspace=workspace)
882+
dfR = fabric.list_relationships(dataset=dataset_id, workspace=workspace_id)
878883
dfR["From Object"] = format_dax_object_name(dfR["From Table"], dfR["From Column"])
879884
dfR["To Object"] = format_dax_object_name(dfR["To Table"], dfR["To Column"])
880885

881886
if extended:
882887
# Used to map the Relationship IDs
883888
rel = fabric.evaluate_dax(
884-
dataset=dataset,
885-
workspace=workspace,
889+
dataset=dataset_id,
890+
workspace=workspace_id,
886891
dax_string="""
887892
SELECT
888893
[ID] AS [RelationshipID]
@@ -893,8 +898,8 @@ def list_relationships(
893898

894899
# USED_SIZE shows the Relationship Size where TABLE_ID starts with R$
895900
cs = fabric.evaluate_dax(
896-
dataset=dataset,
897-
workspace=workspace,
901+
dataset=dataset_id,
902+
workspace=workspace_id,
898903
dax_string="""
899904
SELECT
900905
[TABLE_ID]

src/sempy_labs/_model_bpa.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
create_relationship_name,
1111
save_as_delta_table,
1212
resolve_workspace_capacity,
13-
resolve_dataset_id,
13+
resolve_dataset_name_and_id,
1414
get_language_codes,
1515
_get_max_run_id,
16+
resolve_workspace_name_and_id,
1617
)
1718
from sempy_labs.lakehouse import get_lakehouse_tables, lakehouse_attached
1819
from sempy_labs.tom import connect_semantic_model
@@ -23,11 +24,12 @@
2324
from pyspark.sql.functions import col, flatten
2425
from pyspark.sql.types import StructType, StructField, StringType
2526
import os
27+
from uuid import UUID
2628

2729

2830
@log
2931
def run_model_bpa(
30-
dataset: str,
32+
dataset: str | UUID,
3133
rules: Optional[pd.DataFrame] = None,
3234
workspace: Optional[str] = None,
3335
export: bool = False,
@@ -41,8 +43,8 @@ def run_model_bpa(
4143
4244
Parameters
4345
----------
44-
dataset : str
45-
Name of the semantic model.
46+
dataset : str | UUID
47+
Name or ID of the semantic model.
4648
rules : pandas.DataFrame, default=None
4749
A pandas dataframe containing rules to be evaluated.
4850
workspace : str, default=None
@@ -105,15 +107,18 @@ def map_language(language, language_list):
105107
if language is not None:
106108
language = map_language(language, language_list)
107109

108-
workspace = fabric.resolve_workspace_name(workspace)
110+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
111+
(dataset_name, dataset_id) = resolve_dataset_name_and_id(
112+
dataset, workspace=workspace_id
113+
)
109114

110115
if language is not None and language not in language_list:
111116
print(
112117
f"{icons.yellow_dot} The '{language}' language code is not in our predefined language list. Please file an issue and let us know which language code you are using: https://github.com/microsoft/semantic-link-labs/issues/new?assignees=&labels=&projects=&template=bug_report.md&title=."
113118
)
114119

115120
with connect_semantic_model(
116-
dataset=dataset, workspace=workspace, readonly=True
121+
dataset=dataset_id, workspace=workspace_id, readonly=True
117122
) as tom:
118123

119124
if extended:
@@ -122,7 +127,7 @@ def map_language(language, language_list):
122127
# Do not run BPA for models with no tables
123128
if tom.model.Tables.Count == 0:
124129
print(
125-
f"{icons.warning} The '{dataset}' semantic model within the '{workspace}' workspace has no tables and therefore there are no valid BPA results."
130+
f"{icons.warning} The '{dataset_name}' semantic model within the '{workspace_name}' workspace has no tables and therefore there are no valid BPA results."
126131
)
127132
finalDF = pd.DataFrame(
128133
columns=[
@@ -136,7 +141,9 @@ def map_language(language, language_list):
136141
]
137142
)
138143
else:
139-
dep = get_model_calc_dependencies(dataset=dataset, workspace=workspace)
144+
dep = get_model_calc_dependencies(
145+
dataset=dataset_id, workspace=workspace_id
146+
)
140147

141148
def translate_using_po(rule_file):
142149
current_dir = os.path.dirname(os.path.abspath(__file__))
@@ -382,20 +389,19 @@ def translate_using_spark(rule_file):
382389
runId = max_run_id + 1
383390

384391
now = datetime.datetime.now()
385-
dfD = fabric.list_datasets(workspace=workspace, mode="rest")
386-
dfD_filt = dfD[dfD["Dataset Name"] == dataset]
392+
dfD = fabric.list_datasets(workspace=workspace_id, mode="rest")
393+
dfD_filt = dfD[dfD["Dataset Id"] == dataset_id]
387394
configured_by = dfD_filt["Configured By"].iloc[0]
388-
capacity_id, capacity_name = resolve_workspace_capacity(workspace=workspace)
395+
capacity_id, capacity_name = resolve_workspace_capacity(workspace=workspace_id)
389396
dfExport["Capacity Name"] = capacity_name
390397
dfExport["Capacity Id"] = capacity_id
391-
dfExport["Workspace Name"] = workspace
392-
dfExport["Workspace Id"] = fabric.resolve_workspace_id(workspace)
393-
dfExport["Dataset Name"] = dataset
394-
dfExport["Dataset Id"] = resolve_dataset_id(dataset, workspace)
398+
dfExport["Workspace Name"] = workspace_name
399+
dfExport["Workspace Id"] = workspace_id
400+
dfExport["Dataset Name"] = dataset_name
401+
dfExport["Dataset Id"] = dataset_id
395402
dfExport["Configured By"] = configured_by
396403
dfExport["Timestamp"] = now
397404
dfExport["RunId"] = runId
398-
dfExport["Configured By"] = configured_by
399405
dfExport["RunId"] = dfExport["RunId"].astype("int")
400406

401407
dfExport = dfExport[list(icons.bpa_schema.keys())]

src/sempy_labs/_model_bpa_bulk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ def run_model_bpa_bulk(
119119
dfD_filt = dfD[~dfD["Dataset Name"].isin(skip_models)]
120120

121121
if len(dfD_filt) > 0:
122-
for i2, r2 in dfD_filt.iterrows():
122+
for _, r2 in dfD_filt.iterrows():
123+
dataset_id = r2["Dataset Id"]
123124
dataset_name = r2["Dataset Name"]
124125
config_by = r2["Configured By"]
125-
dataset_id = r2["Dataset Id"]
126126
print(
127127
f"{icons.in_progress} Collecting Model BPA stats for the '{dataset_name}' semantic model within the '{wksp}' workspace."
128128
)
129129
try:
130130
bpa_df = run_model_bpa(
131-
dataset=dataset_name,
131+
dataset=dataset_id,
132132
workspace=wksp,
133133
language=language,
134134
return_dataframe=True,

0 commit comments

Comments
 (0)