from internal.helpers import make_iso_date_now, rds_query, get_user_id from management.helpers import user_and_company_for_email, get_alliance_member, get_caw_owner_id from management.commons import MAX_LOG_ENTRY_LIMIT, EVENT_LOG_TYPES_FRONT_FILTER_STR import logging log = logging.getLogger().getChild("management.logs") def event_log(endpoint, req, event): """workspaceId: ID, allianceId: ID, collectionId: ID, userId: ID, offset: Int, limit: Int""" log.info(f"EVENT_LOG: {req}") sql_params = {} if 'alliance_schema' in req: selected_schema = req['alliance_schema'] management_schema = req['alliance_management_schema'] schema_num = req['alliance_schema'] elif 'workspace_schema' in req: # TODO: update with real lookup for workspaces implementation selected_schema = req['workspace_schema'] schema_num = req['workspace_schema'] management_schema = req['management_schema'] else: raise RuntimeError('Ambiguous combination of parameters') if 'collectionId' in req: collection_id = int(req['collectionId']) sql_params['collection_id'] = collection_id if 'userId' in req: log_user_id, _ = user_and_company_for_email(req['userId']) if log_user_id: sql_params['user_id'] = log_user_id sql_where = f"WHERE type IN ({EVENT_LOG_TYPES_FRONT_FILTER_STR}) {'and' if sql_params else ''} {' and '.join(f'{ww} = %({ww})s' for ww in sql_params.keys())} " count_query = f"SELECT count(*) FROM {selected_schema}.event_log {sql_where};" data_query = f"""SELECT id, '{schema_num}-' || collection_id "collectionId", type "eventType", description "eventDesc", TO_CHAR(timestamp, 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') "timeStamp", user_id FROM {selected_schema}.event_log {sql_where} ORDER BY id DESC LIMIT %(limit)s OFFSET %(offset)s;""" if 'offset' in req: offset = int(req['offset']) if offset < 0: offset = 0 else: offset = 0 sql_params['offset'] = offset if 'limit' in req: limit = int(req['limit']) if limit < 0: limit = MAX_LOG_ENTRY_LIMIT else: limit = MAX_LOG_ENTRY_LIMIT sql_params['limit'] = limit # log.info(f"EVENT_LOG: {repr(count_query)} /// {repr(data_query)} /// {repr(sql_params)}") res_count, res_data = rds_query([count_query, data_query], sql_params) user_id_set = set([res['user_id'] for res in res_data]) sql_user_ids = ','.join([f"'{uid}'" for uid in user_id_set]) alliance_owner_id = get_caw_owner_id(selected_schema, management_schema) user_id_map = {member['user_id']: get_alliance_member(endpoint, {'alliance_schema': selected_schema, 'email': '(deprecated)', 'memberId': member['user_id'], 'member_roles': member['roles'], 'user_name': member['user_name'], 'company_name': member['company_name'], 'member_email': member['email'], 'alliance_owner_id': alliance_owner_id}, event) for member in rds_query(f"SELECT uc.user_id, uc.email, uc.user_name, uc.company_name, ur.roles from commons.user_company uc " f"LEFT JOIN {management_schema}.user_roles ur ON uc.user_id = ur.user_id and ur.id = '{selected_schema}'" f"WHERE uc.user_id IN ({sql_user_ids})")} if sql_user_ids else {} for res in res_data: if res['user_id'] and res['user_id'] in user_id_map: res['member'] = user_id_map[res['user_id']] else: res['member'] = {'__typename': 'MemberRole', 'id': 'xxx', 'name': 'unknown'} del res['user_id'] return {'items': res_data, 'offset': offset, 'length': res_count[0]['count']}