Removed updates to the provider stats table

- again these new come from the notifications history table
- We update this when we sent a notification, so removed from celery tasks
- tests removed also
This commit is contained in:
Martyn Inglis
2016-08-25 10:33:12 +01:00
parent 84ea173ced
commit 708f566c24
5 changed files with 3 additions and 181 deletions

View File

@@ -13,7 +13,6 @@ from app import notify_celery, statsd_client, clients, create_uuid
from app.clients.email import EmailClientException
from app.clients.sms import SmsClientException
from app.dao.notifications_dao import (
update_provider_stats,
get_notification_by_id,
dao_update_notification,
update_notification_status_by_id
@@ -78,13 +77,6 @@ def send_sms_to_provider(self, service_id, notification_id):
)
notification.billable_units = get_sms_fragment_count(template.replaced_content_count)
update_provider_stats(
notification_id,
SMS_TYPE,
provider.get_name(),
billable_units=notification.billable_units
)
notification.sent_at = datetime.utcnow()
notification.sent_by = provider.get_name()
notification.status = 'sending'
@@ -163,12 +155,6 @@ def send_email_to_provider(self, service_id, notification_id):
reply_to_address=service.reply_to_email_address,
)
update_provider_stats(
notification_id,
EMAIL_TYPE,
provider.get_name(),
billable_units=1
)
notification.reference = reference
notification.sent_at = datetime.utcnow()
notification.sent_by = provider.get_name(),

View File

@@ -17,12 +17,9 @@ from app.models import (
Notification,
NotificationHistory,
NotificationStatistics,
TemplateStatistics,
SMS_TYPE,
EMAIL_TYPE,
Template,
ProviderStatistics,
ProviderDetails)
Template)
from app.clients import (
STATISTICS_FAILURE,
STATISTICS_DELIVERED,
@@ -242,33 +239,6 @@ def dao_update_notification(notification):
db.session.commit()
@statsd(namespace="dao")
@transactional
def update_provider_stats(
id_,
notification_type,
provider_name,
billable_units=1):
notification = Notification.query.filter(Notification.id == id_).one()
provider = ProviderDetails.query.filter_by(identifier=provider_name).one()
update_count = db.session.query(ProviderStatistics).filter_by(
day=date.today(),
service_id=notification.service_id,
provider_id=provider.id
).update({'unit_count': ProviderStatistics.unit_count + billable_units})
if update_count == 0:
provider_stats = ProviderStatistics(
day=notification.created_at.date(),
service_id=notification.service_id,
provider_id=provider.id,
unit_count=billable_units
)
db.session.add(provider_stats)
@statsd(namespace="dao")
def get_notification_for_job(service_id, job_id, notification_id):
return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one()

View File

@@ -241,23 +241,6 @@ def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_s
assert not persisted_notification.personalisation
def test_should_update_provider_stats_on_success(notify_db, sample_service, sample_notification, mocker):
provider_stats = provider_statistics_dao.get_provider_statistics(sample_service).all()
assert len(provider_stats) == 0
mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.mmg_client.get_name', return_value="mmg")
send_sms_to_provider(
sample_notification.service_id,
sample_notification.id
)
updated_provider_stats = provider_statistics_dao.get_provider_statistics(sample_service).all()
assert updated_provider_stats[0].provider.identifier == 'mmg'
assert updated_provider_stats[0].unit_count == 1
@pytest.mark.parametrize('research_mode,key_type', [
(True, KEY_TYPE_NORMAL),
(False, KEY_TYPE_TEST)

View File

@@ -33,8 +33,7 @@ from app.dao.notifications_dao import (
get_notifications_for_job,
get_notifications_for_service,
update_notification_status_by_id,
update_notification_status_by_reference,
update_provider_stats,
update_notification_status_by_reference
)
from notifications_utils.template import get_sms_fragment_count
@@ -49,7 +48,6 @@ def test_should_have_decorated_notifications_dao_functions():
assert dao_create_notification.__wrapped__.__name__ == 'dao_create_notification' # noqa
assert update_notification_status_by_id.__wrapped__.__name__ == 'update_notification_status_by_id' # noqa
assert dao_update_notification.__wrapped__.__name__ == 'dao_update_notification' # noqa
assert update_provider_stats.__wrapped__.__name__ == 'update_provider_stats' # noqa
assert update_notification_status_by_reference.__wrapped__.__name__ == 'update_notification_status_by_reference' # noqa
assert get_notification_for_job.__wrapped__.__name__ == 'get_notification_for_job' # noqa
assert get_notifications_for_job.__wrapped__.__name__ == 'get_notifications_for_job' # noqa
@@ -330,10 +328,6 @@ def test_should_not_update_status_one_notification_status_is_delivered(notify_db
status='sending')
assert Notification.query.get(notification.id).status == "sending"
update_provider_stats(
notification.id,
'email',
ses_provider.identifier)
notification.reference = 'reference'
dao_update_notification(notification)
update_notification_status_by_reference('reference', 'delivered', 'delivered')
@@ -344,32 +338,6 @@ def test_should_not_update_status_one_notification_status_is_delivered(notify_db
_assert_notification_stats(notification.service_id, emails_requested=1, emails_delivered=1, emails_failed=0)
def test_should_be_able_to_record_statistics_failure_for_sms(notify_db, notify_db_session, ):
notification = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, status='sending')
assert Notification.query.get(notification.id).status == 'sending'
assert update_notification_status_by_id(notification.id, 'permanent-failure', 'failure')
assert Notification.query.get(notification.id).status == 'permanent-failure'
_assert_notification_stats(notification.service_id, sms_requested=1, sms_delivered=0, sms_failed=1)
def test_should_be_able_to_record_statistics_failure_for_email(sample_email_template, sample_job, ses_provider):
data = _notification_json(sample_email_template, job_id=sample_job.id, status='sending')
notification = Notification(**data)
dao_create_notification(notification, sample_email_template.template_type)
update_provider_stats(
notification.id,
'email',
ses_provider.identifier)
notification.reference = 'reference'
dao_update_notification(notification)
count = update_notification_status_by_reference('reference', 'failed', 'failure')
assert count == 1
assert Notification.query.get(notification.id).status == 'failed'
_assert_notification_stats(notification.service_id, emails_requested=1, emails_delivered=0, emails_failed=1)
def test_should_return_zero_count_if_no_notification_with_id():
assert not update_notification_status_by_id(str(uuid.uuid4()), 'delivered', 'delivered')

View File

@@ -4,92 +4,7 @@ import uuid
import pytest
from app.models import NotificationHistory, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, NOTIFICATION_STATUS_TYPES
from app.dao.notifications_dao import update_provider_stats
from app.dao.provider_statistics_dao import get_provider_statistics, get_fragment_count
from tests.app.conftest import sample_notification as create_sample_notification
def test_should_update_provider_statistics_sms(notify_db,
notify_db_session,
sample_template,
mmg_provider):
n1 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_template)
update_provider_stats(n1.id, 'sms', mmg_provider.identifier)
provider_stats = get_provider_statistics(
sample_template.service,
providers=[mmg_provider.identifier]).one()
assert provider_stats.unit_count == 1
def test_should_update_provider_statistics_email(notify_db,
notify_db_session,
sample_email_template,
ses_provider):
n1 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_email_template)
update_provider_stats(n1.id, 'email', ses_provider.identifier)
provider_stats = get_provider_statistics(
sample_email_template.service,
providers=[ses_provider.identifier]).one()
assert provider_stats.unit_count == 1
def test_should_update_provider_statistics_sms_multi(notify_db,
notify_db_session,
sample_template,
mmg_provider):
n1 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_template,
billable_units=1)
update_provider_stats(n1.id, 'sms', mmg_provider.identifier, n1.billable_units)
n2 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_template,
billable_units=2)
update_provider_stats(n2.id, 'sms', mmg_provider.identifier, n2.billable_units)
n3 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_template,
billable_units=4)
update_provider_stats(n3.id, 'sms', mmg_provider.identifier, n3.billable_units)
provider_stats = get_provider_statistics(
sample_template.service,
providers=[mmg_provider.identifier]).one()
assert provider_stats.unit_count == 7
def test_should_update_provider_statistics_email_multi(notify_db,
notify_db_session,
sample_email_template,
ses_provider):
n1 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_email_template)
update_provider_stats(n1.id, 'email', ses_provider.identifier)
n2 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_email_template)
update_provider_stats(n2.id, 'email', ses_provider.identifier)
n3 = create_sample_notification(
notify_db,
notify_db_session,
template=sample_email_template)
update_provider_stats(n3.id, 'email', ses_provider.identifier)
provider_stats = get_provider_statistics(
sample_email_template.service,
providers=[ses_provider.identifier]).one()
assert provider_stats.unit_count == 3
from app.dao.provider_statistics_dao import get_fragment_count
def test_get_fragment_count_with_no_data(sample_template):