|
| 1 | +import pendulum |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +from airflow import DAG |
| 5 | +from airflow.decorators import task |
| 6 | + |
| 7 | + |
| 8 | +default_args = { |
| 9 | + "owner": "epigraphhub", |
| 10 | + "depends_on_past": False, |
| 11 | + "start_date": pendulum.datetime(2023, 1, 1), |
| 12 | + "email": ["epigraphhub@thegraphnetwork.org"], |
| 13 | + "email_on_failure": True, |
| 14 | + "email_on_retry": False, |
| 15 | + "retries": 2, |
| 16 | + "retry_delay": timedelta(minutes=1), |
| 17 | +} |
| 18 | + |
| 19 | +with DAG( |
| 20 | + dag_id='SINAN_DENG', |
| 21 | + tags=['SINAN', 'Brasil', 'Dengue'], |
| 22 | + schedule='@monthly', |
| 23 | + default_args=default_args, |
| 24 | + catchup=False, |
| 25 | +) as dag: |
| 26 | + from airflow.models import Variable |
| 27 | + |
| 28 | + CONN = Variable.get('egh_conn', deserialize_json=True) |
| 29 | + |
| 30 | + @task.external_python( |
| 31 | + task_id='first', |
| 32 | + python='/opt/py311/bin/python3.11', |
| 33 | + expect_airflow=True |
| 34 | + ) |
| 35 | + def update_dengue(egh_conn: dict): |
| 36 | + from pysus.online_data import parquets_to_dataframe |
| 37 | + from pysus.ftp.databases.sinan import SINAN |
| 38 | + |
| 39 | + sinan = SINAN().load() |
| 40 | + dis_code = "DENG" |
| 41 | + tablename = "sinan_dengue_m" |
| 42 | + files = sinan.get_files(dis_code=disease) |
| 43 | + |
| 44 | + f_stage = {} |
| 45 | + for file in files: |
| 46 | + code, year = sinan.format(file) |
| 47 | + stage = 'prelim' if 'PRELIM' in file.path else 'final' |
| 48 | + |
| 49 | + if not stage in f_stage: |
| 50 | + f_stage[stage] = [year] |
| 51 | + else: |
| 52 | + f_stage[stage].append(year) |
| 53 | + |
| 54 | + for year in f_stage['final']: |
| 55 | + # Check if final is already in DB |
| 56 | + with create_engine(egh_conn['URI']).connect() as conn: |
| 57 | + cur = conn.execute( |
| 58 | + f'SELECT COUNT(*) FROM brasil.{tablename}' |
| 59 | + f' WHERE year = {year} AND prelim = False' |
| 60 | + ) |
| 61 | + count = cur.fetchone() |
| 62 | + |
| 63 | + if not count: |
| 64 | + # Check on prelims |
| 65 | + with create_engine(egh_conn['URI']).connect() as conn: |
| 66 | + cur = conn.execute( |
| 67 | + f'SELECT COUNT(*) FROM brasil.{tablename}' |
| 68 | + f' WHERE year = {year} AND prelim = True' |
| 69 | + ) |
| 70 | + count = cur.fetchone() |
| 71 | + |
| 72 | + if count: |
| 73 | + # Update prelim to final |
| 74 | + cur = conn.execute( |
| 75 | + f'DELETE FROM brasil.{tablename}' |
| 76 | + f' WHERE year = {year} AND prelim = True' |
| 77 | + ) |
| 78 | + |
| 79 | + file = sinan.download(sinan.get_files(dis_code, year)) |
| 80 | + |
| 81 | + df = parquets_to_dataframe(file.path) |
| 82 | + df['year'] = year |
| 83 | + df['prelim'] = False |
| 84 | + df.to_sql( |
| 85 | + name=tablename, |
| 86 | + con=engine.connect(), |
| 87 | + schema=schema, |
| 88 | + if_exists='append', |
| 89 | + index=False |
| 90 | + ) |
| 91 | + |
| 92 | + for year in f_stage['prelim']: |
| 93 | + with create_engine(egh_conn['URI']).connect() as conn: |
| 94 | + # Update prelim |
| 95 | + cur = conn.execute( |
| 96 | + f'DELETE FROM brasil.{tablename}' |
| 97 | + f' WHERE year = {year} AND prelim = True' |
| 98 | + ) |
| 99 | + |
| 100 | + file = sinan.download(sinan.get_files(dis_code, year)) |
| 101 | + |
| 102 | + df = parquets_to_dataframe(file.path) |
| 103 | + df['year'] = year |
| 104 | + df['prelim'] = True |
| 105 | + df.to_sql( |
| 106 | + name=tablename, |
| 107 | + con=engine.connect(), |
| 108 | + schema=schema, |
| 109 | + if_exists='append', |
| 110 | + index=False |
| 111 | + ) |
| 112 | + |
| 113 | + update_dengue(CONN) |
0 commit comments