mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Added DAO methods to add and update the stats table for JOBS
- create_or_update_job_sending_statistics This will try and update an existing row. if this fails as it hasn't been created, then it will insert a row. If this fails as another process has got there first then it should try and update again. This is a code version of upset - update_job_stats_outcome_count Will update the outcome states. Uses the NOTIFICATION_STATUS_TYPES_FAILED to determine if the notification failed. Any thing in DELIVERED will be marked as delivered. Statues not in the FAILED array or delivered provoke no update to the table.
This commit is contained in:
@@ -1,4 +1,79 @@
|
||||
from flask import current_app
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import (
|
||||
JobStatistics,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
LETTER_TYPE,
|
||||
NOTIFICATION_STATUS_TYPES_FAILED,
|
||||
NOTIFICATION_DELIVERED
|
||||
)
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
def persist_initial_job_statistics(notification):
|
||||
pass
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def create_or_update_job_sending_statistics(notification):
|
||||
if __update_job_stats_sent_count(notification) == 0:
|
||||
try:
|
||||
__insert_job_stats(notification)
|
||||
except SQLAlchemyError as e:
|
||||
current_app.logger.exception(e)
|
||||
__update_job_stats_sent_count(notification)
|
||||
|
||||
|
||||
def __update_job_stats_sent_count(notification):
|
||||
update = {
|
||||
JobStatistics.emails_sent:
|
||||
JobStatistics.emails_sent + 1 if notification.notification_type == EMAIL_TYPE else 0,
|
||||
JobStatistics.sms_sent:
|
||||
JobStatistics.sms_sent + 1 if notification.notification_type == SMS_TYPE else 0,
|
||||
JobStatistics.letters_sent:
|
||||
JobStatistics.letters_sent + 1 if notification.notification_type == LETTER_TYPE else 0
|
||||
}
|
||||
return db.session.query(JobStatistics).filter_by(
|
||||
job_id=notification.job_id,
|
||||
).update(update)
|
||||
|
||||
|
||||
def __insert_job_stats(notification):
|
||||
|
||||
stats = JobStatistics(
|
||||
job_id=notification.job_id,
|
||||
emails_sent=1 if notification.notification_type == EMAIL_TYPE else 0,
|
||||
sms_sent=1 if notification.notification_type == SMS_TYPE else 0,
|
||||
letters_sent=1 if notification.notification_type == LETTER_TYPE else 0
|
||||
)
|
||||
db.session.add(stats)
|
||||
|
||||
|
||||
def update_job_stats_outcome_count(notification):
|
||||
update = None
|
||||
|
||||
if notification.status in NOTIFICATION_STATUS_TYPES_FAILED:
|
||||
update = {
|
||||
JobStatistics.emails_failed:
|
||||
JobStatistics.emails_failed + 1 if notification.notification_type == EMAIL_TYPE else 0,
|
||||
JobStatistics.sms_failed:
|
||||
JobStatistics.sms_failed + 1 if notification.notification_type == SMS_TYPE else 0,
|
||||
JobStatistics.letters_failed:
|
||||
JobStatistics.letters_failed + 1 if notification.notification_type == LETTER_TYPE else 0
|
||||
}
|
||||
|
||||
elif notification.status == NOTIFICATION_DELIVERED and notification.notification_type != LETTER_TYPE:
|
||||
update = {
|
||||
JobStatistics.emails_delivered:
|
||||
JobStatistics.emails_delivered + 1 if notification.notification_type == EMAIL_TYPE else 0,
|
||||
JobStatistics.sms_delivered:
|
||||
JobStatistics.sms_delivered + 1 if notification.notification_type == SMS_TYPE else 0
|
||||
}
|
||||
|
||||
if update:
|
||||
return db.session.query(JobStatistics).filter_by(
|
||||
job_id=notification.job_id,
|
||||
).update(update)
|
||||
else:
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user