Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
CPA-261 Average calculation on visualization api
Browse files Browse the repository at this point in the history
 - [x] Average bids average value fixed
 - [x] Round value fixed on average bids and monopolization
 - [ ] Monopolization average value
 - [x] sum, count taken from table instead of goods & services table
  • Loading branch information
sonikabaniya committed Apr 21, 2021
1 parent cd8e496 commit e2c680e
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions visualization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,37 +215,31 @@ def get(self, request):
filter_args = add_filter_args("buyer", buyer, filter_args)

# Month wise average of number of bids for contracts
monthwise_data_count = (
Tender.objects.filter(**filter_args)
.annotate(month=TruncMonth("contract_date"))
.values("month")
.annotate(count=Count("id"))
.order_by("-month")
)
monthwise_data_sum = (
monthwise_data = (
Tender.objects.filter(**filter_args)
.annotate(month=TruncMonth("contract_date"))
.values("month")
.annotate(sum=Sum("goods_services__no_of_bidders"))
.annotate(count=Count("id"), sum=Sum("no_of_bidders"))
.order_by("-month")
)
final_line_chart_data = [
{
"date": monthwise_data_sum[i]["month"],
"value": round(monthwise_data_sum[i]["sum"] / monthwise_data_count[i]["count"], 1)
if monthwise_data_sum[i]["sum"]
"date": monthwise_data[i]["month"],
"value": round(monthwise_data[i]["sum"] / monthwise_data[i]["count"], 1)
if monthwise_data[i]["sum"]
else 0,
}
for i in range(len(monthwise_data_sum))
for i in range(len(monthwise_data))
]

# Difference percentage calculation

# Overall average number of bids for contracts
overall_avg = Tender.objects.filter(**filter_args).aggregate(sum=Sum("goods_services__no_of_bidders"))
overall_avg_count = Tender.objects.filter(**filter_args).count()
overall_avg = Tender.objects.filter(**filter_args).aggregate(
sum=Sum("no_of_bidders"), count=Count("no_of_bidders")
)
result = {
"average": round(overall_avg["sum"] / overall_avg_count) if overall_avg["sum"] else 0,
"average": round(overall_avg["sum"] / overall_avg["count"], 1) if overall_avg["sum"] else 0,
"line_chart": final_line_chart_data,
}
return JsonResponse(result)
Expand Down Expand Up @@ -783,7 +777,7 @@ def get(self, request):
)

result = {
"average": round(overall["count_contract"] / overall["count_supplier"])
"average": round(overall["count_contract"] / overall["count_supplier"], 1)
if overall["count_contract"] and overall["count_supplier"]
else 0,
"line_chart": final_line_chart_data,
Expand Down

0 comments on commit e2c680e

Please sign in to comment.