from app.utils import assert_close_enough NOT_REQUIRED_STORE_IDS = {1600, 213, 348, 496, 1602, 1601, 1503, 4, 708, 1603, 1505} def get_trend_by_store_variables(isrc, store_id): return { "isrc": isrc, "distributors": "theorchard,awal,sme", "orderBy": "streams7Days", "topSize": 5, "daysBack": -28, "storeIds": [store_id], "countries": [], "topCountrySize": 10, "topStoreSize": 10, } def get_sos_variables(isrc, store_ids): return {"isrc": isrc, "storeIds": store_ids, "countries": []} def _get_sos_value(sos_data, field, period_index=None): """Extract SOS value safely, depending on whether it's period-based or total.""" if not sos_data: return 0 if period_index is None: return sos_data[0].get(field, 0) return sos_data[0]["streamsGrowthPeriods"][period_index].get(field, 0) def _fetch_sos_data(graphql, isrc, store_id): """Fetch SoS data for a given ISRC and store.""" response = graphql.fetch_data( "GetGlobalSoundRecordingDetailedSoS", get_sos_variables(isrc, [store_id]) ) return response["globalSoundRecordingByIsrc"]["analytics"][ "aggregatedStreamsBySosV2" ]["byDimension"]["topNAggregates"] def _get_analytics_value(analytics, dimension, field, period_index=None): """Extracts a metric (streams, growth, etc.) from analytics.""" total = analytics[f"aggregatedStreamsBy{dimension}"]["byDimension"]["total"] if period_index is None: return total[field] return total["streamsGrowthPeriods"][period_index][field] def _assert_dimension_consistency(values, reference_id, field): sos = values.get("Sos") for dim in ("Country", "Store"): assert_close_enough( sos, values.get(dim), reference_id, dim, field, ) def _check_consistency(graphql, record_store, store_id, field, label, period_index=None): """Generic helper for stream/growth consistency tests.""" isrc = record_store["isrc"] trend_response = graphql.fetch_data( "GetGlobalSoundRecordingStreamingTrendGO", get_trend_by_store_variables(isrc, store_id), ) analytics = trend_response["globalSoundRecordingByIsrc"]["analytics"] values = {} if store_id not in NOT_REQUIRED_STORE_IDS: sos_data = _fetch_sos_data(graphql, isrc, store_id) values["Sos"] = _get_sos_value(sos_data, field, period_index) for dim in ["Country", "Store"]: values[dim] = _get_analytics_value(analytics, dim, field, period_index) else: for dim in ["Country", "Store", "Sos"]: values[dim] = _get_analytics_value(analytics, dim, field, period_index) _assert_dimension_consistency(values, f"Song:{isrc}", field) # === Actual test wrappers === def check_1d_streams_consistency_by_store(graphql, record_store, store_id): _check_consistency(graphql, record_store, store_id, "streams1Day", "1-day streams") def check_1d_growth_percentage_consistency_by_store(graphql, record_store, store_id): _check_consistency( graphql, record_store, store_id, "streamsGrowthPercentage", "1-day growth %", period_index=0, ) def check_7d_streams_consistency_by_store(graphql, record_store, store_id): _check_consistency(graphql, record_store, store_id, "streams7Days", "7-day streams") def check_7d_growth_percentage_consistency_by_store(graphql, record_store, store_id): _check_consistency( graphql, record_store, store_id, "streamsGrowthPercentage", "7-day growth %", period_index=1, ) def check_all_time_streams_consistency_by_store(graphql, record_store, store_id): _check_consistency(graphql, record_store, store_id, "streamsAllTime", "All time streams")