New notification stats table

- to capture the counts of things that we do
- initial commit captures when we create an email or sms

DOES NOT know about ultimate success only that we asked our partners to ship the notification

Requires some updates when we retry sending in event of error.
This commit is contained in:
Martyn Inglis
2016-03-08 15:23:19 +00:00
parent e99331315e
commit f5f50e00ff
7 changed files with 389 additions and 31 deletions

View File

@@ -1,16 +1,53 @@
from flask import current_app
from app import db
from app.models import Notification, Job
from app.models import Notification, Job, ServiceNotificationStats, TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL
from sqlalchemy import desc
from datetime import datetime
def dao_create_notification(notification):
if notification.job_id:
db.session.query(Job).filter_by(
id=notification.job_id
).update({Job.notifications_sent: Job.notifications_sent + 1})
db.session.add(notification)
db.session.commit()
def dao_create_notification(notification, notification_type):
try:
if notification.job_id:
update_job_sent_count(notification)
day = datetime.utcnow().strftime('%Y-%m-%d')
if notification_type == TEMPLATE_TYPE_SMS:
update = {
ServiceNotificationStats.sms_requested: ServiceNotificationStats.sms_requested + 1
}
else:
update = {
ServiceNotificationStats.emails_requested: ServiceNotificationStats.emails_requested + 1
}
result = db.session.query(ServiceNotificationStats).filter_by(
day=day,
service_id=notification.service_id
).update(update)
if result == 0:
stats = ServiceNotificationStats(
day=day,
service_id=notification.service_id,
sms_requested=1 if notification_type == TEMPLATE_TYPE_SMS else 0,
emails_requested=1 if notification_type == TEMPLATE_TYPE_EMAIL else 0
)
db.session.add(stats)
db.session.add(notification)
db.session.commit()
except:
db.session.rollback()
raise
def update_job_sent_count(notification):
db.session.query(Job).filter_by(
id=notification.job_id
).update({
Job.notifications_sent: Job.notifications_sent + 1,
Job.updated_at: datetime.utcnow()
})
def dao_update_notification(notification):