import json from typing import Dict from client.clients.apollo_api.utils import update_charts_monitoring from client.clients.dsp_api.utils import get_chart_events, get_current_previous_chart from client.clients.user_data_api.utils import get_user_data_api_body, post_event from constants.common import DSP, TTL from logger import logger from utils import get_date def process_charts(dsp: DSP, charts: Dict[str, Dict[str, str]]): """Main Processor to: 1. Create event with additions/removals/moves 2. POST event to User Data Api 3. Update DB tblChartsMonitoring Args: dsp: DSP charts: Dict of delphi updated charts that must pe processed """ for chart_id, dates_and_data in charts.items(): current_updated = dates_and_data["current_updated"] previous_updated = dates_and_data["previous_updated"] apollo_previous_chart_data = dates_and_data["data"] logger.info(f"Starting to process \n" f"Chart id: {chart_id} \n" f"DSP: {dsp.value.capitalize()}.") current, previous = get_current_previous_chart( dsp, chart_id, get_date(current_updated), apollo_previous_chart_data ) if not current: logger.info( f"Got an empty current chart from Delphi:\n" f"Chart id: {chart_id} \n" f"DSP: {dsp.value.capitalize()}\n" f"Current chart Data: \n" f"{current} \n" f"Current Data-Health updated:\n" f"{current_updated}\n" f"Previous updated from Apollo DB\n" f"{previous_updated}" ) continue if dsp == DSP.APPLE: logger.info( f"Current apple chart:\n" f"Chart id: {chart_id} \n" f"DSP: {dsp.value.capitalize()}\n" f"Current chart Data: \n" f"{current} \n" f"Previous chart Data:" f"{previous}" ) additions, removals, moves, processed_datetime, broken_data = get_chart_events(current, previous) if broken_data: logger.error( f"Chart {chart_id} has a broken track data with no Artists!:" f"\n Broken Track(s) from total: {len(broken_data)}/{len(current)}" f"\n Broken Data: \n" f"{broken_data}" ) logger.info( f"Chart {chart_id} have: " f"\n Additions: {len(additions)}," f"\n Removals: {len(removals)}," f"\n Moves: {len(moves)}." ) if any([additions, removals, moves]): logger.info( f"1. Starting to create event for chart: {chart_id} with \n" f"Additions: {len(additions)}, \n" f"Removals: {len(removals)}, \n" f"Moves: {len(moves)}" ) event = get_user_data_api_body( dsp, chart_id, additions, removals, moves, previous_updated, current_updated, processed_datetime, ttl=TTL, ) logger.info( f"2. Created event for \n" f"Chart id: {chart_id} \n" f"DSP: {dsp.value.capitalize()} \n" f"Full event: \n" f"{event}." ) posted_event_response = post_event(event) logger.info( f"3. Posted event with" f"Event id: {posted_event_response['id']} \n" f"For {dsp.value.capitalize()} \n" f"Chart id: {chart_id} \n" f"to User Data Api." ) update_charts_monitoring( dsp, chart_id, current_updated, processed_datetime, json.dumps(current) if dsp == DSP.APPLE else None ) logger.info( f"Updated date for {chart_id} in ChartsMonitoring table." f"DSP: {dsp.value} \n" f"Chart id: {chart_id}" f"Current updated: {current_updated} \n" f"Processed datetime: {processed_datetime}" ) logger.info(f"Finished to process: \n" f"Chart id: {chart_id} \n" f"For DSP: {dsp.value.capitalize()}.")