from datetime import datetime from typing import Any, Dict, List from client import service from constants.common import DSP from utils import get_datetime_string def get_body_data( additions: List[Dict[str, Any]], removals: List[Dict[str, Any]], moves: List[Dict[str, Any]], previous_date: str, current_date: str, processed_datetime: datetime, ) -> Dict: """Create body for event to post Args: additions: List[Dict] with chart additions removals: List[Dict] with chart removals moves: List[Dict] with chart moves previous_date: isoformat previous date the same event related to (previous "max_date" from Delphi charts api) current_date: isoformat current date, the same event related to (current "max_date" from Delphi charts api) processed_datetime: isoformat datetime, when the event was tracked by our system Returns: Dict of a given format to be a body of POST /event/ to User Data Api """ result = { "previous_event_dt": previous_date, "current_event_tracked_dt": processed_datetime.isoformat(), "current_event_dt": get_datetime_string(current_date), "changelog": {"additions": additions, "removals": removals, "moves": moves}, } return result def get_body_meta(dsp: DSP, chart_id: str, domain: str = "charts"): chart_type, breakdown, market = chart_id.split("_") chart_type = chart_type if not dsp == DSP.APPLE else None our_custom_id = f"{dsp.value}_{market}_{chart_type}_{breakdown}" result = { "domain": domain, "id": our_custom_id, "dsp": dsp.value, "country_code": market, "type": chart_type, "breakdown": breakdown, } return result def get_user_data_api_body( dsp: DSP, chart_id: str, additions: List[Dict[str, Any]], removals: List[Dict[str, Any]], moves: List[Dict[str, Any]], previous_date: str, current_date: str, processed_datetime: datetime, code: str = "charts_updated", ttl: int = None, ) -> Dict: """Create body for event to post Args: dsp: DSP chart_id: delphi chart id additions: List[Dict] with chart additions removals: List[Dict] with chart removals moves: List[Dict] with chart moves previous_date: isoformat previous date the same event related to (previous "max_date" from Delphi charts api) current_date: isoformat date, the same event related to (current "max_date" from Delphi charts api) processed_datetime: datetime event was tracked by our system code: ttl: Returns: Dict of a given format to be a body of POST /event/ to User Data Api""" data = get_body_data(additions, removals, moves, previous_date, current_date, processed_datetime) meta = get_body_meta(dsp, chart_id) result = {"code": code, "ttl": ttl, "meta": meta, "data": data} return result def post_event(event, **params): """Post chart event to User Data Api""" return service.user_data.post_events(data=event, **params)