mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Rewrite task to aggregate status by service
This is a step towards parallelising the task by service and day.
This commit is contained in:
@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
|||||||
from flask import current_app
|
from flask import current_app
|
||||||
from notifications_utils.timezones import convert_utc_to_bst
|
from notifications_utils.timezones import convert_utc_to_bst
|
||||||
|
|
||||||
from app import notify_celery
|
from app import db, notify_celery
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.cronitor import cronitor
|
from app.cronitor import cronitor
|
||||||
from app.dao.fact_billing_dao import (
|
from app.dao.fact_billing_dao import (
|
||||||
@@ -11,10 +11,10 @@ from app.dao.fact_billing_dao import (
|
|||||||
update_fact_billing,
|
update_fact_billing,
|
||||||
)
|
)
|
||||||
from app.dao.fact_notification_status_dao import (
|
from app.dao.fact_notification_status_dao import (
|
||||||
fetch_notification_status_for_day,
|
fetch_status_data_for_service_and_day,
|
||||||
update_fact_notification_status,
|
update_fact_notification_status,
|
||||||
)
|
)
|
||||||
from app.models import EMAIL_TYPE, LETTER_TYPE, SMS_TYPE
|
from app.models import EMAIL_TYPE, LETTER_TYPE, SMS_TYPE, Service
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name="create-nightly-billing")
|
@notify_celery.task(name="create-nightly-billing")
|
||||||
@@ -124,15 +124,26 @@ def create_nightly_notification_status_for_day(process_day, notification_type):
|
|||||||
f'create-nightly-notification-status-for-day task for {process_day} type {notification_type}: started'
|
f'create-nightly-notification-status-for-day task for {process_day} type {notification_type}: started'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
for (service_id,) in db.session.query(Service.id):
|
||||||
start = datetime.utcnow()
|
start = datetime.utcnow()
|
||||||
transit_data = fetch_notification_status_for_day(process_day=process_day, notification_type=notification_type)
|
transit_data = fetch_status_data_for_service_and_day(
|
||||||
|
process_day=process_day,
|
||||||
|
notification_type=notification_type,
|
||||||
|
service_id=service_id,
|
||||||
|
)
|
||||||
|
|
||||||
end = datetime.utcnow()
|
end = datetime.utcnow()
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
f'create-nightly-notification-status-for-day task for {process_day} type {notification_type}: '
|
f'create-nightly-notification-status-for-day task for {process_day} type {notification_type}: '
|
||||||
f'data fetched in {(end - start).seconds} seconds'
|
f'data fetched in {(end - start).seconds} seconds'
|
||||||
)
|
)
|
||||||
|
|
||||||
update_fact_notification_status(transit_data, process_day, notification_type)
|
update_fact_notification_status(
|
||||||
|
transit_data=transit_data,
|
||||||
|
process_day=process_day,
|
||||||
|
notification_type=notification_type,
|
||||||
|
service_id=service_id
|
||||||
|
)
|
||||||
|
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
f'create-nightly-notification-status-for-day task for {process_day} type {notification_type}: '
|
f'create-nightly-notification-status-for-day task for {process_day} type {notification_type}: '
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from datetime import datetime, time, timedelta
|
from datetime import datetime, time, timedelta
|
||||||
|
|
||||||
from flask import current_app
|
|
||||||
from notifications_utils.timezones import convert_bst_to_utc
|
from notifications_utils.timezones import convert_bst_to_utc
|
||||||
from sqlalchemy import Date, case, func
|
from sqlalchemy import Date, case, func
|
||||||
from sqlalchemy.dialects.postgresql import insert
|
from sqlalchemy.dialects.postgresql import insert
|
||||||
@@ -36,19 +35,15 @@ from app.utils import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def fetch_notification_status_for_day(process_day, notification_type):
|
def fetch_status_data_for_service_and_day(process_day, service_id, notification_type):
|
||||||
start_date = convert_bst_to_utc(datetime.combine(process_day, time.min))
|
start_date = convert_bst_to_utc(datetime.combine(process_day, time.min))
|
||||||
end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min))
|
end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min))
|
||||||
|
|
||||||
current_app.logger.info("Fetch ft_notification_status for {} to {}".format(start_date, end_date))
|
# query notifications or notification_history for the day, depending on their data retention
|
||||||
|
service = Service.query.get(service_id)
|
||||||
all_data_for_process_day = []
|
|
||||||
services = Service.query.all()
|
|
||||||
# for each service query notifications or notification_history for the day, depending on their data retention
|
|
||||||
for service in services:
|
|
||||||
table = get_notification_table_to_use(service, notification_type, process_day, has_delete_task_run=False)
|
table = get_notification_table_to_use(service, notification_type, process_day, has_delete_task_run=False)
|
||||||
|
|
||||||
data_for_service_and_type = query_for_fact_status_data(
|
return query_for_fact_status_data(
|
||||||
table=table,
|
table=table,
|
||||||
start_date=start_date,
|
start_date=start_date,
|
||||||
end_date=end_date,
|
end_date=end_date,
|
||||||
@@ -56,10 +51,6 @@ def fetch_notification_status_for_day(process_day, notification_type):
|
|||||||
service_id=service.id
|
service_id=service.id
|
||||||
)
|
)
|
||||||
|
|
||||||
all_data_for_process_day += data_for_service_and_type
|
|
||||||
|
|
||||||
return all_data_for_process_day
|
|
||||||
|
|
||||||
|
|
||||||
def query_for_fact_status_data(table, start_date, end_date, notification_type, service_id):
|
def query_for_fact_status_data(table, start_date, end_date, notification_type, service_id):
|
||||||
query = db.session.query(
|
query = db.session.query(
|
||||||
@@ -86,18 +77,19 @@ def query_for_fact_status_data(table, start_date, end_date, notification_type, s
|
|||||||
|
|
||||||
|
|
||||||
@autocommit
|
@autocommit
|
||||||
def update_fact_notification_status(data, process_day, notification_type):
|
def update_fact_notification_status(transit_data, process_day, notification_type, service_id):
|
||||||
table = FactNotificationStatus.__table__
|
table = FactNotificationStatus.__table__
|
||||||
FactNotificationStatus.query.filter(
|
FactNotificationStatus.query.filter(
|
||||||
FactNotificationStatus.bst_date == process_day,
|
FactNotificationStatus.bst_date == process_day,
|
||||||
FactNotificationStatus.notification_type == notification_type
|
FactNotificationStatus.notification_type == notification_type,
|
||||||
|
FactNotificationStatus.service_id == service_id,
|
||||||
).delete()
|
).delete()
|
||||||
|
|
||||||
for row in data:
|
for row in transit_data:
|
||||||
stmt = insert(table).values(
|
stmt = insert(table).values(
|
||||||
bst_date=process_day,
|
bst_date=process_day,
|
||||||
template_id=row.template_id,
|
template_id=row.template_id,
|
||||||
service_id=row.service_id,
|
service_id=service_id,
|
||||||
job_id=row.job_id,
|
job_id=row.job_id,
|
||||||
notification_type=notification_type,
|
notification_type=notification_type,
|
||||||
key_type=row.key_type,
|
key_type=row.key_type,
|
||||||
|
|||||||
Reference in New Issue
Block a user