from typing import List, Dict, Any from smelog.factory import BoundLogger from charts_messages import schemas from charts_messages.client.utils import get_event_by_id, post_messages_to_user_data_api from charts_messages.processings.get_accounts_to_notify import get_users_to_notify from charts_messages.processings.get_messages import get_user_messages from charts_messages.utils import check_received_event, get_tracks_to_process, get_tracks_asdict, deserialize_event, \ get_event_chart_raw_info, get_changelog_key, parse_chart_country_code, get_messages_status_length def common_handler(events: List[Dict[str, Any]], logger: BoundLogger): for event in events: event = deserialize_event(logger, schemas.IncomingEvent(), event) if not event: continue event_id = event["id"] changelog_key = get_changelog_key() if not check_received_event(event): logger.warning(f"{event_id} - id Event is no more actual") continue logger.info(f"Starting to process actual event with:\n" f"Event id: {event_id} \n" f"Target changelog: {changelog_key}") # get event data from user data api data = get_event_by_id(event_id) # get tracks data such as additions/removals/moves based on lambda run configuration from event data tracks = get_tracks_to_process(data) if not tracks: logger.info(f"Event id: {event_id} \n" f"Target changelog: {changelog_key}\n" f"Has {len(tracks)} tracks to process\n") continue logger.info(f"Event id: {event_id} \n" f"Target changelog: {changelog_key}\n" f"Has: {len(tracks)} tracks\n" f"{changelog_key.capitalize()} Tracks Data:" f"{tracks}") # get isrc to track information mapping isrc_track_mapping = get_tracks_asdict(tracks) # get chart information from current event without redundant information (we`ll be needed further) chart = get_event_chart_raw_info(event) # handle delphi-specific market codes chart["country_code"] = parse_chart_country_code(chart["country_code"]) # get all user(s) that want to get a notification based on their settings user_accounts_to_notify = get_users_to_notify(logger, chart, list(isrc_track_mapping.keys())) logger.info(f"Event id: {event_id} \n" f"Target changelog: {changelog_key}\n" f"Has: {len(user_accounts_to_notify)} active user accounts interested in tracks from event\n" f"Starting to check if user(s) has notifications ON and creating feed messages") # messages for every user about track that he wants to get a notification about messages = get_user_messages( logger=logger, event_id=event_id, chart=chart, tracks=isrc_track_mapping, accounts_to_notify=user_accounts_to_notify ) if not messages: logger.info(f"Event id: {event_id}\n" f"Target changelog: {changelog_key}\n" f"Has no messages to create for any user of\n" f"Total users: {len(user_accounts_to_notify)}" f"Because their notifications are OFF ") continue total_messages_count = len(messages) logger.info(f"Event id: {event_id} \n" f"Target changelog: {changelog_key}\n" f"Has: {total_messages_count} Feed messages created \n") logger.info(f"FIRST CREATED MESSAGE EXAMPLE: \n" f"{messages[0]}") message_status = post_messages_to_user_data_api(messages=messages) status_ok, status_failed = get_messages_status_length(message_status) logger.info(f"Event id: {event_id} \n" f"Messages sent:\n" f"Successfully: {status_ok}/{total_messages_count} \n," f"Failed: {status_failed}/{total_messages_count}")