mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-16 09:37:03 +02:00
Refactor get_sliced_domains to get accurate numbers
This commit is contained in:
parent
7bb3439c3e
commit
cedd54c673
3 changed files with 60 additions and 15 deletions
|
@ -467,16 +467,43 @@ def get_sliced_domains(filter_condition, distinct=False):
|
|||
domains_count = DomainInformation.objects.filter(**filter_condition).distinct().count()
|
||||
|
||||
# Round trip 2: Get counts for other slices
|
||||
# This will require either 8 filterd and distinct DB round trips,
|
||||
# or 2 DB round trips plus iteration on domain_permissions for each domain
|
||||
if distinct:
|
||||
generic_org_types_query = (
|
||||
DomainInformation.objects.filter(**filter_condition).values_list("generic_org_type", flat=True).distinct()
|
||||
generic_org_types_query = DomainInformation.objects.filter(**filter_condition).values_list(
|
||||
"domain_id", "generic_org_type"
|
||||
)
|
||||
# Initialize Counter to store counts for each generic_org_type
|
||||
generic_org_type_counts = Counter()
|
||||
|
||||
# Keep track of domains already counted
|
||||
domains_counted = set()
|
||||
|
||||
# Iterate over distinct domains
|
||||
for domain_id, generic_org_type in generic_org_types_query:
|
||||
# Check if the domain has already been counted
|
||||
if domain_id in domains_counted:
|
||||
continue
|
||||
|
||||
# Get all permissions for the current domain
|
||||
domain_permissions = DomainInformation.objects.filter(domain_id=domain_id, **filter_condition).values_list(
|
||||
"domain__permissions", flat=True
|
||||
)
|
||||
|
||||
# Check if the domain has multiple permissions
|
||||
if len(domain_permissions) > 0:
|
||||
# Mark the domain as counted
|
||||
domains_counted.add(domain_id)
|
||||
|
||||
# Increment the count for the corresponding generic_org_type
|
||||
generic_org_type_counts[generic_org_type] += 1
|
||||
else:
|
||||
generic_org_types_query = DomainInformation.objects.filter(**filter_condition).values_list(
|
||||
"generic_org_type", flat=True
|
||||
)
|
||||
generic_org_type_counts = Counter(generic_org_types_query)
|
||||
generic_org_type_counts = Counter(generic_org_types_query)
|
||||
|
||||
# Extract counts for each generic_org_type
|
||||
federal = generic_org_type_counts.get(DomainRequest.OrganizationChoices.FEDERAL, 0)
|
||||
interstate = generic_org_type_counts.get(DomainRequest.OrganizationChoices.INTERSTATE, 0)
|
||||
state_or_territory = generic_org_type_counts.get(DomainRequest.OrganizationChoices.STATE_OR_TERRITORY, 0)
|
||||
|
@ -503,21 +530,16 @@ def get_sliced_domains(filter_condition, distinct=False):
|
|||
]
|
||||
|
||||
|
||||
def get_sliced_requests(filter_condition, distinct=False):
|
||||
def get_sliced_requests(filter_condition):
|
||||
"""Get filtered requests counts sliced by org type and election office."""
|
||||
|
||||
# Round trip 1: Get distinct requests based on filter condition
|
||||
requests_count = DomainRequest.objects.filter(**filter_condition).distinct().count()
|
||||
|
||||
# Round trip 2: Get counts for other slices
|
||||
if distinct:
|
||||
generic_org_types_query = (
|
||||
DomainRequest.objects.filter(**filter_condition).values_list("generic_org_type", flat=True).distinct()
|
||||
)
|
||||
else:
|
||||
generic_org_types_query = DomainRequest.objects.filter(**filter_condition).values_list(
|
||||
"generic_org_type", flat=True
|
||||
)
|
||||
generic_org_types_query = DomainRequest.objects.filter(**filter_condition).values_list(
|
||||
"generic_org_type", flat=True
|
||||
)
|
||||
generic_org_type_counts = Counter(generic_org_types_query)
|
||||
|
||||
federal = generic_org_type_counts.get(DomainRequest.OrganizationChoices.FEDERAL, 0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue