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

Feature/upload function #39

Open
wants to merge 6 commits into
base: feature/upload-function
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 10 additions & 5 deletions etl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pathlib import Path
from datetime import timedelta, datetime
from timeit import default_timer as timer
from sqlalchemy.dialects.postgresql import Insert as insert_stmt


BASE_DIR = Path(__file__).resolve().parent
Expand Down Expand Up @@ -238,6 +239,7 @@ def load(stations_dict):
STATIONS_TABLE_NAME, metadata, autoload=True, autoload_with=engine)

with engine.begin() as connection:

print('Inserting data...')

for record in stations_dict:
Expand All @@ -254,20 +256,23 @@ def load(stations_dict):
}

try:
result = connection.execute(db.insert(stations_table), stations_data)

func_insert_stmt = insert_stmt(stations_table).values()
func_prices_insert_stmt = insert_stmt(prices_table).values()
insert_stmt.bind = engine
result = connection.execute(func_insert_stmt.on_conflict_do_update(constraint=stations_table.primary_key, set_=stations_data), stations_data)
pk = result.inserted_primary_key[0]
print(result)

regular_price_data = create_price_data(record, pk, 'regular')
diesel_price_data = create_price_data(record, pk, 'diesel')
premium_price_data = create_price_data(record, pk, 'premium')

if regular_price_data:
connection.execute(db.insert(prices_table), regular_price_data)
connection.execute(func_prices_insert_stmt.on_conflict_do_update(constraint=prices_table.primary_key, set_=regular_price_data), regular_price_data)
if diesel_price_data:
connection.execute(db.insert(prices_table), diesel_price_data)
connection.execute(func_prices_insert_stmt.on_conflict_do_update(constraint=prices_table.primary_key, set_=diesel_price_data), diesel_price_data)
if premium_price_data:
connection.execute(db.insert(prices_table), premium_price_data)
connection.execute(func_prices_insert_stmt.on_conflict_do_update(constraint=prices_table.primary_key, set_=premium_price_data), premium_price_data)
except Exception:
print('A problem ocurred when inserting records')
traceback.print_exc()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ Rtree==0.9.4
SQLAlchemy==1.3.20
urllib3==1.25.11
xmltodict==0.12.0
lxml==4.6.1