fix-->import distinct to tag batch count in dashboard
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
from django.db.models import Sum, Q
|
||||
from django.db.models.aggregates import Count
|
||||
from django.db.models import Sum, Q, Count
|
||||
from django.db.models.functions import Coalesce
|
||||
|
||||
from apps.authentication.models import Organization
|
||||
from apps.authentication.services.service import get_all_org_child
|
||||
from apps.livestock.models import LiveStockSpecies
|
||||
from apps.tag.models import TagBatch
|
||||
|
||||
|
||||
@@ -18,45 +16,46 @@ class TagBatchService:
|
||||
dashboard data of batch main page
|
||||
"""
|
||||
|
||||
tag_batches = TagBatch.objects.select_related('organization').prefetch_related('tag')
|
||||
qs = TagBatch.objects.select_related('organization')
|
||||
|
||||
if org.type.key != 'ADM':
|
||||
# get batches with org & their child
|
||||
child_org = get_all_org_child(org)
|
||||
child_org.append(org)
|
||||
tag_batches = tag_batches.filter(organization_id__in=[child.id for child in child_org])
|
||||
child_orgs = get_all_org_child(org) # noqa
|
||||
child_orgs.append(org)
|
||||
qs = qs.filter(organization__in=child_orgs)
|
||||
|
||||
tag_batch_data = tag_batches.aggregate(
|
||||
batch_count=Count('id'),
|
||||
base_data = qs.aggregate(
|
||||
batch_count=Count('id', distinct=True),
|
||||
total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0),
|
||||
total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0),
|
||||
tag_count_created_by_batch=Count('tag'),
|
||||
has_distributed_batches_number=Count('id', filter=Q(status='distributed'))
|
||||
has_distributed_batches_number=Count(
|
||||
'id',
|
||||
distinct=True,
|
||||
filter=Q(status__in=[
|
||||
'distributed',
|
||||
])
|
||||
)
|
||||
)
|
||||
|
||||
species = LiveStockSpecies.objects.values('value')
|
||||
|
||||
data_by_species_list = []
|
||||
for spec in species:
|
||||
tag_batch_data_by_species = tag_batches.aggregate(
|
||||
batch_count=Count('id', species_code=spec.get('value')),
|
||||
total_distributed_tags=Coalesce(
|
||||
Sum('total_distributed_tags', filter=Q(species_code=spec.get('value'))), 0
|
||||
),
|
||||
total_remaining_tags=Coalesce(
|
||||
Sum('total_remaining_tags', filter=Q(species_code=spec.get('value'))), 0
|
||||
),
|
||||
tag_count_created_by_batch=Coalesce(
|
||||
Count('tag', filter=Q(species_code=spec.get('value'))), 0
|
||||
),
|
||||
species_data = (
|
||||
qs
|
||||
.values('species_code')
|
||||
.annotate(
|
||||
batch_count=Count('id', distinct=True),
|
||||
total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0),
|
||||
total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0),
|
||||
tag_count_created_by_batch=Count('tag'),
|
||||
has_distributed_batches_number=Count(
|
||||
'id', filter=Q(status='distributed', species_code=spec.get('value'))
|
||||
'id',
|
||||
distinct=True,
|
||||
filter=Q(status__in=[
|
||||
'distributed'
|
||||
])
|
||||
)
|
||||
)
|
||||
tag_batch_data_by_species.update({'species_code': spec.get('value')})
|
||||
.order_by('species_code')
|
||||
)
|
||||
|
||||
data_by_species_list.append(tag_batch_data_by_species)
|
||||
base_data['batch_data_by_species'] = list(species_data)
|
||||
|
||||
tag_batch_data.update({'batch_data_by_species': data_by_species_list})
|
||||
|
||||
return tag_batch_data
|
||||
return base_data
|
||||
|
||||
Reference in New Issue
Block a user