-
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.
Merge branch 'develop' into main-merge
- Loading branch information
Showing
86 changed files
with
1,880 additions
and
473 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
1 change: 1 addition & 0 deletions
1
bin/migrate-oats-data/applications/post_launch/application_components/__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 .boundary_amendments import * |
8 changes: 8 additions & 0 deletions
8
...oats-data/applications/post_launch/application_components/boundary_amendments/__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,8 @@ | ||
from .application_boundary_amendments_insert import ( | ||
clean_boundary_amendments, | ||
insert_application_boundary_amendments, | ||
) | ||
from .application_boundary_amendments_to_components import ( | ||
link_application_boundary_amendments, | ||
clean_linked_boundary_amendments, | ||
) |
130 changes: 130 additions & 0 deletions
130
...unch/application_components/boundary_amendments/application_boundary_amendments_insert.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,130 @@ | ||
from common import ( | ||
setup_and_get_logger, | ||
BATCH_UPLOAD_SIZE, | ||
OATS_ETL_USER, | ||
) | ||
from db import inject_conn_pool | ||
from psycopg2.extras import RealDictCursor | ||
|
||
etl_name = "import_application_boundary_amendments" | ||
logger = setup_and_get_logger(etl_name) | ||
|
||
|
||
@inject_conn_pool | ||
def insert_application_boundary_amendments(conn=None, batch_size=BATCH_UPLOAD_SIZE): | ||
logger.info(f"Start {etl_name}") | ||
with conn.cursor(cursor_factory=RealDictCursor) as cursor: | ||
with open( | ||
"applications/post_launch/sql/application_components/boundary_amendments/application_boundary_amendments_count.sql", | ||
"r", | ||
encoding="utf-8", | ||
) as sql_file: | ||
count_query = sql_file.read() | ||
cursor.execute(count_query) | ||
count_total = dict(cursor.fetchone())["count"] | ||
logger.info(f"Total applications data to insert: {count_total}") | ||
|
||
failed_inserts_count = 0 | ||
successful_inserts_count = 0 | ||
last_component_id = 0 | ||
|
||
with open( | ||
"applications/post_launch/sql/application_components/boundary_amendments/application_boundary_amendments.sql", | ||
"r", | ||
encoding="utf-8", | ||
) as sql_file: | ||
application_sql = sql_file.read() | ||
while True: | ||
cursor.execute( | ||
f"""{application_sql} | ||
AND oaac.alr_appl_component_id > {last_component_id} ORDER BY oaac.alr_appl_component_id;""" | ||
) | ||
|
||
rows = cursor.fetchmany(batch_size) | ||
|
||
if not rows: | ||
break | ||
try: | ||
records_to_be_inserted_count = len(rows) | ||
|
||
_insert_records(conn, cursor, rows) | ||
|
||
successful_inserts_count = ( | ||
successful_inserts_count + records_to_be_inserted_count | ||
) | ||
|
||
last_record = dict(rows[-1]) | ||
last_component_id = last_record["alr_appl_component_id"] | ||
|
||
logger.debug( | ||
f"retrieved/updated items count: {records_to_be_inserted_count}; total successfully insert applications boundary amendments so far {successful_inserts_count}; last updated {last_component_id}" | ||
) | ||
except Exception as err: | ||
logger.exception(err) | ||
conn.rollback() | ||
failed_inserts_count = count_total - successful_inserts_count | ||
last_component_id = last_component_id + 1 | ||
|
||
logger.info( | ||
f"Finished {etl_name}: total amount of successful inserts {successful_inserts_count}, total failed inserts {failed_inserts_count}" | ||
) | ||
|
||
|
||
def _insert_records(conn, cursor, rows): | ||
number_of_rows_to_insert = len(rows) | ||
|
||
if number_of_rows_to_insert > 0: | ||
insert_query = _compile_insert_query(number_of_rows_to_insert) | ||
rows_to_insert = _prepare_data_to_insert(rows) | ||
cursor.execute(insert_query, rows_to_insert) | ||
conn.commit() | ||
|
||
|
||
def _compile_insert_query(number_of_rows_to_insert): | ||
amendments_to_insert = ",".join(["%s"] * number_of_rows_to_insert) | ||
return f""" | ||
INSERT INTO alcs.application_boundary_amendment( | ||
file_number, | ||
type, | ||
area, | ||
year, | ||
period, | ||
oats_component_id, | ||
audit_created_by | ||
) | ||
VALUES{amendments_to_insert} | ||
ON CONFLICT DO NOTHING; | ||
""" | ||
|
||
|
||
def _prepare_data_to_insert(rows): | ||
row_without_last_element = [] | ||
for row in rows: | ||
mapped_row = _map_data(row) | ||
row_without_last_element.append(tuple(mapped_row.values())) | ||
|
||
return row_without_last_element | ||
|
||
|
||
def _map_data(row): | ||
return { | ||
"file_number": row["file_number"], | ||
"type": row["application_decision_component_type_code"], | ||
"area": row["alr_area"], | ||
"year": row["amendment_year"], | ||
"period": row["amendment_period"], | ||
"oats_component_id": row["alr_appl_component_id"], | ||
"audit_created_by": OATS_ETL_USER, | ||
} | ||
|
||
|
||
@inject_conn_pool | ||
def clean_boundary_amendments(conn=None): | ||
logger.info("Start application boundary amendment cleaning") | ||
with conn.cursor() as cursor: | ||
cursor.execute( | ||
f"DELETE FROM alcs.application_boundary_amendment aba WHERE aba.audit_created_by = '{OATS_ETL_USER}' AND aba.audit_updated_by IS NULL" | ||
) | ||
logger.info(f"Deleted items count = {cursor.rowcount}") | ||
conn.commit() | ||
logger.info("Done application boundary amendment cleaning") |
120 changes: 120 additions & 0 deletions
120
...plication_components/boundary_amendments/application_boundary_amendments_to_components.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,120 @@ | ||
from common import ( | ||
setup_and_get_logger, | ||
BATCH_UPLOAD_SIZE, | ||
OATS_ETL_USER, | ||
) | ||
from db import inject_conn_pool | ||
from psycopg2.extras import RealDictCursor | ||
|
||
etl_name = "link_application_boundary_amendments_to_components" | ||
logger = setup_and_get_logger(etl_name) | ||
|
||
|
||
@inject_conn_pool | ||
def link_application_boundary_amendments(conn=None, batch_size=BATCH_UPLOAD_SIZE): | ||
logger.info(f"Start {etl_name}") | ||
with conn.cursor(cursor_factory=RealDictCursor) as cursor: | ||
with open( | ||
"applications/post_launch/sql/application_components/boundary_amendments/link_application_boundary_amendments_to_components_count.sql", | ||
"r", | ||
encoding="utf-8", | ||
) as sql_file: | ||
count_query = sql_file.read() | ||
cursor.execute(count_query) | ||
count_total = dict(cursor.fetchone())["count"] | ||
logger.info(f"Total applications data to insert: {count_total}") | ||
|
||
failed_inserts_count = 0 | ||
successful_inserts_count = 0 | ||
last_component_id = 0 | ||
|
||
with open( | ||
"applications/post_launch/sql/application_components/boundary_amendments/link_application_boundary_amendments_to_components.sql", | ||
"r", | ||
encoding="utf-8", | ||
) as sql_file: | ||
application_sql = sql_file.read() | ||
while True: | ||
cursor.execute( | ||
f"""{application_sql} | ||
WHERE aba.oats_component_id > {last_component_id} ORDER BY aba.oats_component_id;""" | ||
) | ||
|
||
rows = cursor.fetchmany(batch_size) | ||
|
||
if not rows: | ||
break | ||
try: | ||
records_to_be_inserted_count = len(rows) | ||
|
||
_insert_records(conn, cursor, rows) | ||
|
||
successful_inserts_count = ( | ||
successful_inserts_count + records_to_be_inserted_count | ||
) | ||
|
||
last_record = dict(rows[-1]) | ||
last_component_id = last_record["oats_component_id"] | ||
|
||
logger.debug( | ||
f"retrieved/updated items count: {records_to_be_inserted_count}; total successfully insert applications boundary amendments so far {successful_inserts_count}; last updated {last_component_id}" | ||
) | ||
except Exception as err: | ||
logger.exception(err) | ||
conn.rollback() | ||
failed_inserts_count = count_total - successful_inserts_count | ||
last_component_id = last_component_id + 1 | ||
|
||
logger.info( | ||
f"Finished {etl_name}: total amount of successful inserts {successful_inserts_count}, total failed inserts {failed_inserts_count}" | ||
) | ||
|
||
|
||
def _insert_records(conn, cursor, rows): | ||
number_of_rows_to_insert = len(rows) | ||
|
||
if number_of_rows_to_insert > 0: | ||
insert_query = _compile_insert_query(number_of_rows_to_insert) | ||
rows_to_insert = _prepare_data_to_insert(rows) | ||
cursor.execute(insert_query, rows_to_insert) | ||
conn.commit() | ||
|
||
|
||
def _compile_insert_query(number_of_rows_to_insert): | ||
amendments_to_insert = ",".join(["%s"] * number_of_rows_to_insert) | ||
return f""" | ||
INSERT INTO alcs.application_boundary_amendments_to_components( | ||
application_boundary_amendment_uuid, | ||
application_decision_component_uuid | ||
) | ||
VALUES{amendments_to_insert} | ||
ON CONFLICT DO NOTHING; | ||
""" | ||
|
||
|
||
def _prepare_data_to_insert(rows): | ||
row_without_last_element = [] | ||
for row in rows: | ||
mapped_row = _map_data(row) | ||
row_without_last_element.append(tuple(mapped_row.values())) | ||
|
||
return row_without_last_element | ||
|
||
|
||
def _map_data(row): | ||
return { | ||
"application_boundary_amendment_uuid": row["boundary_uuid"], | ||
"application_decision_component_uuid": row["component_uuid"], | ||
} | ||
|
||
|
||
@inject_conn_pool | ||
def clean_linked_boundary_amendments(conn=None): | ||
logger.info("Start application boundary amendment link cleaning") | ||
with conn.cursor() as cursor: | ||
cursor.execute( | ||
f"DELETE FROM alcs.application_boundary_amendments_to_components aba" | ||
) | ||
logger.info(f"Deleted items count = {cursor.rowcount}") | ||
conn.commit() | ||
logger.info("Done application boundary amendment link cleaning") |
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,2 +1,9 @@ | ||
from .application_components.boundary_amendments import ( | ||
clean_boundary_amendments, | ||
clean_linked_boundary_amendments, | ||
) | ||
|
||
|
||
def clean_alcs_applications(): | ||
return | ||
clean_linked_boundary_amendments() | ||
clean_boundary_amendments() |
10 changes: 8 additions & 2 deletions
10
bin/migrate-oats-data/applications/post_launch/migrate_application.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,3 +1,9 @@ | ||
from .application_components.boundary_amendments import ( | ||
insert_application_boundary_amendments, | ||
link_application_boundary_amendments, | ||
) | ||
|
||
|
||
def process_application_etl(batch_size): | ||
# nothing | ||
return | ||
insert_application_boundary_amendments(batch_size) | ||
link_application_boundary_amendments(batch_size) |
14 changes: 14 additions & 0 deletions
14
...launch/sql/application_components/boundary_amendments/application_boundary_amendments.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,14 @@ | ||
SELECT | ||
a.file_number, | ||
adc.alr_area, | ||
adc.application_decision_component_type_code, | ||
oaac.amendment_year, | ||
oaac.amendment_period, | ||
oaac.alr_appl_component_id | ||
FROM | ||
oats.oats_alr_appl_components oaac | ||
JOIN alcs.application_decision_component adc ON oaac.alr_appl_component_id = adc.oats_alr_appl_component_id | ||
JOIN alcs.application_decision ad ON adc.application_decision_uuid = ad."uuid" | ||
JOIN alcs.application a ON ad.application_uuid = a."uuid" | ||
WHERE | ||
oaac.amendment_year IS NOT NULL |
9 changes: 9 additions & 0 deletions
9
.../sql/application_components/boundary_amendments/application_boundary_amendments_count.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,9 @@ | ||
SELECT | ||
count(*) | ||
FROM | ||
oats.oats_alr_appl_components oaac | ||
JOIN alcs.application_decision_component adc ON oaac.alr_appl_component_id = adc.oats_alr_appl_component_id | ||
JOIN alcs.application_decision ad ON adc.application_decision_uuid = ad."uuid" | ||
JOIN alcs.application a ON ad.application_uuid = a."uuid" | ||
WHERE | ||
oaac.amendment_year IS NOT NULL; |
7 changes: 7 additions & 0 deletions
7
...ion_components/boundary_amendments/link_application_boundary_amendments_to_components.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,7 @@ | ||
SELECT | ||
aba."uuid" AS boundary_uuid, | ||
adc."uuid" AS component_uuid, | ||
aba.oats_component_id | ||
FROM | ||
alcs.application_boundary_amendment aba | ||
JOIN alcs.application_decision_component adc ON aba.oats_component_id = adc.oats_alr_appl_component_id |
Oops, something went wrong.