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

clean up helper functions #379

Merged
merged 4 commits into from
Jan 16, 2025
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
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
- pytest-cov
- pytest-mock
- pip:
- semantic-link-sempy>=0.8.3
- semantic-link-sempy>=0.8.5
- azure-identity==1.7.1
- azure-storage-blob>=12.9.0
- pandas-stubs
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers = [
license= { text = "MIT License" }

dependencies = [
"semantic-link-sempy>=0.8.3",
"semantic-link-sempy>=0.8.5",
"anytree",
"powerbiclient",
"polib",
Expand Down
4 changes: 2 additions & 2 deletions src/sempy_labs/_clear_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ def list_storage_account_files(
]
)

onelake = _get_adls_client(storage_account)
fs = onelake.get_file_system_client(container)
client = _get_adls_client(storage_account)
fs = client.get_file_system_client(container)

for x in list(fs.get_paths()):
if not x.is_directory:
Expand Down
32 changes: 11 additions & 21 deletions src/sempy_labs/_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from typing import Optional, Tuple, List
from uuid import UUID
import sempy_labs._icons as icons
import urllib.parse
from azure.core.credentials import TokenCredential, AccessToken
import urllib.parse
import numpy as np
from IPython.display import display, HTML

Expand Down Expand Up @@ -328,9 +328,6 @@ def get_direct_lake_sql_endpoint(

from sempy_labs.tom import connect_semantic_model

(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
(dataset_name, dataset_id) = resolve_dataset_name_and_id(dataset, workspace_id)

# dfP = fabric.list_partitions(dataset=dataset, workspace=workspace)
# dfP_filt = dfP[dfP["Mode"] == "DirectLake"]

Expand All @@ -340,7 +337,7 @@ def get_direct_lake_sql_endpoint(
# )

with connect_semantic_model(
dataset=dataset_id, readonly=True, workspace=workspace_id
dataset=dataset, readonly=True, workspace=workspace
) as tom:
sqlEndpointId = None
for e in tom.model.Expressions:
Expand Down Expand Up @@ -621,9 +618,7 @@ def _conv_b64(file):

def _decode_b64(file, format: Optional[str] = "utf-8"):

result = base64.b64decode(file).decode(format)

return result
return base64.b64decode(file).decode(format)


def is_default_semantic_model(
Expand Down Expand Up @@ -690,36 +685,31 @@ def resolve_item_type(item_id: UUID, workspace: Optional[str | UUID] = None) ->


def resolve_dataset_from_report(
report: str, workspace: Optional[str | UUID] = None
report: str | UUID, workspace: Optional[str | UUID] = None
) -> Tuple[UUID, str, UUID, str]:
"""
Obtains the basic semantic model properties from which the report's data is sourced.

Parameters
----------
report : str
The name of the Power BI report.
report : str | uuid.UUID
The name or ID of the Power BI report.
workspace : str | uuid.UUID, default=None
The Fabric workspace name or ID.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.

Returns
-------
Tuple[UUID, str, UUID, str]
Tuple[uuid.UUID, str, uuid.UUID, str]
The semantic model UUID, semantic model name, semantic model workspace UUID, semantic model workspace name
"""

(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
from sempy_labs.report._generate_report import _get_report

dfR = fabric.list_reports(workspace=workspace_id)
dfR_filt = dfR[dfR["Name"] == report]
if len(dfR_filt) == 0:
raise ValueError(
f"{icons.red_dot} The '{report}' report does not exist within the '{workspace_name}' workspace."
)
dataset_id = dfR_filt["Dataset Id"].iloc[0]
dataset_workspace_id = dfR_filt["Dataset Workspace Id"].iloc[0]
dfR = _get_report(report=report, workspace=workspace)
dataset_id = dfR["Dataset Id"].iloc[0]
dataset_workspace_id = dfR["Dataset Workspace Id"].iloc[0]
dataset_workspace = fabric.resolve_workspace_name(dataset_workspace_id)
dataset_name = resolve_dataset_name(
dataset_id=dataset_id, workspace=dataset_workspace
Expand Down
45 changes: 45 additions & 0 deletions src/sempy_labs/report/_generate_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
_conv_b64,
resolve_report_id,
resolve_dataset_name_and_id,
resolve_item_name_and_id,
lro,
)
import sempy_labs._icons as icons
from sempy._utils._log import log
from uuid import UUID
from sempy.fabric.exceptions import FabricHTTPException


def create_report_from_reportjson(
Expand Down Expand Up @@ -371,3 +373,46 @@ def _create_report(
report_workspace=report_workspace,
dataset_workspace=dataset_workspace,
)


def _get_report(
report: str | UUID, workspace: Optional[str | UUID] = None
) -> pd.DataFrame:

(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
(report_name, report_id) = resolve_item_name_and_id(
item=report, type="Report", workspace=workspace
)

client = fabric.FabricRestClient()
response = client.get(f"/v1.0/myorg/groups/{workspace_id}/reports/{report_id}")

if response.status_code != 200:
raise FabricHTTPException(response)

result = response.json()

new_data = {
"Id": result.get("id"),
"Report Type": result.get("reportType"),
"Name": result.get("name"),
"Web Url": result.get("webUrl"),
"Embed Url": result.get("embedUrl"),
"Is From Pbix": result.get("isFromPbix"),
m-kovalsky marked this conversation as resolved.
Show resolved Hide resolved
"Is Owned By Me": result.get("isOwnedByMe"),
"Dataset Id": result.get("datasetId"),
"Dataset Workspace Id": result.get("datasetWorkspaceId"),
"Users": result.get("users") if result.get("users") is not None else [],
"Subscriptions": (
result.get("subscriptions")
if result.get("subscriptions") is not None
else []
),
}

df = pd.DataFrame([new_data])

bool_cols = ["Is From Pbix", "Is Owned By Me"]
df[bool_cols] = df[bool_cols].astype(bool)

return df
Loading