Cache organisation name in Redis

A lot of pages in the admin app are now generated entirely from Redis,
without touching the API.

The one remaining API call that a lot of pages make, when the user is
platform admin or a member of an organisation, is to get the name of
the current service’s organisation.

This commit adds some code to start caching that as well, which should
speed up page load times for when we’re clicking around the admin app
(it’s typically 100ms just to get the organisation, and more than that
when the API is under load).

This means changing the service model to get the organisation from the
API by ID, not by service ID. Otherwise it would be very hard to clear
the cache if the name of the organisation ever changed.

We can’t cache the whole organisation because it has a
`count_of_live_services` field which can change at any time, without an
update being made.
This commit is contained in:
Chris Hill-Scott
2020-03-20 08:42:33 +00:00
parent c448eb8995
commit cc5701e870
19 changed files with 264 additions and 131 deletions

View File

@@ -1,13 +1,13 @@
from functools import partial
from io import BytesIO
from unittest.mock import call
from unittest.mock import PropertyMock, call
import pytest
from flask import url_for
from freezegun import freeze_time
from tests import organisation_json
from tests.conftest import SERVICE_ONE_ID, normalize_spaces
from tests.conftest import ORGANISATION_ID, SERVICE_ONE_ID, normalize_spaces
class _MockS3Object():
@@ -90,11 +90,16 @@ def test_show_agreement_page(
crown,
expected_links,
):
mocker.patch(
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
org = organisation_json(
crown=crown,
agreement_signed=agreement_signed
)
mocker.patch('app.organisations_client.get_service_organisation', return_value=org)
mocker.patch('app.organisations_client.get_organisation', return_value=org)
page = client_request.get('main.service_agreement', service_id=SERVICE_ONE_ID)
links = page.select('main .govuk-grid-column-five-sixths a')
@@ -118,7 +123,6 @@ def test_unknown_gps_and_trusts_are_redirected(
org_type,
expected_endpoint,
):
mocker.patch('app.organisations_client.get_service_organisation', return_value=None)
service_one['organisation_id'] = None
service_one['organisation_type'] = org_type
client_request.get(
@@ -156,7 +160,12 @@ def test_download_service_agreement(
expected_file_served,
):
mocker.patch(
'app.models.organisation.organisations_client.get_service_organisation',
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
mocker.patch(
'app.models.organisation.organisations_client.get_organisation',
return_value=organisation_json(
crown=crown
)
@@ -187,8 +196,13 @@ def test_download_service_agreement(
def test_show_accept_agreement_page(
client_request,
mocker,
mock_get_service_organisation,
mock_get_organisation,
):
mocker.patch(
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
page = client_request.get('main.service_accept_agreement', service_id=SERVICE_ONE_ID)
assert [
@@ -244,10 +258,15 @@ def test_show_accept_agreement_page(
def test_accept_agreement_page_populates(
client_request,
mocker,
mock_get_service_organisation,
mock_get_organisation,
):
mocker.patch(
'app.models.organisation.organisations_client.get_service_organisation',
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
mocker.patch(
'app.models.organisation.organisations_client.get_organisation',
return_value=organisation_json(
agreement_signed_version='1.2',
agreement_signed_on_behalf_of_name='Firstname Lastname',
@@ -328,11 +347,17 @@ def test_accept_agreement_page_populates(
))
def test_accept_agreement_page_validates(
mocker,
client_request,
mock_get_service_organisation,
mock_get_organisation,
data,
expected_errors,
):
mocker.patch(
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
page = client_request.post(
'main.service_accept_agreement',
service_id=SERVICE_ONE_ID,
@@ -353,7 +378,7 @@ def test_accept_agreement_page_validates(
'on_behalf_of_email': 'test@example.com',
},
call(
'7aa5d4e9-4385-4488-a489-07812ba13383',
ORGANISATION_ID,
agreement_signed_version=1.2,
agreement_signed_on_behalf_of_name='Firstname Lastname',
agreement_signed_on_behalf_of_email_address='test@example.com',
@@ -368,7 +393,7 @@ def test_accept_agreement_page_validates(
'on_behalf_of_email': 'test@example.com',
},
call(
'7aa5d4e9-4385-4488-a489-07812ba13383',
ORGANISATION_ID,
agreement_signed_version=1.2,
agreement_signed_on_behalf_of_name='',
agreement_signed_on_behalf_of_email_address='',
@@ -383,7 +408,7 @@ def test_accept_agreement_page_validates(
'on_behalf_of_email': '',
},
call(
'7aa5d4e9-4385-4488-a489-07812ba13383',
ORGANISATION_ID,
agreement_signed_version=1.2,
agreement_signed_on_behalf_of_name='',
agreement_signed_on_behalf_of_email_address='',
@@ -392,12 +417,18 @@ def test_accept_agreement_page_validates(
),
))
def test_accept_agreement_page_persists(
mocker,
client_request,
mock_get_service_organisation,
mock_get_organisation,
mock_update_organisation,
data,
expected_persisted,
):
mocker.patch(
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
client_request.post(
'main.service_accept_agreement',
service_id=SERVICE_ONE_ID,
@@ -433,7 +464,12 @@ def test_show_confirm_agreement_page(
expected_paragraph,
):
mocker.patch(
'app.models.organisation.organisations_client.get_service_organisation',
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
mocker.patch(
'app.models.organisation.organisations_client.get_organisation',
return_value=organisation_json(
agreement_signed_version='1.2',
agreement_signed_on_behalf_of_name=name,
@@ -447,7 +483,7 @@ def test_show_confirm_agreement_page(
@pytest.mark.parametrize('http_method', ('get', 'post'))
def test_confirm_agreement_page_403s_if_previous_step_not_taken(
client_request,
mock_get_service_organisation,
mock_get_organisation,
http_method,
):
getattr(client_request, http_method)(
@@ -465,7 +501,12 @@ def test_confirm_agreement_page_persists(
fake_uuid,
):
mocker.patch(
'app.models.organisation.organisations_client.get_service_organisation',
'app.models.service.Service.organisation_id',
new_callable=PropertyMock,
return_value=ORGANISATION_ID,
)
mocker.patch(
'app.models.organisation.organisations_client.get_organisation',
return_value=organisation_json(agreement_signed_version='1.2')
)
client_request.post(