def update_trending_cache(self, db, redis, time):
    resp = generate_trending(db, time, None, 1000, 0)
    resp_json = json.dumps(resp)
    redis_key = f"trending-{time}"
    # Cache value for 5 minutes
    redis.set(redis_key, resp_json, 300)
    logger.info(f"index_cache.py | Updated trending cache {redis_key}")
Exemple #2
0
def trending(time):
    (limit, offset) = get_pagination_vars()
    # Increment total trending count
    REDIS.incr(trending_cache_total_key, 1)

    genre = request.args.get("genre", default=None, type=str)
    if genre is None:
        redis_key = f"trending-{time}"
        redis_cache_value = REDIS.get(redis_key)
        if redis_cache_value is not None:
            json_cache = json.loads(redis_cache_value.decode('utf-8'))
            if json_cache is not None:
                num_cached_entries = len(json_cache['listen_counts'])
                logger.info(
                    f'Cache for {redis_key}, {num_cached_entries} entries, request limit {limit}'
                )
                if offset + limit <= num_cached_entries:
                    json_cache['listen_counts'] = json_cache['listen_counts'][
                        offset:offset + limit]
                    logger.info(f'Returning cache for {redis_key}')
                    # Increment cache hit count
                    REDIS.incr(trending_cache_hits_key, 1)
                    return api_helpers.success_response(json_cache)
    # Increment cache miss count
    REDIS.incr(trending_cache_miss_key, 1)
    # Recalculate trending values if necessary
    final_resp = generate_trending(get_db_read_replica(), time, genre, limit,
                                   offset)
    return api_helpers.success_response(final_resp)
def generate_unpopulated_trending(session, genre, time_range):
    trending_tracks = generate_trending(session, time_range, genre,
                                        TRENDING_LIMIT, 0)

    track_scores = [
        z(time_range, 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)
Exemple #4
0
        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)
def get_trending_tracks(args):
    (limit, offset) = get_pagination_vars()
    current_user_id = get_current_user_id(required=False)

    db = get_db_read_replica()

    time = args.get('time')
    # Identity understands allTime as millennium.
    # TODO: Change this in https://github.com/AudiusProject/audius-protocol/pull/768/files
    query_time = time
    if time == 'allTime':
        query_time = 'millennium'

    with db.scoped_session() as session:
        trending_tracks = generate_trending(get_db_read_replica(), 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 = session.query(Track).filter(
            Track.is_current == True, Track.is_unlisted == False,
            Track.stem_of == None, Track.track_id.in_(track_ids)).all()
        tracks = helpers.query_result_to_list(tracks)

        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)
            for track in sorted_tracks:
                user = users[track['owner_id']]
                if user:
                    track['user'] = user
        return sorted_tracks
def generate_unpopulated_trending(session,
                                  genre,
                                  time_range,
                                  strategy,
                                  limit=TRENDING_LIMIT):
    trending_tracks = generate_trending(session, time_range, genre, limit, 0,
                                        strategy)

    track_scores = [
        strategy.get_track_score(time_range, track)
        for track in trending_tracks["listen_counts"]
    ]
    # Re apply the limit just in case we did decide to include more tracks in the scoring than the limit
    sorted_track_scores = sorted(track_scores,
                                 key=lambda k: (k["score"], k["track_id"]),
                                 reverse=True)[:limit]
    track_ids = [track["track_id"] for track in sorted_track_scores]

    tracks = get_unpopulated_tracks(session, track_ids)
    return (tracks, track_ids)
def get_trending_tracks(args):
    (limit, offset) = get_pagination_vars()
    current_user_id = get_current_user_id(required=False)

    db = get_db_read_replica()
    time = args.get('time')
    with db.scoped_session() as session:
        trending_tracks = generate_trending(get_db_read_replica(), 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 = session.query(Track).filter(
            Track.is_current == True, Track.is_unlisted == False,
            Track.stem_of == None, Track.track_id.in_(track_ids)).all()
        tracks = helpers.query_result_to_list(tracks)

        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)
            for track in sorted_tracks:
                user = users[track['owner_id']]
                if user:
                    track['user'] = user
        return sorted_tracks