-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
308 lines (274 loc) · 11.2 KB
/
extract.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
import argparse
import copy
import json
import logging
import os
import time
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class CountryProcessor:
def __init__(self, config_json=None, language_json="language.json"):
if config_json is None:
raise ValueError("Config JSON couldn't be found")
if isinstance(config_json, dict):
self.config = config_json
elif os.path.exists(config_json):
with open(config_json) as f:
self.config = json.load(f)
else:
raise ValueError("Invalid value for config_json")
self.languages = None
if isinstance(language_json, dict):
self.languages = language_json
elif os.path.exists(language_json):
with open(language_json) as f:
self.languages = json.load(f)
self.RAW_DATA_API_BASE_URL = os.environ.get("RAW_DATA_API_BASE_URL")
self.RAWDATA_API_AUTH_TOKEN = os.environ.get("RAWDATA_API_AUTH_TOKEN")
def generate_filtered_config(self, export):
config_temp = copy.deepcopy(self.config)
for key in export["properties"].keys():
# overwrite config.json keys if it is already in predefined export keys
config_temp[key] = export["properties"].get(key)
print(config_temp.get("iso3"))
if config_temp.get("iso3"):
if self.languages:
language_select = self.languages.get(config_temp.get("iso3"))
if language_select:
for category_group in config_temp.get("categories"):
for category in category_group:
category_group[category]["select"] = (
category_group[category].get("select") + language_select
)
return json.dumps(config_temp)
def process_export(self, export):
request_config = self.generate_filtered_config(export)
response = self.retry_post_request(request_config)
return response
def retry_post_request(self, request_config):
retry_strategy = Retry(
total=2, # Number of retries
status_forcelist=[429, 502],
allowed_methods=["POST"],
backoff_factor=1,
)
adapter = HTTPAdapter(max_retries=retry_strategy)
with requests.Session() as req_session:
req_session.mount("https://", adapter)
req_session.mount("http://", adapter)
try:
HEADERS = {
"Content-Type": "application/json",
"Access-Token": self.RAWDATA_API_AUTH_TOKEN,
}
RAW_DATA_SNAPSHOT_URL = f"{self.RAW_DATA_API_BASE_URL}/custom/snapshot/"
response = req_session.post(
RAW_DATA_SNAPSHOT_URL,
headers=HEADERS,
data=request_config,
timeout=10,
)
response.raise_for_status()
return response.json()["task_id"]
except requests.exceptions.RetryError as e:
self.handle_rate_limit()
return self.retry_post_request(request_config)
def handle_rate_limit(self):
logging.warning("Rate limit reached. Waiting for 1 minute before retrying.")
time.sleep(61)
def retry_get_request(self, url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
logging.error("Error in GET request: %s", str(e))
return {"status": "ERROR"}
def track_tasks_status(self, task_ids):
results = {}
for task_id in task_ids:
status_url = f"{self.RAW_DATA_API_BASE_URL}/tasks/status/{task_id}/"
response = self.retry_get_request(status_url)
if response["status"] == "SUCCESS":
results[task_id] = response["result"]
elif response["status"] in ["PENDING", "STARTED"]:
while True:
response = self.retry_get_request(status_url)
if response["status"] in ["SUCCESS", "ERROR", "FAILURE"]:
results[task_id] = response["result"]
logging.info(
"Task %s is %s , Moving to fetch next one",
task_id,
response["status"],
)
break
logging.warning(
"Task %s is %s. Retrying in 30 seconds...",
task_id,
response["status"],
)
time.sleep(30)
else:
results[task_id] = "FAILURE"
logging.info("%s tasks stats is fetched, Dumping result", len(results))
with open("result.json", "w") as f:
json.dump(results, f, indent=2)
logging.info("Done ! Find result at result.json")
def clean_hdx_export_response(self, feature):
feature["properties"].pop("id")
feature["properties"]["dataset"]["dataset_locations"] = list(
feature["properties"]["dataset"]["dataset_locations"]
)
feature["properties"].pop("cid")
if feature["properties"].get("categories") is None:
feature["properties"].pop("categories")
if feature["geometry"].get("type") is None:
feature.pop("geometry")
else:
feature.pop("iso3")
return feature
def get_scheduled_exports(self, frequency):
combined_results = []
max_retries = 3
limit = 100
skip = 0
while True:
for retry in range(max_retries):
try:
active_projects_api_url = f"{self.RAW_DATA_API_BASE_URL}/cron/?update_frequency={frequency}&skip={skip}&limit={limit}"
response = requests.get(active_projects_api_url, timeout=10)
response.raise_for_status()
data = response.json()
if not data:
return combined_results
combined_results.extend(data)
break
except Exception as e:
logging.warning(
f"Request failed (attempt {retry + 1}/{max_retries}): {e}"
)
else:
raise Exception(
f"Failed to fetch scheduled projects after {max_retries} attempts"
)
skip += limit
def get_hdx_project_details(self, key, value):
project_api_url = f"{self.RAW_DATA_API_BASE_URL}/cron/?{key}={value}"
max_retries = 3
for retry in range(max_retries):
try:
logging.info("Fetching Hdx export details %s:%s", key, value)
response = requests.get(project_api_url, timeout=20)
response.raise_for_status()
response = response.json()
if not response[0]:
logging.error("Feature not found")
return None
feature = self.clean_hdx_export_response(response[0])
return feature
except Exception as ex:
logging.warning(
"Request failed (attempt %s/%s): %s", retry + 1, max_retries, ex
)
logging.error(
"Failed to fetch hdx export details %s:%s after 3 retries", key, value
)
return None
def init_call(self, iso3=None, ids=None, fetch_scheduled_exports=None):
all_export_details = []
if iso3:
for country in iso3:
all_export_details.append(
self.get_hdx_project_details(key="iso3", value=country.upper())
)
if ids:
for hdx_id in ids:
all_export_details.append(
self.get_hdx_project_details(key="id", value=hdx_id)
)
if fetch_scheduled_exports:
frequency = fetch_scheduled_exports
logger.info(
"Retrieving scheduled projects with frequency of %s",
frequency,
)
scheduled_exports = self.get_scheduled_exports(frequency)
for export in scheduled_exports:
if export:
all_export_details.append(self.clean_hdx_export_response(export))
task_ids = []
logger.info("Supplied %s exports", len(all_export_details))
for export in all_export_details:
if export:
task_id = self.process_export(export)
if task_id is not None:
task_ids.append(task_id)
logging.info(
"Request : All request to %s has been sent, Logging %s task_ids",
self.RAW_DATA_API_BASE_URL,
len(task_ids),
)
logging.info(task_ids)
return task_ids
def lambda_handler(event, context):
config_json = os.environ.get("CONFIG_JSON", None)
if config_json is None:
raise ValueError("Config JSON couldn't be found in env")
if os.environ.get("RAWDATA_API_AUTH_TOKEN", None) is None:
raise ValueError("RAWDATA_API_AUTH_TOKEN environment variable not found.")
iso3 = event.get("iso3", None)
ids = event.get("ids", None)
fetch_scheduled_exports = event.get("fetch_scheduled_exports", "daily")
hdx_processor = CountryProcessor(config_json)
hdx_processor.init_call(
iso3=iso3, ids=ids, fetch_scheduled_exports=fetch_scheduled_exports
)
def main():
parser = argparse.ArgumentParser(
description="Triggers extraction request for Hdx extractions projects"
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--iso3",
nargs="+",
type=str,
help="List of country ISO3 codes, add multiples by space",
)
group.add_argument(
"--ids",
nargs="+",
type=int,
help="List of hdx exports id, add multiples by space",
)
group.add_argument(
"--fetch-scheduled-exports",
nargs="?",
const="daily",
type=str,
metavar="frequency",
help="Fetch schedule exports with an optional frequency (default is daily)",
)
parser.add_argument(
"--track",
action="store_true",
default=False,
help="Track the status of tasks and dumps result, Use it carefully as it waits for all tasks to complete",
)
args = parser.parse_args()
config_json = os.environ.get("CONFIG_JSON", "config.json")
language_json = os.environ.get("LANGUAGE_JSON", "language.json")
if os.environ.get("RAWDATA_API_AUTH_TOKEN", None) is None:
raise ValueError("RAWDATA_API_AUTH_TOKEN environment variable not found.")
hdx_processor = CountryProcessor(config_json, language_json)
task_ids = hdx_processor.init_call(
iso3=args.iso3,
ids=args.ids,
fetch_scheduled_exports=args.fetch_scheduled_exports,
)
if args.track:
hdx_processor.track_tasks_status(task_ids)
if __name__ == "__main__":
main()