"""Logic for maxwell records.""" from datetime import datetime from datetime import timezone from src.constants import maxwell from src.constants import sql from src.model.snowflake import bulk_insert def log_records(records): """Log maxwell records into Snowflake. Args: records (dict): A dictionary where the keys are table names (str), and the values are lists of dictionaries """ for table, changes in records.items(): table_name = sql.TABLE_NAME_TEMPLATE.format(table_name=table) columns = [sql.LOG_TIMESTAMP, sql.LOG_ACTION] data = [] for change in changes: record_data = { sql.LOG_TIMESTAMP: datetime.fromtimestamp( change[maxwell.MAXWELL_TIMESTAMP], tz=timezone.utc ).isoformat(), sql.LOG_ACTION: change[maxwell.MAXWELL_TYPE], } record_data.update(change[maxwell.MAXWELL_DATA]) columns.extend( [key for key in record_data.keys() if key not in columns] ) data.append(record_data) bulk_insert(table_name, columns, data)