Skip to content

Commit b75d53e

Browse files
committed
add views matching original knesset method / field names
1 parent b6dd76f commit b75d53e

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

airflow/knesset_data_pipelines/run_pipeline.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def get_soup_handle_server_error(first_url, processed_entry_ids=None, **kwargs):
242242
raise Exception(f'failed to find successful response starting from url {first_url}')
243243

244244

245-
def add_dataservice_collection_resource_odata_v4(params, proxies, stats):
245+
def add_dataservice_collection_resource_odata_v4(params, proxies, stats, limit_rows=None):
246246
url_base = os.path.join(config.SERVICE_URLS_V4[params['service-name']], params['method-name'])
247247
timeout = params.pop('__timeout__', config.DEFAULT_REQUEST_TIMEOUT_SECONDS_V4)
248248
odata_count = None
@@ -274,15 +274,17 @@ def add_dataservice_collection_resource_odata_v4(params, proxies, stats):
274274
skip += 100
275275
if num_entries == 0:
276276
break
277-
assert stats['rows'] == odata_count, f'invalid rows count: {stats["rows"]} != {odata_count} for url {url}'
277+
if limit_rows and stats['rows'] >= limit_rows:
278+
break
279+
assert limit_rows or stats['rows'] == odata_count, f'invalid rows count: {stats["rows"]} != {odata_count} for url {url}'
278280

279281

280282
def add_dataservice_collection_resource(params, proxies=None, stats=None, limit_rows=None, stop_on_throttled_error=False, start_url=None, load_from=None):
281283
if stats is None:
282284
stats = defaultdict(int)
283285
if params.get('odata-v4', True):
284-
assert not start_url and not load_from and not stop_on_throttled_error and not limit_rows, 'odata-v4 does not support start_url, load_from, stop_on_throttled_error, limit_rows'
285-
yield from add_dataservice_collection_resource_odata_v4(params, proxies, stats)
286+
assert not start_url and not load_from and not stop_on_throttled_error, 'odata-v4 does not support start_url, load_from, stop_on_throttled_error'
287+
yield from add_dataservice_collection_resource_odata_v4(params, proxies, stats, limit_rows)
286288
return
287289
if load_from:
288290
print(f'loading from {load_from}')
@@ -404,12 +406,26 @@ def _run_pipeline(table_name, storage_url, pipeline_id, storage_path, dataservic
404406
] if dump_to_db else []),
405407
).process()
406408
if dump_to_db:
409+
alias_table_name = dataservice_params['method-name'] if dataservice_params.get("service-name") == "api" else None
407410
with db.get_db_engine().connect() as conn:
408411
with conn.begin():
409-
conn.execute(dedent(f'''
410-
drop table if exists {table_name};
411-
alter table {temp_table_name} rename to {table_name};
412-
'''))
412+
sql = []
413+
if alias_table_name:
414+
sql.append(f'drop view if exists "{alias_table_name}";')
415+
sql.append(f'drop table if exists {table_name};')
416+
sql.append(f'alter table {temp_table_name} rename to {table_name};')
417+
if alias_table_name:
418+
alias_table_name = dataservice_params['method-name']
419+
sql.append(f'create view "{alias_table_name}" as select')
420+
for i, field_name in enumerate(dataservice_params['fields']):
421+
alias_field_name = field_name
422+
if dataservice_params['fields'][field_name].get('primaryKey'):
423+
alias_field_name = 'Id'
424+
if i != 0:
425+
sql.append(',')
426+
sql.append(f'"{field_name}" as "{alias_field_name}"')
427+
sql.append(f'from {table_name};')
428+
conn.execute("\n".join(sql))
413429
if dump_to_storage:
414430
upload_to_storage(storage_path, storage_url, pipeline_name)
415431
pprint(dict(stats))

0 commit comments

Comments
 (0)