import asyncio from apollo_utils.core.constants.dsp import DSP from apollo_utils.core.constants.market import Market from datetime import date from typing import Any, Dict, List, Optional, Tuple, Union from server import config from server.cache.utils import cached from server.client import services from server.scenarios.playlists.amazon.markets import get_amazon_playlist_str_id_country_code_pairs from server.utils.common import prefix_str def _get_amazon_string_ids_and_markets( amazon_int_id: Union[int, str], amazon_pairs: List[Tuple[str, str]] ) -> Tuple[List[str], List[str]]: amazon_string_playlist_ids, amazon_country_codes = [], [] for str_id, c_code in amazon_pairs: amazon_string_playlist_ids.append(f"{str_id}:{amazon_int_id}_{c_code}"), amazon_country_codes.append(c_code) return amazon_string_playlist_ids, amazon_country_codes @cached(ttl=config.AMAZON_PLAYLIST_WORLDWIDE_STREAMS_GRAPH_TTL, noself=True) async def get_amazon_worldwide_streams_graph(amazon_int_id: Union[int, str], start_date: date, end_date: date): amazon_playlist_str_id_markets = await get_amazon_playlist_str_id_country_code_pairs( playlist_id=prefix_str(v=amazon_int_id, prefix=DSP.AMAZON.value, delimiter="_") ) pl_ids, country_codes = _get_amazon_string_ids_and_markets(amazon_int_id, amazon_playlist_str_id_markets) full_graph_result = await services.dsp.get_v1_playlists_streams_graph( playlist_id=pl_ids, start_date=start_date, end_date=end_date, markets=country_codes, vendor=[DSP.AMAZON.value], group_playlist_id_as=amazon_int_id, group_market_as=Market.WORLDWIDE, ) return full_graph_result def _get_graphs_result(graphs_results: List[Dict[str, Any]]) -> Dict[str, Any]: """Gather streams graph result to a single dict with all graphs in "items" Args: graphs_results: List of streams graph results Returns: Finalized result for many streams_graph(s) gathered to one into items """ result = {} for graph_item in graphs_results: if not result: result = graph_item continue result["items"].extend(graph_item["items"]) return result async def get_amazon_streams_graph_with_worldwide( playlist_id: List[str], start_date: Optional[date], end_date: Optional[date], markets: List[str], vendor: List[str] ): """Get amazon playlists worldwide streams graph for period Args: playlist_id: List of amazon full id(s) like -> ['B08GNX63NT:120_es', 'B00KMI2S3W:120_us'] start_date: Streams period start date end_date: Streams period end date markets: list of country codes' vendor: """ split_playlist_id = playlist_id[0].split(":") string_playlist_id, int_playlist_id = split_playlist_id[0], split_playlist_id[1].split("_")[0] tasks = [] markets.remove(Market.WORLDWIDE) if markets: tasks.append( services.dsp.get_v1_playlists_streams_graph( playlist_id=playlist_id, start_date=start_date, end_date=end_date, markets=markets, vendor=vendor, ) ) tasks.append( get_amazon_worldwide_streams_graph(amazon_int_id=int_playlist_id, start_date=start_date, end_date=end_date) ) graphs_result = await asyncio.gather(*tasks) return _get_graphs_result(graphs_result)