"""Logic Tier for Analytics.""" from podcast.constants import analytics as analytics_const from podcast.constants.feature_flag import FEATURE_PODCAST_IA_RESTRUCTURE from podcast.logic import user as user_logic from podcast.logic import user_v2 as user_v2_logic from podcast.models import ows_analytics from podcast.models import podcast as podcast_model from podcast.utils import feature_flag_utils def top_episodes( date_range, countries, players, limit, offset, network_id, podcast_id, sort_field, sort_order, start_date=None, end_date=None): """Top Episodes.""" if podcast_id: podcast = podcast_model.get_podcast_by_id(podcast_id) if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_show_family_or_raise(podcast['show_family_id']) else: user_logic.current_user_owns_podcast_or_raise(podcast) podcast_ids = [podcast_id] else: podcast_ids = _get_podcast_ids(network_id) if podcast_ids: return ows_analytics.top_episodes( podcast_ids, date_range, countries, players, limit, offset, sort_field, sort_order, start_date, end_date) else: return { 'items': [], 'pagination': { 'total_records': 0 } } def episode_daily_downloads(episode_ids, date_range, countries, players, start_date=None, end_date=None): """Episodes daily downloads.""" if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_episodes_or_raise(episode_ids) else: user_logic.current_user_owns_episodes_or_raise(episode_ids) return ows_analytics.episode_daily_downloads(episode_ids, date_range, countries, players, start_date, end_date) def top_podcasts( date_range, countries, players, limit, offset, network_id, sort_field, sort_order, start_date=None, end_date=None): """Top Podcasts.""" podcast_ids = _get_podcast_ids(network_id) if podcast_ids: return ows_analytics.top_podcasts( podcast_ids, date_range, countries, players, limit, offset, sort_field, sort_order, start_date, end_date) else: return { 'items': [], 'pagination': { 'total_records': 0 } } def podcast_daily_downloads(podcast_ids, date_range, countries, players, start_date=None, end_date=None): """Podcasts daily downloads.""" if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_podcast_ids_or_raise(podcast_ids) else: user_logic.current_user_owns_podcast_ids_or_raise(podcast_ids) return ows_analytics.podcast_daily_downloads(podcast_ids, date_range, countries, players, start_date, end_date) def top_networks(date_range, countries, players, limit, offset, sort_field, sort_order, start_date=None, end_date=None): """Top Networks.""" network_ids = user_logic.network_ids_for_current_user() return ows_analytics.top_networks( network_ids, date_range, countries, players, limit, offset, sort_field, sort_order, start_date, end_date) def network_daily_downloads(network_ids, date_range, countries, players, start_date=None, end_date=None): """Networks daily downloads.""" if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_network_ids_or_raise(network_ids) else: user_logic.current_user_owns_network_ids_or_raise(network_ids) return ows_analytics.network_daily_downloads(network_ids, date_range, countries, players, start_date, end_date) def country_daily_downloads(object_id, object_type, date_range, countries, players, start_date=None, end_date=None): """Country daily downloads.""" if object_type == analytics_const.NETWORK: if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_network_ids_or_raise([object_id]) else: user_logic.current_user_owns_network_ids_or_raise([object_id]) elif object_type == analytics_const.PODCAST: if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_podcast_ids_or_raise([object_id]) else: user_logic.current_user_owns_podcast_ids_or_raise([object_id]) else: if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_episodes_or_raise([object_id]) else: user_logic.current_user_owns_episodes_or_raise([object_id]) return ows_analytics.country_daily_downloads( object_id, object_type, date_range, countries, players, start_date, end_date) def player_daily_downloads(object_id, object_type, date_range, countries, players, start_date=None, end_date=None): """Player daily downloads.""" if object_type == analytics_const.NETWORK: if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_network_ids_or_raise([object_id]) else: user_logic.current_user_owns_network_ids_or_raise([object_id]) elif object_type == analytics_const.PODCAST: if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_podcast_ids_or_raise([object_id]) else: user_logic.current_user_owns_podcast_ids_or_raise([object_id]) else: if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_episodes_or_raise([object_id]) else: user_logic.current_user_owns_episodes_or_raise([object_id]) return ows_analytics.player_daily_downloads( object_id, object_type, date_range, countries, players, start_date, end_date) def get_chartable_podcasts_by_ids(podcast_ids): """Chartable podcasts.""" if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): user_v2_logic.current_user_owns_podcast_ids_or_raise(podcast_ids) else: user_logic.current_user_owns_podcast_ids_or_raise(podcast_ids) return ows_analytics.get_chartable_podcasts_by_ids(podcast_ids) def get_chartable_podcast_chart(store, category, country, report_date): """Get podcast chart.""" if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): podcast_ids = user_v2_logic.podcast_ids_owned_by_current_user() else: podcast_ids = user_logic.podcast_ids_owned_by_current_user() return ows_analytics.get_podcast_chart( store, category, country, report_date, podcast_ids) def get_chartable_episode_chart(category, country, report_date): """Get episode chart.""" if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): podcast_ids = user_v2_logic.podcast_ids_owned_by_current_user() else: podcast_ids = user_logic.podcast_ids_owned_by_current_user() return ows_analytics.get_episode_chart( category, country, report_date, podcast_ids) def _get_network_ids(network_id): if network_id: user_logic.current_user_owns_network_ids_or_raise([network_id]) return [network_id] else: return user_logic.network_ids_for_current_user() def _get_podcast_ids(network_id): if feature_flag_utils.get_feature_flag(FEATURE_PODCAST_IA_RESTRUCTURE): all_owned_podcast_ids = user_v2_logic.podcast_ids_owned_by_current_user() if network_id: user_v2_logic.current_user_owns_network_ids_or_raise([network_id], True) podcast_ids = podcast_model.get_podcast_ids_by_network_ids([network_id]) return list(set(all_owned_podcast_ids).intersection(set(podcast_ids))) else: all_owned_podcast_ids = user_logic.podcast_ids_owned_by_current_user() if network_id: user_logic.current_user_owns_network_ids_or_raise([network_id], True) podcast_ids = podcast_model.get_podcast_ids_by_network_ids([network_id]) return list(set(all_owned_podcast_ids).intersection(set(podcast_ids))) return all_owned_podcast_ids