mirror of
https://github.com/ansible/awx.git
synced 2026-01-15 03:40:42 -03:30
Remove the hosts count from related_field_counts in the org api endpoints
It is probably not needed, and adds an additional db query.
This commit is contained in:
parent
4d06ae48d3
commit
3312ebcb05
@ -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]
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user