def list_user_tasks_by_user(user_id, correlation_id=None): try: user_id = utils.validate_uuid(user_id) except utils.DetailedValueError: raise # check that user exists try: user_result = get_user_by_id(user_id, correlation_id)[0] except IndexError: errorjson = {"user_id": user_id, "correlation_id": str(correlation_id)} raise utils.ObjectDoesNotExistError("user does not exist", errorjson) result = execute_query(sql_q.LIST_USER_TASKS_SQL, (str(user_id), ), correlation_id) # add url field to each user_task in result edited_result = list() for ut in result: user_task = UserTask(correlation_id=correlation_id) user_task.from_dict(ut_dict=ut) user_task.id = ut["user_task_id"] user_task.first_name = user_result["first_name"] user_task.last_name = user_result["last_name"] user_task.email = user_result["email"] ut["url"] = user_task.calculate_url() del ut["base_url"] del ut["external_task_id"] del ut["user_specific_url"] del ut["user_task_url"] del ut["anon_project_specific_user_id"] del ut["anonymise_url"] edited_result.append(ut) return edited_result
def check_if_user_task_exists(user_id, project_task_id, correlation_id): return execute_query( CHECK_IF_USER_TASK_EXISTS_SQL, (str(user_id), str(project_task_id)), correlation_id, False, )
def get_project_task_with_project_attributes(project_task_id, correlation_id=None): return execute_query( sql_q.GET_PROJECT_TASK_WITH_PROJECT_ATTRIBUTES_SQL, (str(project_task_id), ), correlation_id, )
def get_task_signup_data_for_crm(user_task_id, correlation_id): extra_data = execute_query(SIGNUP_DETAILS_SELECT_SQL, (str(user_task_id),), correlation_id) if len(extra_data) == 1: return extra_data[0] else: errorjson = {'user_task_id': user_task_id, 'correlation_id': str(correlation_id)} raise DetailedValueError('could not load details for user task', errorjson)
def get_entity_updates_for_entity(entity_name: str, entity_id: uuid, correlation_id): return execute_query( GET_ENTITY_UPDATES_FOR_ENTITY_SQL, (entity_name, str(entity_id)), correlation_id, )
def check_user_id_and_external_account(user_id, external_system_id, correlation_id): return execute_query( CHECK_USER_ID_AND_EXTERNAL_ACCOUNT_SQL, (str(user_id), str(external_system_id)), correlation_id, )
def get_user_by_email(user_email, correlation_id): sql_where_clause = " WHERE email = %s" user_json = execute_query( sql_q.BASE_USER_SELECT_SQL + sql_where_clause, (str(user_email), ), correlation_id, ) return append_calculated_properties_to_list(user_json)
def list_users_by_project(project_id, logger=None, correlation_id=None): if logger is None: logger = utils.get_logger() users = execute_query( base_sql=sql_q.LIST_USERS_BY_PROJECT_SQL, params=(project_id, ), correlation_id=correlation_id, ) return users
def anon_user_task_id_2_parameter(anon_ut_id, parameter_name, correlation_id=None): try: return execute_query( sql_q.ANON_USER_TASK_ID_2_ID_SQL, [anon_ut_id], correlation_id, )[0][parameter_name] except IndexError: errorjson = {"anon_ut_id": anon_ut_id, "correlation_id": str(correlation_id)} raise utils.ObjectDoesNotExistError("user task does not exist", errorjson)
def get_project_with_tasks(project_uuid, correlation_id): # take base sql query and insert a where clause to get specified project sql_where_clause = " AND id = %s " # put where clause before final order by order_by_index = sql_q.BASE_PROJECT_SELECT_SQL.rfind("order by") sql = (sql_q.BASE_PROJECT_SELECT_SQL[:order_by_index] + sql_where_clause + sql_q.BASE_PROJECT_SELECT_SQL[order_by_index:]) result = execute_query(sql, (str(project_uuid), ), correlation_id, True, False) return result
def get_user_by_any_anon_id(anon_id, correlation_id=None): try: anon_id = utils.validate_uuid(anon_id) except utils.DetailedValueError as err: err.add_correlation_id(correlation_id) raise err user_json = execute_query(sql_q.GET_USER_BY_ANY_ANON_ID_SQL, ((str(anon_id), ), ) * 2, correlation_id) return append_calculated_properties_to_list(user_json)
def get_task_signup_data_for_crm(self): extra_data = execute_query(sql_q.SIGNUP_DETAILS_SELECT_SQL, (str(self.id), ), self._correlation_id) if len(extra_data) == 1: return extra_data[0] else: errorjson = { "user_task_id": self.id, "correlation_id": str(self._correlation_id), } raise utils.DetailedValueError( "could not load details for user task", errorjson)
def apsuid_2_user_id(anon_project_specific_user_id, correlation_id=None): try: return execute_query( sql_q.GET_USER_BY_ANON_PROJECT_SPECIFIC_USER_ID_SQL, [anon_project_specific_user_id], correlation_id, )[0]["id"] except IndexError: errorjson = { "anon_project_specific_user_id": anon_project_specific_user_id, "correlation_id": str(correlation_id), } raise utils.ObjectDoesNotExistError("user project does not exist", errorjson)
def get_user_by_id(user_id, correlation_id=None): try: user_id = utils.validate_uuid(user_id) except utils.DetailedValueError as err: err.add_correlation_id(correlation_id) raise err sql_where_clause = " WHERE id = %s" user_json = execute_query(sql_q.BASE_USER_SELECT_SQL + sql_where_clause, (str(user_id), ), correlation_id) return append_calculated_properties_to_list(user_json)
def list_user_projects(user_id, correlation_id): try: user_id = utils.validate_uuid(user_id) except utils.DetailedValueError: raise # check that user exists result = get_user_by_id(user_id, correlation_id) if len(result) == 0: errorjson = {"user_id": user_id, "correlation_id": str(correlation_id)} raise utils.ObjectDoesNotExistError("user does not exist", errorjson) return execute_query(LIST_USER_PROJECTS_SQL, (str(user_id), ), correlation_id)
def get_by_url_code(cls, url_code, correlation_id): sql_where_clause = " WHERE url_code = %s" ug_json = execute_query( sql_q.USER_GROUP_WITH_TASKS_SQL + sql_where_clause, (str(url_code), ), correlation_id, ) if len(ug_json) > 0: ug_json = ug_json[0] return cls.from_json(ug_json, correlation_id) else: return None
def get_by_id(cls, user_group_id, correlation_id): try: user_group_id = validate_uuid(user_group_id) except DetailedValueError as err: err.add_correlation_id(correlation_id) raise err sql_where_clause = " WHERE id = %s" ug_json = execute_query( sql_q.USER_GROUP_BASE_SELECT_SQL + sql_where_clause, (str(user_group_id), ), correlation_id, ) if len(ug_json) > 0: ug_json = ug_json[0] return cls.from_json(ug_json, correlation_id) else: return None
def list_projects_with_tasks(correlation_id): result = execute_query(sql_q.BASE_PROJECT_SELECT_SQL, None, correlation_id, True, False) project_lists = { "testing": [], "active": [], "complete": [], } for project in result: tasks = [] testing_tasks = [] for task in project["tasks"]: if task["status"] == "testing": testing_tasks.append(task) else: tasks.append(task) project["tasks"] = testing_tasks + tasks project_lists[project["status"]].append(project) return (project_lists["testing"] + project_lists["active"] + project_lists["complete"])
def get_existing_user_project_id(user_id, project_id, correlation_id): return execute_query( GET_EXISTING_USER_PROJECT_ID_SQL, (str(project_id), str(user_id)), correlation_id, )
def update_cochrane_progress(event, context): """ AWS lambda handler to update progress of task hosted by Cochrane Crowd """ def sort_progress_by_task(progress_dict_): """ Converts task ids in Cochrane progress report from integer to string. Sorts Cochrane progress report by tasks. Returns a sorted dictionary in the format: { "4000": [{"count":6,"uuid":"2a328477-c47e-4758-a43f-2db537983438"}, {"count":7,"uuid":"2ef570b4-c0d5-4de9-8198-f8287942e4e4"}], "5500": [{"count":5,"uuid":"2a328477-c47e-4758-a43f-2db537983438"}, {"count":19,"uuid":"2ef570b4-c0d5-4de9-8198-f8287942e4e4"}] } """ tasks_progress = dict() for r in progress_dict_["records"]: r["task"] = str(r["task"]) if r["task"] in tasks_progress.keys(): tasks_progress[r["task"]].append( {"count": r["count"], "uuid": r["uuid"]} ) else: tasks_progress[r["task"]] = [{"count": r["count"], "uuid": r["uuid"]}] return tasks_progress logger = get_logger() correlation_id = get_correlation_id(event) external_system_name = "Cochrane Crowd" try: external_system_id = pg_utils.execute_query( sql_q.external_system_id_by_name, [external_system_name] )[0]["id"] except TypeError as err: logger.error( f'Could not find an external system named "{external_system_name}"' ) raise err progress_dict = get_progress() progress_info_modified = progress_dict["daterun"] progress_by_task = sort_progress_by_task(progress_dict) user_tasks_sql_queries, project_tasks_sql_queries = list(), list() for external_task_id, v in progress_by_task.items(): logger.info(f"Working on task {external_task_id}") project_task_assessments = 0 for record in v: user_id = record["uuid"] user_task_assessments = record["count"] user_task_progress_info_json = json.dumps( {"total assessments": user_task_assessments} ) user_tasks_sql_queries.append( ( sql_q.UPDATE_USER_TASK_PROGRESS_SQL, ( user_task_progress_info_json, user_id, external_task_id, external_system_id, ), ) ) project_task_assessments += user_task_assessments project_task_progress_info_json = json.dumps( {"total assessments": project_task_assessments} ) project_tasks_sql_queries.append( ( sql_q.UPDATE_PROJECT_TASK_PROGRESS_SQL, ( project_task_progress_info_json, progress_info_modified, external_task_id, external_system_id, ), ) ) multiple_sql_queries = [x[0] for x in user_tasks_sql_queries] + [ x[0] for x in project_tasks_sql_queries ] multiple_params = [x[1] for x in user_tasks_sql_queries] + [ x[1] for x in project_tasks_sql_queries ] updated_rows = pg_utils.execute_non_query_multiple( multiple_sql_queries, multiple_params, correlation_id ) assert len(updated_rows) == len(project_tasks_sql_queries) + len( user_tasks_sql_queries ), ( "Number of updated database rows does not match number of " "executed sql queries" ) return { "updated_project_tasks": len(project_tasks_sql_queries), "updated_user_tasks": len(user_tasks_sql_queries), }
def get_project_task(project_task_id, correlation_id=None): return execute_query(sql_q.GET_PROJECT_TASK_SQL, (str(project_task_id), ), correlation_id)
def list_projects(correlation_id): return execute_query(sql_q.LIST_PROJECTS_SQL, None, correlation_id)
def get_project_task_by_external_task_id(external_task_id, correlation_id=None): return execute_query(sql_q.TASKS_BY_EXTERNAL_ID_SQL, (str(external_task_id), ), correlation_id)
def get_user_task(ut_id, correlation_id=None): result = execute_query(GET_USER_TASK_SQL, (str(ut_id), ), correlation_id) return result