DAG: 1wsd19_safety_inspection

schedule: 0 15 * * *


Task Instance: getMongoDB


Task Instance Details

Dependencies Blocking Task From Getting Scheduled
Dependency Reason
Task Instance State Task is in the 'failed' state which is not a valid state for execution. The task must be cleared in order to be run.
Dagrun Running Task instance's dagrun was not in the 'running' state but in the state 'failed'.
Attribute: python_callable
  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
def getMongoDB(**context):
    token = context.get("ti").xcom_pull(key="token")
    response_s01 = requests.get(
        url=f"{dRoW_api_end_url}/api/module/document-export/airflow/workflow/60cac3736e0eeb6af04a3884?export_type=0",
        headers={
            "x-access-token": f"Bearer {token}",
            "ICWPxAccessKey": "nd@201907ICWP_[1AG:4UdI){n=b~"
        }
    )

    response_s02 = requests.get(
        url=f"{dRoW_api_end_url}/api/module/document-export/airflow/workflow/611f840fe5ba2a51bf4c524d?export_type=0",
        headers={
            "x-access-token": f"Bearer {token}",
            "ICWPxAccessKey": "nd@201907ICWP_[1AG:4UdI){n=b~"
        }
    )

    RISC_Data_01 = json.loads(response_s01.text)
    RISC_Data_02 = json.loads(response_s02.text)

    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"

    conn_string = ('postgres://' +
                           dbUserName + ':' + 
                           dbUserPassword +
                           '@' + host + ':' + port +
                           '/' + database)
    
    db = create_engine(conn_string)
    conn = db.connect()

    contract_title = "1wsd19"
    dailies_tables = [
        "1. Arc Welding",
        "2. Excavation",
        "3. Formwork",
        "4. Temporary support to Pressure Test and Connection Pipe Works",
        "5. Work in Manholes"
    ]
    weekly_tables = [
        "1. Earth Moving Plant (e.g. Excavators, Backhoes)",
        "2. Lifting Operation",
        "3. Mechanical Material Handling (Load-shifting Machinery)",
        "4. Excavation",
        "5. Transportation",
        "6. Mechanical Plant and Equipment",
        "7. Working at height",
        "8. ladder & Staircase",
        "9. Protection against Falling Objects",
        "10. Welding / Cutting Operations and Equipment",
        "11. Hand tools",
        "12. Compressed Air Tools",
        "13. Concrete Formworks",
        "14. Woodworking Machines",
        "15. Electrical Supply System, Electrical Works & Electric Hand Tools",
        "16. Personal protective equipments and related facilities",
        "17. Storage of dangerous goods (e.g. fuels, gas cylinders and other hazardous chemicals, paints)",
        "18. Fire prevention and protection (e.g. fire extinguishers, escape routes)",
        "19. Housekeeping",
        "20. Noise control",
        "21. Confined Spaces",
        "22. Welfare Facilities",
        "23. Traffic diversion and control (e.g. lighting, signing & guarding)",
        "24. Other"
    ]

    full_df = pd.DataFrame()
    monthly_summary = {}
    with conn:
        for entry in RISC_Data_01:
            df_nested_list = json_normalize(entry['data'])
            # List to hold object for each table
            df_list = []
            # Inspection date
            date_of_inspection = df_nested_list['Date & Time'][0]

            # Process each table dynamically
            for key in dailies_tables:
                if key not in df_nested_list:
                    continue
                
                df_table = df_nested_list[key]
                [group_key, group_name] = key.split(". ")

                for i in range(len(df_table)):
                    record = df_table[i]
                    dict_record = {
                        'Date of Inspection': date_of_inspection,
                        'Month': date_of_inspection[:7],
                        'Contract Title': contract_title,
                        'Group No.': group_key,
                        'Group': group_name,
                        'Item No.': str(i),
                        'Description': record[group_key + ". Behaviour to be monitored"],
                        'Result': record[group_key + ". Result"],
                        'Template': 'A. Daily Inspection Checklist',
                    }
                    df_list.append(dict_record)

                    # if 'Date completed' not in dict_record or 'Agreed date for completion' not in dict_record:
                    #     dict_record['On Time'] = None
                    # elif not dict_record['Date completed'] or not dict_record['Agreed date for completion']:
                    #     dict_record['On Time'] = None
                    # elif dict_record['Date completed'] <= dict_record['Agreed date for completion']:
                    #     dict_record['On Time'] = "On-Time"
                    # else:
                    #     dict_record['On Time'] = "Late"

                    if date_of_inspection[:7] in monthly_summary:
                        monthly_summary[date_of_inspection[:7]]['items'] += 1
                        if record[group_key + ". Result"] == 'At Risk':
                            monthly_summary[date_of_inspection[:7]]['concern'] += 1
                    else:
                        monthly_summary[date_of_inspection[:7]] = {
                            'items': 1,
                            'concern': 1 if record[group_key + ". Result"] == 'At Risk' else 0
                        }

            df_combined = pd.DataFrame(data=df_list)

            # Append non-compliant records
            if not full_df.empty and not df_combined.empty:
                full_df = pd.concat([full_df, df_combined], ignore_index=True)
            elif not df_combined.empty:
                full_df = df_combined


        for entry in RISC_Data_02:
            df_nested_list = json_normalize(entry['data'])

            # List to hold object for each table
            df_list = []
            # Inspection date
            date_of_inspection = df_nested_list['A03 Inspection Date time']

            if 'A - Follow-up Summary' in df_nested_list and df_nested_list['A - Follow-up Summary'] > 0:
                total_late_retification = 0
                for summaryData in df_nested_list['A - Follow-up Summary']:
                    if ("A11 Agreed Date" in summaryData and "A11 Agreed Date" in summaryData and not (summaryData["A11 Agreed Date"]!='') and (not (summaryData["A12 Date Completed"]!='')) and (summaryData["A11 Agreed Date"].astype('datetime64[ns]') < summaryData["A12 Date Completed"].astype('datetime64[ns]')).bool()):
                        total_late_retification += 1
            else:
                total_late_retification = 0

            for key in df_nested_list:
                group_key = key.replace(".", "").split(" ")[0]
                if 'checklist' not in group_name.lower():
                    continue
                group_name = weekly_tables[int(group_key) - 1]

                for i in range(len(df_nested_list[key])):
                    record = df_nested_list[key][i]
                    item_desc = ""
                    result = False
                    for record_key in record:
                        if "item" in record_key.lower():
                            item_desc = record[record_key]
                        if "result" in record_key.lower():
                            result = record[record_key]

                    dict_record = {
                        'Date of Inspection': date_of_inspection,
                        'Month': date_of_inspection[:7],
                        'Contract Title': contract_title,
                        'Group No.': group_key,
                        'Group': group_name,
                        'Item No.': item_desc.split(" ")[0],
                        'Description': item_desc,
                        'Result': result,
                        'Template': 'B. Weekly Safety Inspection',
                    }
                    df_list.append(dict_record)


        # Sort by date of inspection
        non_compliant_df = full_df[full_df['Safety Compliance'] == 'No']
        non_compliant_df.sort_values(by=['Date of Inspection', 'Contract Title', 'Item No.'], inplace=True)
        # Clean up column names for SQL
        non_compliant_df.columns = non_compliant_df.columns.str.replace(' ', '_').str.replace(r'[().%]', '', regex=True).str.replace('/', '_')

        # Retrieve only relevant columns
        final_df = non_compliant_df[['Date_of_Inspection', 'Month', 'Contract_Title', 'Template', 'Group_No', 'Group', 'Item_No', 'Description', 'Location', 'Safety_Compliance', 'Date_completed', 'Agreed_date_for_completion', 'On_Time']]

        # Write to SQL database
        final_df.to_sql('safety_inspection_1wsd19', con=conn, if_exists='replace', index=False)

        # Create a summary df
        summary_dict = []
        for k, v in monthly_summary.items():
            summary_dict.append({
                'Month': k,
                'Items': v['items'],
                'Concerns': v['concern']
            })
        summary_df = pd.DataFrame(data=summary_dict)
        summary_df.to_sql('safety_inspection_summary_1wsd19', con=conn, if_exists='replace', index=False)
Task Instance Attributes
Attribute Value
dag_id 1wsd19_safety_inspection
duration 16.916986
end_date 2025-12-10 08:25:40.763837+00:00
execution_date 2025-12-08T15:00:00+00:00
executor_config {}
generate_command <function TaskInstance.generate_command at 0x7fb2f39d0320>
hostname a7c46ba165e9
is_premature False
job_id 75
key ('1wsd19_safety_inspection', 'getMongoDB', <Pendulum [2025-12-08T15:00:00+00:00]>, 3)
log <Logger airflow.task (INFO)>
log_filepath /usr/local/airflow/logs/1wsd19_safety_inspection/getMongoDB/2025-12-08T15:00:00+00:00.log
log_url http://localhost:8080/admin/airflow/log?execution_date=2025-12-08T15%3A00%3A00%2B00%3A00&task_id=getMongoDB&dag_id=1wsd19_safety_inspection
logger <Logger airflow.task (INFO)>
mark_success_url http://localhost:8080/success?task_id=getMongoDB&dag_id=1wsd19_safety_inspection&execution_date=2025-12-08T15%3A00%3A00%2B00%3A00&upstream=false&downstream=false
max_tries 1
metadata MetaData(bind=None)
next_try_number 3
operator PythonOperator
pid 16793
pool default_pool
prev_attempted_tries 2
previous_execution_date_success None
previous_start_date_success None
previous_ti None
previous_ti_success None
priority_weight 1
queue default
queued_dttm 2025-12-10 08:25:20.044949+00:00
raw False
run_as_user None
start_date 2025-12-10 08:25:23.846851+00:00
state failed
task <Task(PythonOperator): getMongoDB>
task_id getMongoDB
test_mode False
try_number 3
unixname airflow
Task Attributes
Attribute Value
dag <DAG: 1wsd19_safety_inspection>
dag_id 1wsd19_safety_inspection
depends_on_past False
deps {<TIDep(Trigger Rule)>, <TIDep(Previous Dagrun State)>, <TIDep(Not In Retry Period)>}
do_xcom_push True
downstream_list []
downstream_task_ids set()
email None
email_on_failure True
email_on_retry True
end_date None
execution_timeout None
executor_config {}
extra_links []
global_operator_extra_link_dict {}
inlets []
lineage_data None
log <Logger airflow.task.operators (INFO)>
logger <Logger airflow.task.operators (INFO)>
max_retry_delay None
on_failure_callback None
on_retry_callback None
on_success_callback None
op_args []
op_kwargs {'name': 'Dylan'}
operator_extra_link_dict {}
operator_extra_links ()
outlets []
owner airflow
params {}
pool default_pool
priority_weight 1
priority_weight_total 1
provide_context True
queue default
resources None
retries 1
retry_delay 0:05:00
retry_exponential_backoff False
run_as_user None
schedule_interval 0 15 * * *
shallow_copy_attrs ('python_callable', 'op_kwargs')
sla None
start_date 2023-01-17T00:00:00+00:00
subdag None
task_concurrency None
task_id getMongoDB
task_type PythonOperator
template_ext []
template_fields ('templates_dict', 'op_args', 'op_kwargs')
templates_dict None
trigger_rule all_success
ui_color #ffefeb
ui_fgcolor #000
upstream_list [<Task(PythonOperator): getDrowToken>]
upstream_task_ids {'getDrowToken'}
wait_for_downstream False
weight_rule downstream