Ejemplo n.º 1
0
def get_time_trending(cache_args, time, limit):
    time_params = {**cache_args, 'time':time}
    time_cache_key = extract_key(request_cache_path, time_params.items())
    time_trending = use_redis_cache(time_cache_key, TRENDING_TTL_SEC, lambda: get_trending(time_params))
    time_trending_track_ids = [{"track_id": track['track_id']} for track in time_trending]
    time_trending_track_ids = time_trending_track_ids[:limit]
    return time_trending_track_ids
Ejemplo n.º 2
0
 def get_cache_key(self):
     """Construct a cache key from genre + user + time"""
     request_items = to_dict(request.args)
     request_items.pop('limit', None)
     request_items.pop('offset', None)
     key = extract_key(request.path, request_items.items())
     return key
Ejemplo n.º 3
0
def make_cache_key(args):
    cache_keys = {
        "limit": args.get("limit"),
        "offset": args.get("offset"),
        "track_id": args.get("track_id"),
    }
    return extract_key(f"unpopulated-remix-parents:{request.path}", cache_keys.items())
Ejemplo n.º 4
0
def make_cache_key(args):
    ids = map(lambda x: str(x["id"]), args.get("identifiers"))
    ids = ",".join(ids)
    cache_keys = {
        "ids": ids,
        "filter_deleted": args.get("filter_deleted"),
        "with_users": args.get("with_user"),
    }
    key = extract_key(f"unpopulated-tracks:{request.path}", cache_keys.items())
    return key
Ejemplo n.º 5
0
def get_trending_tracks(args):
    limit, offset, current_user_id = args.get("limit"), args.get(
        "offset"), args.get("current_user_id")

    db = get_db_read_replica()

    time = args.get('time')
    query_time = None if time not in ["day", "week", "month", "year"] else time

    with db.scoped_session() as session:

        def get_unpopulated_trending():
            trending_tracks = generate_trending(session, query_time,
                                                args.get('genre', None), limit,
                                                offset)

            track_scores = [
                z(time, track) for track in trending_tracks['listen_counts']
            ]
            sorted_track_scores = sorted(track_scores,
                                         key=lambda k: k['score'],
                                         reverse=True)

            track_ids = [track['track_id'] for track in sorted_track_scores]

            tracks = get_unpopulated_tracks(session, track_ids)
            return (tracks, track_ids)

        # get scored trending tracks, either
        # through cached redis value, or through `get_unpopulated_trending`
        cache_keys = {"genre": args.get("genre"), "time": args.get("time")}
        key = extract_key(f"generated-trending:{request.path}",
                          cache_keys.items())
        (tracks, track_ids) = use_redis_cache(key, SCORES_CACHE_DURATION_SEC,
                                              get_unpopulated_trending)

        # populate track metadata
        tracks = populate_track_metadata(session, track_ids, tracks,
                                         current_user_id)
        tracks_map = {track['track_id']: track for track in tracks}

        # Re-sort the populated tracks b/c it loses sort order in sql query
        sorted_tracks = [tracks_map[track_id] for track_id in track_ids]

        if args.get("with_users", False):
            user_id_list = get_users_ids(sorted_tracks)
            users = get_users_by_id(session, user_id_list, current_user_id)
            for track in sorted_tracks:
                user = users[track['owner_id']]
                if user:
                    track['user'] = user
        return sorted_tracks
Ejemplo n.º 6
0
def make_cache_key(args):
    cache_keys = {
        "user_id": args.get("user_id"),
        "with_users": args.get("with_users")
    }

    if args.get("playlist_id"):
        ids = args.get("playlist_id")
        ids = map(str, ids)
        ids = ",".join(ids)
        cache_keys["playlist_id"] = ids

    key = extract_key(f"unpopulated-playlist:{request.path}",
                      cache_keys.items())
    return key
def get_time_trending(cache_args, time, limit, strategy):
    time_params = {**cache_args, "time": time}

    path = request_cache_path
    if strategy.version != DEFAULT_TRENDING_VERSIONS[TrendingType.TRACKS]:
        path += f"/{strategy.version.value}"

    time_cache_key = extract_key(path, time_params.items())
    time_trending = use_redis_cache(
        time_cache_key, TRENDING_TTL_SEC,
        lambda: get_trending(time_params, strategy))
    time_trending_track_ids = [{
        "track_id": track["track_id"]
    } for track in time_trending]
    time_trending_track_ids = time_trending_track_ids[:limit]
    return time_trending_track_ids