mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-08 23:43:12 -04:00
fix core daos
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from sqlalchemy import asc, desc, or_, select, text, union
|
from sqlalchemy import asc, delete, desc, func, or_, select, text, union, update
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
from sqlalchemy.sql import functions
|
from sqlalchemy.sql import functions
|
||||||
@@ -109,11 +109,12 @@ def _update_notification_status(
|
|||||||
def update_notification_status_by_id(
|
def update_notification_status_by_id(
|
||||||
notification_id, status, sent_by=None, provider_response=None, carrier=None
|
notification_id, status, sent_by=None, provider_response=None, carrier=None
|
||||||
):
|
):
|
||||||
notification = (
|
stmt = (
|
||||||
Notification.query.with_for_update()
|
select(Notification)
|
||||||
|
.with_for_update()
|
||||||
.filter(Notification.id == notification_id)
|
.filter(Notification.id == notification_id)
|
||||||
.first()
|
|
||||||
)
|
)
|
||||||
|
notification = db.session.execute(stmt).scalars().first()
|
||||||
|
|
||||||
if not notification:
|
if not notification:
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
@@ -156,9 +157,8 @@ def update_notification_status_by_id(
|
|||||||
@autocommit
|
@autocommit
|
||||||
def update_notification_status_by_reference(reference, status):
|
def update_notification_status_by_reference(reference, status):
|
||||||
# this is used to update emails
|
# this is used to update emails
|
||||||
notification = Notification.query.filter(
|
stmt = select(Notification).filter(Notification.reference == reference)
|
||||||
Notification.reference == reference
|
notification = db.session.execute(stmt).scalars().first()
|
||||||
).first()
|
|
||||||
|
|
||||||
if not notification:
|
if not notification:
|
||||||
current_app.logger.error(
|
current_app.logger.error(
|
||||||
@@ -200,19 +200,20 @@ def get_notifications_for_job(
|
|||||||
|
|
||||||
|
|
||||||
def dao_get_notification_count_for_job_id(*, job_id):
|
def dao_get_notification_count_for_job_id(*, job_id):
|
||||||
return Notification.query.filter_by(job_id=job_id).count()
|
stmt = select(func.count(Notification.id)).filter_by(job_id=job_id)
|
||||||
|
return db.session.execute(stmt).scalar()
|
||||||
|
|
||||||
|
|
||||||
def dao_get_notification_count_for_service(*, service_id):
|
def dao_get_notification_count_for_service(*, service_id):
|
||||||
notification_count = Notification.query.filter_by(service_id=service_id).count()
|
stmt = select(func.count(Notification.id)).filter_by(service_id=service_id)
|
||||||
return notification_count
|
return db.session.execute(stmt).scalar()
|
||||||
|
|
||||||
|
|
||||||
def dao_get_failed_notification_count():
|
def dao_get_failed_notification_count():
|
||||||
failed_count = Notification.query.filter_by(
|
stmt = select(func.count(Notification.id)).filter_by(
|
||||||
status=NotificationStatus.FAILED
|
status=NotificationStatus.FAILED
|
||||||
).count()
|
)
|
||||||
return failed_count
|
return db.session.execute(stmt).scalar()
|
||||||
|
|
||||||
|
|
||||||
def get_notification_with_personalisation(service_id, notification_id, key_type):
|
def get_notification_with_personalisation(service_id, notification_id, key_type):
|
||||||
@@ -220,11 +221,12 @@ def get_notification_with_personalisation(service_id, notification_id, key_type)
|
|||||||
if key_type:
|
if key_type:
|
||||||
filter_dict["key_type"] = key_type
|
filter_dict["key_type"] = key_type
|
||||||
|
|
||||||
return (
|
stmt = (
|
||||||
Notification.query.filter_by(**filter_dict)
|
select(Notification)
|
||||||
|
.filter_by(**filter_dict)
|
||||||
.options(joinedload(Notification.template))
|
.options(joinedload(Notification.template))
|
||||||
.one()
|
|
||||||
)
|
)
|
||||||
|
return db.session.execute(stmt).scalars().one()
|
||||||
|
|
||||||
|
|
||||||
def get_notification_by_id(notification_id, service_id=None, _raise=False):
|
def get_notification_by_id(notification_id, service_id=None, _raise=False):
|
||||||
@@ -233,9 +235,13 @@ def get_notification_by_id(notification_id, service_id=None, _raise=False):
|
|||||||
if service_id:
|
if service_id:
|
||||||
filters.append(Notification.service_id == service_id)
|
filters.append(Notification.service_id == service_id)
|
||||||
|
|
||||||
query = Notification.query.filter(*filters)
|
stmt = select(Notification).filter(*filters)
|
||||||
|
|
||||||
return query.one() if _raise else query.first()
|
return (
|
||||||
|
db.session.execute(stmt).scalars().one()
|
||||||
|
if _raise
|
||||||
|
else db.session.execute(stmt).scalars().first()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_notifications_for_service(
|
def get_notifications_for_service(
|
||||||
@@ -415,12 +421,13 @@ def move_notifications_to_notification_history(
|
|||||||
deleted += delete_count_per_call
|
deleted += delete_count_per_call
|
||||||
|
|
||||||
# Deleting test Notifications, test notifications are not persisted to NotificationHistory
|
# Deleting test Notifications, test notifications are not persisted to NotificationHistory
|
||||||
Notification.query.filter(
|
stmt = delete(Notification).filter(
|
||||||
Notification.notification_type == notification_type,
|
Notification.notification_type == notification_type,
|
||||||
Notification.service_id == service_id,
|
Notification.service_id == service_id,
|
||||||
Notification.created_at < timestamp_to_delete_backwards_from,
|
Notification.created_at < timestamp_to_delete_backwards_from,
|
||||||
Notification.key_type == KeyType.TEST,
|
Notification.key_type == KeyType.TEST,
|
||||||
).delete(synchronize_session=False)
|
)
|
||||||
|
db.session.execute(stmt)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return deleted
|
return deleted
|
||||||
@@ -442,8 +449,9 @@ def dao_timeout_notifications(cutoff_time, limit=100000):
|
|||||||
current_statuses = [NotificationStatus.SENDING, NotificationStatus.PENDING]
|
current_statuses = [NotificationStatus.SENDING, NotificationStatus.PENDING]
|
||||||
new_status = NotificationStatus.TEMPORARY_FAILURE
|
new_status = NotificationStatus.TEMPORARY_FAILURE
|
||||||
|
|
||||||
notifications = (
|
stmt = (
|
||||||
Notification.query.filter(
|
select(Notification)
|
||||||
|
.filter(
|
||||||
Notification.created_at < cutoff_time,
|
Notification.created_at < cutoff_time,
|
||||||
Notification.status.in_(current_statuses),
|
Notification.status.in_(current_statuses),
|
||||||
Notification.notification_type.in_(
|
Notification.notification_type.in_(
|
||||||
@@ -451,14 +459,17 @@ def dao_timeout_notifications(cutoff_time, limit=100000):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.all()
|
|
||||||
)
|
)
|
||||||
|
notifications = db.session.execute(stmt).scalars().all()
|
||||||
|
|
||||||
Notification.query.filter(
|
stmt = (
|
||||||
Notification.id.in_([n.id for n in notifications]),
|
update(Notification)
|
||||||
).update(
|
.filter(Notification.id.in_([n.id for n in notifications]))
|
||||||
{"status": new_status, "updated_at": updated_at}, synchronize_session=False
|
.update(
|
||||||
|
{"status": new_status, "updated_at": updated_at}, synchronize_session=False
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
db.session.execute(stmt)
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return notifications
|
return notifications
|
||||||
@@ -466,15 +477,21 @@ def dao_timeout_notifications(cutoff_time, limit=100000):
|
|||||||
|
|
||||||
@autocommit
|
@autocommit
|
||||||
def dao_update_notifications_by_reference(references, update_dict):
|
def dao_update_notifications_by_reference(references, update_dict):
|
||||||
updated_count = Notification.query.filter(
|
stmt = (
|
||||||
Notification.reference.in_(references)
|
update(Notification)
|
||||||
).update(update_dict, synchronize_session=False)
|
.filter(Notification.reference.in_(references))
|
||||||
|
.update(update_dict)
|
||||||
|
)
|
||||||
|
updated_count = db.stmt.execute(stmt)
|
||||||
|
|
||||||
updated_history_count = 0
|
updated_history_count = 0
|
||||||
if updated_count != len(references):
|
if updated_count != len(references):
|
||||||
updated_history_count = NotificationHistory.query.filter(
|
stmt = (
|
||||||
NotificationHistory.reference.in_(references)
|
select(NotificationHistory)
|
||||||
).update(update_dict, synchronize_session=False)
|
.filter(NotificationHistory.reference.in_(references))
|
||||||
|
.update(update_dict, synchronize_session=False)
|
||||||
|
)
|
||||||
|
updated_history_count = db.stmt.execute(stmt)
|
||||||
|
|
||||||
return updated_count, updated_history_count
|
return updated_count, updated_history_count
|
||||||
|
|
||||||
@@ -541,18 +558,21 @@ def dao_get_notifications_by_recipient_or_reference(
|
|||||||
|
|
||||||
|
|
||||||
def dao_get_notification_by_reference(reference):
|
def dao_get_notification_by_reference(reference):
|
||||||
return Notification.query.filter(Notification.reference == reference).one()
|
stmt = select(Notification).filter(Notification.reference == reference)
|
||||||
|
return db.session.execute(stmt).scalars().one()
|
||||||
|
|
||||||
|
|
||||||
def dao_get_notification_history_by_reference(reference):
|
def dao_get_notification_history_by_reference(reference):
|
||||||
try:
|
try:
|
||||||
# This try except is necessary because in test keys and research mode does not create notification history.
|
# This try except is necessary because in test keys and research mode does not create notification history.
|
||||||
# Otherwise we could just search for the NotificationHistory object
|
# Otherwise we could just search for the NotificationHistory object
|
||||||
return Notification.query.filter(Notification.reference == reference).one()
|
stmt = select(Notification).filter(Notification.reference == reference)
|
||||||
|
return db.session.execute(stmt).scalars().one()
|
||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
return NotificationHistory.query.filter(
|
stmt = select(NotificationHistory).filter(
|
||||||
NotificationHistory.reference == reference
|
NotificationHistory.reference == reference
|
||||||
).one()
|
)
|
||||||
|
return db.session.execute(stmt).scalars().one()
|
||||||
|
|
||||||
|
|
||||||
def dao_get_notifications_processing_time_stats(start_date, end_date):
|
def dao_get_notifications_processing_time_stats(start_date, end_date):
|
||||||
@@ -590,11 +610,12 @@ def dao_get_notifications_processing_time_stats(start_date, end_date):
|
|||||||
|
|
||||||
|
|
||||||
def dao_get_last_notification_added_for_job_id(job_id):
|
def dao_get_last_notification_added_for_job_id(job_id):
|
||||||
last_notification_added = (
|
stmt = (
|
||||||
Notification.query.filter(Notification.job_id == job_id)
|
select(Notification)
|
||||||
|
.filter(Notification.job_id == job_id)
|
||||||
.order_by(Notification.job_row_number.desc())
|
.order_by(Notification.job_row_number.desc())
|
||||||
.first()
|
|
||||||
)
|
)
|
||||||
|
last_notification_added = db.session.execute(stmt).scalars().first()
|
||||||
|
|
||||||
return last_notification_added
|
return last_notification_added
|
||||||
|
|
||||||
@@ -602,11 +623,12 @@ def dao_get_last_notification_added_for_job_id(job_id):
|
|||||||
def notifications_not_yet_sent(should_be_sending_after_seconds, notification_type):
|
def notifications_not_yet_sent(should_be_sending_after_seconds, notification_type):
|
||||||
older_than_date = utc_now() - timedelta(seconds=should_be_sending_after_seconds)
|
older_than_date = utc_now() - timedelta(seconds=should_be_sending_after_seconds)
|
||||||
|
|
||||||
notifications = Notification.query.filter(
|
stmt = select(Notification).filter(
|
||||||
Notification.created_at <= older_than_date,
|
Notification.created_at <= older_than_date,
|
||||||
Notification.notification_type == notification_type,
|
Notification.notification_type == notification_type,
|
||||||
Notification.status == NotificationStatus.CREATED,
|
Notification.status == NotificationStatus.CREATED,
|
||||||
).all()
|
)
|
||||||
|
notifications = db.session.execute(stmt).all()
|
||||||
return notifications
|
return notifications
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ def _create_service_invite(invited_user, invite_link_host):
|
|||||||
redis_store.set(
|
redis_store.set(
|
||||||
f"email-personalisation-{saved_notification.id}",
|
f"email-personalisation-{saved_notification.id}",
|
||||||
json.dumps(personalisation),
|
json.dumps(personalisation),
|
||||||
ex=2*24*60*60,
|
ex=2 * 24 * 60 * 60,
|
||||||
)
|
)
|
||||||
send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY)
|
send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user