"""Participant Track Streams Logic.""" import datetime from typing import Any, Mapping from oto import response as oto_response from analytics.constants import cache from analytics.constants.parameters import ALL_TIME from analytics.logic import data_availability from analytics.queries.participant_track_streams import ( ParticipantTrackStreamsAll, ParticipantTrackStreamsStore, ) from analytics.schemas.participants import ( ParticipantStreamsAllSchema, ParticipantStreamsStoreSchema, ) from analytics.utils import store_availability from analytics.utils import streams as streams_utils from analytics.utils.cache import cache_in_redis from analytics.validation.schema import schema_dump @cache_in_redis(ttl=cache.ONE_DAY) def get_track_streams_all( query_params: Mapping[str, Any], permissions: Mapping[str, Any], ): """Return track streams for global participant ID. Args: query_params: Dict with global_participant_id, distributors, country_ids, store_ids, start_date, end_date. permissions: Dict with permission_* keys. Returns: oto.response.Response with track streams payload. """ global_participant_id = query_params["global_participant_id"] response_body = {"global_participant_id": global_participant_id, "items": []} start_date = query_params.get("start_date") end_date = query_params.get("end_date") if not (start_date and end_date): start_date, end_date = data_availability.get_date_range( data_availability.HIGHWATERMARK_DATE, days=28 ) all_time = start_date == ALL_TIME store_ids = query_params.get("store_ids", []) if not store_ids: store_ids = store_availability.get_store_ids() else: store_ids = sorted( list(set(store_ids).intersection(store_availability.get_store_ids())) ) if not store_ids: schema = ParticipantStreamsAllSchema() return oto_response.Response(schema_dump(schema, response_body)) end_date_str = ( end_date.strftime("%Y-%m-%d") if isinstance(end_date, datetime.date) else end_date ) query_input = { **permissions, "global_participant_id": global_participant_id, "store_ids": store_ids, "distributors": query_params["distributors"], "end_date": end_date_str, "country_ids": query_params.get("country_ids", []), "all_time": all_time, "transfer_product_ownership_enabled": query_params.get( "transfer_product_ownership_enabled", False ), } if not all_time: start_date_str = ( start_date.strftime("%Y-%m-%d") if isinstance(start_date, datetime.date) else start_date ) query_input["start_date"] = start_date_str result = ParticipantTrackStreamsAll(query_input).execute() streams_timeseries = [ dict(row._mapping) if hasattr(row, "_mapping") else dict(row) for row in result ] track_streams = streams_utils.get_streams_all( streams_timeseries, start_date, end_date ) if track_streams: response_body["items"] = track_streams schema = ParticipantStreamsAllSchema() return oto_response.Response(schema_dump(schema, response_body)) @cache_in_redis(ttl=cache.ONE_DAY) def get_track_streams_store( query_params: Mapping[str, Any], permissions: Mapping[str, Any], ): """Return track streams by store for global participant ID. Args: query_params: Dict with global_participant_id, distributors, country_ids, store_ids, start_date, end_date. permissions: Dict with permission_* keys. Returns: oto.response.Response with track streams by store payload. """ global_participant_id = query_params["global_participant_id"] response_body = {"global_participant_id": global_participant_id, "stores": []} start_date = query_params.get("start_date") end_date = query_params.get("end_date") if not (start_date and end_date): start_date, end_date = data_availability.get_date_range( data_availability.HIGHWATERMARK_DATE, days=28 ) all_time = start_date == ALL_TIME store_ids = query_params.get("store_ids", []) if not store_ids: store_ids = store_availability.get_store_ids() else: store_ids = sorted( list(set(store_ids).intersection(store_availability.get_store_ids())) ) if not store_ids: schema = ParticipantStreamsStoreSchema() return oto_response.Response(schema_dump(schema, response_body)) end_date_str = ( end_date.strftime("%Y-%m-%d") if isinstance(end_date, datetime.date) else end_date ) query_input = { **permissions, "global_participant_id": global_participant_id, "store_ids": store_ids, "distributors": query_params["distributors"], "end_date": end_date_str, "country_ids": query_params.get("country_ids", []), "all_time": all_time, "transfer_product_ownership_enabled": query_params.get( "transfer_product_ownership_enabled", False ), } if not all_time: start_date_str = ( start_date.strftime("%Y-%m-%d") if isinstance(start_date, datetime.date) else start_date ) query_input["start_date"] = start_date_str result = ParticipantTrackStreamsStore(query_input).execute() streams_timeseries = [ dict(row._mapping) if hasattr(row, "_mapping") else dict(row) for row in result ] track_streams = streams_utils.get_streams_by_store( streams_timeseries, start_date, end_date ) if track_streams: response_body["stores"] = track_streams schema = ParticipantStreamsStoreSchema() return oto_response.Response(schema_dump(schema, response_body))