-
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.
Updates 2024-07-29 - Added in National Charge Point asset bronze data
- Loading branch information
1 parent
ccc4b0c
commit 173f637
Showing
5 changed files
with
61 additions
and
48 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
58 changes: 21 additions & 37 deletions
58
...ter/assets/infrastructure_data_assets/analytics_platform_national_charge_points_assets.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 |
---|---|---|
@@ -1,41 +1,25 @@ | ||
# from ...utils.requests_helper import return_api_data_json | ||
# from ...utils.io_manager import S3JSONManager | ||
# from ...models.infrastructure_data_models.national_charge_point_model import ChargeDeviceResponse | ||
# from datetime import datetime | ||
# from dagster import asset, AssetExecutionContext, AssetIn | ||
|
||
# API_ENDPOINT = "https://ukpowernetworks.opendatasoft.com/api/explore/v2.1/catalog/datasets/ozev-ukpn-national-chargepoint-register/records?limit=50" | ||
|
||
# def fetch_national_charge_point_data() -> ChargeDeviceResponse: | ||
# url = API_ENDPOINT | ||
# data = return_api_data_json(url) | ||
# return ChargeDeviceResponse.model_validate(data, strict=True) | ||
from typing import Any | ||
from ...utils.requests_helper import stream_json | ||
from ...utils.url_links import asset_urls | ||
# from ...models.infrastructure_data_models.national_charge_point_model import ChargeDeviceResponse | ||
from dagster import AssetExecutionContext, asset | ||
|
||
# @asset( | ||
# group_name="energy_assets", | ||
# io_manager_key="S3Parquet" | ||
# ) | ||
# def national_charge_point_data_bronze(): | ||
# response = fetch_national_charge_point_data() | ||
# return response | ||
DONWLOADLINK = asset_urls.get("national_charge_points") | ||
|
||
def fetch_national_charge_point_data() -> Any: | ||
try: | ||
url = DONWLOADLINK | ||
return stream_json(url, set_chunk=5000) | ||
except Exception as error: | ||
raise error | ||
|
||
# def national_charge_point_data_silver(): | ||
# # Fetch the data | ||
# response = fetch_national_charge_point_data() | ||
|
||
# # Extract the results | ||
# charge_devices = response.results | ||
|
||
# # Convert to a list of dictionaries | ||
# data = [device.model_dump() for device in charge_devices] | ||
|
||
# # Create a DataFrame | ||
# df = pd.DataFrame(data) | ||
|
||
# # Handle the nested geo_point structure | ||
# df['lon'] = df['geo_point'].apply(lambda x: x['lon']) | ||
# df['lat'] = df['geo_point'].apply(lambda x: x['lat']) | ||
# df = df.drop('geo_point', axis=1) | ||
|
||
# return df | ||
@asset( | ||
group_name="infrastructure_assets", | ||
io_manager_key="S3Json" | ||
) | ||
def national_charge_point_data_bronze(context: AssetExecutionContext) -> Any: | ||
context.log.info("Started Processing Data") | ||
response = fetch_national_charge_point_data() | ||
context.log.info("Finished Processing Data") | ||
return response |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
import requests | ||
import json | ||
|
||
def return_api_data_json(url_link: str): | ||
def return_json(url_link: str) -> json: | ||
try: | ||
response = requests.get(url_link) | ||
response.raise_for_status() | ||
data = response.json() | ||
return data | ||
except requests.RequestException as error: | ||
print(f"An error occurred: {error}") | ||
raise | ||
raise | ||
|
||
|
||
def stream_json(url: str, set_chunk: int) -> json: | ||
try: | ||
response = requests.get(url, stream=True) | ||
buffer = '' | ||
for chunk in response.iter_content(set_chunk): | ||
buffer += chunk.decode('utf-8') | ||
return json.loads(buffer) | ||
except requests.RequestException as error: | ||
print(f"An error occurred: {error}") | ||
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