add detailed flag to GET service api client

returns current (past 7 days) notification stats as well as service info
This commit is contained in:
Leo Hemsted
2016-07-19 13:53:27 +01:00
parent d9944107da
commit 4451a8634d
5 changed files with 39 additions and 16 deletions

View File

@@ -324,7 +324,11 @@ def register_errorhandlers(application):
@application.errorhandler(HTTPError)
def render_http_error(error):
application.logger.error("API called failed with status {} message {}".format(error.status_code, error.message))
application.logger.error("API {} failed with status {} message {}".format(
error.response.url,
error.status_code,
error.message
))
error_code = error.status_code
if error_code not in [401, 404, 403, 500]:
error_code = 500

View File

@@ -154,9 +154,7 @@ def get_dashboard_partials(service_id):
return {
'totals': render_template(
'views/dashboard/_totals.html',
statistics=add_rates_to(sum_of_statistics(
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
))
statistics=get_dashboard_totals(service_id)
),
'template-statistics': render_template(
'views/dashboard/template-statistics.html',
@@ -178,6 +176,12 @@ def get_dashboard_partials(service_id):
}
def get_dashboard_totals(service_id):
return add_rates_to(sum_of_statistics(
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
))
def calculate_usage(usage):
# TODO: Don't hardcode these - get em from the API
sms_free_allowance = 250000

View File

@@ -41,12 +41,14 @@ class ServiceAPIClient(NotificationsAPIClient):
data = _attach_current_user({})
return self.delete(endpoint, data)
def get_service(self, service_id, *params):
def get_service(self, service_id, detailed=False):
"""
Retrieve a service.
"""
params = {'detailed': True} if detailed else {}
return self.get(
'/service/{0}'.format(service_id))
'/service/{0}'.format(service_id),
params=params)
def get_services(self, *params):
"""

View File

@@ -100,7 +100,6 @@ def test_get_started_is_hidden_once_templates_exist(
mock_has_permissions,
mock_get_usage
):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
return_value=copy.deepcopy(stub_template_stats))
@@ -376,14 +375,6 @@ def test_menu_all_services_for_platform_admin_user(mocker,
assert url_for('main.view_notifications', service_id=service_one['id'], message_type='sms') in page
assert url_for('main.api_keys', service_id=service_one['id']) not in page
# Should this be here??
# template_json = mock_get_service_templates(service_one['id'])['data'][0]
# assert url_for(
# 'main.edit_service_template',
# service_id=service_one['id'],
# template_id=template_json['id']) in page
def test_route_for_service_permissions(mocker,
app_,
@@ -424,3 +415,20 @@ def test_aggregate_template_stats():
assert item['usage_count'] == 13
elif item['template'].id == 2:
assert item['usage_count'] == 206
def test_service_dashboard_updates_gets_detailed_service(mocker,
app_,
active_user_with_permissions,
service_one,
mock_get_service,
mock_get_service_statistics,
mock_get_usage,
):
with app_.test_request_context(), app_.test_client() as client:
client.login(active_user_with_permissions, mocker, service_one)
response = client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
assert response.status_code == 200
# mock_get_service_statistics.assert_not_called()
# mock_get_service.assert_called_once_with(service_one.id, detailed=True)

View File

@@ -55,10 +55,15 @@ def fake_uuid():
@pytest.fixture(scope='function')
def mock_get_service(mocker, api_user_active):
def _get(service_id):
def _get(service_id, detailed=False):
service = service_json(
service_id, "Test Service", [api_user_active.id], message_limit=1000,
active=False, restricted=True)
if detailed:
service['statistics'] = {
'email': {'requested': 0, 'delivered': 0, 'failed': 0},
'sms': {'requested': 0, 'delivered': 0, 'failed': 0}
}
return {'data': service}
return mocker.patch('app.service_api_client.get_service', side_effect=_get)