Slim down the /organisations response

At the moment this response returns a list of service IDs for hundreds
of organisations.

The admin app doesn’t use this information, but having to wait for it to
be serialized and sent across the network slows it down all the same.
This commit is contained in:
Chris Hill-Scott
2019-06-13 15:54:57 +01:00
parent 4dd245ca87
commit 0955403a3b
4 changed files with 29 additions and 11 deletions

View File

@@ -376,6 +376,12 @@ class Organisation(db.Model):
if service.active and not service.restricted
]
@property
def domain_list(self):
return [
domain.domain for domain in self.domains
]
def serialize(self):
return {
"id": str(self.id),
@@ -391,13 +397,20 @@ class Organisation(db.Model):
"agreement_signed_on_behalf_of_name": self.agreement_signed_on_behalf_of_name,
"agreement_signed_on_behalf_of_email_address": self.agreement_signed_on_behalf_of_email_address,
"agreement_signed_version": self.agreement_signed_version,
"domains": [
domain.domain for domain in self.domains
],
"domains": self.domain_list,
"request_to_go_live_notes": self.request_to_go_live_notes,
"count_of_live_services": len(self.live_services),
}
def serialize_for_list(self):
return {
'name': self.name,
'id': str(self.id),
'active': self.active,
'count_of_live_services': len(self.live_services),
'domains': self.domain_list,
}
class Service(db.Model, Versioned):
__tablename__ = 'services'

View File

@@ -45,7 +45,7 @@ def handle_integrity_error(exc):
@organisation_blueprint.route('', methods=['GET'])
def get_organisations():
organisations = [
org.serialize() for org in dao_get_organisations()
org.serialize_for_list() for org in dao_get_organisations()
]
return jsonify(organisations)

View File

@@ -620,15 +620,18 @@ def create_domain(domain, organisation_id):
return domain
def create_organisation(name='test_org_1', active=True, organisation_type=None):
def create_organisation(name='test_org_1', active=True, organisation_type=None, domains=None):
data = {
'name': name,
'active': active,
'organisation_type': organisation_type
'organisation_type': organisation_type,
}
organisation = Organisation(**data)
dao_create_organisation(organisation)
for domain in domains or []:
create_domain(domain, organisation.id)
return organisation

View File

@@ -16,7 +16,7 @@ from tests.app.db import (
def test_get_all_organisations(admin_request, notify_db_session):
create_organisation(name='inactive org', active=False)
create_organisation(name='active org')
create_organisation(name='active org', domains=['example.com'])
response = admin_request.get(
'organisation.get_organisations',
@@ -27,9 +27,11 @@ def test_get_all_organisations(admin_request, notify_db_session):
assert response[0]['name'] == 'active org'
assert response[0]['active'] is True
assert response[0]['count_of_live_services'] == 0
assert response[0]['domains'] == ['example.com']
assert response[1]['name'] == 'inactive org'
assert response[1]['active'] is False
assert response[1]['count_of_live_services'] == 0
assert response[1]['domains'] == []
def test_get_organisation_by_id(admin_request, notify_db_session):
@@ -78,10 +80,10 @@ def test_get_organisation_by_id(admin_request, notify_db_session):
def test_get_organisation_by_id_returns_domains(admin_request, notify_db_session):
org = create_organisation()
create_domain('foo.gov.uk', org.id)
create_domain('bar.gov.uk', org.id)
org = create_organisation(domains=[
'foo.gov.uk',
'bar.gov.uk',
])
response = admin_request.get(
'organisation.get_organisation_by_id',