mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Add limit_days argument to notification statistics endpoint
Allows getting notification counts for a given number of days to support services with custom data retention periods (admin dashboard page should still display counts for the last 7 days, while the notifications page displays all stored notifications).
This commit is contained in:
@@ -246,9 +246,9 @@ def delete_service_and_all_associated_db_objects(service):
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_fetch_stats_for_service(service_id):
|
||||
def dao_fetch_stats_for_service(service_id, limit_days):
|
||||
# We always want between seven and eight days
|
||||
start_date = midnight_n_days_ago(7)
|
||||
start_date = midnight_n_days_ago(limit_days)
|
||||
return _stats_for_service_query(service_id).filter(
|
||||
Notification.created_at >= start_date
|
||||
).all()
|
||||
|
||||
@@ -165,7 +165,11 @@ def get_service_by_id(service_id):
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/statistics')
|
||||
def get_service_notification_statistics(service_id):
|
||||
return jsonify(data=get_service_statistics(service_id, request.args.get('today_only') == 'True'))
|
||||
return jsonify(data=get_service_statistics(
|
||||
service_id,
|
||||
request.args.get('today_only') == 'True',
|
||||
int(request.args.get('limit_days', 7))
|
||||
))
|
||||
|
||||
|
||||
@service_blueprint.route('', methods=['POST'])
|
||||
@@ -423,10 +427,13 @@ def get_detailed_service(service_id, today_only=False):
|
||||
return detailed_service_schema.dump(service).data
|
||||
|
||||
|
||||
def get_service_statistics(service_id, today_only):
|
||||
def get_service_statistics(service_id, today_only, limit_days=7):
|
||||
# today_only flag is used by the send page to work out if the service will exceed their daily usage by sending a job
|
||||
stats_fn = dao_fetch_todays_stats_for_service if today_only else dao_fetch_stats_for_service
|
||||
stats = stats_fn(service_id)
|
||||
if today_only:
|
||||
stats = dao_fetch_todays_stats_for_service(service_id)
|
||||
else:
|
||||
stats = dao_fetch_stats_for_service(service_id, limit_days=limit_days)
|
||||
|
||||
return statistics.format_statistics(stats)
|
||||
|
||||
|
||||
|
||||
@@ -534,7 +534,7 @@ def test_fetch_stats_filters_on_service(sample_notification):
|
||||
message_limit=1000)
|
||||
dao_create_service(service_two, sample_notification.service.created_by)
|
||||
|
||||
stats = dao_fetch_stats_for_service(service_two.id)
|
||||
stats = dao_fetch_stats_for_service(service_two.id, 7)
|
||||
assert len(stats) == 0
|
||||
|
||||
|
||||
@@ -546,7 +546,7 @@ def test_fetch_stats_ignores_historical_notification_data(sample_notification):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 1
|
||||
|
||||
stats = dao_fetch_stats_for_service(service_id)
|
||||
stats = dao_fetch_stats_for_service(service_id, 7)
|
||||
assert len(stats) == 0
|
||||
|
||||
|
||||
@@ -557,7 +557,7 @@ def test_fetch_stats_counts_correctly(notify_db, notify_db_session, sample_templ
|
||||
create_notification(notify_db, notify_db_session, template=sample_email_template, status='technical-failure')
|
||||
create_notification(notify_db, notify_db_session, template=sample_template, status='created')
|
||||
|
||||
stats = dao_fetch_stats_for_service(sample_template.service_id)
|
||||
stats = dao_fetch_stats_for_service(sample_template.service_id, 7)
|
||||
stats = sorted(stats, key=lambda x: (x.notification_type, x.status))
|
||||
assert len(stats) == 3
|
||||
|
||||
@@ -591,7 +591,7 @@ def test_fetch_stats_counts_should_ignore_team_key(
|
||||
create_notification(
|
||||
notify_db, notify_db_session)
|
||||
|
||||
stats = dao_fetch_stats_for_service(sample_template.service_id)
|
||||
stats = dao_fetch_stats_for_service(sample_template.service_id, 7)
|
||||
assert len(stats) == 1
|
||||
assert stats[0].notification_type == 'sms'
|
||||
assert stats[0].status == 'created'
|
||||
@@ -634,7 +634,7 @@ def test_fetch_stats_should_not_gather_notifications_older_than_7_days(sample_te
|
||||
create_notification_db(sample_template,)
|
||||
|
||||
with freeze_time('Monday 16th July 2018 12:00'):
|
||||
stats = dao_fetch_stats_for_service(sample_template.service_id)
|
||||
stats = dao_fetch_stats_for_service(sample_template.service_id, 7)
|
||||
|
||||
assert len(stats) == rows_returned
|
||||
|
||||
|
||||
Reference in New Issue
Block a user