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

Commit

Permalink
Merge pull request #262 from open-contracting/CPA-261
Browse files Browse the repository at this point in the history
CPA-261 Average calculation on visualization api
  • Loading branch information
KushalRaj committed Apr 22, 2021
2 parents cd8e496 + 848ba47 commit b6247d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion covidadmin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@

FIXER_IO_API_KEY = env("FIXER_IO_API_KEY")

WAGTAIL_SITE_NAME = "Covid 19 procurement explorer"
WAGTAIL_SITE_NAME = "Covid-19 Procurement Explorer"

MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = env("MEDIA_URL")
Expand Down
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 b6247d7

Please sign in to comment.