"""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.store_outages import add_outage_error_to_stores from sound_recordings.models import source_of_streams as sos_model from sound_recordings.schemas.source_of_streams import SourceOfStreamsSchema from sound_recordings.validation.schema import schema_dump def _get_sos_breakdown(breakdown): return { "active": { "growth_percentage": breakdown["streams_active_7_days_growth"], "total": breakdown["streams_active_7_days"], "value": breakdown["streams_active_7_days"] / breakdown["streams_7_days"] if breakdown["streams_7_days"] else 0, }, "passive": { "growth_percentage": breakdown["streams_passive_7_days_growth"], "total": breakdown["streams_passive_7_days"], "value": breakdown["streams_passive_7_days"] / breakdown["streams_7_days"] if breakdown["streams_7_days"] else 0, }, "collection": { "growth_percentage": breakdown["streams_collection_7_days_growth"], "total": breakdown["streams_collection_7_days"], "value": breakdown["streams_collection_7_days"] / breakdown["streams_7_days"] if breakdown["streams_7_days"] else 0, }, } def _get_dynamic_sos_breakdown(breakdown): return { "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, }, } @tracer.wrap(name="get_source_breakdown") def get_source_breakdown( request_context, isrc, countries, store_ids, start_date, end_date, distributors ): """Return source of streams 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 # an empty response, but with sources response_body = { "isrc": isrc, "active": {}, "passive": {}, "collection": {}, "sources": add_outage_error_to_stores(sources), } permissions_filter = permissions.get_permissions_filter(request_context) schema = SourceOfStreamsSchema() if ( not (start_date and end_date) and not (countries or len(countries) > 0) and not (store_ids or len(store_ids) > 0) ): breakdown = sos_model.get_sos_breakdown(permissions_filter, isrc, distributors) if breakdown is None: return oto.response.Response(schema_dump(schema, response_body)) sos_breakdown = _get_sos_breakdown(breakdown) else: if not (start_date and end_date): start_date, end_date = data_availability.get_date_range( data_availability.HIGHWATERMARK_DATE, days=7 ) breakdown = sos_model.get_dynamic_sos_breakdown( permissions_filter, isrc, distributors, countries, store_ids, start_date, end_date, ) if breakdown is None: return oto.response.Response(schema_dump(schema, response_body)) sos_breakdown = _get_dynamic_sos_breakdown(breakdown) breakdown_ = {**response_body, **sos_breakdown} return oto.response.Response(schema_dump(schema, breakdown_))