Skip to content

Commit

Permalink
minor code fixes for dagster uni dbt course
Browse files Browse the repository at this point in the history
  • Loading branch information
C00ldudeNoonan committed Sep 25, 2024
1 parent b4a216b commit 0c51b69
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To demonstrate, we’re going to intentionally make a bug in our dbt model code,
from {{ source('raw_taxis', 'zones') }}
)
select
{{ dbt_utils.generate_surrogate_key(['zone_id']) }} as zone_id,
zone_id,
zone as zone_name,
borough,
zone_name like '%Airport' as is_airport,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ Open the `assets/dbt.py` file and do the following:

4. Now, let’s fill in the `get_asset_key` method with our own logic for defining asset keys.

1. There are two properties that we’ll want from `dbt_resource_props`: the `type` (ex., model, source, seed, snapshot) and the `name`, such as `trips` or `stg_trips`. Access both of those properties from the `dbt_resource_props` argument and store them in their own respective variables (`type` and `name`):
1. There are two properties that we’ll want from `dbt_resource_props`: the `resource_type` (ex., model, source, seed, snapshot) and the `name`, such as `trips` or `stg_trips`. Access both of those properties from the `dbt_resource_props` argument and store them in their own respective variables (`type` and `name`):

```python
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
resource_type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]
```

Expand All @@ -63,7 +63,7 @@ Open the `assets/dbt.py` file and do the following:

```python
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
resource_type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]

return AssetKey(f"taxi_{name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,53 +107,52 @@ Now we’re ready to create the asset!
At this point, the `airport_trips` asset should look like this:
```python
@asset(
deps=["location_metrics"],
)
def airport_trips(database: DuckDBResource) -> MaterializeResult:
"""
A chart of where trips from the airport go
"""
query = """
select
zone,
destination_borough,
trips
from location_metrics
where from_airport
"""
with database.get_connection() as conn:
airport_trips = conn.execute(query).fetch_df()
fig = px.bar(
airport_trips,
x="zone",
y="trips",
color="destination_borough",
barmode="relative",
labels={
"zone": "Zone",
"trips": "Number of Trips",
"destination_borough": "Destination Borough"
},
)
pio.write_image(fig, constants.AIRPORT_TRIPS_FILE_PATH)
with open(constants.AIRPORT_TRIPS_FILE_PATH, 'rb') as file:
image_data = file.read()
@asset(
deps=["location_metrics"],
)
def airport_trips(database: DuckDBResource) -> MaterializeResult:
"""
A chart of where trips from the airport go
"""
query = """
select
zone,
destination_borough,
trips
from location_metrics
where from_airport
"""
with database.get_connection() as conn:
airport_trips = conn.execute(query).fetch_df()
fig = px.bar(
airport_trips,
x="zone",
y="trips",
color="destination_borough",
barmode="relative",
labels={
"zone": "Zone",
"trips": "Number of Trips",
"destination_borough": "Destination Borough"
},
)
pio.write_image(fig, constants.AIRPORT_TRIPS_FILE_PATH)
with open(constants.AIRPORT_TRIPS_FILE_PATH, 'rb') as file:
image_data = file.read()
# Convert the image data to base64
base64_data = base64.b64encode(image_data).decode('utf-8')
md_content = f"![Image](data:image/jpeg;base64,{base64_data})"
return MaterializeResult(
metadata={
"preview": MetadataValue.md(md_content)
}
)
base64_data = base64.b64encode(image_data).decode('utf-8')
md_content = f"![Image](data:image/jpeg;base64,{base64_data})"
return MaterializeResult(
metadata={
"preview": MetadataValue.md(md_content)
}
)
```
5. Reload your code location to see the new `airport_trips` asset within the `metrics` group. Notice how the asset graph links the dependency between the `location_metrics` dbt asset and the new `airport_trips` chart asset:
Expand Down

0 comments on commit 0c51b69

Please sign in to comment.