from datetime import date from typing import Any, Dict, List from server.client.clients.delphi_client import DelphiClient from server.constants.delphi.streams.group_by import StreamsGroupBy from server.constants.delphi.streams.include import StreamsInclude from server.legacy.core import constants from server.utils import combine async def get_amazon_tracks_streams( client: DelphiClient, isrc_list: List[str] or str, start_date: date or str, end_date: date or str, country_code: str or None = None, group_by: List[str] or None = None, offset: int or None = None, limit: int or None = None, ) -> list: """Get amazon tracks streams by ISRC and date range. It returns count which is current items count in response, Args: client: Delphi client. isrc_list: A list of track ISRC. start_date: Date interval from. end_date: Date interval to. country_code: Country code. group_by: Group items by column. limit: Limit value. offset: Offset value. Returns: Streams per ISRC, country, date. """ with_sub_dsp = group_by and StreamsGroupBy.SUB_DSP in group_by response = await client.get_streams( isrc_list=isrc_list, dsp_list=[constants.AMAZON], start_date=start_date, end_date=end_date, country_code_list=[country_code] if country_code else None, group_by=group_by, offset=offset, limit=limit, include=[StreamsInclude.ALL], ) return client.format_streams(response, dsp_prefix=with_sub_dsp and constants.AMAZON or None) async def get_amazon_track_streams_v1( client: DelphiClient, isrc_list: List[str] or str, start_date: date or str, end_date: date or str, country_code: str or None = None, group_by: List[str] or None = None, combine_isrc: bool = True, combine_tiers: bool = False, ) -> List[Dict[str, Any]]: """Get amazon tracks streams by ISRC list and date range. Returns combined data for all passed isrc. The result contains streams data items, each - for unique pair date + market with summed up values for all isrc from the list passed. Args: client: Delphi client. isrc_list: A list of track ISRC or single ISRC. start_date: Date interval from. end_date: Date interval to. country_code: Country code. group_by: Group items by column. combine_isrc: flag to get summed up data for all passed isrc. combine_tiers: flag to get combined data for tiers. Returns: Summed up for all isrc streams per country, date. """ data = await client.get_streams( isrc_list=isrc_list, dsp_list=[constants.AMAZON], start_date=start_date, end_date=end_date, country_code_list=[country_code] if country_code else None, group_by=group_by, include=[StreamsInclude.ALL], ) need_to_combine_isrc = combine_isrc and len(isrc_list) > 1 need_tiers = group_by and StreamsGroupBy.SUB_DSP in group_by need_to_combine_tiers = combine_tiers and need_tiers data = client.format_streams( data, remove_dsp=(not need_tiers), dsp_prefix=(need_tiers and constants.AMAZON or None) ) group_keys = ["country_code"] combine_rules = [] if need_to_combine_tiers: combine_rules.append(combine.combine_rule(key_from="dsp", key_to="tier_level", inner_keys=("streams",))) if not need_to_combine_isrc: group_keys.append("isrc") if need_to_combine_isrc: combine_rules.append(combine.combine_rule(key_from="isrc", value=",".join(isrc_list))) if need_tiers and not need_to_combine_tiers: group_keys.append("dsp") if not combine_rules: return data if group_by and StreamsGroupBy.DATE in group_by: group_keys.append(StreamsGroupBy.DATE) return combine.get_combined_data(data, group_keys=group_keys, combine_rules=combine_rules)