mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Removed contented updates the notifications stats table
- As before this is now driven from the notifications history table - Removed from updates and create - Signatures changes to removed unused params hits many files - Also potential issue around rate limiting - we used to get the number sent per day from the stats table - which was a single row lookup, now we have to count this. This applies to EVERY API CALL. Probably not a good thing and should be addressed urgently.
This commit is contained in:
@@ -17,14 +17,8 @@ from app.models import (
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
NotificationStatistics,
|
||||
SMS_TYPE,
|
||||
EMAIL_TYPE,
|
||||
Template)
|
||||
from app.clients import (
|
||||
STATISTICS_FAILURE,
|
||||
STATISTICS_DELIVERED,
|
||||
STATISTICS_REQUESTED
|
||||
)
|
||||
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
@@ -120,21 +114,7 @@ def dao_get_last_template_usage(template_id):
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def dao_create_notification(notification, notification_type):
|
||||
update_count = db.session.query(NotificationStatistics).filter_by(
|
||||
day=notification.created_at.date(),
|
||||
service_id=notification.service_id
|
||||
).update(_update_notification_stats_query(notification_type, 'requested'))
|
||||
|
||||
if update_count == 0:
|
||||
stats = NotificationStatistics(
|
||||
day=notification.created_at.date(),
|
||||
service_id=notification.service_id,
|
||||
sms_requested=1 if notification_type == SMS_TYPE else 0,
|
||||
emails_requested=1 if notification_type == EMAIL_TYPE else 0
|
||||
)
|
||||
db.session.add(stats)
|
||||
|
||||
def dao_create_notification(notification):
|
||||
if not notification.id:
|
||||
# need to populate defaulted fields before we create the notification history object
|
||||
notification.id = uuid.uuid4()
|
||||
@@ -142,38 +122,10 @@ def dao_create_notification(notification, notification_type):
|
||||
notification.status = 'created'
|
||||
|
||||
notification_history = NotificationHistory.from_notification(notification)
|
||||
|
||||
db.session.add(notification)
|
||||
db.session.add(notification_history)
|
||||
|
||||
|
||||
def _update_notification_stats_query(notification_type, status):
|
||||
mapping = {
|
||||
SMS_TYPE: {
|
||||
STATISTICS_REQUESTED: NotificationStatistics.sms_requested,
|
||||
STATISTICS_DELIVERED: NotificationStatistics.sms_delivered,
|
||||
STATISTICS_FAILURE: NotificationStatistics.sms_failed
|
||||
},
|
||||
EMAIL_TYPE: {
|
||||
STATISTICS_REQUESTED: NotificationStatistics.emails_requested,
|
||||
STATISTICS_DELIVERED: NotificationStatistics.emails_delivered,
|
||||
STATISTICS_FAILURE: NotificationStatistics.emails_failed
|
||||
}
|
||||
}
|
||||
return {
|
||||
mapping[notification_type][status]: mapping[notification_type][status] + 1
|
||||
}
|
||||
|
||||
|
||||
def _update_statistics(notification, notification_statistics_status):
|
||||
db.session.query(NotificationStatistics).filter_by(
|
||||
day=notification.created_at.date(),
|
||||
service_id=notification.service_id
|
||||
).update(
|
||||
_update_notification_stats_query(notification.notification_type, notification_statistics_status)
|
||||
)
|
||||
|
||||
|
||||
def _decide_permanent_temporary_failure(current_status, status):
|
||||
# Firetext will send pending, then send either succes or fail.
|
||||
# If we go from pending to delivered we need to set failure type as temporary-failure
|
||||
@@ -183,12 +135,8 @@ def _decide_permanent_temporary_failure(current_status, status):
|
||||
return status
|
||||
|
||||
|
||||
def _update_notification_status(notification, status, notification_statistics_status):
|
||||
def _update_notification_status(notification, status):
|
||||
status = _decide_permanent_temporary_failure(current_status=notification.status, status=status)
|
||||
|
||||
if notification_statistics_status:
|
||||
_update_statistics(notification, notification_statistics_status)
|
||||
|
||||
notification.status = status
|
||||
dao_update_notification(notification)
|
||||
return True
|
||||
@@ -196,7 +144,7 @@ def _update_notification_status(notification, status, notification_statistics_st
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def update_notification_status_by_id(notification_id, status, notification_statistics_status=None):
|
||||
def update_notification_status_by_id(notification_id, status):
|
||||
notification = Notification.query.with_lockmode("update").filter(
|
||||
Notification.id == notification_id,
|
||||
or_(Notification.status == 'created',
|
||||
@@ -208,25 +156,24 @@ def update_notification_status_by_id(notification_id, status, notification_stati
|
||||
|
||||
return _update_notification_status(
|
||||
notification=notification,
|
||||
status=status,
|
||||
notification_statistics_status=notification_statistics_status
|
||||
status=status
|
||||
)
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def update_notification_status_by_reference(reference, status, notification_statistics_status):
|
||||
notification = Notification.query.filter(Notification.reference == reference,
|
||||
or_(Notification.status == 'sending',
|
||||
Notification.status == 'pending')
|
||||
).first()
|
||||
def update_notification_status_by_reference(reference, status):
|
||||
notification = Notification.query.filter(
|
||||
Notification.reference == reference,
|
||||
or_(Notification.status == 'sending',
|
||||
Notification.status == 'pending')).first()
|
||||
|
||||
if not notification:
|
||||
return False
|
||||
|
||||
return _update_notification_status(
|
||||
notification=notification,
|
||||
status=status,
|
||||
notification_statistics_status=notification_statistics_status
|
||||
status=status
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user