mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-05 15:48:25 -04:00
[WIP]
This commit is contained in:
@@ -907,6 +907,17 @@ def process_row_from_job(job_id, job_row_number):
|
|||||||
@click.option('-s', '--start_date', required=True, help='Start date', type=click_dt(format='%Y-%m-%d %H:%M'))
|
@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'))
|
@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):
|
def cycle_notification_history_table(limit_row_count, start_date, end_date):
|
||||||
|
print(f"Starting cycle_notification_history_table for {start_date} to {end_date}")
|
||||||
|
day_start = start_date
|
||||||
|
day_end = start_date + timedelta(days=1)
|
||||||
|
|
||||||
|
while day_end < end_date:
|
||||||
|
populate_notification_history_pivot(limit_row_count, day_start, day_end)
|
||||||
|
day_start = day_start + timedelta(days=1)
|
||||||
|
day_end = day_end + timedelta(days=1)
|
||||||
|
|
||||||
|
|
||||||
|
def populate_notification_history_pivot(limit_row_count, start_date, end_date):
|
||||||
# This relies on the notification_history_pivot table being created
|
# This relies on the notification_history_pivot table being created
|
||||||
# and a trigger on notification_history has been created
|
# and a trigger on notification_history has been created
|
||||||
# what limit should we use here
|
# what limit should we use here
|
||||||
|
|||||||
@@ -19,3 +19,19 @@ cf v3-push notify-cycle-history
|
|||||||
|
|
||||||
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"
|
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"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- Deploy new command - cycle_notification_history_table to prod
|
||||||
|
- script_1.sql
|
||||||
|
- create nh_pivot
|
||||||
|
- create nh trigger
|
||||||
|
- create new index on nh
|
||||||
|
- foreign_keys.sql
|
||||||
|
- run command for each day in nh --> start October 1, 2019
|
||||||
|
-- all data in NH
|
||||||
|
-
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
6
database_maintenance/foreign_keys.sql
Normal file
6
database_maintenance/foreign_keys.sql
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
ALTER TABLE notification_history_pivot ADD CONSTRAINT fk_notification_history_notification_status FOREIGN KEY (notification_status) REFERENCES notification_status_types(name)
|
||||||
|
ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_api_key_id_fkey FOREIGN KEY (api_key_id) REFERENCES api_keys(id)
|
||||||
|
ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_job_id_fkey FOREIGN KEY (job_id) REFERENCES jobs(id)
|
||||||
|
ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_key_type_fkey FOREIGN KEY (key_type) REFERENCES key_types(name)
|
||||||
|
ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_service_id_fkey FOREIGN KEY (service_id) REFERENCES services(id)
|
||||||
|
ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_templates_history_fkey FOREIGN KEY (template_id, template_version) REFERENCES templates_history(id, version)
|
||||||
26
database_maintenance/script_1.sql
Normal file
26
database_maintenance/script_1.sql
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
CREATE TABLE notification_history_pivot AS SELECT * FROM notification_history WHERE 1=2;
|
||||||
|
ALTER TABLE notification_history_pivot ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
|
-- Update values of notification_status, billable_units, updated_at, sent_by, sent_at based on new updates coming into the notification_history table.
|
||||||
|
CREATE OR REPLACE FUNCTION update_pivot_table()
|
||||||
|
RETURNS TRIGGER
|
||||||
|
LANGUAGE plpgsql AS
|
||||||
|
$$
|
||||||
|
BEGIN
|
||||||
|
UPDATE notification_history_pivot SET notification_status = NEW.notification_status,
|
||||||
|
billable_units = NEW.billable_units,
|
||||||
|
updated_at = NEW.updated_at,
|
||||||
|
sent_by = NEW.sent_by,
|
||||||
|
sent_at = NEW.sent_at
|
||||||
|
WHERE notification_history_pivot.id = NEW.id;
|
||||||
|
|
||||||
|
RETURN NEW;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
|
||||||
|
DROP TRIGGER IF EXISTS update_pivot on notification_history;
|
||||||
|
CREATE TRIGGER update_pivot AFTER UPDATE OF notification_status, billable_units, updated_at, sent_by, sent_at ON notification_history
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE PROCEDURE update_pivot_table();
|
||||||
|
|
||||||
|
create index concurrently created_id_nh on notification_history (created_at, id);
|
||||||
Reference in New Issue
Block a user