"""Logic for retrieving top markets information.""" import oto.response from ddtrace import tracer from sound_recordings.constants import store from sound_recordings.logic import permissions from sound_recordings.logic.store_outages import add_outage_error_to_stores from sound_recordings.models import top_markets as top_markets_model from sound_recordings.schemas.top_markets import TopMarketsSchema from sound_recordings.validation.schema import schema_dump @tracer.wrap(name="get_top_markets") def get_top_markets(request_context, isrc, distributors, store_ids=[], countries=[]): """Return top markets. Args: request_context (RequestContext): RequestContext class isrc (str): ISRC of track to fetch breakdown for distributors (list): List of distributors names store_ids (list): List of store ids to filter by Returns: oto.response.Response: SOS breakdown """ sources = store.ALL_TOP_MARKETS_SOURCES # an empty response, but with sources response_body = { "isrc": isrc, "items": [], "sources": add_outage_error_to_stores(sources), } permissions_filter = permissions.get_permissions_filter(request_context) top_markets = top_markets_model.get_top_markets( permissions_filter, isrc, distributors, store_ids, countries ) if top_markets: response_body["items"] = top_markets schema = TopMarketsSchema() return oto.response.Response(schema_dump(schema, response_body))