Merge pull request #516 from alphagov/fix-counts

Fix update script
This commit is contained in:
Rebecca Law
2016-07-06 16:54:07 +01:00
committed by GitHub

View File

@@ -17,26 +17,37 @@ import sqlalchemy as sa
def upgrade(): def upgrade():
op.execute('update notifications set notification_type = (select cast(cast(template_type as text) as notification_type) from templates where templates.id= notifications.template_id)') op.execute('update notifications set notification_type = (select cast(cast(template_type as text) as notification_type) from templates where templates.id= notifications.template_id)')
conn = op.get_bind() conn = op.get_bind()
del_sql = "select n.id, results.* from (select 'failed' as stat_type, count(status) as count, notification_type, date(created_at) as day, service_id " \ reset_counts = "update notification_statistics set emails_requested = 0, emails_delivered = 0, emails_failed=0," \
"from notifications where status in ('temporary-failure', 'permanent-failure', 'technical-failure') group by service_id, notification_type, date(created_at) " \ "sms_requested = 0, sms_delivered = 0, sms_failed=0 where day > '2016-06-30'"
"union select 'delivered' as stat_type, count(status) , notification_type, date(created_at) as day, service_id " \ op.execute(reset_counts)
"from notifications where status in ('delivered') group by service_id, notification_type, date(created_at)) as results, " \ all_notifications = "select * from notifications where date(created_at) > '2016-06-30' order by created_at;"
"notification_statistics n " \
"where n.day = results.day and n.service_id = results.service_id order by results.day;"
results = conn.execute(del_sql) results = conn.execute(all_notifications)
res = results.fetchall() res = results.fetchall()
for x in res: for x in res:
if x.stat_type == 'delivered' and x.notification_type == 'email': print(' in loop {} {}'.format(x.notification_type, x.created_at))
op.execute("update notification_statistics set emails_delivered = {} where id = '{}'".format(x.count, x.id)) created = x.created_at.strftime("%Y-%m-%d")
if x.stat_type == 'failed' and x.notification_type == 'email': if x.notification_type == 'email' and x.status == 'delivered':
op.execute("update notification_statistics set emails_failed = {} where id = '{}'".format(x.count, x.id)) sql = "update notification_statistics set emails_requested = emails_requested + 1, " \
if x.stat_type == 'delivered' and x.notification_type == 'sms': "emails_delivered = emails_delivered + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id)
op.execute("update notification_statistics set sms_delivered = {} where id = '{}'".format(x.count, x.id)) if x.notification_type == 'sms' and x.status == 'delivered':
if x.stat_type == 'failed' and x.notification_type == 'sms': sql = "update notification_statistics set sms_requested = sms_requested + 1, " \
op.execute("update notification_statistics set sms_failed = {} where id = '{}'".format(x.count, x.id)) "sms_delivered = sms_delivered + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id)
if x.notification_type == 'email' and x.status in ['technical-failure', 'temporary-failure', 'permanent-failure']:
sql = "update notification_statistics set emails_requested = emails_requested + 1, " \
"emails_failed = emails_failed + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id)
if x.notification_type == 'sms' and x.status in ['technical-failure', 'temporary-failure', 'permanent-failure']:
sql = "update notification_statistics set sms_requested = sms_requested + 1, " \
"sms_failed = sms_failed + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id)
if x.notification_type == 'email' and x.status in ['created', 'sending', 'pending']:
sql = "update notification_statistics set emails_requested = emails_requested + 1 " \
" where day = date('{}') and service_id = '{}'".format(created, x.service_id)
if x.notification_type == 'sms' and x.status in ['created', 'sending', 'pending']:
sql = "update notification_statistics set sms_requested = sms_requested + 1 " \
" where day = date('{}') and service_id = '{}'".format(created, x.service_id)
print(sql)
conn.execute(sql)
def downgrade(): def downgrade():
### commands auto generated by Alembic - please adjust! ### ### commands auto generated by Alembic - please adjust! ###