def handle_awards(filters: dict, results_object: dict) -> dict: """Turn the filters into the result dictionary when dealing with Awards For performance reasons, there are two execution paths. One is to use a "summary" award materialized view which contains a subset of fields and has some aggregation to speed up the large general queries. For more specific queries, use concurrent SQL queries to obtain the counts """ queryset, model = spending_by_award_count(filters) # Will return None, None if it cannot use a summary matview if not model: # DON'T use `queryset` in the conditional! Wasteful DB query return fetch_all_category_counts(filters, category_to_award_materialized_views()) queryset = queryset.values("type").annotate(category_count=Sum("counts")) for award in queryset: if award["type"] is None or award["type"] not in all_awards_types_to_category: result_key = "other" else: result_key = all_awards_types_to_category[award["type"]] if result_key == "other_financial_assistance": result_key = "other" results_object[result_key] += award["category_count"] return results_object
def post(self, request): models = [{'name': 'subawards', 'key': 'subawards', 'type': 'boolean', 'default': False}] models.extend(copy.deepcopy(AWARD_FILTER)) models.extend(copy.deepcopy(PAGINATION)) json_request = TinyShield(models).block(request.data) filters = json_request.get("filters", None) subawards = json_request["subawards"] if filters is None: raise InvalidParameterException("Missing required request parameters: 'filters'") results = { "contracts": 0, "idvs": 0, "grants": 0, "direct_payments": 0, "loans": 0, "other": 0 } if not subawards else { "subcontracts": 0, "subgrants": 0 } if "award_type_codes" in filters and "no intersection" in filters["award_type_codes"]: # "Special case": there will never be results when the website provides this value return Response({"results": results}) if subawards: queryset = subaward_filter(filters) else: queryset, model = spending_by_award_count(filters) if subawards: queryset = queryset.values('award_type').annotate(category_count=Count('subaward_id')) elif model == 'SummaryAwardView': queryset = queryset.values('category').annotate(category_count=Sum('counts')) else: queryset = queryset.values('category').annotate(category_count=Count('category')) categories = { 'contract': 'contracts', 'idv': 'idvs', 'grant': 'grants', 'direct payment': 'direct_payments', 'loans': 'loans', 'other': 'other' } if not subawards else {'procurement': 'subcontracts', 'grant': 'subgrants'} category_name = 'category' if not subawards else 'award_type' # DB hit here for award in queryset: if award[category_name] is None: result_key = 'other' if not subawards else 'subcontracts' elif award[category_name] not in categories.keys(): result_key = 'other' else: result_key = categories[award[category_name]] results[result_key] += award['category_count'] return Response({"results": results})
def post(self, request): models = [{'name': 'subawards', 'key': 'subawards', 'type': 'boolean', 'default': False}] models.extend(copy.deepcopy(AWARD_FILTER)) models.extend(copy.deepcopy(PAGINATION)) json_request = TinyShield(models).block(request.data) filters = json_request.get("filters", None) subawards = json_request["subawards"] if filters is None: raise InvalidParameterException("Missing required request parameters: 'filters'") results = { "contracts": 0, "idvs": 0, "grants": 0, "direct_payments": 0, "loans": 0, "other": 0 } if not subawards else { "subcontracts": 0, "subgrants": 0 } if "award_type_codes" in filters and "no intersection" in filters["award_type_codes"]: # "Special case": there will never be results when the website provides this value return Response({"results": results}) if subawards: queryset = subaward_filter(filters) else: queryset, model = spending_by_award_count(filters) if subawards: queryset = queryset.values('award_type').annotate(category_count=Count('subaward_id')) elif model == 'SummaryAwardView': queryset = queryset.values('category').annotate(category_count=Sum('counts')) else: queryset = queryset.values('category').annotate(category_count=Count('category')) categories = { 'contract': 'contracts', 'idv': 'idvs', 'grant': 'grants', 'direct payment': 'direct_payments', 'loans': 'loans', 'other': 'other' } if not subawards else {'procurement': 'subcontracts', 'grant': 'subgrants'} category_name = 'category' if not subawards else 'award_type' # DB hit here for award in queryset: if award[category_name] is None: result_key = 'other' if not subawards else 'subcontracts' elif award[category_name] not in categories.keys(): result_key = 'other' else: result_key = categories[award[category_name]] results[result_key] += award['category_count'] return Response({"results": results})
def post(self, request): """Return all budget function/subfunction titles matching the provided search text""" json_request = request.data filters = json_request.get("filters", None) if filters is None: raise InvalidParameterException( "Missing one or more required request parameters: filters") queryset, model = spending_by_award_count(filters) if model == 'SummaryAwardView': queryset = queryset \ .values("category") \ .annotate(category_count=Sum('counts')) else: # for IDV CONTRACTS category is null. change to contract queryset = queryset \ .values('category') \ .annotate(category_count=Count(Coalesce('category', Value('contract')))) \ .values('category', 'category_count') results = { "contracts": 0, "grants": 0, "direct_payments": 0, "loans": 0, "other": 0 } categories = { 'contract': 'contracts', 'grant': 'grants', 'direct payment': 'direct_payments', 'loans': 'loans', 'other': 'other' } # DB hit here for award in queryset: if award['category'] is None: result_key = 'contracts' elif award['category'] not in categories.keys(): result_key = 'other' else: result_key = categories[award['category']] results[result_key] += award['category_count'] # build response return Response({"results": results})
def post(self, request): """Return all budget function/subfunction titles matching the provided search text""" models = [{ 'name': 'subawards', 'key': 'subawards', 'type': 'boolean', 'default': False }] models.extend(copy.deepcopy(AWARD_FILTER)) models.extend(copy.deepcopy(PAGINATION)) '''for m in models: if m['name'] in ('award_type_codes', 'fields'): m['optional'] = True''' json_request = TinyShield(models).block(request.data) filters = json_request.get("filters", None) subawards = json_request["subawards"] if filters is None: raise InvalidParameterException( "Missing one or more required request parameters: filters") results = { "contracts": 0, "grants": 0, "direct_payments": 0, "loans": 0, "other": 0 } if not subawards else { "subcontracts": 0, "subgrants": 0 } if "award_type_codes" in filters and "no intersection" in filters[ "award_type_codes"]: # "Special case": there will never be results when the website provides this value return Response({"results": results}) if subawards: # We do not use matviews for Subaward filtering, just the Subaward download filters queryset = subaward_filter(filters) else: queryset, model = spending_by_award_count(filters) if subawards: queryset = queryset \ .values('award_type') \ .annotate(category_count=Count('subaward_id')) elif model == 'SummaryAwardView': queryset = queryset \ .values('category') \ .annotate(category_count=Sum('counts')) else: # for IDV CONTRACTS category is null. change to contract queryset = queryset \ .values('category') \ .annotate(category_count=Count(Coalesce('category', Value('contract')))) \ .values('category', 'category_count') categories = { 'contract': 'contracts', 'grant': 'grants', 'direct payment': 'direct_payments', 'loans': 'loans', 'other': 'other' } if not subawards else { 'procurement': 'subcontracts', 'grant': 'subgrants' } category_name = 'category' if not subawards else 'award_type' # DB hit here for award in queryset: if award[category_name] is None: result_key = 'contracts' if not subawards else 'subcontracts' elif award[category_name] not in categories.keys(): result_key = 'other' else: result_key = categories[award[category_name]] results[result_key] += award['category_count'] # build response return Response({"results": results})
def post(self, request): """Return all budget function/subfunction titles matching the provided search text""" json_request = request.data filters = json_request.get("filters", None) subawards = json_request.get("subawards", False) if filters is None: raise InvalidParameterException("Missing one or more required request parameters: filters") if type(subawards) is not bool: raise InvalidParameterException("subawards does not have a valid value") if subawards: # We do not use matviews for Subaward filtering, just the Subaward download filters queryset = subaward_filter(filters) else: queryset, model = spending_by_award_count(filters) if subawards: queryset = queryset \ .values('award_type') \ .annotate(category_count=Count('id')) elif model == 'SummaryAwardView': queryset = queryset \ .values('category') \ .annotate(category_count=Sum('counts')) else: # for IDV CONTRACTS category is null. change to contract queryset = queryset \ .values('category') \ .annotate(category_count=Count(Coalesce('category', Value('contract')))) \ .values('category', 'category_count') results = { "contracts": 0, "grants": 0, "direct_payments": 0, "loans": 0, "other": 0 } if not subawards else { "subcontracts": 0, "subgrants": 0 } categories = { 'contract': 'contracts', 'grant': 'grants', 'direct payment': 'direct_payments', 'loans': 'loans', 'other': 'other' } if not subawards else {'procurement': 'subcontracts', 'grant': 'subgrants'} category_name = 'category' if not subawards else 'award_type' # DB hit here for award in queryset: if award[category_name] is None: result_key = 'contracts' if not subawards else 'subcontracts' elif award[category_name] not in categories.keys(): result_key = 'other' else: result_key = categories[award[category_name]] results[result_key] += award['category_count'] # build response return Response({"results": results})