-
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.
Update applicant on NOI and Application (#1364)
update audit_created_at on parcel and owner for application and noi so the dates will different from each other and allow sorting update applicant on application_submission update applicant on noi_submission
- Loading branch information
Showing
15 changed files
with
448 additions
and
43 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
109 changes: 109 additions & 0 deletions
109
bin/migrate-oats-data/applications/submissions/application_applicant_on_submissions.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,109 @@ | ||
from common import ( | ||
setup_and_get_logger, | ||
BATCH_UPLOAD_SIZE, | ||
) | ||
from db import inject_conn_pool | ||
from psycopg2.extras import RealDictCursor, execute_batch | ||
|
||
etl_name = "process_application_applicant_on_submissions" | ||
logger = setup_and_get_logger(etl_name) | ||
|
||
|
||
@inject_conn_pool | ||
def process_application_applicant_on_submissions( | ||
conn=None, batch_size=BATCH_UPLOAD_SIZE | ||
): | ||
logger.info(f"Start {etl_name}") | ||
with conn.cursor(cursor_factory=RealDictCursor) as cursor: | ||
with open( | ||
"applications/submissions/sql/applicant/application_applicant_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 update: {count_total}") | ||
|
||
failed_updates_count = 0 | ||
successful_updates_count = 0 | ||
last_updated_id = "00000000-0000-0000-0000-000000000000" | ||
|
||
with open( | ||
"applications/submissions/sql/applicant/application_applicant.sql", | ||
"r", | ||
encoding="utf-8", | ||
) as sql_file: | ||
submission_query = sql_file.read() | ||
while True: | ||
cursor.execute( | ||
f"{submission_query} AND submission_uuid > '{last_updated_id}' ORDER BY submission_uuid;" | ||
) | ||
|
||
rows = cursor.fetchmany(batch_size) | ||
|
||
if not rows: | ||
break | ||
try: | ||
records_to_be_updated_count = len(rows) | ||
|
||
_update_records(conn, batch_size, cursor, rows) | ||
|
||
successful_updates_count = ( | ||
successful_updates_count + records_to_be_updated_count | ||
) | ||
last_updated_id = dict(rows[-1])["submission_uuid"] | ||
|
||
logger.debug( | ||
f"retrieved/updated items count: {records_to_be_updated_count}; total successfully updated applications so far {successful_updates_count}; last updated submission_uuid: {last_updated_id}" | ||
) | ||
except Exception as err: | ||
# this is NOT going to be caused by actual data update failure. This code is only executed when the code error appears or connection to DB is lost | ||
logger.exception() | ||
conn.rollback() | ||
failed_updates_count = count_total - successful_updates_count | ||
last_updated_id = last_updated_id + 1 | ||
|
||
logger.info( | ||
f"Finished {etl_name}: total amount of successful updates {successful_updates_count}, total failed updates {failed_updates_count}" | ||
) | ||
|
||
|
||
def _update_records(conn, batch_size, cursor, rows): | ||
parsed_data_list = _prepare_data(rows) | ||
|
||
if len(rows) > 0: | ||
execute_batch( | ||
cursor, | ||
_update_query, | ||
parsed_data_list, | ||
page_size=batch_size, | ||
) | ||
|
||
conn.commit() | ||
|
||
|
||
def _prepare_data(rows): | ||
data_list = [] | ||
for row in rows: | ||
data_list.append(_map_data(row)) | ||
|
||
return data_list | ||
|
||
|
||
def _map_data(row): | ||
applicant = ( | ||
row.get("last_name") if row.get("last_name") else row.get("organization_name") | ||
) | ||
|
||
if applicant and row.get("owner_count_extension"): | ||
applicant = f"{applicant} {row.get('owner_count_extension')}" | ||
|
||
return {"applicant": applicant, "submission_uuid": row["submission_uuid"]} | ||
|
||
|
||
_update_query = """ | ||
UPDATE alcs.application_submission | ||
SET applicant = %(applicant)s | ||
WHERE "uuid" = %(submission_uuid)s; | ||
""" |
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
43 changes: 43 additions & 0 deletions
43
bin/migrate-oats-data/applications/submissions/sql/applicant/application_applicant.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,43 @@ | ||
WITH ranked_parcels AS ( | ||
SELECT *, | ||
ROW_NUMBER() OVER ( | ||
PARTITION BY application_submission_uuid | ||
ORDER BY audit_created_at | ||
) AS rn | ||
FROM alcs.application_parcel | ||
), | ||
first_parcel_per_submission AS ( | ||
SELECT uuid, | ||
audit_created_at, | ||
application_submission_uuid | ||
FROM ranked_parcels | ||
WHERE rn = 1 | ||
), | ||
ranked_owners AS ( | ||
SELECT *, | ||
ROW_NUMBER() OVER ( | ||
PARTITION BY app_own.application_parcel_uuid | ||
ORDER BY appo.audit_created_at | ||
) AS rn, | ||
fp.application_submission_uuid as submission_uuid | ||
FROM alcs.application_owner appo | ||
JOIN alcs.application_parcel_owners_application_owner app_own ON app_own.application_owner_uuid = appo.uuid | ||
JOIN first_parcel_per_submission AS fp ON fp.uuid = app_own.application_parcel_uuid | ||
JOIN alcs.application_submission nois ON nois.uuid = fp.application_submission_uuid | ||
) | ||
SELECT last_name, | ||
organization_name, | ||
( | ||
CASE | ||
WHEN ( | ||
SELECT count(*) | ||
FROM alcs.application_parcel_owners_application_owner | ||
WHERE application_parcel_uuid = ranked_owners.application_parcel_uuid | ||
) > 1 THEN 'et al.' | ||
ELSE '' | ||
END | ||
) as owner_count_extension, | ||
submission_uuid | ||
FROM ranked_owners | ||
WHERE rn = 1 | ||
AND applicant ILIKE 'unknown' |
31 changes: 31 additions & 0 deletions
31
bin/migrate-oats-data/applications/submissions/sql/applicant/application_applicant_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,31 @@ | ||
WITH ranked_parcels AS ( | ||
SELECT *, | ||
ROW_NUMBER() OVER ( | ||
PARTITION BY application_submission_uuid | ||
ORDER BY audit_created_at | ||
) AS rn | ||
FROM alcs.application_parcel | ||
), | ||
first_parcel_per_submission AS ( | ||
SELECT uuid, | ||
audit_created_at, | ||
application_submission_uuid | ||
FROM ranked_parcels | ||
WHERE rn = 1 | ||
), | ||
ranked_owners AS ( | ||
SELECT *, | ||
ROW_NUMBER() OVER ( | ||
PARTITION BY app_own.application_parcel_uuid | ||
ORDER BY appo.audit_created_at | ||
) AS rn, | ||
fp.application_submission_uuid as submission_uuid | ||
FROM alcs.application_owner appo | ||
JOIN alcs.application_parcel_owners_application_owner app_own ON app_own.application_owner_uuid = appo.uuid | ||
JOIN first_parcel_per_submission AS fp ON fp.uuid = app_own.application_parcel_uuid | ||
JOIN alcs.application_submission nois ON nois.uuid = fp.application_submission_uuid | ||
) | ||
SELECT count(*) | ||
FROM ranked_owners | ||
WHERE rn = 1 | ||
AND applicant ILIKE 'unknown' |
Oops, something went wrong.