Skip to content

Commit 33d03e5

Browse files
committed
feat: add country-specific renewables retrieval and update CEVS computation to use Parquet datasets
1 parent f77c6a8 commit 33d03e5

2 files changed

Lines changed: 58 additions & 15 deletions

File tree

api/clients/eea_client.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ def get_countries_renewables(self) -> List[Dict[str, Any]]:
9494
})
9595
return normalized_data
9696

97+
def get_country_renewables(self, country: Optional[str]) -> Optional[Dict[str, Any]]:
98+
"""
99+
Mengambil data energi terbarukan untuk negara tertentu.
100+
"""
101+
if not country:
102+
return None
103+
104+
all_countries = self.get_countries_renewables()
105+
country_lower = country.strip().lower()
106+
107+
for record in all_countries:
108+
if record.get("country", "").strip().lower() == country_lower:
109+
return record
110+
return None
111+
97112
def get_industrial_pollution(self) -> List[Dict[str, Any]]:
98113
"""
99114
Mengambil dan menormalkan data tren polusi industri.
@@ -102,9 +117,46 @@ def get_industrial_pollution(self) -> List[Dict[str, Any]]:
102117
dataset_id = "industrial-releases-of-pollutants-to-water"
103118
raw_data = self._get_parquet_data(dataset_id)
104119

105-
# Logika normalisasi Anda sebelumnya sudah bagus, dapat diterapkan di sini
106-
# ...
120+
normalized_data = []
121+
for record in raw_data:
122+
year = record.get("year")
123+
if not year:
124+
continue
125+
126+
def to_float(v):
127+
try:
128+
return float(v) if v not in (None, "") else None
129+
except Exception:
130+
return None
131+
132+
normalized_data.append({
133+
"year": int(year),
134+
"cd_hg_ni_pb": to_float(record.get("cd_hg_ni_pb")),
135+
"toc": to_float(record.get("toc")),
136+
"total_n": to_float(record.get("total_n")),
137+
"total_p": to_float(record.get("total_p")),
138+
"gva": to_float(record.get("gva")),
139+
})
107140

108-
return raw_data # Kembalikan data yang dinormalisasi
141+
# Sort by year
142+
normalized_data.sort(key=lambda x: x.get("year", 0))
143+
return normalized_data
144+
145+
def compute_pollution_trend(self, records: List[Dict[str, Any]], window: int = 3) -> Dict[str, Any]:
146+
"""
147+
Menghitung tren sederhana berdasarkan data polusi.
148+
"""
149+
def slope_for(key: str) -> Dict[str, Any]:
150+
vals = [r.get(key) for r in records if isinstance(r.get(key), (int, float))]
151+
if len(vals) < 2:
152+
return {"slope": 0.0, "increase": False}
153+
sel = vals[-window:] if len(vals) >= window else vals
154+
s = float(sel[-1] - sel[0])
155+
return {"slope": s, "increase": s > 0.0}
156+
157+
return {
158+
"total_n": slope_for("total_n"),
159+
"total_p": slope_for("total_p")
160+
}
109161

110162
__all__ = ["EEAClient"]

api/services/cevs_aggregator.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ def compute_cevs_for_company(company_name: str, *, company_country: Optional[str
4343
iso_norm = iso_client.get_iso14001_certifications(country=company_country, limit=100)
4444
has_iso = any(_normalize_name(r.get("nama_perusahaan")) and company_key in _normalize_name(r.get("nama_perusahaan")) for r in iso_norm)
4545

46-
# EEA: use indicator sample plus new CSV-based datasets (renewables and industrial pollution)
46+
# EEA: use new Parquet-based datasets (renewables and industrial pollution)
4747
eea_client = EEAClient()
48-
eea_norm = eea_client.get_indicator(country=company_country or None, limit=50)
4948
# New: country renewables row and EU average row for comparison
5049
renew_row = eea_client.get_country_renewables(company_country) if company_country else None
5150
renew_all = eea_client.get_countries_renewables()
@@ -71,7 +70,6 @@ def compute_cevs_for_company(company_name: str, *, company_country: Optional[str
7170
"base": 50.0,
7271
"iso_bonus": 0.0,
7372
"epa_penalty": 0.0,
74-
"eea_bonus": 0.0,
7573
"renewables_bonus": 0.0,
7674
"pollution_penalty": 0.0,
7775
"policy_bonus": 0.0,
@@ -86,11 +84,6 @@ def compute_cevs_for_company(company_name: str, *, company_country: Optional[str
8684
components["epa_penalty"] = -epa_penalty
8785
score -= epa_penalty
8886

89-
# EEA bonus placeholder: presence of any indicator entries yields small boost
90-
eea_bonus = 5.0 if eea_norm else 0.0
91-
components["eea_bonus"] = eea_bonus
92-
score += eea_bonus
93-
9487
# Renewables bonus (dynamic): reward exceeding target and EU average
9588
renew_bonus = 0.0
9689
renew_details: Dict[str, Any] = {}
@@ -204,17 +197,15 @@ def slope_for(key: str) -> Dict[str, Any]:
204197
"sources": {
205198
"epa_matches": len(epa_matches),
206199
"iso_count": len(iso_norm),
207-
"eea_count": len(eea_norm),
208-
"renewables_source": os.getenv("EEA_RENEWABLES_SOURCE") or os.getenv("EEA_CSV_URL") or "local:countries-breakdown-actual-res-progress-13.csv",
209-
"pollution_source": os.getenv("EEA_POLLUTION_SOURCE") or "local:industrial-releases-of-pollutants-to.csv",
200+
"renewables_source": os.getenv("EEA_RENEWABLES_SOURCE") or "EEA Parquet API",
201+
"pollution_source": os.getenv("EEA_POLLUTION_SOURCE") or "EEA Parquet API",
210202
"edgar_source": os.getenv("EDGAR_XLSX_PATH") or "local:EDGAR_emiss_on_UCDB_2024.xlsx",
211203
"policy_source": os.getenv("POLICY_XLSX_PATH") or "local:Annex III_Best practices and justifications.xlsx",
212204
"pollution_trend_source": os.getenv("CEVS_POLLUTION_SOURCE") or "auto",
213205
},
214206
"details": {
215207
"epa": epa_matches,
216208
"iso": iso_norm,
217-
"eea": eea_norm,
218209
"renewables": {"country_row": renew_row, "eu_row": eu_row, "bonus_calc": renew_details},
219210
"pollution_trend": pol_details or pol_trend,
220211
"policy": policy_details,

0 commit comments

Comments
 (0)