"""Methods for RDS reports.""" import copy from pymysql import err from accounting import config from accounting import const from accounting.config import log from accounting.models import sql from accounting.models import sql_fp DB_NULL = 'null' def get_batch(cursor, limit, offset=0): """Get a batch of records from RDS. Args: cursor: MySQL cursor limit: number of records to get offset: offset from the beginning """ try: get_batch_query = sql.GET_BATCH.format( table_name=config.MYSQL_TABLE_NAME) # TODO: Remove this once fingerprinting data is live if config.USE_MRR is False: get_batch_query = sql_fp.GET_BATCH.format( table_name=config.MYSQL_TABLE_NAME) cursor.execute(get_batch_query, { 'limit': limit, 'offset': offset }) except err.MySQLError as ex: log.exception('Failed to get MySql batch') raise ex def update_report(connection, records): """Update the report in RDS. Args: connection: MySQL connection records: list of enriched records """ if config.USE_MRR: _update_report_row_by_row(connection, records) else: _update_report_bulk(connection, records) def _update_report_row_by_row(connection, records): query = sql.UPDATE_REPORT.format( table_name=config.MYSQL_TABLE_NAME) try: with connection.cursor() as cursor: for record in records: query_params = copy.deepcopy(record) if query_params[const.TUID] == DB_NULL: query_params[const.TUID] = None if query_params[const.INTERNAL_CONFLICT] == DB_NULL: query_params[const.INTERNAL_CONFLICT] = None cursor.execute(query, query_params) connection.commit() except err.MySQLError as ex: connection.rollback() log.exception('Failed to update MySql') raise ex def _update_report_bulk(connection, records): bulk_update_sql = sql_fp.BULK_UPDATE_REPORT.format( table_name=config.MYSQL_TABLE_NAME) try: with connection.cursor() as cursor: cursor.execute(sql_fp.CREATE_TMP_UPDATE_TABLE) cursor.execute(sql_fp.TRUNCATE_TMP_UPDATE_TABLE) rows = [ ( r[const.SERVICE], r[const.DATE_COLUMN_NAME], r[const.ISRC], r[const.TERRITORY], None if r[const.TUID] == DB_NULL else r[const.TUID], None if r[const.INTERNAL_CONFLICT] == DB_NULL else r[const.INTERNAL_CONFLICT], r.get(const.RULES_SUMMARY), ) for r in records ] cursor.executemany(sql_fp.INSERT_TMP_UPDATE, rows) cursor.execute(bulk_update_sql) connection.commit() except err.MySQLError as ex: connection.rollback() log.exception('Failed to update MySql') raise ex