Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get government function data from V2 cube for expediture breakdown #680

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
31 changes: 21 additions & 10 deletions scorecard/profile_data/api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,7 @@ def get_queries(self):
"cube": "incexp_v2",
"aggregate": "amount.sum",
"cut": {
"item.code": [
"2000", "2100", "2200", "2300", "2400",
"2500", "2600", "2700", "2800", "2900",
"3000",
],
"item.code": V2_SPENDING_CODES,
"amount_type.code": ["ADJB"],
"demarcation.code": [self.geo_code],
"period_length.length": ["year"],
Expand Down Expand Up @@ -480,11 +476,7 @@ def get_queries(self):
"cube": "incexp_v2",
"aggregate": "amount.sum",
"cut": {
"item.code": [
"2000", "2100", "2200", "2300", "2400",
"2500", "2600", "2700", "2800", "2900",
"3000",
],
"item.code": V2_SPENDING_CODES,
"amount_type.code": ["AUDA"],
"demarcation.code": [self.geo_code],
"period_length.length": ["year"],
Expand Down Expand Up @@ -863,6 +855,25 @@ def get_queries(self):
"results_structure": self.noop_structure,
"order": "financial_year_end.year:desc,function.category_label:asc",
},
"expenditure_functional_breakdown_v2": {
"cube": "incexp_v2",
"aggregate": "amount.sum",
"cut": {
"item.code": V2_SPENDING_CODES,
"amount_type.code": ["AUDA"],
"demarcation.code": [self.geo_code],
"period_length.length": ["year"],
"financial_year_end.year": self.years + [self.budget_year],
},
"drilldown": [
"function.category_label",
"financial_year_end.year",
"amount_type.code",
],
"query_type": "aggregate",
"results_structure": self.noop_structure,
"order": "financial_year_end.year:desc,function.category_label:asc",
},
"expenditure_trends": {
"cube": "incexp",
"aggregate": "amount.sum",
Expand Down
33 changes: 25 additions & 8 deletions scorecard/profile_data/indicators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from collections import defaultdict


def get_indicator_calculators(has_comparisons=None):
calculators = [
CashCoverage,
Expand Down Expand Up @@ -71,8 +72,12 @@ def get_muni_specifics(cls, api_data):
items = v1_data[year]

results = {
"local": {"amount": 0, },
"government": {"amount": 0, },
"local": {
"amount": 0,
},
"government": {
"amount": 0,
},
"year": year,
"ref": api_data.references["lges"],
}
Expand Down Expand Up @@ -110,7 +115,6 @@ class LocalRevenueBreakdown(IndicatorCalculator):

@classmethod
def get_muni_specifics(cls, api_data):

v1_groups = [
("Property rates", ["0200", "0300"]),
("Service Charges", ["0400"]),
Expand Down Expand Up @@ -194,7 +198,11 @@ def get_muni_specifics(cls, api_data):
contracting_code = "4200"

total = sum(x["amount.sum"] for x in results)
contracting_items = [x["amount.sum"] for x in results if x["item.code"] == contracting_code]
contracting_items = [
x["amount.sum"]
for x in results
if x["item.code"] == contracting_code
]
contracting = percent(contracting_items[0], total)
# Prefer KeyError but crash before we use it in case we have more than expectexd
assert len(contracting_items) <= 1
Expand All @@ -203,7 +211,11 @@ def get_muni_specifics(cls, api_data):
contracting = None

values.append(
{"date": year, "result": contracting, "rating": "", }
{
"date": year,
"result": contracting,
"rating": "",
}
)

return {
Expand Down Expand Up @@ -246,7 +258,11 @@ def get_muni_specifics(cls, api_data):
staff = None

values.append(
{"date": year, "result": staff, "rating": "", }
{
"date": year,
"result": staff,
"rating": "",
}
)

return {
Expand All @@ -270,6 +286,7 @@ def get_muni_specifics(cls, api_data):
GAPD_label = "Governance, Administration, Planning and Development"

results = api_data.results["expenditure_functional_breakdown"]
results.extend(api_data.results["expenditure_functional_breakdown_v2"])
grouped_results = []

for year, yeargroup in groupby(results, lambda r: r["financial_year_end.year"]):
Expand Down Expand Up @@ -311,6 +328,6 @@ def get_muni_specifics(cls, api_data):
except (KeyError, IndexError):
continue

grouped_results = sorted(
grouped_results, key=lambda r: (r["date"], r["item"]))
grouped_results = sorted(grouped_results, key=lambda r: (r["date"], r["item"]))

return {"values": grouped_results}
Loading