-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ec6e61
commit fa0b371
Showing
6 changed files
with
131 additions
and
27 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
analytics_platform_dagster/assets/environment_data_assets/green_belt.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,83 @@ | ||
import requests | ||
import pyarrow as pa | ||
import pandas as pd | ||
import io | ||
|
||
from typing import List, Dict, Any | ||
from pydantic import ValidationError | ||
from ...utils.variables_helper.url_links import asset_urls | ||
from dagster import AssetExecutionContext, AssetIn, asset | ||
from ...utils.slack_messages.slack_message import with_slack_notification | ||
from ...models.environment_data_models.green_belt_model import ( | ||
GreenBeltResponse, | ||
) | ||
|
||
@asset( | ||
group_name="environment_data", | ||
io_manager_key="S3Parquet" | ||
) | ||
def green_belt_bronze(context: AssetExecutionContext): | ||
""" | ||
Write green belt data out to raw staging area | ||
Returns: | ||
Parquet in S3. | ||
""" | ||
url = asset_urls.get("green_belt") | ||
|
||
if url is None: | ||
raise ValueError("No url!") | ||
|
||
validation_errors = [] | ||
try: | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
data = response.json() | ||
try: | ||
GreenBeltResponse.model_validate(data) | ||
except ValidationError as e: | ||
validation_errors = e.errors() | ||
|
||
df = pd.DataFrame(data) | ||
df = df.astype(str) | ||
context.log.info(f"Processed {len(df)} records with {len(validation_errors)} validation errors") | ||
|
||
parquet_buffer = io.BytesIO() | ||
df.to_parquet(parquet_buffer, engine="pyarrow") | ||
parquet_bytes = parquet_buffer.getvalue() | ||
|
||
context.log.info("Successfully processed batch into Parquet format") | ||
return parquet_bytes | ||
|
||
except Exception as e: | ||
context.log.error(f"Error processing data: {str(e)}") | ||
raise e | ||
|
||
@asset( | ||
group_name="environment_data", | ||
io_manager_key="DeltaLake", | ||
metadata={"mode": "overwrite"}, | ||
ins={ | ||
"green_belt_bronze": AssetIn( | ||
"green_belt_bronze" | ||
) | ||
}, | ||
required_resource_keys={"slack"} | ||
) | ||
@with_slack_notification("GB Green Belt") | ||
def green_belt_silver( | ||
context: AssetExecutionContext, green_belt_bronze | ||
) -> pd.DataFrame: | ||
""" | ||
Write green belt data out to Delta Lake | ||
Returns: | ||
Delta Lake table in S3. | ||
""" | ||
try: | ||
df = pd.DataFrame(green_belt_bronze) | ||
return df | ||
|
||
except Exception as e: | ||
context.log.error(f"Error processing data: {e}") | ||
raise |
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
24 changes: 24 additions & 0 deletions
24
analytics_platform_dagster/models/environment_data_models/green_belt_model.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,24 @@ | ||
from pydantic import BaseModel, Field | ||
from typing import Optional, List | ||
|
||
class GreenBeltEntity(BaseModel): | ||
dataset: Optional[str] = None | ||
end_date: Optional[str] = Field(None, alias='end-date') | ||
entity: Optional[str] = None | ||
entry_date: Optional[str] = Field(None, alias='entry-date') | ||
geometry: Optional[str] = None | ||
green_belt_core: Optional[str] = Field(None, alias='green-belt-core') | ||
local_authority_district: Optional[str] = Field(None, alias='local-authority-district') | ||
name: Optional[str] = None | ||
organisation_entity: Optional[str] = Field(None, alias='organisation-entity') | ||
point: Optional[str] = None | ||
prefix: Optional[str] = None | ||
reference: Optional[str] = None | ||
start_date: Optional[str] = Field(None, alias='start-date') | ||
typology: Optional[str] = None | ||
|
||
class GreenBeltResponse(BaseModel): | ||
entities: List[GreenBeltEntity] | ||
|
||
class Config: | ||
allow_populate_by_name = True |
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