From edc869ba841b2ec5b21d7c347f949f39fd7583d2 Mon Sep 17 00:00:00 2001 From: cwy-p8d-u1 <sven-ove.haensel@stud.hs-hannover.de> Date: Sat, 16 Mar 2024 11:03:07 +0100 Subject: [PATCH] added exception handling --- .../cypher/memgraph/memgraph_queries.py | 89 ++++++++++--------- .../queries/cypher/neo4j/neo4j_queries.py | 80 +++++++++-------- .../queries/cypher/ongdb/cypher_queries.py | 78 ++++++++-------- 3 files changed, 128 insertions(+), 119 deletions(-) diff --git a/code/eval/queries/cypher/memgraph/memgraph_queries.py b/code/eval/queries/cypher/memgraph/memgraph_queries.py index 0ebc562..51868ec 100644 --- a/code/eval/queries/cypher/memgraph/memgraph_queries.py +++ b/code/eval/queries/cypher/memgraph/memgraph_queries.py @@ -63,51 +63,52 @@ def load_queries_from_file(file_path): # logging.info(f"DBMS answered in: {execution_time} ms\n") def execute_and_log_query(query_key, query): - start_time = time.time() - with driver.session() as session: - result = session.run(query) - # end_time = time.time() - row_count = 0 - if query_key == '2-hop': - for record in result: - nodes = record.get('nodes') - if nodes is not None: # Check if 'nodes' is not None - row_count += len(nodes) + try: + start_time = time.time() + with driver.session() as session: + result = session.run(query) + # end_time = time.time() + row_count = 0 + if query_key == '2-hop': + for record in result: + nodes = record.get('nodes') + if nodes is not None: # Check if 'nodes' is not None + row_count += len(nodes) + else: + row_count= len(result.data()) + summary = result.consume() + end_time = time.time() + + # Check if the attributes are None and set them to 0 if they are + result_available_after = summary.result_available_after if summary.result_available_after is not None else 0 + result_consumed_after = summary.result_consumed_after if summary.result_consumed_after is not None else 0 + row_count = row_count if row_count is not None else 0 + execution_time = result_available_after + total_time2 = result_available_after + result_consumed_after + total_time = end_time - start_time + logging.info(f"Results for Query {query_key}:") + logging.info(f"Python executed in: {total_time}s") + + if row_count == 0: + logging.info(f"Number of rows not available") else: - row_count= len(result.data()) - summary = result.consume() - end_time = time.time() - - # Check if the attributes are None and set them to 0 if they are - result_available_after = summary.result_available_after if summary.result_available_after is not None else 0 - result_consumed_after = summary.result_consumed_after if summary.result_consumed_after is not None else 0 - row_count = row_count if row_count is not None else 0 - execution_time = result_available_after - total_time2 = result_available_after + result_consumed_after - total_time = end_time - start_time - logging.info(f"Results for Query {query_key}:") - logging.info(f"Python executed in: {total_time}s") - - if row_count == 0: - logging.info(f"Number of rows not available") - else: - logging.info(f"Number of rows {row_count}") - if execution_time == 0: - logging.warning(f"No internal DBMS metric available") - else: - logging.info(f"DBMS answered in: {execution_time} ms\n") - logging.info(f"App python answered in: {total_time} ms\n") - logging.info(f"App db answered in: {total_time2} ms\n") - - - - # Open the CSV file in append mode and write the log information - writer = csv.writer(csv_log_file) - # Write a header if the file is newly created or empty, else append the data - csv_log_file.seek(0, 2) # Move the cursor to the end of the file - if csv_log_file.tell() == 0: # If file is empty, write a header - writer.writerow(['Query Key', 'Start Time', 'End Time','Fetched nodes', 'Execution Time (ms)', 'Total Time (s)']) - writer.writerow([query_key, start_time, end_time, row_count, execution_time, total_time]) + logging.info(f"Number of rows {row_count}") + if execution_time == 0: + logging.warning(f"No internal DBMS metric available") + else: + logging.info(f"DBMS answered in: {execution_time} ms\n") + logging.info(f"App python answered in: {total_time} ms\n") + logging.info(f"App db answered in: {total_time2} ms\n") + # Open the CSV file in append mode and write the log information + writer = csv.writer(csv_log_file) + # Write a header if the file is newly created or empty, else append the data + csv_log_file.seek(0, 2) # Move the cursor to the end of the file + if csv_log_file.tell() == 0: # If file is empty, write a header + writer.writerow(['Query Key', 'Start Time', 'End Time','Fetched nodes', 'Execution Time (ms)', 'Total Time (s)']) + writer.writerow([query_key, start_time, end_time, row_count, execution_time, total_time]) + except Exception as e: + logging.error(f"Failed to execute query {query_key}: {e}") + # Continue with the next query even if the current one fails # Function to schedule and execute all queries def schedule_and_execute_queries(): diff --git a/code/eval/queries/cypher/neo4j/neo4j_queries.py b/code/eval/queries/cypher/neo4j/neo4j_queries.py index 046936d..7b8e379 100644 --- a/code/eval/queries/cypher/neo4j/neo4j_queries.py +++ b/code/eval/queries/cypher/neo4j/neo4j_queries.py @@ -64,44 +64,48 @@ def load_queries_from_file(file_path): # logging.info(f"DBMS answered in: {execution_time} ms\n") def execute_and_log_query(query_key, query): - start_time = time.time() - with driver.session() as session: - result = session.run(query) - # end_time = time.time() - row_count= len(result.data()) - summary = result.consume() - end_time = time.time() - - # Check if the attributes are None and set them to 0 if they are - result_available_after = summary.result_available_after if summary.result_available_after is not None else 0 - result_consumed_after = summary.result_consumed_after if summary.result_consumed_after is not None else 0 - row_count = row_count if row_count is not None else 0 - execution_time = result_available_after - total_time2 = result_available_after + result_consumed_after - total_time = end_time - start_time - logging.info(f"Results for Query {query_key}:") - logging.info(f"Python executed in: {total_time}s") - - if row_count == 0: - logging.info(f"Number of rows not available") - else: - logging.info(f"Number of rows {row_count}") - if execution_time == 0: - logging.warning(f"No internal DBMS metric available") - else: - logging.info(f"DBMS answered in: {execution_time} ms\n") - logging.info(f"App python answered in: {total_time} ms\n") - logging.info(f"App db answered in: {total_time2} ms\n") - - - - # Open the CSV file in append mode and write the log information - writer = csv.writer(csv_log_file) - # Write a header if the file is newly created or empty, else append the data - csv_log_file.seek(0, 2) # Move the cursor to the end of the file - if csv_log_file.tell() == 0: # If file is empty, write a header - writer.writerow(['Query Key', 'Start Time', 'End Time','Fetched nodes', 'Execution Time (ms)', 'Total Time (s)']) - writer.writerow([query_key, start_time, end_time, row_count, execution_time, total_time]) + try: + start_time = time.time() + with driver.session() as session: + result = session.run(query) + # end_time = time.time() + row_count= len(result.data()) + summary = result.consume() + end_time = time.time() + + # Check if the attributes are None and set them to 0 if they are + result_available_after = summary.result_available_after if summary.result_available_after is not None else 0 + result_consumed_after = summary.result_consumed_after if summary.result_consumed_after is not None else 0 + row_count = row_count if row_count is not None else 0 + execution_time = result_available_after + total_time2 = result_available_after + result_consumed_after + total_time = end_time - start_time + logging.info(f"Results for Query {query_key}:") + logging.info(f"Python executed in: {total_time}s") + + if row_count == 0: + logging.info(f"Number of rows not available") + else: + logging.info(f"Number of rows {row_count}") + if execution_time == 0: + logging.warning(f"No internal DBMS metric available") + else: + logging.info(f"DBMS answered in: {execution_time} ms\n") + logging.info(f"App python answered in: {total_time} ms\n") + logging.info(f"App db answered in: {total_time2} ms\n") + + + + # Open the CSV file in append mode and write the log information + writer = csv.writer(csv_log_file) + # Write a header if the file is newly created or empty, else append the data + csv_log_file.seek(0, 2) # Move the cursor to the end of the file + if csv_log_file.tell() == 0: # If file is empty, write a header + writer.writerow(['Query Key', 'Start Time', 'End Time','Fetched nodes', 'Execution Time (ms)', 'Total Time (s)']) + writer.writerow([query_key, start_time, end_time, row_count, execution_time, total_time]) + except Exception as e: + logging.error(f"Failed to execute query {query_key}: {e}") + # Continue with the next query even if the current one fails # Function to schedule and execute all queries def schedule_and_execute_queries(): diff --git a/code/eval/queries/cypher/ongdb/cypher_queries.py b/code/eval/queries/cypher/ongdb/cypher_queries.py index 59fe811..e03df8b 100644 --- a/code/eval/queries/cypher/ongdb/cypher_queries.py +++ b/code/eval/queries/cypher/ongdb/cypher_queries.py @@ -64,43 +64,47 @@ def load_queries_from_file(file_path): # logging.info(f"DBMS answered in: {execution_time} ms\n") def execute_and_log_query(query_key, query): - start_time = time.time() - with driver.session() as session: - result = session.run(query) - row_count= len(result.data()) - summary = result.consume() - end_time = time.time() - - - # Check if the attributes are None and set them to 0 if they are - result_available_after = summary.result_available_after if summary.result_available_after is not None else 0 - result_consumed_after = summary.result_consumed_after if summary.result_consumed_after is not None else 0 - row_count = row_count if row_count is not None else 0 - execution_time = result_available_after - total_time2 = result_available_after + result_consumed_after - total_time = end_time - start_time - - logging.info(f"Results for Query {query_key}:") - logging.info(f"Python executed in: {end_time - start_time}s") - - if row_count == 0: - logging.info(f"Number of rows not available") - else: - logging.info(f"Number of rows {row_count}") - if execution_time == 0: - logging.warning(f"No internal DBMS metric available") - else: - logging.info(f"DBMS answered in: {execution_time} ms\n") - logging.info(f"App python answered in: {total_time} ms\n") - logging.info(f"App db answered in: {total_time2} ms\n") - - # Open the CSV file in append mode and write the log information - writer = csv.writer(csv_log_file) - # Write a header if the file is newly created or empty, else append the data - csv_log_file.seek(0, 2) # Move the cursor to the end of the file - if csv_log_file.tell() == 0: # If file is empty, write a header - writer.writerow(['Query Key', 'Start Time', 'End Time','Fetched nodes', 'Execution Time (ms)', 'Total Time (s)']) - writer.writerow([query_key, start_time, end_time, row_count, execution_time, total_time]) + try: + start_time = time.time() + with driver.session() as session: + result = session.run(query) + row_count= len(result.data()) + summary = result.consume() + end_time = time.time() + + + # Check if the attributes are None and set them to 0 if they are + result_available_after = summary.result_available_after if summary.result_available_after is not None else 0 + result_consumed_after = summary.result_consumed_after if summary.result_consumed_after is not None else 0 + row_count = row_count if row_count is not None else 0 + execution_time = result_available_after + total_time2 = result_available_after + result_consumed_after + total_time = end_time - start_time + + logging.info(f"Results for Query {query_key}:") + logging.info(f"Python executed in: {end_time - start_time}s") + + if row_count == 0: + logging.info(f"Number of rows not available") + else: + logging.info(f"Number of rows {row_count}") + if execution_time == 0: + logging.warning(f"No internal DBMS metric available") + else: + logging.info(f"DBMS answered in: {execution_time} ms\n") + logging.info(f"App python answered in: {total_time} ms\n") + logging.info(f"App db answered in: {total_time2} ms\n") + + # Open the CSV file in append mode and write the log information + writer = csv.writer(csv_log_file) + # Write a header if the file is newly created or empty, else append the data + csv_log_file.seek(0, 2) # Move the cursor to the end of the file + if csv_log_file.tell() == 0: # If file is empty, write a header + writer.writerow(['Query Key', 'Start Time', 'End Time','Fetched nodes', 'Execution Time (ms)', 'Total Time (s)']) + writer.writerow([query_key, start_time, end_time, row_count, execution_time, total_time]) + except Exception as e: + logging.error(f"Failed to execute query {query_key}: {e}") + # Continue with the next query even if the current one fails # Function to schedule and execute all queries def schedule_and_execute_queries(): -- GitLab