From 0d8e0c13d0912b5d23e440f78192cbe07a9ef313 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 5 May 2020 11:12:06 +0100 Subject: [PATCH] Iterate day by day --- app/commands.py | 16 ++++++++++------ database_maintenance/README.md | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/commands.py b/app/commands.py index 65a35b85d..c102616f8 100644 --- a/app/commands.py +++ b/app/commands.py @@ -904,9 +904,9 @@ def process_row_from_job(job_id, job_row_number): @notify_command() @click.option('-l', '--limit_row_count', required=True, help='Limit row count for the insert stmt') -@click.option('-s', '--start_date', required=True, help='Start') -@click.option('-e', '--end_date', required=True, help='Limit row count for the insert stmt') -def cycle_notification_history_table(limit_row_count): +@click.option('-s', '--start_date', required=True, help='Start date', type=click_dt(format='%Y-%m-%d %H:%M')) +@click.option('-e', '--end_date', required=True, help='End date', type=click_dt(format='%Y-%m-%d %H:%M')) +def cycle_notification_history_table(limit_row_count, start_date, end_date): # This relies on the notification_history_pivot table being created # and a trigger on notification_history has been created # what limit should we use here @@ -914,7 +914,9 @@ def cycle_notification_history_table(limit_row_count): # If this command needs to be run more than once you will need to drop nh_temp. # Especially if the nightly task to delete notifications has run (more data has been added to notification_history) populate_temp_table = """ - CREATE TABLE IF NOT EXISTS nh_temp AS SELECT id FROM notification_history + CREATE TABLE IF NOT EXISTS nh_temp AS SELECT id FROM notification_history + where created_at >= :start_date + and created_at < :end_date """ index_temp_table = """ CREATE INDEX IF NOT EXISTS nh_temp_idx ON nh_temp (id) @@ -939,8 +941,10 @@ def cycle_notification_history_table(limit_row_count): WHERE n.id = t.id limit :limit_row_count """ - print("Starting cycle notification history: ", datetime.utcnow()) - db.session.execute(populate_temp_table) + print(f"Starting cycle notification history for start_date: {start_date} and " + f"end_date {end_date} and limit: {limit_row_count}") + + db.session.execute(populate_temp_table, {"start_date": start_date, "end_date": end_date}) db.session.execute(delete_temp_rows) db.session.execute(index_temp_table) rows_remaining = db.session.execute(rows_in_temp).fetchall()[0][0] diff --git a/database_maintenance/README.md b/database_maintenance/README.md index 5421bbeb5..e4798f352 100644 --- a/database_maintenance/README.md +++ b/database_maintenance/README.md @@ -17,4 +17,5 @@ cf v3-create-app notify-cycle-history cf v3-apply-manifest -f cycle-history-manifest.yml cf v3-push notify-cycle-history -cf run-task notify-cycle-history "flask command cycle-notification-history-table -l 100000" \ No newline at end of file +cf run-task notify-cycle-history "flask command cycle-notification-history-table -l 100000 -s '2020-03-18 00:00' -e '2020-03-19 00:00" + \ No newline at end of file