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

Commit

Permalink
CPA-297 Handling custom jobs
Browse files Browse the repository at this point in the history
[x] Created custom jobs for red_flag, summarize_supplier and summarize_buyer
[x] Added new ScheduleRunner class in schedule.py
[x] Added Schedule Runner on tender create/edit/delete
[x] Removed fill_contract_values from management command
[x] Management command can now handle all case for country code
  • Loading branch information
Suyoj Man Tamrakar authored and Kushal Raj Shrestha committed Jul 16, 2021
1 parent 6a04eed commit facbf4b
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 227 deletions.
45 changes: 14 additions & 31 deletions country/management/commands/evaluate_contract_red_flag.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
from django.core.management.base import BaseCommand

from country.models import Country, Tender
from country.tasks import clear_redflag, process_redflag, process_redflag6, process_redflag7
from country.models import Country
from country.tasks import evaluate_contract_red_flag


class Command(BaseCommand):
help = "Compute red flag for specific country"

def add_arguments(self, parser):
parser.add_argument("country", nargs="+", type=str)

def handle(self, *args, **options):
country_code = options["country"][0]
country_code = str(country_code).upper()
country_list = Country.objects.exclude(country_code_alpha_2="gl").values_list(
"country_code_alpha_2", flat=True
parser.add_argument("country", type=str)

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"].upper()
try:
Country.objects.get(country_code_alpha_2=country_alpha_code)
except Exception:
return self.stdout.write("Country alpha code doesnt exist")
evaluate_contract_red_flag.apply_async(
args=(country_alpha_code,),
queue="covid19",
)

if country_code not in country_list:
self.stderr.write("Country code is invalid.")
return

country = Country.objects.get(country_code_alpha_2=country_code)

self.stdout.write(f"Processing Red flag for {country.name}")

country_tenders = Tender.objects.filter(
country_id=country.id, supplier__isnull=False, buyer__isnull=False
).values("id", "buyer__buyer_name", "supplier__supplier_name", "supplier__supplier_address")

for tender in country_tenders:
tender_id = tender["id"]
self.stdout.write("Created task for id :" + str(tender_id))
clear_redflag.apply_async(args=(tender_id,), queue="covid19")
process_redflag6.apply_async(args=(tender_id,), queue="covid19")
process_redflag7.apply_async(args=(tender_id,), queue="covid19")
process_redflag.apply_async(args=(tender_id,), queue="covid19")

self.stdout.write("Done")
return "Done"
12 changes: 0 additions & 12 deletions country/management/commands/fill_contract_values.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def add_arguments(self, parser):
parser.add_argument("country", type=str)

def handle(self, *args, **kwargs):
country = kwargs["country"]
country = kwargs["country"].upper()
country_contract_excel.apply_async(args=(country,), queue="covid19")
183 changes: 96 additions & 87 deletions country/management/commands/generate_excel_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@
class Command(BaseCommand):
help = "Generate Excel Summary"

def add_arguments(self, parser):
parser.add_argument("country", type=str, nargs="?", default=False)

def handle(self, *args, **kwargs):
self.stdout.write("Exporting")
country_alpha_code = kwargs["country"] or None
if country_alpha_code:
country_alpha_code = country_alpha_code.upper()
country = Country.objects.filter(country_code_alpha_2=country_alpha_code).first()
data = get_statistics(country)
OverallSummary.objects.filter(country=country).delete()
OverallSummary.objects.create(statistic=data, country=country)
return "Done"
os.makedirs(os.path.join("media", "export"), exist_ok=True)
workbook = xlsxwriter.Workbook("media/export/Overall Country Summary.xlsx")
worksheet = workbook.add_worksheet()
Expand Down Expand Up @@ -53,93 +64,7 @@ def handle(self, *args, **kwargs):
for country in countries:
columns = 0
row += 1
data = {}
report = (
Tender.objects.filter(country__country_code_alpha_2=country.country_code_alpha_2)
.values("id")
.aggregate(
total_contracts=Count("id", distinct=True),
total_usd=Sum("contract_value_usd"),
total_local=Sum("contract_value_local"),
direct_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="direct")),
limited_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="limited")),
open_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="open")),
selective_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="selective")),
not_identified_method_contracts=Count(
"id", distinct=True, filter=Q(procurement_procedure="not_identified")
),
direct_amount=Sum("contract_value_usd", filter=Q(procurement_procedure="direct")),
limited_amount=Sum("contract_value_usd", filter=Q(procurement_procedure="limited")),
open_amount=Sum("contract_value_usd", filter=Q(procurement_procedure="open")),
selective_amount=Sum(
"contract_value_usd",
filter=Q(procurement_procedure="selective"),
),
not_identified_contracts_amount=Sum(
"contract_value_usd",
filter=Q(procurement_procedure="not_identified"),
),
no_of_buyers=Count("buyer_id", distinct=True),
no_of_suppliers=Count("supplier_id", distinct=True),
time_span_max=Max("contract_date"),
time_span_min=Min("contract_date"),
quantity_red_flag=Count("red_flag"),
sum_red_flag=Sum("contract_value_usd", exclude=Q(red_flag=None)),
quantity_of_equity_contracts=Count("equity_category"),
total_value_of_equity_contracts=Sum("contract_value_usd", exclude=Q(equity_category=None)),
)
)
if report["time_span_max"] and report["time_span_min"] is not None:
timespan = report["time_span_max"] - report["time_span_min"]
else:
timespan = ""
data["country"] = country.name
data["total_contracts"] = report["total_contracts"]
data["total_amount_usd"] = round(report["total_usd"], 2) if report["total_usd"] is not None else 0
data["total_amount_local"] = round(report["total_local"], 2) if report["total_local"] is not None else 0
data["direct_contracts"] = report["direct_contracts"]
data["limited_contracts"] = report["limited_contracts"]
data["open_contracts"] = report["open_contracts"]
data["selective_contracts"] = report["selective_contracts"]
data["not_identified_method_contracts"] = report["not_identified_method_contracts"]
data["direct_contracts_amount"] = (
round(report["direct_amount"], 2) if report["direct_amount"] is not None else 0
)
data["limited_contracts_amount"] = (
round(report["limited_amount"], 2) if report["limited_amount"] is not None else 0
)
data["open_contracts_amount"] = round(report["open_amount"], 2) if report["open_amount"] is not None else 0
data["selective_contracts_amount"] = (
round(report["selective_amount"], 2) if report["selective_amount"] is not None else 0
)
data["not_identified_contracts_amount"] = (
round(report["not_identified_contracts_amount"], 2)
if report["not_identified_contracts_amount"] is not None
else 0
)
data["no_of_buyers"] = report["no_of_buyers"]
data["no_of_suppliers"] = report["no_of_suppliers"]
data["quantity_of_red_flags"] = report["quantity_red_flag"]
data["value_of_red_flag_contracts"] = (
round(report["sum_red_flag"], 2) if report["sum_red_flag"] is not None else 0
)
data["quantity_of_equity_contracts"] = report["quantity_of_equity_contracts"]
data["total_value_of_equity_contracts"] = (
round(report["total_value_of_equity_contracts"], 2)
if report["total_value_of_equity_contracts"] is not None
else 0
)
data["time_span"] = str(timespan)[:-9]
data["gdp_per_capita"] = country.gdp
data["healthcare_budget"] = country.healthcare_budget
data["percentage_of_gdp_to_healthcare_budget"] = country.healthcare_gdp_pc
data["country_data_download"] = (
"https://"
+ socket.gethostbyname(socket.gethostname())
+ "/media/export/"
+ country.name
+ "_summary.xlsx"
)
data = get_statistics(country)

for key, value in data.items():
worksheet.write(row, columns, value)
Expand All @@ -151,3 +76,87 @@ def handle(self, *args, **kwargs):
except Exception as e:
self.stderr.write(str(e))
workbook.close()
return "Finished"


def get_statistics(country):
data = {}
report = (
Tender.objects.filter(country__country_code_alpha_2=country.country_code_alpha_2)
.values("id")
.aggregate(
total_contracts=Count("id", distinct=True),
total_usd=Sum("contract_value_usd"),
total_local=Sum("contract_value_local"),
direct_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="direct")),
limited_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="limited")),
open_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="open")),
selective_contracts=Count("id", distinct=True, filter=Q(procurement_procedure="selective")),
not_identified_method_contracts=Count(
"id", distinct=True, filter=Q(procurement_procedure="not_identified")
),
direct_amount=Sum("contract_value_usd", filter=Q(procurement_procedure="direct")),
limited_amount=Sum("contract_value_usd", filter=Q(procurement_procedure="limited")),
open_amount=Sum("contract_value_usd", filter=Q(procurement_procedure="open")),
selective_amount=Sum(
"contract_value_usd",
filter=Q(procurement_procedure="selective"),
),
not_identified_contracts_amount=Sum(
"contract_value_usd",
filter=Q(procurement_procedure="not_identified"),
),
no_of_buyers=Count("buyer_id", distinct=True),
no_of_suppliers=Count("supplier_id", distinct=True),
time_span_max=Max("contract_date"),
time_span_min=Min("contract_date"),
quantity_red_flag=Count("red_flag"),
sum_red_flag=Sum("contract_value_usd", exclude=Q(red_flag=None)),
quantity_of_equity_contracts=Count("equity_category"),
total_value_of_equity_contracts=Sum("contract_value_usd", exclude=Q(equity_category=None)),
)
)
if report["time_span_max"] and report["time_span_min"] is not None:
timespan = report["time_span_max"] - report["time_span_min"]
else:
timespan = ""
data["country"] = country.name
data["total_contracts"] = report["total_contracts"]
data["total_amount_usd"] = round(report["total_usd"], 2) if report["total_usd"] is not None else 0
data["total_amount_local"] = round(report["total_local"], 2) if report["total_local"] is not None else 0
data["direct_contracts"] = report["direct_contracts"]
data["limited_contracts"] = report["limited_contracts"]
data["open_contracts"] = report["open_contracts"]
data["selective_contracts"] = report["selective_contracts"]
data["not_identified_method_contracts"] = report["not_identified_method_contracts"]
data["direct_contracts_amount"] = round(report["direct_amount"], 2) if report["direct_amount"] is not None else 0
data["limited_contracts_amount"] = (
round(report["limited_amount"], 2) if report["limited_amount"] is not None else 0
)
data["open_contracts_amount"] = round(report["open_amount"], 2) if report["open_amount"] is not None else 0
data["selective_contracts_amount"] = (
round(report["selective_amount"], 2) if report["selective_amount"] is not None else 0
)
data["not_identified_contracts_amount"] = (
round(report["not_identified_contracts_amount"], 2)
if report["not_identified_contracts_amount"] is not None
else 0
)
data["no_of_buyers"] = report["no_of_buyers"]
data["no_of_suppliers"] = report["no_of_suppliers"]
data["quantity_of_red_flags"] = report["quantity_red_flag"]
data["value_of_red_flag_contracts"] = round(report["sum_red_flag"], 2) if report["sum_red_flag"] is not None else 0
data["quantity_of_equity_contracts"] = report["quantity_of_equity_contracts"]
data["total_value_of_equity_contracts"] = (
round(report["total_value_of_equity_contracts"], 2)
if report["total_value_of_equity_contracts"] is not None
else 0
)
data["time_span"] = str(timespan)[:-9]
data["gdp_per_capita"] = country.gdp
data["healthcare_budget"] = country.healthcare_budget
data["percentage_of_gdp_to_healthcare_budget"] = country.healthcare_gdp_pc
data["country_data_download"] = (
"https://" + socket.gethostbyname(socket.gethostname()) + "/media/export/" + country.name + "_summary.xlsx"
)
return data
2 changes: 1 addition & 1 deletion country/management/commands/summarize_country_buyer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def add_arguments(self, parser):
parser.add_argument("country", type=str)

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"]
country_alpha_code = kwargs["country"].upper()
try:
country = Country.objects.get(country_code_alpha_2=country_alpha_code)
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion country/management/commands/summarize_country_supplier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def add_arguments(self, parser):
parser.add_argument("country", type=str)

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"]
country_alpha_code = kwargs["country"].upper()
try:
country = Country.objects.get(country_code_alpha_2=country_alpha_code)
except Exception:
Expand Down
Loading

0 comments on commit facbf4b

Please sign in to comment.