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

Week 2 Project Submission #35

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
27 changes: 20 additions & 7 deletions week_1/project/week_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from typing import List

from dagster import In, Nothing, Out, job, op, usable_as_dagster_type
from dagster import In, Nothing, Out, job, op, usable_as_dagster_type, get_dagster_logger
from pydantic import BaseModel


Expand Down Expand Up @@ -50,16 +50,29 @@ def get_s3_data(context):
return output


@op
def process_data():
pass
@op(
ins={"stocks": In(dagster_type=List[Stock])},
out={"agg_max": Out(dagster_type=Aggregation)},
description="Determine the stock with the highest value"
)
def process_data(stocks: List[Stock]) -> Aggregation:
# Find the stock with the highest value
max_stock = max(stocks, key=lambda x: x.high)
return Aggregation(date=max_stock.date, high=max_stock.high)


@op
def put_redis_data():
@op(
ins={"agg_max": In(dagster_type=Aggregation)},
out=None,
description="Write to Redis"
)
def put_redis_data(agg_max: Aggregation) -> None:
# Log the output
logger = get_dagster_logger()
logger.info(f"Write {agg_max} to Redis.")
pass


@job
def week_1_pipeline():
pass
put_redis_data(process_data(get_s3_data()))
45 changes: 33 additions & 12 deletions week_2/dagster_ucr/project/week_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,47 @@
from dagster_ucr.resources import mock_s3_resource, redis_resource, s3_resource


@op
def get_s3_data():
pass
@op(
config_schema={"s3_key": str},
required_resource_keys={"s3"},
out={"stocks": Out(dagster_type=List[Stock])},
tags={"kind": "s3"},
description="Get a list of stocks from an S3 file",
)
def get_s3_data(context) -> List[Stock]:
stocks = context.resources.s3.get_data(context.op_config["s3_key"])
return [Stock.from_list(stock) for stock in stocks]


@op
def process_data():
# Use your op from week 1
pass
@op(
ins={"stocks": In(dagster_type=List[Stock])},
out={"agg_max": Out(dagster_type=Aggregation)},
tags={"kind": "transformation"},
description="Determine the stock with the highest value"
)
def process_data(stocks: List[Stock]) -> Aggregation:
# Find the stock with the highest value
max_stock = max(stocks, key=lambda x: x.high)
return Aggregation(date=max_stock.date, high=max_stock.high)


@op
def put_redis_data():
pass
@op(
ins={"agg_max": In(dagster_type=Aggregation)},
required_resource_keys={"redis"},
out=Out(Nothing),
tags={"kind": "redis"},
description="Write to Redis"
)
def put_redis_data(context, agg_max: Aggregation) -> None:
data = context.resources.redis.put_data(str(agg_max.date), str(agg_max.high))
context.log.info(f"Write {data} to Redis.")


@graph
def week_2_pipeline():
# Use your graph from week 1
pass
s3_data = get_s3_data()
highest_stock = process_data(s3_data)
put_redis_data(highest_stock)


local = {
Expand Down
33 changes: 27 additions & 6 deletions week_2/dagster_ucr/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,34 @@ def mock_s3_resource():
return s3_mock


@resource
def s3_resource():
@resource(
config_schema={
"bucket": Field(String),
"access_key": Field(String),
"secret_key": Field(String),
"endpoint_url": Field(String),
},
description="A resource that can connect to S3."
)
def s3_resource(context) -> S3:
"""This resource defines a S3 client"""
pass
return S3(
bucket=context.resource_config["bucket"],
access_key=context.resource_config["access_key"],
secret_key=context.resource_config["secret_key"],
endpoint_url=context.resource_config["endpoint_url"],
)


@resource
def redis_resource():
@resource(
config_schema={
"host": Field(String),
"port": Field(Int),
}
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add here a description as well, just mentioning it because I saw everywhere else descriptions :)

def redis_resource(context) -> Redis:
"""This resource defines a Redis client"""
pass
return Redis(
host=context.resource_config["host"],
port=context.resource_config["port"],
)