forked from OCHA-DAP/hapi-pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipelines.py
376 lines (352 loc) · 14.1 KB
/
pipelines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import logging
from datetime import datetime
from typing import Dict, Optional
from hdx.api.configuration import Configuration
from hdx.api.utilities.hdx_error_handler import HDXErrorHandler
from hdx.location.adminlevel import AdminLevel
from hdx.scraper.framework.runner import Runner
from hdx.scraper.framework.utilities.reader import Read
from hdx.scraper.framework.utilities.sources import Sources
from hdx.utilities.typehint import ListTuple
from sqlalchemy.orm import Session
from hapi.pipelines.database.admins import Admins
from hapi.pipelines.database.conflict_event import ConflictEvent
from hapi.pipelines.database.currency import Currency
from hapi.pipelines.database.food_price import FoodPrice
from hapi.pipelines.database.food_security import FoodSecurity
from hapi.pipelines.database.funding import Funding
from hapi.pipelines.database.humanitarian_needs import HumanitarianNeeds
from hapi.pipelines.database.idps import IDPs
from hapi.pipelines.database.locations import Locations
from hapi.pipelines.database.metadata import Metadata
from hapi.pipelines.database.national_risk import NationalRisk
from hapi.pipelines.database.operational_presence import OperationalPresence
from hapi.pipelines.database.org import Org
from hapi.pipelines.database.org_type import OrgType
from hapi.pipelines.database.population import Population
from hapi.pipelines.database.poverty_rate import PovertyRate
from hapi.pipelines.database.refugees_and_returnees import RefugeesAndReturnees
from hapi.pipelines.database.sector import Sector
from hapi.pipelines.database.wfp_commodity import WFPCommodity
from hapi.pipelines.database.wfp_market import WFPMarket
logger = logging.getLogger(__name__)
class Pipelines:
def __init__(
self,
configuration: Configuration,
session: Session,
today: datetime,
themes_to_run: Optional[Dict] = None,
scrapers_to_run: Optional[ListTuple[str]] = None,
error_handler: Optional[HDXErrorHandler] = None,
use_live: bool = True,
countries_to_run: Optional[ListTuple[str]] = None,
):
self._configuration = configuration
self._session = session
self._themes_to_run = themes_to_run
self._locations = Locations(
configuration=configuration,
session=session,
use_live=use_live,
countries=countries_to_run,
)
self._countries = self._locations.hapi_countries
self._error_handler = error_handler
reader = Read.get_reader("hdx")
libhxl_dataset = AdminLevel.get_libhxl_dataset(
retriever=reader
).cache()
libhxl_format_dataset = AdminLevel.get_libhxl_dataset(
url=AdminLevel.formats_url, retriever=reader
).cache()
self._admins = Admins(
configuration,
session,
self._locations,
libhxl_dataset,
error_handler,
)
admin1_config = configuration["admin1"]
self._adminone = AdminLevel(admin_config=admin1_config, admin_level=1)
admin2_config = configuration["admin2"]
self._admintwo = AdminLevel(admin_config=admin2_config, admin_level=2)
self._adminone.setup_from_libhxl_dataset(libhxl_dataset)
self._adminone.load_pcode_formats_from_libhxl_dataset(
libhxl_format_dataset
)
self._admintwo.setup_from_libhxl_dataset(libhxl_dataset)
self._admintwo.load_pcode_formats_from_libhxl_dataset(
libhxl_format_dataset
)
self._admintwo.set_parent_admins_from_adminlevels([self._adminone])
logger.info("Admin one name mappings:")
self._adminone.output_admin_name_mappings()
logger.info("Admin two name mappings:")
self._admintwo.output_admin_name_mappings()
logger.info("Admin two name replacements:")
self._admintwo.output_admin_name_replacements()
self._org_type = OrgType(
session=session,
)
self._sector = Sector(
session=session,
)
self._currency = Currency(session=session, configuration=configuration)
Sources.set_default_source_date_format("%Y-%m-%d")
self._runner = Runner(
self._countries,
today=today,
error_handler=error_handler,
scrapers_to_run=scrapers_to_run,
)
self._configurable_scrapers = {}
self.create_configurable_scrapers()
self._metadata = Metadata(
runner=self._runner, session=session, today=today
)
def setup_configurable_scrapers(
self, prefix, level, suffix_attribute=None, adminlevel=None
):
if self._themes_to_run:
if prefix not in self._themes_to_run:
return None, None, None, None
countryiso3s = self._themes_to_run[prefix]
else:
countryiso3s = None
source_configuration = Sources.create_source_configuration(
suffix_attribute=suffix_attribute,
admin_sources=True,
adminlevel=adminlevel,
)
suffix = f"_{level}"
if countryiso3s:
configuration = {}
# This assumes format prefix_iso_.... eg.
# population_gtm
iso3_index = len(prefix) + 1
for key, value in self._configuration[f"{prefix}{suffix}"].items():
if len(key) < iso3_index + 3:
continue
countryiso3 = key[iso3_index : iso3_index + 3]
if countryiso3.upper() not in countryiso3s:
continue
configuration[key] = value
else:
configuration = self._configuration[f"{prefix}{suffix}"]
return configuration, source_configuration, suffix, countryiso3s
def create_configurable_scrapers(self):
def _create_configurable_scrapers(
prefix, level, suffix_attribute=None, adminlevel=None
):
configuration, source_configuration, suffix, countryiso3s = (
self.setup_configurable_scrapers(
prefix, level, suffix_attribute, adminlevel
)
)
if not configuration:
return
scraper_names = self._runner.add_configurables(
configuration,
level,
adminlevel=adminlevel,
source_configuration=source_configuration,
suffix=suffix,
countryiso3s=countryiso3s,
)
current_scrapers = self._configurable_scrapers.get(prefix, [])
self._configurable_scrapers[prefix] = (
current_scrapers + scraper_names
)
_create_configurable_scrapers("national_risk", "national")
_create_configurable_scrapers("refugees_and_returnees", "national")
_create_configurable_scrapers("idps", "national")
_create_configurable_scrapers(
"idps", "adminone", adminlevel=self._adminone
)
_create_configurable_scrapers(
"idps", "admintwo", adminlevel=self._admintwo
)
_create_configurable_scrapers("conflict_event", "national")
_create_configurable_scrapers(
"conflict_event", "admintwo", adminlevel=self._admintwo
)
def run(self):
self._runner.run()
def output_population(self):
if not self._themes_to_run or "population" in self._themes_to_run:
population = Population(
session=self._session,
metadata=self._metadata,
admins=self._admins,
configuration=self._configuration,
error_handler=self._error_handler,
)
population.populate()
def output_operational_presence(self):
if (
not self._themes_to_run
or "operational_presence" in self._themes_to_run
):
org = Org(
session=self._session,
metadata=self._metadata,
configuration=self._configuration,
)
org.populate()
operational_presence = OperationalPresence(
session=self._session,
metadata=self._metadata,
admins=self._admins,
configuration=self._configuration,
error_handler=self._error_handler,
)
operational_presence.populate()
def output_food_security(self):
if not self._themes_to_run or "food_security" in self._themes_to_run:
food_security = FoodSecurity(
session=self._session,
metadata=self._metadata,
admins=self._admins,
adminone=self._adminone,
admintwo=self._admintwo,
countryiso3s=self._countries,
configuration=self._configuration,
error_handler=self._error_handler,
)
food_security.populate()
def output_humanitarian_needs(self):
if (
not self._themes_to_run
or "humanitarian_needs" in self._themes_to_run
):
humanitarian_needs = HumanitarianNeeds(
session=self._session,
metadata=self._metadata,
admins=self._admins,
configuration=self._configuration,
error_handler=self._error_handler,
)
humanitarian_needs.populate()
def output_national_risk(self):
if not self._themes_to_run or "national_risk" in self._themes_to_run:
results = self._runner.get_hapi_results(
self._configurable_scrapers["national_risk"]
)
national_risk = NationalRisk(
session=self._session,
metadata=self._metadata,
locations=self._locations,
results=results,
)
national_risk.populate()
def output_refugees_and_returnees(self):
if (
not self._themes_to_run
or "refugees_and_returnees" in self._themes_to_run
):
results = self._runner.get_hapi_results(
self._configurable_scrapers["refugees_and_returnees"]
)
refugees_and_returnees = RefugeesAndReturnees(
session=self._session,
metadata=self._metadata,
locations=self._locations,
results=results,
)
refugees_and_returnees.populate()
def output_idps(self):
if not self._themes_to_run or "idps" in self._themes_to_run:
results = self._runner.get_hapi_results(
self._configurable_scrapers["idps"]
)
idps = IDPs(
session=self._session,
metadata=self._metadata,
admins=self._admins,
results=results,
error_handler=self._error_handler,
)
idps.populate()
def output_funding(self):
if not self._themes_to_run or "funding" in self._themes_to_run:
funding = Funding(
session=self._session,
metadata=self._metadata,
countryiso3s=self._countries,
locations=self._locations,
configuration=self._configuration,
error_handler=self._error_handler,
)
funding.populate()
def output_poverty_rate(self):
if not self._themes_to_run or "poverty_rate" in self._themes_to_run:
poverty_rate = PovertyRate(
session=self._session,
metadata=self._metadata,
admins=self._admins,
configuration=self._configuration,
error_handler=self._error_handler,
)
poverty_rate.populate()
def output_conflict_event(self):
if not self._themes_to_run or "conflict_event" in self._themes_to_run:
results = self._runner.get_hapi_results(
self._configurable_scrapers["conflict_event"]
)
conflict_event = ConflictEvent(
session=self._session,
metadata=self._metadata,
admins=self._admins,
results=results,
configuration=self._configuration,
error_handler=self._error_handler,
)
conflict_event.populate()
def output_food_prices(self):
if not self._themes_to_run or "food_prices" in self._themes_to_run:
wfp_commodity = WFPCommodity(
session=self._session,
datasetinfo=self._configuration["wfp_commodity"],
)
wfp_commodity.populate()
wfp_market = WFPMarket(
session=self._session,
datasetinfo=self._configuration["wfp_market"],
countryiso3s=self._countries,
admins=self._admins,
adminone=self._adminone,
admintwo=self._admintwo,
configuration=self._configuration,
error_handler=self._error_handler,
)
wfp_market.populate()
food_price = FoodPrice(
session=self._session,
datasetinfo=self._configuration["wfp_countries"],
countryiso3s=self._countries,
metadata=self._metadata,
currency=self._currency,
commodity=wfp_commodity,
market=wfp_market,
error_handler=self._error_handler,
)
food_price.populate()
def output(self):
self._locations.populate()
self._admins.populate()
self._metadata.populate()
self._org_type.populate()
self._sector.populate()
self._currency.populate()
self.output_population()
self.output_operational_presence()
self.output_food_security()
self.output_humanitarian_needs()
self.output_national_risk()
self.output_refugees_and_returnees()
self.output_idps()
self.output_funding()
self.output_poverty_rate()
self.output_conflict_event()
self.output_food_prices()