-
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-sigma] Add dagster-sigma snapshot CLI, which allows persisti…
…ng the contents of a Sigma workspace to a file (#25913) ## Summary Introduces a utility CLI for `dagster-sigma` which loads a code location containing one or more `SigmaOrganizationDefsLoader`s and serializes the repository load data to a file. Adds the capability for the Sling defs loader to ingest this snapshot from a file, allowing orgs with large amounts of Sigma data to ingest at CI time or manually vs each code location load. In the future, we expect to subsume this behavior into a framework level solution which doesn't tie loading external state into code location load. For now, this is a bit of a workaround. ```shell dagster-sigma snapshot --python-module my_dagster_package --save-to snapshot.snap ``` ```python ... specs = load_sigma_asset_specs(sigma_workspace, snapshot_path=Path(__file__).parent / "snapshot.snap") ... ``` ## How I Tested These Changes Unit tests. ## Changelog > [dagster-sigma] Introduced an experimental `dagster-sigma snapshot` command, allowing Sigma workspaces to be captured to a file for faster subsequent loading.
- Loading branch information
Showing
5 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
python_modules/libraries/dagster-sigma/dagster_sigma/cli.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,61 @@ | ||
import click | ||
from dagster import _check as check | ||
from dagster._cli.workspace.cli_target import ( | ||
get_repository_python_origin_from_kwargs, | ||
python_origin_target_argument, | ||
) | ||
from dagster._core.definitions.definitions_load_context import ( | ||
DefinitionsLoadContext, | ||
DefinitionsLoadType, | ||
) | ||
from dagster._core.definitions.repository_definition.repository_definition import RepositoryLoadData | ||
from dagster._serdes.utils import serialize_value | ||
from dagster._utils.env import environ | ||
from dagster._utils.hosted_user_process import recon_repository_from_origin | ||
from dagster._utils.warnings import experimental_warning | ||
|
||
SNAPSHOT_ENV_VAR_NAME = "DAGSTER_SIGMA_IS_GENERATING_SNAPSHOT" | ||
SIGMA_RECON_DATA_PREFIX = "sigma_" | ||
|
||
|
||
@click.group(name="sigma") | ||
def app(): | ||
"""Commands for working with the dagster-sigma integration.""" | ||
|
||
|
||
@app.command(name="snapshot", help="Snapshot sigma instance data") | ||
@python_origin_target_argument | ||
@click.option("--output-path", "-o", help="Path to save the snapshot to", required=True) | ||
def sigma_snapshot_command(**kwargs) -> None: | ||
experimental_warning("The `dagster-sigma snapshot` command") | ||
with environ({SNAPSHOT_ENV_VAR_NAME: "1"}): | ||
DefinitionsLoadContext.set( | ||
DefinitionsLoadContext( | ||
load_type=DefinitionsLoadType.INITIALIZATION, repository_load_data=None | ||
) | ||
) | ||
|
||
repository_origin = get_repository_python_origin_from_kwargs(kwargs) | ||
|
||
pending_data = DefinitionsLoadContext.get().get_pending_reconstruction_metadata() | ||
load_data = ( | ||
RepositoryLoadData(reconstruction_metadata=pending_data) if pending_data else None | ||
) | ||
recon_repo = recon_repository_from_origin(repository_origin) | ||
repo_def = recon_repo.get_definition() | ||
|
||
load_data = load_data if pending_data else repo_def.repository_load_data | ||
load_data = RepositoryLoadData( | ||
reconstruction_metadata={ | ||
k: v | ||
for k, v in check.not_none(load_data).reconstruction_metadata.items() | ||
if k.startswith(SIGMA_RECON_DATA_PREFIX) | ||
} | ||
) | ||
if not load_data.reconstruction_metadata: | ||
raise click.UsageError("No Sigma data found in the repository") | ||
click.echo(f"Saving {len(load_data.reconstruction_metadata)} cached Sigma data") | ||
|
||
output_path = kwargs["output_path"] | ||
with open(output_path, "w") as file: | ||
file.write(serialize_value(load_data)) |
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
27 changes: 27 additions & 0 deletions
27
python_modules/libraries/dagster-sigma/dagster_sigma_tests/pending_repo_snapshot.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,27 @@ | ||
import os | ||
|
||
from dagster import EnvVar, asset, define_asset_job | ||
from dagster._core.definitions.definitions_class import Definitions | ||
from dagster._utils.env import environ | ||
from dagster_sigma import SigmaBaseUrl, SigmaOrganization, load_sigma_asset_specs | ||
|
||
fake_client_id = "fake_client_id" | ||
fake_client_secret = "fake_client_secret" | ||
snapshot_path = os.getenv("SIGMA_SNAPSHOT_PATH") or None | ||
|
||
with environ({"SIGMA_CLIENT_ID": fake_client_id, "SIGMA_CLIENT_SECRET": fake_client_secret}): | ||
fake_token = "fake_token" | ||
resource = SigmaOrganization( | ||
base_url=SigmaBaseUrl.AWS_US, | ||
client_id=EnvVar("SIGMA_CLIENT_ID"), | ||
client_secret=EnvVar("SIGMA_CLIENT_SECRET"), | ||
) | ||
|
||
@asset | ||
def my_materializable_asset(): | ||
pass | ||
|
||
sigma_specs = load_sigma_asset_specs(resource, snapshot_path=snapshot_path) | ||
defs = Definitions( | ||
assets=[my_materializable_asset, *sigma_specs], jobs=[define_asset_job("all_asset_job")] | ||
) |
33 changes: 33 additions & 0 deletions
33
python_modules/libraries/dagster-sigma/dagster_sigma_tests/test_asset_specs.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
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