"""Backfill user show family table by mapping user podcast table. Run this in dev with command PYTHONPATH=. env/bin/python3.8 dev/backfill_user_show_family_table.py -- -env dev """ import argparse import logging import sys from podcast.connectors import mysql from podcast.constants import error from podcast.models import podcast as podcast_model from podcast.models import show_family as show_family_model from podcast.models import user as user_model from podcast.utils import api_utils log = logging.getLogger('main') log.setLevel(logging.DEBUG) fmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') sh = logging.StreamHandler(sys.stdout) sh.setFormatter(fmt) log.addHandler(sh) users = { 'dev': '366', 'qa': '1298', 'prod': '88' } request_headers = None def get_all_users_podcasts(): """Return all entries from user_podcast table. Returns: list of dict: containing the user podcast. """ with mysql.pod_db_session(read_only=True) as session: return session.query(user_model.user_podcast_table).all() def get_all_users_show_families(): """Return all entries from user_show_family table. Returns: list of dict: containing the user_show_family. """ with mysql.pod_db_session(read_only=True) as session: return session.query(user_model.user_show_family_table).all() def update_user_and_show_families(user_id, show_family_id, current_user_id): """Update user and it's show families. Args: user_id (str): unique user identifier. show_family_id (int): unique show_family identifier. current_user_id (str): id of the current user based on env. Returns: dict: containing the user dict. """ with mysql.pod_db_session() as session: query = session.query(user_model.User).filter(user_model.User.id == user_id) user = query.first() if not user: raise Exception(error.ERROR_MESSAGE_USER_NOT_FOUND) if not user.active: raise Exception(error.ERROR_MESSAGE_USER_IS_DELETED) show_family_ids = [show_family['id'] for show_family in user.to_dict()['show_families']] show_family_ids.append(show_family_id) user.show_families = session.query(show_family_model.ShowFamily).filter( show_family_model.ShowFamily.id.in_(show_family_ids) ).all() query.update({'updated_by': current_user_id}) result = query.first() return result.to_dict() def backfill_user_show_family_table(current_user_id): """Backfill user show family table by mapping user podcast table. Args: current_user_id (str): user id of the user based on env. """ error_count = 0 errored_user_and_podcast_ids = [] print('Fetching all users podcasts') users_podcasts = get_all_users_podcasts() print('Fetching all users show_families') users_show_families = get_all_users_show_families() for user_podcast in users_podcasts: try: user_id = user_podcast[0] podcast_id = user_podcast[1] print(f'\nRunning for user_podcast having user_id and podcast_id: {(user_id, podcast_id)}') podcast = podcast_model.get_podcast_by_id(podcast_id) show_family_id = podcast['show_family_id'] if show_family_id is None: raise Exception('Show Family does not exists for podcast_id: {podcast_id}') if (user_id, show_family_id) not in users_show_families: updated_user = update_user_and_show_families(user_id, show_family_id, current_user_id) print('Created user_show_family entry and updated user show_families: {}'.format( updated_user['show_families'])) users_show_families.append((user_id, show_family_id)) else: print(f'Entry for user_show_family: {(user_id, show_family_id)} already exists.') except Exception as e: errored_user_and_podcast_ids.append((user_id, podcast_id)) error_count += 1 log.info( f'Got an exception for user_podcast: {(user_id, podcast_id)}\n' f'Exception trace: {e}\n' ) print(f'\nErrored count: {error_count} and errored_user_and_podcast_ids: {errored_user_and_podcast_ids} \n') def main(): """Extract shell arguments backfill user show family table by mapping user podcast table.""" global request_headers argparser = argparse.ArgumentParser(prog='Backfill user show family table') argparser.add_argument('-env', required=True, help='env', default='qa') args = argparser.parse_args() env = args.env user_id = users[env] def get_user_id(): """Mock get user id return value.""" return user_id api_utils.get_user_id = get_user_id request_headers = { 'Grass-Account-Type': 'vendor', 'Grass-Account-Id': '1', 'Correlation-Id': '12', 'Orchard-Identity-UUID': 'podcast-admin', 'Content-Type': 'application/json' } log.info(f'Running backfill_user_show_family_table for {env} environment.\n') backfill_user_show_family_table(user_id) if __name__ == '__main__': main()