-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/alcs 1130 Notice of intent submission base etl (#1006)
notice of intent submission base import connection.commit fix for clean functions fix for exhausted connection pool
- Loading branch information
Showing
16 changed files
with
219 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
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
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
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
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
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
1 change: 1 addition & 0 deletions
1
bin/migrate-oats-data/noi/notice_of_intent_submissions/__init__.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 @@ | ||
from .notice_of_intent_submission_init import init_notice_of_intent_submissions, clean_notice_of_intent_submissions |
128 changes: 128 additions & 0 deletions
128
bin/migrate-oats-data/noi/notice_of_intent_submissions/notice_of_intent_submission_init.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,128 @@ | ||
from common import log_end, log_start, OATS_ETL_USER | ||
from db import inject_conn_pool | ||
from common import BATCH_UPLOAD_SIZE | ||
from psycopg2.extras import execute_batch, RealDictCursor | ||
import traceback | ||
|
||
etl_name = "init_notice_of_intent_submissions" | ||
|
||
|
||
@inject_conn_pool | ||
def init_notice_of_intent_submissions(conn=None, batch_size=BATCH_UPLOAD_SIZE): | ||
""" | ||
This function is responsible for initializing the notice_of_intent_submission in ALCS. | ||
Args: | ||
conn (psycopg2.extensions.connection): PostgreSQL database connection. Provided by the decorator. | ||
batch_size (int): The number of items to process at once. Defaults to BATCH_UPLOAD_SIZE. | ||
""" | ||
|
||
log_start(etl_name) | ||
with conn.cursor(cursor_factory=RealDictCursor) as cursor: | ||
with open( | ||
"noi/sql/notice_of_intent_submission/notice_of_intent_submission_init_count.sql", | ||
"r", | ||
encoding="utf-8", | ||
) as sql_file: | ||
count_query = sql_file.read() | ||
cursor.execute(count_query) | ||
count_total = dict(cursor.fetchone())["count"] | ||
print("- Total Notice of Intent Submission data to insert: ", count_total) | ||
|
||
failed_inserts = 0 | ||
successful_inserts_count = 0 | ||
last_submission_id = 0 | ||
|
||
with open( | ||
"noi/sql/notice_of_intent_submission/notice_of_intent_submission_init.sql", | ||
"r", | ||
encoding="utf-8", | ||
) as sql_file: | ||
submission_sql = sql_file.read() | ||
while True: | ||
cursor.execute( | ||
f"{submission_sql} WHERE noig.alr_application_id > {last_submission_id} ORDER BY noig.alr_application_id;" | ||
) | ||
|
||
rows = cursor.fetchmany(batch_size) | ||
|
||
if not rows: | ||
break | ||
try: | ||
submissions_to_be_inserted_count = len(rows) | ||
|
||
_insert_notice_of_intent_submissions(conn, batch_size, cursor, rows) | ||
|
||
successful_inserts_count = ( | ||
successful_inserts_count + submissions_to_be_inserted_count | ||
) | ||
last_submission_id = dict(rows[-1])["alr_application_id"] | ||
|
||
print( | ||
f"retrieved/inserted items count: {submissions_to_be_inserted_count}; total successfully inserted submissions so far {successful_inserts_count}; last inserted application_id: {last_submission_id}" | ||
) | ||
except Exception as e: | ||
conn.rollback() | ||
str_err = str(e) | ||
trace_err = traceback.format_exc() | ||
print(str_err) | ||
print(trace_err) | ||
log_end(etl_name, str_err, trace_err) | ||
failed_inserts = count_total - successful_inserts_count | ||
last_submission_id = last_submission_id + 1 | ||
|
||
print("Total amount of successful inserts:", successful_inserts_count) | ||
print("Total failed inserts:", failed_inserts) | ||
log_end(etl_name) | ||
|
||
|
||
def _insert_notice_of_intent_submissions(conn, batch_size, cursor, rows): | ||
""" """ | ||
query = _get_insert_query() | ||
parsed_data_list = _prepare_oats_alr_applications_data(rows) | ||
|
||
if len(parsed_data_list) > 0: | ||
execute_batch(cursor, query, parsed_data_list, page_size=batch_size) | ||
|
||
conn.commit() | ||
|
||
|
||
def _get_insert_query(): | ||
query = f""" | ||
INSERT INTO alcs.notice_of_intent_submission ( | ||
file_number, | ||
local_government_uuid, | ||
type_code, | ||
applicant, | ||
is_draft, | ||
audit_created_by | ||
) | ||
VALUES ( | ||
%(file_number)s, | ||
%(local_government_uuid)s, | ||
%(type_code)s, | ||
%(applicant)s, | ||
false, | ||
'{OATS_ETL_USER}' | ||
) | ||
""" | ||
return query | ||
|
||
|
||
def _prepare_oats_alr_applications_data(row_data_list): | ||
data_list = [] | ||
for row in row_data_list: | ||
data_list.append(dict(row)) | ||
return data_list | ||
|
||
|
||
@inject_conn_pool | ||
def clean_notice_of_intent_submissions(conn=None): | ||
print("Start notice_of_intent_submissions cleaning") | ||
with conn.cursor() as cursor: | ||
cursor.execute( | ||
f"DELETE FROM alcs.notice_of_intent_submission nois WHERE nois.audit_created_by = '{OATS_ETL_USER}'" | ||
) | ||
print(f"Deleted items count = {cursor.rowcount}") | ||
|
||
conn.commit() |
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
19 changes: 19 additions & 0 deletions
19
...igrate-oats-data/noi/sql/notice_of_intent_submission/notice_of_intent_submission_init.sql
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,19 @@ | ||
WITH noi_components_grouped AS ( | ||
SELECT oaac.alr_application_id | ||
FROM oats.oats_alr_appl_components oaac | ||
JOIN oats.oats_alr_applications oaa ON oaa.alr_application_id = oaac.alr_application_id | ||
WHERE oaa.application_class_code = 'NOI' | ||
GROUP BY oaac.alr_application_id | ||
HAVING count(oaac.alr_application_id) < 2 -- ignore notice of intents wit multiple components | ||
) | ||
SELECT noi.file_number, | ||
noi.type_code, | ||
noi.local_government_uuid, | ||
oc.alr_change_code, | ||
noig.alr_application_id, | ||
noi.applicant, | ||
noi.alr_area, | ||
oc.alr_appl_component_id | ||
FROM noi_components_grouped noig | ||
LEFT JOIN alcs.notice_of_intent noi ON noi.file_number = noig.alr_application_id::TEXT | ||
JOIN oats.oats_alr_appl_components oc ON noig.alr_application_id = oc.alr_application_id |
Oops, something went wrong.