-
Notifications
You must be signed in to change notification settings - Fork 8
Mareh A. #6
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
Open
mareh-aboghanem
wants to merge
1
commit into
HackYourAssignment:main
Choose a base branch
from
mareh-aboghanem:week6/mareh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Mareh A. #6
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,3 @@ | ||
| Name StartTime Status | ||
| --------------------------- ------------------------- --------- | ||
| mareh-aboghanem-job-u6ikgff 2026-06-10T20:14:29+00:00 Succeeded |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |
| Database for PostgreSQL. When you finish the assignment it will run as a | ||
| Container App Job triggered from the Azure Portal or the CLI. | ||
|
|
||
| Replace every `raise NotImplementedError` below with a real implementation. | ||
|
|
||
| Reference chapters: | ||
| - Blob upload: Data Track/Week 6/week_6__3_azure_blob_storage.md | ||
|
|
@@ -16,13 +15,18 @@ | |
| import logging | ||
| import os | ||
| from datetime import date | ||
| import sys | ||
| import json | ||
| from contextlib import closing | ||
| import psycopg2 | ||
| from azure.storage.blob import BlobServiceClient | ||
|
|
||
| logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s") | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # TASK 3 hint: quiet the Azure SDK so its DEBUG output does not drown your own | ||
| # pipeline logs. The right call lives in Chapter 5 (Viewing logs). | ||
|
|
||
| logging.getLogger("azure").setLevel(logging.WARNING) | ||
|
|
||
| def get_config() -> dict: | ||
| """Return configuration read from environment variables. | ||
|
|
@@ -37,9 +41,29 @@ def get_config() -> dict: | |
|
|
||
| Raise RuntimeError with a clear message if a required variable is missing. | ||
| """ | ||
| raise NotImplementedError( | ||
| "Task 3: read POSTGRES_URL and AZURE_STORAGE_CONNECTION_STRING from os.environ" | ||
| ) | ||
| conn_postgres=os.environ.get("POSTGRES_URL") | ||
| if not conn_postgres: | ||
| logging.info( | ||
| "POSTGRES_URL is not set.\n" | ||
| "Retrieve it from Key Vault using the CLI, then export it before running:\n\n" | ||
| " export POSTGRES_URL=\"$(az keyvault secret show --vault-name kv-hyf-data --name postgres-url --query value -o tsv)\"\n", | ||
| file=sys.stderr, | ||
| ) | ||
| raise RuntimeError("missing POSTGRES_URL") | ||
| conn=os.environ.get("AZURE_STORAGE_CONNECTION_STRING") | ||
| if not conn: | ||
| logging.info( | ||
| "AZURE_STORAGE_CONNECTION_STRING is not set.\n" | ||
| "Retrieve it from Key Vault using the CLI, then export it before running:\n\n" | ||
| " export AZURE_STORAGE_CONNECTION_STRING=\"$(az keyvault secret show --vault-name kv-hyf-data --name storage-connection-string --query value -o tsv)\"\n", | ||
| file=sys.stderr, | ||
| ) | ||
| raise RuntimeError("missing AZURE_STORAGE_CONNECTION_STRING") | ||
| return { | ||
| "postgres_url": conn_postgres, | ||
| "azure_storage_connection_string": conn, | ||
| "source_name": os.environ.get("SOURCE_NAME", "weather"), | ||
| } | ||
|
|
||
|
|
||
| def fetch_records() -> list[dict]: | ||
|
|
@@ -49,7 +73,13 @@ def fetch_records() -> list[dict]: | |
| one dict with a stable key set (for example: station, timestamp, | ||
| temperature_c, humidity_pct). | ||
| """ | ||
| raise NotImplementedError("Task 3: return a list of at least one record") | ||
| mock_record = { | ||
| "station": "Amsterdam", | ||
| "timestamp": "2024-06-01T12:00:00Z", | ||
| "temperature_c": 20.5, | ||
| "humidity_pct": 60, | ||
| } | ||
| return [mock_record] | ||
|
|
||
|
|
||
| def upload_raw_to_blob(records: list[dict], blob_conn_str: str, source: str) -> str: | ||
|
|
@@ -62,7 +92,12 @@ def upload_raw_to_blob(records: list[dict], blob_conn_str: str, source: str) -> | |
| teacher has pre-created it). Overwrite if the blob already exists so | ||
| same-day reruns succeed. | ||
| """ | ||
| raise NotImplementedError("Task 1 + Task 3: upload records to blob storage") | ||
| blob_service_client = BlobServiceClient.from_connection_string(blob_conn_str) | ||
| container_client = blob_service_client.get_container_client("raw") | ||
| blob_name = f"raw/{source}/{date.today().isoformat()}.json" | ||
| json_data = json.dumps(records) | ||
| container_client.upload_blob(blob_name, data=json_data, overwrite=True) | ||
|
Comment on lines
+97
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path is not being built correctly, raw is being added multiple times |
||
| return blob_name | ||
|
|
||
|
|
||
| def write_to_postgres(records: list[dict], postgres_url: str) -> int: | ||
|
|
@@ -78,7 +113,39 @@ def write_to_postgres(records: list[dict], postgres_url: str) -> int: | |
|
|
||
| See Chapter 4 for the connection-and-cursor pattern this is based on. | ||
| """ | ||
| raise NotImplementedError("Task 2 + Task 3: insert rows into Azure Postgres") | ||
| # its already added sslmode=require to the connection string, so we can just use it as is | ||
| with closing(psycopg2.connect(postgres_url)) as conn: | ||
| # with psycopg2.connect(postgres_url)as conn: | ||
| with conn.cursor() as cur: | ||
| cur.execute("CREATE SCHEMA IF NOT EXISTS dev_mareh;") | ||
| cur.execute("SET search_path TO dev_mareh;") | ||
| cur.execute(""" | ||
| CREATE TABLE IF NOT EXISTS weather_readings ( | ||
| station VARCHAR(50), | ||
| timestamp TIMESTAMPTZ, | ||
| temperature_c FLOAT, | ||
| humidity_pct INT, | ||
| PRIMARY KEY (station, timestamp) | ||
| ); | ||
| """) | ||
| for each_record in records: | ||
| cur.execute( | ||
| """ | ||
| INSERT INTO weather_readings (station, timestamp, temperature_c, humidity_pct) | ||
| VALUES (%s, %s, %s, %s) | ||
| ON CONFLICT (station, timestamp) DO UPDATE | ||
| SET temperature_c = EXCLUDED.temperature_c, | ||
| humidity_pct = EXCLUDED.humidity_pct; | ||
| """, | ||
| ( | ||
| each_record["station"], | ||
| each_record["timestamp"], | ||
| each_record["temperature_c"], | ||
| each_record["humidity_pct"], | ||
| ), | ||
| ) | ||
| conn.commit() | ||
| return len(records) | ||
|
|
||
|
|
||
| def run() -> None: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to use logger.error(...) or print(..., file=sys.stderr), to display errors