"""Utils for analytic models.""" import hashlib import json def get_cache_key(params=None): """Generate key for cached cohorts query result. The params used to construct the key should be the same dictionary used to construct our SQL for execution. Args: params (dict): should have the following keys: labelid (int) - labelid from dim_label table subaccountid (int) - subaccountid from dim_subaccount table isrcs (list[str]) - list of track isrc values store_ids (list[int]) - list of store_id from dim_store table feed_ids (list[int]) - list of available feeds fetched from ows-analytics/feeds artist_ids (list[int]) - list of artist ids territory_code (str) - territory code region_code (str) - region code start_date (str) - start of query range end_date (str) - end of query range distributors (list): list of distributors names Returns: string: Key for cache record identifier """ for k, v in params.items(): if(isinstance(v, list)): params[k] = sorted(v) if(isinstance(v, set)): params[k] = sorted(list(v)) return hashlib.sha256( json.dumps(params, sort_keys=True).encode() ).hexdigest() def playlists_with_status(playlists, status_code): """Attach HTTP status code for each playlist. Args: playlists (list): List of playlists. status_code (int): HTTP code of failure. Returns: list: List of playlists with HTTP status code. """ return [{ 'playlist_link': playlist['playlist_link'], 'status_code': status_code} for playlist in playlists]