"""Logic for retrieving sound-recording streams by product timeseries.""" from ddtrace import tracer from oto import response as oto_response from sound_recordings.logic import data_availability, permissions from sound_recordings.models import streams_by_product as streams_product_model from sound_recordings.schemas.streams_by_product import StreamsByProductSchema from sound_recordings.utils import streams as streams_utils from sound_recordings.validation.schema import schema_dump @tracer.wrap(name="get_streams_by_product") def get_streams_by_product( request_context, isrc, distributors, countries=[], store_ids=[], start_date=None, end_date=None, ): """Get streams-by-product timeseries for ISRC, given account ID & type. Args: request_context (RequestContext): RequestContext class isrc (str): ISRC of track to fetch downloads for distributors (list): List of distributors names countries (list): List of country codes to filter by store_ids (list): List of store ids to filter by start_date (datetime.date): Start date end_date (datetime.date): End date Returns: oto_response.Response with streams by product payload. """ # empty response response_body = {"isrc": isrc, "products": []} permissions_filter = permissions.get_permissions_filter(request_context) if not (start_date and end_date): start_date, end_date = data_availability.get_date_range( data_availability.HIGHWATERMARK_DATE, days=28 ) streams_timeseries = streams_product_model.get_streams_by_product( permissions_filter, isrc, distributors, countries, store_ids, start_date, end_date, ) streams_by_product = streams_utils.get_streams_by_product( streams_timeseries, start_date, end_date ) if not streams_by_product or len(streams_by_product) == 0: return oto_response.create_not_found_response() response_body["products"] = streams_by_product schema = StreamsByProductSchema() return oto_response.Response(schema_dump(schema, response_body))