-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dagster-tableau] Detach spec loading, assets def building from Table…
…au resource (#25459) ## Summary As part of migrating to the APIs mocked out in https://www.notion.so/dagster/Asset-integration-customization-options-11a18b92e462807c94c2f84675021120 and https://www.notion.so/dagster/Power-BI-semantic-model-API-options-11918b92e4628006a25af3f58a6b48d9, detaches the asset spec loading process from the Tableau resource, and moves building asset definitions explicitly into user code. ```python resource = TableauCloudWorkspace( connected_app_client_id=EnvVar("CONNECTED_APP_CLIENT_ID"), connected_app_secret_id=EnvVar("CONNECTED_APP_SECRET_ID"), connected_app_secret_value=EnvVar("CONNECTED_APP_SECRET_VALUE"), username=EnvVar("USERNAME"), site_name=EnvVar("SITE_NAME"), pod_name=EnvVar("POD_NAME"), ) tableau_specs = load_tableau_asset_specs( workspace=resource, ) non_executable_asset_specs = [ spec for spec in tableau_specs if spec.tags.get("dagster-tableau/asset_type") == "data_source" ] executable_asset_specs = [ spec for spec in tableau_specs if spec.tags.get("dagster-tableau/asset_type") in ["dashboard", "sheet"] ] defs = Definitions( assets=[ build_tableau_executable_assets_definition( resource_key="tableau", workspace=resource, specs=executable_asset_specs, refreshable_workbook_ids=["b75fc023-a7ca-4115-857b-4342028640d0"], ), *non_executable_asset_specs, ], resources={"tableau": resource}, ) ``` ## How I Tested These Changes New unit test, updated existing unit tests.
- Loading branch information
1 parent
e813170
commit e897e05
Showing
8 changed files
with
333 additions
and
126 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
python_modules/libraries/dagster-tableau/dagster_tableau/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
python_modules/libraries/dagster-tableau/dagster_tableau/assets.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from typing import Optional, Sequence, cast | ||
|
||
from dagster import ( | ||
AssetExecutionContext, | ||
AssetsDefinition, | ||
AssetSpec, | ||
ObserveResult, | ||
Output, | ||
multi_asset, | ||
) | ||
|
||
from dagster_tableau.resources import BaseTableauWorkspace | ||
|
||
|
||
def build_tableau_executable_assets_definition( | ||
resource_key: str, | ||
specs: Sequence[AssetSpec], | ||
refreshable_workbook_ids: Optional[Sequence[str]] = None, | ||
) -> AssetsDefinition: | ||
"""Returns the AssetsDefinition of the executable assets in the Tableau workspace. | ||
Args: | ||
resource_key (str): The resource key to use for the Tableau resource. | ||
specs (Sequence[AssetSpec]): The asset specs of the executable assets in the Tableau workspace. | ||
refreshable_workbook_ids (Optional[Sequence[str]]): A list of workbook IDs. The workbooks provided must | ||
have extracts as data sources and be refreshable in Tableau. | ||
When materializing your Tableau assets, the workbooks provided are refreshed, | ||
refreshing their sheets and dashboards before pulling their data in Dagster. | ||
This feature is equivalent to selecting Refreshing Extracts for a workbook in Tableau UI | ||
and only works for workbooks for which the data sources are extracts. | ||
See https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_workbooks_and_views.htm#update_workbook_now | ||
for documentation. | ||
Returns: | ||
AssetsDefinition: The AssetsDefinition of the executable assets in the Tableau workspace. | ||
""" | ||
|
||
@multi_asset( | ||
name=f"tableau_sync_site_{resource_key}", | ||
compute_kind="tableau", | ||
can_subset=False, | ||
specs=specs, | ||
required_resource_keys={resource_key}, | ||
) | ||
def asset_fn(context: AssetExecutionContext): | ||
tableau = cast(BaseTableauWorkspace, getattr(context.resources, resource_key)) | ||
with tableau.get_client() as client: | ||
refreshed_workbooks = set() | ||
for refreshable_workbook_id in refreshable_workbook_ids or []: | ||
refreshed_workbooks.add(client.refresh_and_poll(refreshable_workbook_id)) | ||
for spec in specs: | ||
data = client.get_view(spec.metadata.get("id")) | ||
asset_key = spec.key | ||
if ( | ||
spec.metadata.get("workbook_id") | ||
and spec.metadata.get("workbook_id") in refreshed_workbooks | ||
): | ||
yield Output( | ||
value=None, | ||
output_name="__".join(asset_key.path), | ||
metadata={ | ||
"workbook_id": data.workbook_id, | ||
"owner_id": data.owner_id, | ||
"name": data.name, | ||
"contentUrl": data.content_url, | ||
"createdAt": data.created_at.strftime("%Y-%m-%dT%H:%M:%S") | ||
if data.created_at | ||
else None, | ||
"updatedAt": data.updated_at.strftime("%Y-%m-%dT%H:%M:%S") | ||
if data.updated_at | ||
else None, | ||
}, | ||
) | ||
else: | ||
yield ObserveResult( | ||
asset_key=asset_key, | ||
metadata={ | ||
"workbook_id": data.workbook_id, | ||
"owner_id": data.owner_id, | ||
"name": data.name, | ||
"contentUrl": data.content_url, | ||
"createdAt": data.created_at.strftime("%Y-%m-%dT%H:%M:%S") | ||
if data.created_at | ||
else None, | ||
"updatedAt": data.updated_at.strftime("%Y-%m-%dT%H:%M:%S") | ||
if data.updated_at | ||
else None, | ||
}, | ||
) | ||
|
||
return asset_fn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.