from flask import jsonify, request from analytics import config from analytics.api import app from analytics.constants.access import ACCESS_ANALYTICS from analytics.features import ( is_insights_line_soundcloud_collection_as_active_enabled, is_insights_transfer_product_ownership_enabled, ) from analytics.handler_utils import _get_query_string_params from analytics.logic.permissions import get_permission_values from analytics.logic.sound_recording_related_videos import ( get_sound_recording_related_videos_by_isrc, ) from analytics.logic.sound_recording_timeseries import ( get_sound_recording_summary, get_sound_recording_timeseries, ) from analytics.validation.access import verify_profile @app.route(config.SOUND_RECORDING_TIMESERIES_PATH) @verify_profile(access=ACCESS_ANALYTICS) def sound_recording_timeseries(isrc): """Return timeseries for a sound recording. Route: GET /sound-recording//timeseries Path params: isrc (str): ISRC identifier. Query params: start_date (str): Start date YYYY-MM-DD. end_date (str): End date YYYY-MM-DD. type (str): Timeseries type. Values: TRACK_STREAMS, TRACK_STREAMS_BY_STORE, TRACK_STREAMS_BY_COUNTRY, TRACK_STREAMS_BY_PRODUCT, TRACK_STREAMS_BY_SOS, TRACK_STREAMS_BY_SOS_V2, TRACK_DOWNLOADS, TRACK_DOWNLOADS_BY_COUNTRY, TRACK_DOWNLOADS_BY_PRODUCT, TRACK_DOWNLOADS_BY_STORE. resolution (str): Time resolution. countries (list[str]): Country codes to filter by. distributors (list[str]): Distributor names. store_ids (list[int]): Store IDs to filter by. ids (list[str]): IDs to filter aggregation by. stream_sources (list[str]): Source of streams filter. Snowflake tables: - V_STREAMS_BY_TRACK_FEED_DISTRIBUTOR_DAILY - V_STREAMS_BY_TRACK_COUNTRY_FEED_DISTRIBUTOR_DAILY - DOWNLOADS_BY_TRACK_FEED_DISTRIBUTOR_DAILY - DOWNLOADS_BY_TRACK_COUNTRY_FEED_DISTRIBUTOR_DAILY JOINs: - DIM_RELEASE Returns: JSON with timeseries items. """ path_params = {"isrc": isrc} query_params = _get_query_string_params( ["start_date", "end_date", "type", "resolution"], path_params ) query_params = { **query_params, "countries": request.args.getlist("countries"), "distributors": [d.lower() for d in request.args.getlist("distributors")], "store_ids": request.args.getlist("store_ids"), "ids": request.args.getlist("ids"), "stream_sources": [s.lower() for s in request.args.getlist("stream_sources")], "transfer_product_ownership_enabled": is_insights_transfer_product_ownership_enabled(), "line_soundcloud_collection_as_active_enabled": is_insights_line_soundcloud_collection_as_active_enabled(), } permissions = get_permission_values() timeseries = get_sound_recording_timeseries(query_params, permissions) response_body = {"items": timeseries} return jsonify(response_body), 200 @app.route(config.SOUND_RECORDING_SUMMARY_PATH) @verify_profile(access=ACCESS_ANALYTICS) def sound_recording_summary(isrc): """Return summary for a sound recording. Route: GET /sound-recording//summary Path params: isrc (str): ISRC identifier. Query params: start_date (str): Start date YYYY-MM-DD. end_date (str): End date YYYY-MM-DD. type (str): Summary type. Values: TOTAL, COUNTRY, SOS, STORE, SOUND_RECORDING, PRODUCT. order_by (str): Sort field. order_dir (str): Sort direction. countries (list[str]): Country codes to filter by. distributors (list[str]): Distributor names. store_ids (list[int]): Store IDs to filter by. ids (list[str]): IDs to filter aggregation by. stream_sources (list[str]): Source of streams filter. Snowflake tables: - V_STREAMS_BY_TRACK_FEED_DISTRIBUTOR_DAILY - V_STREAMS_BY_TRACK_COUNTRY_FEED_DISTRIBUTOR_DAILY - DOWNLOADS_BY_TRACK_FEED_DISTRIBUTOR_DAILY - DOWNLOADS_BY_TRACK_COUNTRY_FEED_DISTRIBUTOR_DAILY JOINs: - DIM_RELEASE Returns: JSON with summary items and total count. """ path_params = {"isrc": isrc} query_params = _get_query_string_params( [ "start_date", "end_date", "type", "order_by", "order_dir", ], path_params, ) query_params = { **query_params, "countries": request.args.getlist("countries"), "distributors": [d.lower() for d in request.args.getlist("distributors")], "store_ids": request.args.getlist("store_ids"), "summary": True, "ids": request.args.getlist("ids"), "stream_sources": [s.lower() for s in request.args.getlist("stream_sources")], "transfer_product_ownership_enabled": is_insights_transfer_product_ownership_enabled(), "line_soundcloud_collection_as_active_enabled": is_insights_line_soundcloud_collection_as_active_enabled(), } permissions = get_permission_values() summary = get_sound_recording_summary(query_params, permissions) response_body = {"items": summary["items"], "total_count": summary["total_count"]} return jsonify(response_body), 200 @app.route(config.SOUND_RECORDING_RELATED_VIDEOS_BY_ISRC_PATH) @verify_profile(access=ACCESS_ANALYTICS) def sound_recording_related_videos_by_isrc(isrc): """Return related video IDs for a sound recording. Route: GET /sound-recording//related-videos Path params: isrc (str): ISRC identifier. Snowflake tables: - SOUNDRECORDING_TO_RELATED_VIDEOS_PRIMARY_AUDIO_PRODUCT_V3_HYBRID JOINs: - YOUTUBE_VIDEO Returns: JSON with list of related video IDs. """ path_params = {"isrc": isrc} permissions = get_permission_values() video_ids = get_sound_recording_related_videos_by_isrc(path_params, permissions) response_body = {"video_ids": video_ids} return jsonify(response_body), 200