Rebuild: Dagster code location with the sample dlt and dbt assets
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
__pycache__/
|
||||
.pytest_cache/
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Dagster code location: every heavy step of the platform is an asset or job defined here."""
|
||||
|
||||
import dagster as dg
|
||||
import dlt
|
||||
from dagster_dbt import DbtCliResource, DbtProject, dbt_assets
|
||||
from dagster_dlt import DagsterDltResource, DagsterDltTranslator, dlt_assets
|
||||
from dagster_dlt.translator import DltResourceTranslatorData
|
||||
|
||||
from persistance.config import DBT_DIR
|
||||
from persistance.destination import platform
|
||||
from persistance.sources.sample import sample_patients
|
||||
|
||||
WRITES = {"writes": "platform"} # dagster.yaml lets one run with this tag at a time (SQLite)
|
||||
|
||||
dbt_project = DbtProject(project_dir=DBT_DIR, profiles_dir=DBT_DIR)
|
||||
dbt_project.prepare_if_dev()
|
||||
|
||||
|
||||
class BronzeKeys(DagsterDltTranslator):
|
||||
"""Name dlt assets bronze/<table>, the key dbt's sources point at."""
|
||||
|
||||
def get_asset_spec(self, data: DltResourceTranslatorData) -> dg.AssetSpec:
|
||||
spec = super().get_asset_spec(data)
|
||||
return spec.replace_attributes(key=dg.AssetKey(["bronze", data.resource.table_name]))
|
||||
|
||||
|
||||
@dlt_assets(
|
||||
dlt_source=sample_patients(),
|
||||
dlt_pipeline=dlt.pipeline("sample", destination=platform(), dataset_name="bronze"),
|
||||
group_name="ingest",
|
||||
dagster_dlt_translator=BronzeKeys(),
|
||||
)
|
||||
def sample_ingest(context: dg.AssetExecutionContext, dlt: DagsterDltResource):
|
||||
yield from dlt.run(context=context)
|
||||
|
||||
|
||||
@dbt_assets(manifest=dbt_project.manifest_path)
|
||||
def transforms(context: dg.AssetExecutionContext, dbt: DbtCliResource):
|
||||
yield from dbt.cli(["build"], context=context).stream()
|
||||
|
||||
|
||||
sample_job = dg.define_asset_job("sample", selection=dg.AssetSelection.all(), tags=WRITES)
|
||||
|
||||
defs = dg.Definitions(
|
||||
assets=[sample_ingest, transforms],
|
||||
jobs=[sample_job],
|
||||
resources={"dlt": DagsterDltResource(), "dbt": DbtCliResource(project_dir=dbt_project)},
|
||||
)
|
||||
@@ -0,0 +1,17 @@
|
||||
[project]
|
||||
name = "models"
|
||||
version = "0.0.0"
|
||||
description = "Dagster assets and jobs; one module per model."
|
||||
requires-python = ">=3.12"
|
||||
dependencies = ["core", "persistance", "dagster", "dagster-webserver", "dagster-dbt", "dagster-dlt", "mlflow-skinny"]
|
||||
|
||||
[build-system]
|
||||
requires = ["uv_build>=0.12,<0.13"]
|
||||
build-backend = "uv_build"
|
||||
|
||||
[tool.uv.build-backend]
|
||||
module-name = "models"
|
||||
module-root = ""
|
||||
[tool.uv.sources]
|
||||
core = { workspace = true }
|
||||
persistance = { workspace = true }
|
||||
@@ -0,0 +1,10 @@
|
||||
import mlflow
|
||||
|
||||
|
||||
def test_logs_a_run(tmp_path):
|
||||
mlflow.set_tracking_uri(f"sqlite:///{tmp_path / 'mlflow.db'}")
|
||||
with mlflow.start_run() as run:
|
||||
mlflow.log_param("seed", 7)
|
||||
mlflow.log_metric("auc", 0.5)
|
||||
got = mlflow.get_run(run.info.run_id)
|
||||
assert got.data.params == {"seed": "7"} and got.data.metrics == {"auc": 0.5}
|
||||
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine, make_url, text
|
||||
|
||||
from synthetic.hospital import generate
|
||||
|
||||
POSTGRES = os.environ.get("POSTGRES_TEST_URL") # set by CI's postgres job
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"backend",
|
||||
["sqlite", pytest.param("postgres", marks=pytest.mark.skipif(not POSTGRES, reason="no PG"))],
|
||||
)
|
||||
def test_sample_job_loads_and_reloads_without_duplicates(backend, tmp_path):
|
||||
generate(tmp_path / "synthetic", seed=7, patients_per_domain=20)
|
||||
(tmp_path / "dagster").mkdir()
|
||||
env = {
|
||||
**os.environ,
|
||||
"ISSSTELEON_DATA": str(tmp_path),
|
||||
"DLT_DATA_DIR": str(tmp_path / "dlt"),
|
||||
"DAGSTER_HOME": str(tmp_path / "dagster"),
|
||||
}
|
||||
if backend == "sqlite":
|
||||
url = env["PLATFORM_DATABASE_URL"] = f"sqlite:///{tmp_path / 'platform.db'}"
|
||||
attach = [f"ATTACH '{tmp_path}/platform__{s}.db' AS {s}" for s in ("bronze", "silver")]
|
||||
else:
|
||||
env.update(PLATFORM_DATABASE_URL=POSTGRES, DBT_TARGET="postgres")
|
||||
url = make_url(POSTGRES).set(drivername="postgresql+psycopg2") # dlt's driver
|
||||
attach = []
|
||||
with create_engine(url).begin() as con:
|
||||
con.exec_driver_sql("drop schema if exists bronze cascade")
|
||||
con.exec_driver_sql("drop schema if exists silver cascade")
|
||||
|
||||
for _ in range(2): # each run in its own process, as the Dagster daemon launches them
|
||||
subprocess.run(
|
||||
["dagster", "job", "execute", "-m", "models.definitions", "-j", "sample"],
|
||||
env=env,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
with create_engine(url).connect() as con:
|
||||
for statement in attach:
|
||||
con.exec_driver_sql(statement)
|
||||
assert con.execute(text("select count(*) from bronze.patients")).scalar() == 20
|
||||
assert con.execute(text("select count(*) from silver.cacu_patients")).scalar() == 20
|
||||
Reference in New Issue
Block a user