From 3312ebcb05c015b0663f5648c5e56743e6d8d255 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Mon, 18 Feb 2019 16:35:22 -0500 Subject: [PATCH] Remove the hosts count from related_field_counts in the org api endpoints It is probably not needed, and adds an additional db query. --- awx/api/serializers.py | 2 +- awx/api/views/mixin.py | 8 ++---- awx/api/views/organization.py | 2 -- .../api/test_organization_counts.py | 27 ++++++------------- 4 files changed, 11 insertions(+), 28 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 8dbb1e273e..299a34e554 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1259,7 +1259,7 @@ class OrganizationSerializer(BaseSerializer): if counts_dict is not None and summary_dict is not None: if obj.id not in counts_dict: summary_dict['related_field_counts'] = { - 'inventories': 0, 'teams': 0, 'users': 0, 'hosts': 0, + 'inventories': 0, 'teams': 0, 'users': 0, 'job_templates': 0, 'admins': 0, 'projects': 0} else: summary_dict['related_field_counts'] = counts_dict[obj.id] diff --git a/awx/api/views/mixin.py b/awx/api/views/mixin.py index 191327c64f..ee174d5091 100644 --- a/awx/api/views/mixin.py +++ b/awx/api/views/mixin.py @@ -29,7 +29,7 @@ from awx.main.models.ha import ( ) from awx.main.models.organization import Team from awx.main.models.projects import Project -from awx.main.models.inventory import Inventory, Host +from awx.main.models.inventory import Inventory from awx.main.models.jobs import JobTemplate from awx.conf.license import ( feature_enabled, @@ -235,8 +235,6 @@ class OrganizationCountsMixin(object): db_results['projects'] = project_qs\ .values('organization').annotate(Count('organization')).order_by('organization') - db_results['hosts'] = Host.objects.active_counts_by_org() - # Other members and admins of organization are always viewable db_results['users'] = org_qs.annotate( users=Count('member_role__members', distinct=True), @@ -248,7 +246,7 @@ class OrganizationCountsMixin(object): org_id = org['id'] count_context[org_id] = { 'inventories': 0, 'teams': 0, 'users': 0, 'job_templates': 0, - 'admins': 0, 'projects': 0, 'hosts': 0} + 'admins': 0, 'projects': 0} for res, count_qs in db_results.items(): if res == 'job_templates_project': @@ -257,8 +255,6 @@ class OrganizationCountsMixin(object): org_reference = JT_inventory_reference elif res == 'users': org_reference = 'id' - elif res == 'hosts': - org_reference = 'inventory__organization' else: org_reference = 'organization' for entry in count_qs: diff --git a/awx/api/views/organization.py b/awx/api/views/organization.py index 0a6e40ba53..3c46ad3cca 100644 --- a/awx/api/views/organization.py +++ b/awx/api/views/organization.py @@ -17,7 +17,6 @@ from awx.conf.license import ( from awx.main.models import ( ActivityStream, Inventory, - Host, Project, JobTemplate, WorkflowJobTemplate, @@ -120,7 +119,6 @@ class OrganizationDetail(RelatedJobsPreventDeleteMixin, RetrieveUpdateDestroyAPI organization__id=org_id).count() org_counts['job_templates'] = JobTemplate.accessible_objects(**access_kwargs).filter( project__organization__id=org_id).count() - org_counts['hosts'] = Host.objects.org_active_count(org_id) full_context['related_field_counts'] = {} full_context['related_field_counts'][org_id] = org_counts diff --git a/awx/main/tests/functional/api/test_organization_counts.py b/awx/main/tests/functional/api/test_organization_counts.py index 77552079e1..9c4f536b09 100644 --- a/awx/main/tests/functional/api/test_organization_counts.py +++ b/awx/main/tests/functional/api/test_organization_counts.py @@ -1,5 +1,3 @@ -import itertools - import pytest from awx.api.versioning import reverse @@ -7,7 +5,7 @@ from awx.api.versioning import reverse @pytest.fixture def organization_resource_creator(organization, user): - def rf(users, admins, job_templates, projects, inventories, teams, hosts): + def rf(users, admins, job_templates, projects, inventories, teams): # Associate one resource of every type with the organization for i in range(users): @@ -19,10 +17,7 @@ def organization_resource_creator(organization, user): for i in range(teams): organization.teams.create(name='org-team %s' % i) for i in range(inventories): - organization.inventories.create(name="associated-inv %s" % i) - for i, inventory in zip(range(hosts), itertools.cycle(organization.inventories.all())): - # evenly distribute the hosts over all of the org's inventories - inventory.hosts.create(name="host %s" % i) + inventory = organization.inventories.create(name="associated-inv %s" % i) for i in range(projects): organization.projects.create(name="test-proj %s" % i, description="test-proj-desc") @@ -53,8 +48,7 @@ COUNTS_PRIMES = { 'job_templates': 3, 'projects': 3, 'inventories': 7, - 'teams': 5, - 'hosts': 7, + 'teams': 5 } COUNTS_ZEROS = { 'users': 0, @@ -62,8 +56,7 @@ COUNTS_ZEROS = { 'job_templates': 0, 'projects': 0, 'inventories': 0, - 'teams': 0, - 'hosts': 0, + 'teams': 0 } @@ -91,7 +84,6 @@ def test_org_counts_detail_member(resourced_organization, user, get): response = get(reverse('api:organization_detail', kwargs={'pk': resourced_organization.pk}), member_user) assert response.status_code == 200 - assert response.data['max_hosts'] == 0 counts = response.data['summary_fields']['related_field_counts'] assert counts == { @@ -100,8 +92,7 @@ def test_org_counts_detail_member(resourced_organization, user, get): 'job_templates': 0, 'projects': 0, 'inventories': 0, - 'teams': 0, - 'hosts': 7, + 'teams': 0 } @@ -123,17 +114,16 @@ def test_org_counts_list_member(resourced_organization, user, get): member_user = resourced_organization.member_role.members.get(username='org-member 1') response = get(reverse('api:organization_list'), member_user) assert response.status_code == 200 - assert response.data['results'][0]['max_hosts'] == 0 counts = response.data['results'][0]['summary_fields']['related_field_counts'] + assert counts == { 'users': COUNTS_PRIMES['users'], # Policy is that members can see other users and admins 'admins': COUNTS_PRIMES['admins'], 'job_templates': 0, 'projects': 0, 'inventories': 0, - 'teams': 0, - 'hosts': 7, + 'teams': 0 } @@ -240,6 +230,5 @@ def test_JT_associated_with_project(organizations, project, user, get): 'job_templates': 1, 'projects': 1, 'inventories': 0, - 'teams': 0, - 'hosts': 0, + 'teams': 0 }