"""Logic for retrieving source of streams breakdown.""" import oto.response from ddtrace import tracer from sound_recordings.constants import store from sound_recordings.logic import data_availability, permissions from sound_recordings.logic.parallel import parallel from sound_recordings.logic.store_outages import add_outage_error_to_stores from sound_recordings.models import streams_breakdown as streams_breakdown_model from sound_recordings.schemas.streams_breakdown import StreamsBreakdownSchema from sound_recordings.utils import store_availability from sound_recordings.validation.schema import schema_dump def _get_breakdown( breakdown, source_of_streams_sources, streams_by_subscription_sources ): return { "source_of_streams": { "active": { "growth_percentage": None, "total": breakdown["streams_active"], "value": breakdown["streams_active"] / breakdown["streams"] if breakdown["streams"] else 0, }, "passive": { "growth_percentage": None, "total": breakdown["streams_passive"], "value": breakdown["streams_passive"] / breakdown["streams"] if breakdown["streams"] else 0, }, "collection": { "growth_percentage": None, "total": breakdown["streams_collection"], "value": breakdown["streams_collection"] / breakdown["streams"] if breakdown["streams"] else 0, }, "unknown": { "growth_percentage": None, "total": breakdown["streams"] - breakdown["streams_active"] - breakdown["streams_passive"] - breakdown["streams_collection"], "value": ( breakdown["streams"] - breakdown["streams_active"] - breakdown["streams_passive"] - breakdown["streams_collection"] ) / breakdown["streams"] if breakdown["streams"] else 0, }, "sources": source_of_streams_sources, }, "streams_by_subscription": { "subscription": { "growth_percentage": None, "total": breakdown["subscription"], "value": breakdown["subscription"] / breakdown["streams"] if breakdown["streams"] else 0, }, "ad_supported": { "growth_percentage": None, "total": breakdown["ad_supported"], "value": breakdown["ad_supported"] / breakdown["streams"] if breakdown["streams"] else 0, }, "mid_tier": { "growth_percentage": None, "total": breakdown["mid_tier"], "value": breakdown["mid_tier"] / breakdown["streams"] if breakdown["streams"] else 0, }, "sources": streams_by_subscription_sources, }, } @tracer.wrap(name="get_streams_breakdown") def get_streams_breakdown( request_context, isrc, countries, store_ids, start_date, end_date, distributors ): """Return subscription type breakdown. Args: request_context (RequestContext): RequestContext class isrc (str): ISRC of track to fetch breakdown for 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 distributors (list): List of distributors names Returns: oto.response.Response: SOS breakdown """ sources = store.ALL_SOURCE_OF_STREAMS_SOURCES 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=7 ) requests = { "streams_breakdown": { "func": streams_breakdown_model.get_streams_breakdown, "args": ( permissions_filter, isrc, distributors, countries, store_ids, start_date, end_date, ), }, "source_of_streams_sources": { "func": add_outage_error_to_stores, "args": (sources,), }, "streams_by_subscription_sources": { "func": add_outage_error_to_stores, "args": (store_availability.get_sources(),), }, } result = parallel(requests) breakdown = result.message["streams_breakdown"] source_of_streams_sources = result.message["source_of_streams_sources"] streams_by_subscription_sources = result.message["streams_by_subscription_sources"] # an empty response, but with sources response_body = { "isrc": isrc, "source_of_streams": { "active": {}, "passive": {}, "collection": {}, "unknown": {}, "sources": source_of_streams_sources, }, "streams_by_subscription": { "subscription": {}, "ad_supported": {}, "mid_tier": {}, "sources": streams_by_subscription_sources, }, } schema = StreamsBreakdownSchema() if breakdown is None: return oto.response.Response(schema_dump(schema, response_body)) sst_breakdown = _get_breakdown( breakdown, source_of_streams_sources, streams_by_subscription_sources ) return oto.response.Response( schema_dump(schema, {**response_body, **sst_breakdown}) )