create new stats endpoint

the admin app currently calls get_detailed_service, which gets
notification stats, adds them on to the rest of the detailed service
endpoint, and returns them. However, the admin app then only looks at
the stats - it doesn't look at the rest of the service object.

This is called in a few high profile places - the dashboard, the
notification summary page, and when you send a job. By creating a
separate endpoint that ignores the rest of the service object (and no
marshmallow too!), the hope is that we'll improve some slowness we've
been seeing.

Note: The old detailed function will still need to stay - it's used
by get_services(detailed=True) for the platform admin page.
This commit is contained in:
Leo Hemsted
2018-05-09 11:36:42 +01:00
parent 54d0e64582
commit c30f13ea67
2 changed files with 45 additions and 4 deletions

View File

@@ -170,6 +170,11 @@ def get_service_by_id(service_id):
return jsonify(data=data)
@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'))
@service_blueprint.route('', methods=['POST'])
def create_service():
data = request.get_json()
@@ -411,12 +416,16 @@ def get_monthly_notification_stats(service_id):
def get_detailed_service(service_id, today_only=False):
service = dao_fetch_service_by_id(service_id)
service.statistics = get_service_statistics(service_id, today_only)
return detailed_service_schema.dump(service).data
def get_service_statistics(service_id, today_only):
# 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)
service.statistics = statistics.format_statistics(stats)
return detailed_service_schema.dump(service).data
return statistics.format_statistics(stats)
def get_detailed_services(start_date, end_date, only_active=False, include_from_test_key=True):

View File

@@ -3085,3 +3085,35 @@ def test_get_platform_stats_creates_zero_stats(client, notify_db_session):
assert json_resp['email'] == {'failed': 1, 'requested': 2, 'delivered': 1}
assert json_resp['letter'] == {'failed': 0, 'requested': 0, 'delivered': 0}
assert json_resp['sms'] == {'failed': 0, 'requested': 4, 'delivered': 3}
@pytest.mark.parametrize('today_only, stats', [
(False, {'requested': 2, 'delivered': 1, 'failed': 0}),
(True, {'requested': 1, 'delivered': 0, 'failed': 0})
], ids=['seven_days', 'today'])
def test_get_service_notification_statistics(admin_request, sample_template, today_only, stats):
with freeze_time('2000-01-01T12:00:00'):
create_notification(sample_template, status='delivered')
with freeze_time('2000-01-02T12:00:00'):
create_notification(sample_template, status='created')
resp = admin_request.get(
'service.get_service_notification_statistics',
service_id=sample_template.service_id,
today_only=today_only
)
assert set(resp['data'].keys()) == {SMS_TYPE, EMAIL_TYPE, LETTER_TYPE}
assert resp['data'][SMS_TYPE] == stats
def test_get_service_notification_statistics_with_unknown_service(admin_request):
resp = admin_request.get(
'service.get_service_notification_statistics',
service_id=uuid.uuid4()
)
assert resp['data'] == {
SMS_TYPE: {'requested': 0, 'delivered': 0, 'failed': 0},
EMAIL_TYPE: {'requested': 0, 'delivered': 0, 'failed': 0},
LETTER_TYPE: {'requested': 0, 'delivered': 0, 'failed': 0},
}