def _process_single_parent_results(cursor, parent_id, parent_history_code, parent_id_results): """Private method for processing child results for parent records that only have a single result. """ p_id = parent_id p_history_code = parent_history_code p_id_results = parent_id_results p_provenance_id = int(p_id_results[0][0]) child_hist = cursor.execute(CHILD_HIST_SELECT_QUERY % (p_history_code, 'N', 'N')) c_results = cursor.fetchall() for child in c_results: c_id = int(child[0]) c_uuid = int(child[2]) c_service_id = int(child[3]) c_category_id = int(child[4]) c_event_id = int(child[5]) c_username = str(child[6]) c_createdate = int(child[7]) c_histid_status = cursor.execute(PROV_HIST_SELECT_ID % (c_uuid, c_event_id, c_category_id, c_service_id, c_username, c_createdate)) c_id_results = cursor.fetchall() if len(c_id_results) == 1: c_provenance_id = int(c_id_results[0][0]) c_histid_status = cursor.execute(PROV_HIST_INSERT_ID % (c_provenance_id, p_provenance_id)) if c_histid_status == 1: c_process_status = cursor.execute( PROV_HIST_UPDATE_STATUS % ('Y', c_id)) if c_process_status == 1: info_msg = ("Child row updated: [" + "parent_provenance_id, " + "child_provenance_id]:" + "[" + str(p_id) + "," + str(c_id) + "]") track_history_info(info_msg) else: err_msg = ("Failed to update child row " + "status: [parent_provenance_id, " + "child_provenance_id]:" + "[" + str(p_id) + "," + str(c_id) + "]") track_history_errors(err_msg) notify_support(err_msg, SCRIPTNAME) else: err_msg = ("Failed History Recording, at child row" + " [parent_provenance_id, " + "child_provenance_id]:" + "[" + str(p_id) + "," + str(c_id) + "]") track_history_errors(err_msg) notify_support(err_msg, SCRIPTNAME) else: err_msg = ("Error in retrieving child row from " + "provenance table, multiple entries or " + "no entry found. [parent_provenance_id, " + "child_provenance_id]: [" + str(p_id) + "," + str(c_id) + "]") track_history_errors(err_msg) notify_support(err_msg, SCRIPTNAME) p_process_status = cursor.execute(PROV_HIST_UPDATE_STATUS % ('Y', p_id)) if p_process_status == 1: info_msg = ("History Recorded: [parent_provenance_id]:" + "[" + str(p_id) + "]") track_history_info(info_msg) else: err_msg = ("Failed updating parent row status:" + "[parent_provenance_id, " + "child_provenance_id]:" + "[" + str(p_id) + "," + str(c_id) + "]") track_history_errors(err_msg) notify_support(err_msg, SCRIPTNAME)
def process_valid_request(req_tuple): if req_tuple.version == None: req_tuple.version = "Default" event_id = get_id(req_tuple.event_name, "EVENT", req_tuple.version) category_id = get_id(req_tuple.category_name, "CATEGORY", req_tuple.version) service_id = get_id(req_tuple.service_name, "SERVICE", req_tuple.version) if event_id != "EMPTY" and category_id != "EMPTY" and service_id != "EMPTY": all_data = str(req_tuple) try: conn = MySQLdb.connect( host=PROV_DB_HOST, user=PROV_DB_USERNAME, passwd=PROV_DB_PASSWORD, db=PROV_DB_NAME) cursor = conn.cursor() cursor.execute(QUERY_CHECK_UUID % (req_tuple.uuid)) check_results = cursor.fetchall() if len(check_results) == 1: if (req_tuple.proxy_username is None and req_tuple.event_data is None): insert_status = cursor.execute( QUERY_NO_PROXY_DATA % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.request_ipaddress, req_tuple.created_date)) if str(insert_status) == "1": info_msg = "Success: " + all_data log_info(info_msg) else: err_msg = "QUERY_NO_PROXY_DATA query failed" + all_data log_errors(err_msg) audit_insert = cursor.execute(AUDIT_NO_PROXY_DATA % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.request_ipaddress, req_tuple.created_date)) if audit_insert != 1: failed_inserts_audit(all_data) elif req_tuple.proxy_username != None: insert_status = cursor.execute(QUERY_PROXY % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.proxy_username, req_tuple.request_ipaddress, req_tuple.created_date)) if str(insert_status) == "1": info_msg = "Success: " + all_data log_info(info_msg) else: err_msg = "QUERY_PROXY query failed" + all_data log_errors(err_msg) audit_insert = cursor.execute(AUDIT_PROXY % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.proxy_username, req_tuple.request_ipaddress, req_tuple.created_date)) if audit_insert != 1: failed_inserts_audit(all_data) elif req_tuple.event_data != None: insert_status = cursor.execute(QUERY_DATA % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.event_data, req_tuple.request_ipaddress, req_tuple.created_date)) if str(insert_status) == "1": info_msg = "Success: " + all_data log_info(info_msg) else: err_msg = "QUERY_DATA query failed" + all_data log_errors(err_msg) audit_insert = cursor.execute(AUDIT_DATA % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.event_data, req_tuple.request_ipaddress, req_tuple.created_date)) if audit_insert != 1: failed_inserts_audit(all_data) else: insert_status = cursor.execute(QUERY_ALL % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.proxy_username, req_tuple.event_data, req_tuple.request_ipaddress, req_tuple.created_date)) if str(insert_status) == "1": info_msg = "Success: " + all_data log_info(info_msg) else: err_msg = "QUERY_ALL query failed" + all_data log_errors(err_msg) audit_insert = cursor.execute(AUDIT_ALL % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.proxy_username, req_tuple.event_data, req_tuple.request_ipaddress, req_tuple.created_date)) if audit_insert != 1: failed_inserts_audit(all_data) if req_tuple.track_history == "1": if req_tuple.track_history_code != None: history_data = str( req_tuple.track_history_code) + " " + all_data cursor.execute(HIST_SELECT_QUERY % (req_tuple.track_history_code)) results = cursor.fetchall() if len(results) != 0: hist_status = cursor.execute(HIST_INSERT_QUERY % (req_tuple.track_history_code, req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.created_date)) if str(hist_status) == "1": info_msg = ("History request recorded:" + " " + str(req_tuple.track_history_code) + " " + all_data) log_info(info_msg) else: err_msg = "HIST_INSERT_QUERY failed" + \ history_data log_errors(err_msg) track_history_errors(history_data) else: parent_query = "Y" hist_status = cursor.execute( HIST_INSERT_QUERY_PARENT % ( req_tuple.track_history_code, req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.created_date, parent_query)) if str(hist_status) == "1": info_msg = ("History request recorded:" + " " + str(req_tuple.track_history_code) + " " + all_data) log_info(info_msg) else: err_msg = "HIST_INSERT_QUERY_PARENT failed" + \ history_data log_errors(err_msg) track_history_errors(history_data) else: history_data = req_tuple.get_history_data() history_code = get_history_code(history_data) info_msg = "History code generated: " + \ str(history_code) + " " + all_data log_info(info_msg) else: if req_tuple.track_history_code != None: err_msg = ("Track History flag not set but history " + "code was sent. Please check history " + "tracking error logs. " + str(req_tuple.track_history_code)) log_errors(err_msg) history_data = str(req_tuple.track_history_code) + ","\ + str(all_data) track_history_errors(history_data) cursor.close() webstatus = '200 OK' if (req_tuple.track_history == "1" and req_tuple.track_history_code == None): data = json.dumps({'result': {'Status': 'Success', 'Details': 'Provenance recorded', 'History code': history_code}}, indent=4) elif (req_tuple.track_history == None and req_tuple.track_history_code != None): data = json.dumps({'result': {'Status': 'Success', 'Details': 'Provenance recorded', 'Warning': 'Track history flag is not' + 'set but history code was sent'}}, indent=4) else: data = json.dumps({'result': {'Status': 'Success', 'Details': 'Provenance recorded'}}, indent=4) return (data, webstatus) else: cursor.close() webstatus = '500 Internal Server Error' data = json.dumps({'result': {'Status': 'Failed', 'Details': 'Provenance not recorded', 'Report': 'More than one record found for given ' + ' UUID. Support has been notified'}}, indent=4) err_msg = "Duplicate UUID enery found: " + all_data # notify_support log_errors(err_msg) failed_inserts_audit(all_data) return (data, webstatus) except Exception as exc: err_msg = "EXCEPTION: " + str(exc) + ": " + all_data log_exception(err_msg) audit_insert = cursor.execute( AUDIT_ALL % (req_tuple.uuid, req_tuple.event_id, req_tuple.category_id, req_tuple.service_id, req_tuple.username, req_tuple.proxy_username, req_tuple.event_data, req_tuple.request_ipaddress, req_tuple.created_date)) if audit_insert != 1: failed_inserts_audit(all_data) cursor.close() webstatus = '500 Internal Server Error' data = json.dumps({'result': {'Status': 'Failed', 'Details': 'Provenance was not recorded. Audit data ' + 'recorded.'}}, indent=4) return (data, webstatus) else: webstatus = '400 Bad Request' data = json.dumps({'result': {'Status': 'Failed', 'Details': 'Incorrect Service/Category/Event data.'}}, indent=4) return (data, webstatus)