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 import Sum, Q, Count
|
||||||
from django.db.models.aggregates import Count
|
|
||||||
from django.db.models.functions import Coalesce
|
from django.db.models.functions import Coalesce
|
||||||
|
|
||||||
from apps.authentication.models import Organization
|
from apps.authentication.models import Organization
|
||||||
from apps.authentication.services.service import get_all_org_child
|
from apps.authentication.services.service import get_all_org_child
|
||||||
from apps.livestock.models import LiveStockSpecies
|
|
||||||
from apps.tag.models import TagBatch
|
from apps.tag.models import TagBatch
|
||||||
|
|
||||||
|
|
||||||
@@ -18,45 +16,46 @@ class TagBatchService:
|
|||||||
dashboard data of batch main page
|
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':
|
if org.type.key != 'ADM':
|
||||||
# get batches with org & their child
|
child_orgs = get_all_org_child(org) # noqa
|
||||||
child_org = get_all_org_child(org)
|
child_orgs.append(org)
|
||||||
child_org.append(org)
|
qs = qs.filter(organization__in=child_orgs)
|
||||||
tag_batches = tag_batches.filter(organization_id__in=[child.id for child in child_org])
|
|
||||||
|
|
||||||
tag_batch_data = tag_batches.aggregate(
|
base_data = qs.aggregate(
|
||||||
batch_count=Count('id'),
|
batch_count=Count('id', distinct=True),
|
||||||
total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0),
|
total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0),
|
||||||
total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0),
|
total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0),
|
||||||
tag_count_created_by_batch=Count('tag'),
|
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')
|
species_data = (
|
||||||
|
qs
|
||||||
data_by_species_list = []
|
.values('species_code')
|
||||||
for spec in species:
|
.annotate(
|
||||||
tag_batch_data_by_species = tag_batches.aggregate(
|
batch_count=Count('id', distinct=True),
|
||||||
batch_count=Count('id', species_code=spec.get('value')),
|
total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0),
|
||||||
total_distributed_tags=Coalesce(
|
total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0),
|
||||||
Sum('total_distributed_tags', filter=Q(species_code=spec.get('value'))), 0
|
tag_count_created_by_batch=Count('tag'),
|
||||||
),
|
|
||||||
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
|
|
||||||
),
|
|
||||||
has_distributed_batches_number=Count(
|
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 base_data
|
||||||
|
|
||||||
return tag_batch_data
|
|
||||||
|
|||||||
Reference in New Issue
Block a user