mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 03:41:43 -05:00
Removed the group by day aspects of template stats. Not needed. Grouped by template only.
This commit is contained in:
@@ -137,23 +137,27 @@ def dao_get_7_day_agg_notification_statistics_for_service(service_id,
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_usage(service_id, limit_days=None):
|
||||
|
||||
table = NotificationHistory
|
||||
|
||||
if limit_days and limit_days <= 7: # can get this data from notifications table
|
||||
table = Notification
|
||||
|
||||
query = db.session.query(
|
||||
func.count(NotificationHistory.template_id).label('count'),
|
||||
NotificationHistory.template_id,
|
||||
func.DATE(NotificationHistory.created_at).label('day'),
|
||||
func.count(table.template_id).label('count'),
|
||||
table.template_id,
|
||||
Template.name,
|
||||
Template.template_type
|
||||
)
|
||||
|
||||
query_filter = [NotificationHistory.service_id == service_id]
|
||||
query_filter = [table.service_id == service_id]
|
||||
if limit_days is not None:
|
||||
query_filter.append(NotificationHistory.created_at >= days_ago(limit_days))
|
||||
query_filter.append(table.created_at >= days_ago(limit_days))
|
||||
|
||||
return query.filter(*query_filter) \
|
||||
.join(Template)\
|
||||
.group_by(NotificationHistory.template_id, func.DATE(NotificationHistory.created_at), Template.name, Template.template_type)\
|
||||
.order_by(desc(func.DATE(NotificationHistory.created_at)), asc(Template.name))\
|
||||
.all() # noqa
|
||||
.group_by(table.template_id, Template.name, Template.template_type)\
|
||||
.order_by(asc(Template.name))\
|
||||
.all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
@@ -37,7 +37,6 @@ def get_template_statistics_for_service_by_day(service_id):
|
||||
def serialize(data):
|
||||
return {
|
||||
'count': data.count,
|
||||
'day': str(data.day),
|
||||
'template_id': str(data.template_id),
|
||||
'template_name': data.name,
|
||||
'template_type': data.template_type
|
||||
|
||||
@@ -137,14 +137,11 @@ def test_should_by_able_to_get_template_count_from_notifications_history_across_
|
||||
|
||||
results = dao_get_template_usage(sample_service.id)
|
||||
|
||||
assert len(results) == 5
|
||||
assert len(results) == 2
|
||||
|
||||
assert [(row.name, row.template_type, row.count, row.day) for row in results] == [
|
||||
('Email Template Name', 'email', 2, datetime(today.year, today.month, today.day).date()),
|
||||
('Template Name', 'sms', 1, datetime(today.year, today.month, today.day).date()),
|
||||
('Email Template Name', 'email', 3, datetime(yesterday.year, yesterday.month, yesterday.day).date()),
|
||||
('Template Name', 'sms', 1, datetime(yesterday.year, yesterday.month, yesterday.day).date()),
|
||||
('Template Name', 'sms', 3, datetime(one_month_ago.year, one_month_ago.month, one_month_ago.day).date())
|
||||
assert [(row.name, row.template_type, row.count) for row in results] == [
|
||||
('Email Template Name', 'email', 5),
|
||||
('Template Name', 'sms', 5)
|
||||
]
|
||||
|
||||
|
||||
@@ -177,29 +174,24 @@ def test_should_by_able_to_get_template_count_from_notifications_history_with_da
|
||||
assert len(results_day_one) == 2
|
||||
|
||||
results_day_two = dao_get_template_usage(sample_service.id, limit_days=1)
|
||||
assert len(results_day_two) == 4
|
||||
assert len(results_day_two) == 2
|
||||
|
||||
results_day_30 = dao_get_template_usage(sample_service.id, limit_days=31)
|
||||
assert len(results_day_30) == 5
|
||||
assert len(results_day_30) == 2
|
||||
|
||||
assert [(row.name, row.template_type, row.count, row.day) for row in results_day_one] == [
|
||||
('Email Template Name', 'email', 2, datetime(today.year, today.month, today.day).date()),
|
||||
('Template Name', 'sms', 1, datetime(today.year, today.month, today.day).date())
|
||||
assert [(row.name, row.template_type, row.count) for row in results_day_one] == [
|
||||
('Email Template Name', 'email', 2),
|
||||
('Template Name', 'sms', 1)
|
||||
]
|
||||
|
||||
assert [(row.name, row.template_type, row.count, row.day) for row in results_day_two] == [
|
||||
('Email Template Name', 'email', 2, datetime(today.year, today.month, today.day).date()),
|
||||
('Template Name', 'sms', 1, datetime(today.year, today.month, today.day).date()),
|
||||
('Email Template Name', 'email', 3, datetime(yesterday.year, yesterday.month, yesterday.day).date()),
|
||||
('Template Name', 'sms', 1, datetime(yesterday.year, yesterday.month, yesterday.day).date())
|
||||
assert [(row.name, row.template_type, row.count) for row in results_day_two] == [
|
||||
('Email Template Name', 'email', 5),
|
||||
('Template Name', 'sms', 2),
|
||||
]
|
||||
|
||||
assert [(row.name, row.template_type, row.count, row.day) for row in results_day_30] == [
|
||||
('Email Template Name', 'email', 2, datetime(today.year, today.month, today.day).date()),
|
||||
('Template Name', 'sms', 1, datetime(today.year, today.month, today.day).date()),
|
||||
('Email Template Name', 'email', 3, datetime(yesterday.year, yesterday.month, yesterday.day).date()),
|
||||
('Template Name', 'sms', 1, datetime(yesterday.year, yesterday.month, yesterday.day).date()),
|
||||
('Template Name', 'sms', 3, datetime(one_month_ago.year, one_month_ago.month, one_month_ago.day).date())
|
||||
assert [(row.name, row.template_type, row.count) for row in results_day_30] == [
|
||||
('Email Template Name', 'email', 5),
|
||||
('Template Name', 'sms', 5),
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -55,24 +55,25 @@ def test_get_template_statistics_for_service(notify_db, notify_db_session, notif
|
||||
assert json_resp['data'][0]['template_id'] == str(email.id)
|
||||
assert json_resp['data'][0]['template_name'] == email.name
|
||||
assert json_resp['data'][0]['template_type'] == email.template_type
|
||||
assert json_resp['data'][0]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][1]['count'] == 2
|
||||
assert json_resp['data'][1]['template_id'] == str(sms.id)
|
||||
assert json_resp['data'][1]['template_name'] == sms.name
|
||||
assert json_resp['data'][1]['template_type'] == sms.template_type
|
||||
assert json_resp['data'][1]['day'] == '2016-08-18'
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
def test_get_template_statistics_for_service_by_day(notify_db, notify_db_session, notify_api, sample_service):
|
||||
def test_get_template_statistics_for_service_limited_by_day(notify_db, notify_db_session, notify_api, sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session, service=sample_service)
|
||||
email = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||
today = datetime.now()
|
||||
a_week_ago = datetime.now() - timedelta(days=7)
|
||||
a_month_ago = datetime.now() - timedelta(days=30)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_month_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_month_ago, service=sample_service, template=email)
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
@@ -92,12 +93,10 @@ def test_get_template_statistics_for_service_by_day(notify_db, notify_db_session
|
||||
assert json_resp['data'][0]['template_id'] == str(email.id)
|
||||
assert json_resp['data'][0]['template_name'] == email.name
|
||||
assert json_resp['data'][0]['template_type'] == email.template_type
|
||||
assert json_resp['data'][0]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][1]['count'] == 1
|
||||
assert json_resp['data'][1]['template_id'] == str(sms.id)
|
||||
assert json_resp['data'][1]['template_name'] == sms.name
|
||||
assert json_resp['data'][1]['template_type'] == sms.template_type
|
||||
assert json_resp['data'][1]['day'] == '2016-08-18'
|
||||
|
||||
response_for_a_week = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
@@ -107,19 +106,38 @@ def test_get_template_statistics_for_service_by_day(notify_db, notify_db_session
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response_for_a_week.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 4
|
||||
assert json_resp['data'][0]['count'] == 1
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['count'] == 2
|
||||
assert json_resp['data'][0]['template_name'] == 'Email Template Name'
|
||||
assert json_resp['data'][0]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][1]['count'] == 1
|
||||
assert json_resp['data'][1]['count'] == 2
|
||||
assert json_resp['data'][1]['template_name'] == 'Template Name'
|
||||
|
||||
response_for_a_month = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 30}
|
||||
)
|
||||
|
||||
assert response_for_a_month.status_code == 200
|
||||
json_resp = json.loads(response_for_a_month.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['count'] == 3
|
||||
assert json_resp['data'][0]['template_name'] == 'Email Template Name'
|
||||
assert json_resp['data'][1]['count'] == 3
|
||||
assert json_resp['data'][1]['template_name'] == 'Template Name'
|
||||
|
||||
response_for_all = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
assert response_for_all.status_code == 200
|
||||
json_resp = json.loads(response_for_all.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['count'] == 3
|
||||
assert json_resp['data'][0]['template_name'] == 'Email Template Name'
|
||||
assert json_resp['data'][1]['count'] == 3
|
||||
assert json_resp['data'][1]['template_name'] == 'Template Name'
|
||||
assert json_resp['data'][1]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][2]['count'] == 1
|
||||
assert json_resp['data'][2]['template_name'] == 'Email Template Name'
|
||||
assert json_resp['data'][2]['day'] == '2016-08-11'
|
||||
assert json_resp['data'][3]['count'] == 1
|
||||
assert json_resp['data'][3]['template_name'] == 'Template Name'
|
||||
assert json_resp['data'][3]['day'] == '2016-08-11'
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
|
||||
Reference in New Issue
Block a user