import json from typing import Any, Dict, List, Mapping from analytics.constants import cache from analytics.logic.permissions import get_cross_attribution_pairs from analytics.queries.account import ( AccountTopContentAllTimeStreams, AccountTopContentDownloads, AccountTopContentStreams, ) from analytics.queries.format import format_row from analytics.utils.cache import cache_in_redis @cache_in_redis(ttl=cache.ONE_DAY) def get_top_content( query_params: Mapping[str, Any], permissions: Mapping[str, Any], ) -> List[Dict]: """Get releases (products) for an account.""" if query_params.get("aggregation_type").lower() == "downloads": query = AccountTopContentDownloads({**query_params, **permissions}) else: if query_params.get("transfer_product_ownership_enabled"): query_params["cross_attribution_pairs"] = get_cross_attribution_pairs( query_params["account_id"], query_params["account_type"] ) if query_params.get("start_date"): query = AccountTopContentStreams({**query_params, **permissions}) else: query = AccountTopContentAllTimeStreams({**query_params, **permissions}) result = [format_row(data_point) for data_point in query.execute()] if result: parsed = json.loads(result[0]["result"].lower()) else: parsed = {} return parsed