DAG: 6wsd21_icwp_site_diary_general

schedule: 15 7 * * *


6wsd21_icwp_site_diary_general

Toggle wrap
  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
try:

    from datetime import timedelta, datetime
    from airflow import DAG
    
    from airflow.operators.python_operator import PythonOperator
    from airflow.operators.http_operator import SimpleHttpOperator
    from datetime import datetime
    from pandas.io.json import json_normalize
    from airflow.operators.postgres_operator import PostgresOperator

    import pandas as pd
    import json
    import requests
    import numpy as np

    import psycopg2
    from sqlalchemy import create_engine
    #print("All Dag moudules are sucessfully imported")

except Exception as e:
    print("Error {} ".format(e))

dRoW_api_end_url = "https://drow.cloud"

def getDrowToken(**context):

    response = requests.post(
    url=f"{dRoW_api_end_url}/api/auth/authenticate",
    data={
    "username": "icwp2@drow.cloud",
    "password": "dGVzdDAxQHRlc3QuY29t"
    }
    ).json()
    context["ti"].xcom_push(key="token", value=response['token'])


def getMongoDB(**context):
    token = context.get("ti").xcom_pull(key="token")
    response = requests.get(
    url=f"{dRoW_api_end_url}/api/module/document-export/airflow/workflow/62fcf02150d4844434ad9311?export_type=0",
    headers={
    "x-access-token": f"Bearer {token}",
    "ICWPxAccessKey": "nd@201907ICWP_[1AG:4UdI){n=b~"
    }
    )
    #print('got_data')
    RISC_Data = json.loads(response.text)
    Mapping= {
    "A01 Date" : "a01_date",

    # "1. General_compelete": "general_complete",
    # "1. General_incompelete": "general_incomplete",
    # "2. Flammable Liquids / Gases_compelete": "flammable_liquids_gases_complete",
    # "2. Flammable Liquids / Gases_incompelete": "flammable_liquids_gases_incomplete",
    # "3. Hazardous Substances_compelete": "general_complete",
    # "3. Hazardous Substances_incompelete": "general_incomplete",
    }
    # safety_cats = [
    #     '2. Reportable Accident',
    #     '12. Unsafe conditions identified during inspections',
    #     '13. Near-miss Reports',
    #     '14. Incident Reports',
    #     '15. LD/MD Improvement and Suspension Notice',
    #     '16. Safety Convictions Records'
    # ]

    #print('start transform')
    host                  = 'drowdatewarehouse.crlwwhgepgi7.ap-east-1.rds.amazonaws.com'  

        # User name of the database server
    dbUserName            = 'dRowAdmin'  

    # Password for the database user
    dbUserPassword        = 'drowsuper'  

    # Name of the database 
    database              = 'drowDateWareHouse'

    # Character set
    charSet               = "utf8mb4"  

    port                  = "5432"

    # #cursor Type
    # cusrsorType            = pymysql.cursors.DictCursor



    #create_engine('mysql+mysqldb://root:password@localhost:3306/mydbname', echo = False)
    conn_string = ('postgres://' +
                           dbUserName + ':' + 
                           dbUserPassword +
                           '@' + host + ':' + port +
                           '/' + database)

    # df = context.get("ti").xcom_pull(key="InsertData")
    # print(df)
    # conn_string = 'postgres://user:password@host/data1'
    
    db = create_engine(conn_string)
    conn = db.connect()
    #print('db connected')
    with conn as conn:
        df3=pd.read_sql("SELECT * FROM public.site_diary_activities_general_count_6wsd21;",
            conn,
            parse_dates=["a01_date"])
        df = pd.DataFrame()
        _df = pd.DataFrame()
        for x in RISC_Data:
            df_nested_list = json_normalize(x['data'])
            #print('process 1')
            df2 = df_nested_list.reindex(columns=Mapping.keys())
            date_str = x['data']['A01 Date']
    
            if date_str is not None:
                date = datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%f%z')
                print("df3")
                print(df3)
                no_of_site_activities = df3.query('a01_date == @date')
        # Rest of your code...
            else:
        # Handle the case when the date_str is None
                print("A01 Date is None for the current data.")
            # date = datetime.strptime(x['data']['A01 Date'], '%Y-%m-%dT%H:%M:%S.%f%z')
            # print("df3")
            # print(df3)
            # no_of_site_activities = df3.query('a01_date == @date')
            # if(not pd.isna(no_of_site_activities['Count'][0])):
            if(len(no_of_site_activities['Count'])!= 0):
                df2['no_of_site_activities'] = no_of_site_activities['Count'].iloc[0]
            else:
                df2['no_of_site_activities'] = 0
            # df2=df_nested_list
            if (x['data']['A01 Date']!= None and datetime.strptime(x['data']['A01 Date'], '%Y-%m-%dT%H:%M:%S.%f%z') < datetime.strptime('2022-10-01T00:00:00.000Z', '%Y-%m-%dT%H:%M:%S.%f%z')):
                df2['complete_or_incomplete'] = 'complete'
            else :
                if x['Status'] == 'F : GO Upload & Print SD' or x['Status'] == 'Z : SD Completed':
                    df2['complete_or_incomplete'] = 'complete'
                else:
                    df2['complete_or_incomplete'] = 'incomplete'

            if len(x['ApproveLogSummary']) > 0:
                pmd_sign_date = [data for data in x['ApproveLogSummary'] if data.get('statusName')=="E : IOW Upload & Print SD"]
                contractor_sign_date = [data for data in x['ApproveLogSummary'] if data.get('statusName')=="C : Contractor Agree SD"]
                supervisor_sign_date = [data for data in x['ApproveLogSummary'] if data.get('statusName')=="B : IOW Sign SD"]
            if len(pmd_sign_date) > 0 and ('to' in pmd_sign_date[len(pmd_sign_date)-1]):
                pmd_receive_time = pmd_sign_date[len(pmd_sign_date)-1]['from']
                pmd_sign_time = pmd_sign_date[len(pmd_sign_date)-1]['to']
                df2['pmd_sign_time'] = pmd_sign_date[len(pmd_sign_date)-1]['to']
                df2['Overdue_PMD']= (pmd_sign_time != '' and pmd_receive_time != '' and (datetime.strptime(pmd_sign_time, '%Y-%m-%dT%H:%M:%S.%f%z') -  datetime.strptime(pmd_receive_time, '%Y-%m-%dT%H:%M:%S.%f%z')).days > 7)
            else: 
                df2['pmd_sign_time'] = None
                df2['Overdue_PMD'] = None

            if len(contractor_sign_date) > 0 and ('to' in contractor_sign_date[len(contractor_sign_date)-1]):
                cr_receive_time = contractor_sign_date[len(contractor_sign_date)-1]['from']
                cr_sign_time = contractor_sign_date[len(contractor_sign_date)-1]['to']
                df2['cr_sign_time'] = contractor_sign_date[len(contractor_sign_date)-1]['to']
                df2['Overdue_CR']= (cr_sign_time != '' and cr_receive_time != '' and (datetime.strptime(cr_sign_time, '%Y-%m-%dT%H:%M:%S.%f%z') -  datetime.strptime(cr_receive_time, '%Y-%m-%dT%H:%M:%S.%f%z')).days > 7)
            else: 
                df2['cr_sign_time'] = None
                df2['Overdue_CR'] = None

            if len(supervisor_sign_date) > 0 and ('to' in supervisor_sign_date[len(supervisor_sign_date)-1]):
                sup_receive_time = supervisor_sign_date[len(supervisor_sign_date)-1]['from']
                sup_sign_time = supervisor_sign_date[len(supervisor_sign_date)-1]['to']
                df2['sup_sign_time'] = supervisor_sign_date[len(supervisor_sign_date)-1]['to']
                df2['Overdue_SUP']= (sup_sign_time != '' and sup_receive_time != '' and (datetime.strptime(sup_sign_time, '%Y-%m-%dT%H:%M:%S.%f%z') -  datetime.strptime(sup_receive_time, '%Y-%m-%dT%H:%M:%S.%f%z')).days > 7)
            else: 
                df2['sup_sign_time'] = None
                df2['Overdue_SUP'] = None
            
            df6 = df_nested_list[Mapping.keys()]
            df4 = pd.DataFrame()
            if len(x['data']["A04 Contractor's Management Team"]) > 0:
                _df3 = df6.copy()
                for c in x['data']["A04 Contractor's Management Team"]:
                    labourName = str(c['A04.1 Ctr Post'])
                    labourNum = 0
                    if ('A04.2 Ctr No.' in c) and not c['A04.2 Ctr No.'] is None:
                        labourNum = c['A04.2 Ctr No.']
                    else:
                        labourNum = 0
                    _df3['contractor_management_post_name'] = labourName
                    _df3['contractor_management_number'] = labourNum
                    df4 = df4.append(_df3)
            
            df2.rename(columns=Mapping, inplace=True)
            df4.rename(columns=Mapping, inplace=True)
            df2.columns = df2.columns.str.replace(' ', '_').str.replace('.', '_').str.replace('(', '_').str.replace(')', '').str.replace('%', 'percent').str.replace('/', '_').str.replace('__', '_')
            df4.columns = df4.columns.str.replace(' ', '_').str.replace('.', '_').str.replace('(', '_').str.replace(')', '').str.replace('%', 'percent').str.replace('/', '_').str.replace('__', '_')
            # df2.drop(columns=['2_Reportable_Accident', '16_Safety_Convictions_Records', '14_Incident_Reports', '15_LD_MD_Improvement_and_Suspension_Notice', '13_Near-miss_Reports','12_Unsafe_conditions_identified_during_inspections'], axis=1)
            df = df.append(df2)
            _df = _df.append(df4) 
            # _df = _df.append(df4)
        df['a01_date']=df['a01_date'].apply(pd.to_datetime)
        _df['a01_date']=_df['a01_date'].apply(pd.to_datetime)
        df.to_sql('site_diary_general_6wsd21', con=conn, if_exists='replace', index= False)
        _df.to_sql('site_diary_general_contractor_management_6wsd21', con=conn, if_exists='replace', index= False)

# */2 * * * * Execute every two minute 
with DAG(
        dag_id="6wsd21_icwp_site_diary_general",
        schedule_interval="15 7 * * *",
        default_args={
            "owner": "airflow",
            "retries": 1,
            "retry_delay": timedelta(minutes=5),
            "start_date": datetime(2023, 1, 17)
        },
        catchup=False) as f:
    
    getMongoDB = PythonOperator(
        task_id="getMongoDB",
        python_callable=getMongoDB,
        op_kwargs={"name": "Dylan"},
        provide_context=True,
    )

    # reformData = PythonOperator(
    #     task_id="reformData",
    #     python_callable=reformData,
    #     provide_context=True,
    #     # op_kwargs={"name": "Dylan"}
    # )

    getDrowToken = PythonOperator(
        task_id="getDrowToken",
        python_callable=getDrowToken,
        provide_context=True,
        # op_kwargs={"name": "Dylan"}
    )

    # insertData = PythonOperator(
    #     task_id="insetDateToPG",
    #     python_callable=insertData,
    #     provide_context=True,
    #     # op_kwargs={"name": "Dylan"}
    # )

    # create_table = PostgresOperator(
    #     sql = create_table_sql_query,
    #     task_id = "create_table_task",
    #     postgres_conn_id = "postgres_rds",
    # )

    # insert_data = PostgresOperator(
    #     sql = insert_data_sql_query,
    #     task_id = "insertData_sql_query_task",
    #     postgres_conn_id = "postgres_rds",
    # )



# getDrowToken >> getMongoDB >> reformData >> create_table
# create_table >> getDrowToken >> getMongoDB >> reformData >> insertData
# getDrowToken >> getMongoDB >> reformData >> insertData
# create_table >> 
getDrowToken >> getMongoDB
    
            # if df2['F2. 監理代表 Checked by SupD on Date'].isnull().bool():
            #     df2['report_complete_or_incomplete'] = 'incomplete'
            # else:
            #     df2['report_complete_or_incomplete'] = 'complete'
            # if not df2['F2. 監理代表 Checked by SupD on Date'].isnull().bool() and not df2['D4 Submission Date'].isnull().bool() and (df2["F2. 監理代表 Checked by SupD on Date"].astype('datetime64[ns]') < df2["D4 Submission Date"].astype('datetime64[ns]')).bool():
            #     df2['nc_report'] = True
            # else:
            #     df2['nc_report'] = False
            # if (not df2['F2. 監理代表 Checked by SupD on Date'].isnull().bool() and not df2['A1a 巡查日期 Inspection Date'].isnull().bool()):
            #     df2['complete_time_in_days'] = (((df2['F2. 監理代表 Checked by SupD on Date'].astype('datetime64[ns]') - 
            #     df2['A1a 巡查日期 Inspection Date'].astype('datetime64[ns]'))/ np.timedelta64(1, 'h'))/24).round(2)
            #     if df2['complete_time_in_days'].isnull().bool() or df2['complete_time_in_days'].lt(0).bool():
            #         df2['complete_time_in_days'] = 0
            # else:
            #     df2['complete_time_in_days'] = 0
            # if(not df2["C02 - Survey Checked on Date Time"].isnull().bool() and not df2["A10 - Request Submission Date Time"].isnull().bool() and ((df2["C02 - Survey Checked on Date Time"].astype('datetime64[ns]') < df2["A10 - Request Submission Date Time"].astype('datetime64[ns]')).bool())):
            #     print((df2["C02 - Survey Checked on Date Time"].astype('datetime64[ns]') < df2["A10 - Request Submission Date Time"].astype('datetime64[ns]')))
            # else:
            #     print((df2["C02 - Survey Checked on Date Time"].astype('datetime64[ns]') < df2["A10 - Request Submission Date Time"].astype('datetime64[ns]')))

            # if (not df2["C02 - Survey Checked on Date Time"].isnull().bool() and (not df2["A10 - Request Submission Date Time"].isnull().bool()) and (df2["C02 - Survey Checked on Date Time"].astype('datetime64[ns]') < df2["A10 - Request Submission Date Time"].astype('datetime64[ns]')).bool()):
            #     df2['nc_report'] = True
            # else:
            #     df2['nc_report'] = False

            # if (not df2["C02 - Survey Checked on Date Time"].isnull().bool() and not df2["A10 - Request Submission Date Time"].isnull().bool() and (((df2["C02 - Survey Checked on Date Time"].astype('datetime64[ns]') - df2["A10 - Request Submission Date Time"].astype('datetime64[ns]'))) < pd.Timedelta(24, unit='h')).bool()):
            #     df2['urgent_report'] = True
            # else:
            #     df2['urgent_report'] = False

            # if (not df2['E01 - Received on behalf of Contractor on Date Time'].isnull().bool() and not df2['A10 - Request Submission Date Time'].isnull().bool()):
            #     df2['elapsed_time'] = (((df2['E01 - Received on behalf of Contractor on Date Time'].astype('datetime64[ns]') - 
            #     df2['A10 - Request Submission Date Time'].astype('datetime64[ns]'))/ np.timedelta64(1, 'h'))/24).round(2)
            #     if df2['elapsed_time'].isnull().bool() or df2['elapsed_time'].lt(0).bool():
            #         df2['elapsed_time'] = 0
            # else:
            #     df2['elapsed_time'] = 0
            
            # if (not df2["C02 - Survey Checked on Date Time"].isnull().bool() and not df2["D01 - Countersigned on Date Time"].isnull().bool() and (((df2["D01 - Countersigned on Date Time"].astype('datetime64[ns]') - df2["C02 - Survey Checked on Date Time"].astype('datetime64[ns]')))>= pd.Timedelta(24, unit='h')).bool()):
            #     df2['overdue_report'] = True
            # else:
            #     df2['overdue_report'] = False
            
            # if (not df2["C02 - Survey Checked on Date Time"].isnull().bool() and not df2["C12 Time Pass to Senior or Contractor"].isnull().bool() and (((df2["C12 Time Pass to Senior or Contractor"].astype('datetime64[ns]') - df2["C02 - Survey Checked on Date Time"].astype('datetime64[ns]')))>= pd.Timedelta(24, unit='h')).bool()):
            #     df2['delayed_approval_report'] = True
            # else:
            #     df2['delayed_approval_report'] = False

            # if ((df2['A01a - Request No. Revision']=="-A").bool()):
            #     df2['fail_in_first_inspection'] = True
            # else:
            #     df2['fail_in_first_inspection'] = False
            
            # if (not df2["C12 Time Pass to Senior or Contractor"].isnull().bool() and not df2["C02 - Survey Checked on Date Time"].isnull().bool() and not df2["E01 - Received on behalf of Contractor on Date Time"].isnull().bool()):
            #     df2['complete_incomplete_outstanding_report'] = 'complete'
            # elif ((df2["C12 Time Pass to Senior or Contractor"].isnull().bool() or df2["E01 - Received on behalf of Contractor on Date Time"].isnull().bool()) and not df2["C02 - Survey Checked on Date Time"].isnull().bool()):
            #     df2['complete_incomplete_outstanding_report'] = 'in-complete'
            # elif ((not df2["C12 Time Pass to Senior or Contractor"].isnull().bool() or df2["E01 - Received on behalf of Contractor on Date Time"].isnull().bool()) and df2["C02 - Survey Checked on Date Time"].isnull().bool()):
            #     df2['complete_incomplete_outstanding_report'] = 'outstanding'
            # else:
            #     df2['complete_incomplete_outstanding_report'] = 'outstanding'