diff --git a/apps/herd/services/services.py b/apps/herd/services/services.py index b1630fa..cdef63c 100644 --- a/apps/herd/services/services.py +++ b/apps/herd/services/services.py @@ -54,6 +54,30 @@ def get_rancher_statistics(rancher: Rancher = None) -> typing.Any: return [{'name': key, 'value': value} for key, value in stats.items()], stats +def get_rancher_statistic_by_herd(rancher: Rancher = None) -> typing.Any: + """ get statistics of a rancher by his herds """ # noqa + + herds = rancher.herd.all() + if herds.exists(): + stats = herds.aggregate( + herd_count=Count("id", distinct=True), + light_count=Sum('light_livestock_number'), + heavy_count=Sum('heavy_livestock_number'), + sheep=Sum('light_livestock_number'), # noqa + cow=Sum('heavy_livestock_number'), + ) + else: + stats = { + 'herd_count': 0, + 'light_count': 0, + 'heavy_count': 0, + 'sheep': 0, + 'cow': 0, + } + + return [{'name': key, 'value': value} for key, value in stats.items()], stats + + def rancher_quota_weight( rancher: Rancher, inventory_entry: InventoryEntry = None, @@ -89,7 +113,7 @@ def rancher_quota_weight( herds = rancher.herd.all() herd_livestock_gropes = [herd.activity for herd in herds] - livestock_counts_list, livestock_counts_dict = get_rancher_statistics(rancher) + livestock_counts_list, livestock_counts_dict = get_rancher_statistic_by_herd(rancher) total_weight = 0 merged = {}