def query_and_fetchall(sql_query: str, *args) -> List[Dict]: """ takes a sql query string and returns the all rows of the results of the query Args: sql_query: the SQL query Returns: List[Dict]: All rows from the query """ try: pg_conn, pg_cur = get_pg_connection() pg_cur.execute(sql_query, args) result = pg_cur.fetchall() pg_conn.commit() pg_cur.close() pg_conn.close() return result except Exception as e: traceback.print_exc() logger_service.log_error('DB ERROR', traceback.format_exc()) raise e
def get_pg_connection() -> (Dict, Dict): """ Gets pg connection and cursor from postgres Returns: (Dict, Dict): the pg connection and cursor """ try: postgres_url = os.getenv('POSTGRES_URL') if(postgres_url): result = urlparse.urlparse(postgres_url) username = result.username password = result.password database = result.path[1:] hostname = result.hostname port = result.port pg_conn = psycopg2.connect( database = database, user = username, password = password, host = hostname, port = port ) else: pg_conn = connect(host='postgres-db', user='******', password='******', dbname='ai') pg_cur = pg_conn.cursor(cursor_factory=DictCursor) return pg_conn, pg_cur except Exception as e: traceback.print_exc() logger_service.log_error('DB ERROR', traceback.format_exc()) raise e
def fail_dicom_eval(eval_id): traceback.print_exc() error_message = f'evaluation for study {eval_id} failed' logger_service.log_error(error_message, traceback.format_exc()) # update eval status to FAILED eval_db.fail_eval(eval_id) messaging_service.send_notification(error_message, 'eval_failed')
def fail_classification(orthanc_ids): # catch errors and print output print('classification of study', orthanc_ids, 'failed') traceback.print_exc() logger_service.log_error(f'classfying {orthanc_ids} failed', traceback.format_exc()) # remove studies from the db that failed on classfication for orthanc_id in orthanc_ids: study_db.remove_study_by_id(orthanc_id)
def fail_evals(model_id, eval_ids): traceback.print_exc() error_message = f'evaluation using model {model_id} failed' logger_service.log_error(error_message, traceback.format_exc()) for eval_id in eval_ids: eval_db.fail_eval(eval_id) messaging_service.send_notification(error_message, 'eval_failed')
def fail_experiment(experiment): """ """ messaging_service.send_notification( f'Failed experiment {experiment["name"]}', 'experiment_failed') experiment_db.set_experiment_failed(experiment['id']) traceback.print_exc() experiment_id = experiment['id'] logger_service.log_error(f'experiment {experiment_id} failed', traceback.format_exc())
def run_jobs(): """ checks the database for current eval jobs and evaluates studies """ # Get currently running jobs print('runnning jobs') jobs = eval_service.get_eval_jobs() for job in jobs: try: evaluate_studies.delay(job['modelId'], 1) except Exception as e: logger_service.log_error(f'{job.id} failed', traceback.format_exc()) traceback.print_exc()
def query(sql_query: str, *args): """ takes a sql query string and returns the all rows of the results of the query Args: sql_query: the SQL query """ try: pg_conn, pg_cur = get_pg_connection() pg_cur.execute(sql_query, args) pg_conn.commit() pg_cur.close() pg_conn.close() except Exception as e: traceback.print_exc() logger_service.log_error('DB ERROR', traceback.format_exc()) raise e
def fail_model(model_id): traceback.print_exc() error_message = f'evaluation using model {model_id} failed' logger_service.log_error(error_message, traceback.format_exc()) messaging_service.send_notification(error_message, 'eval_failed')