Skip to content

Commit

Permalink
Search Tags 2 (#40)
Browse files Browse the repository at this point in the history
* Return saved tracks and followed users a la full search for tag search
  • Loading branch information
hareeshnagaraj authored Sep 11, 2019
1 parent a5a870c commit 7cfaa39
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 13 deletions.
15 changes: 11 additions & 4 deletions discovery-provider/src/queries/query_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ def get_followee_count_dict(session, user_ids):
def get_track_play_counts(track_ids):
identity_url = shared_config['discprov']['identity_service_url']
querystring = {}
track_listen_counts = {}
key_str = "id[{}]"
index = 0

Expand All @@ -536,18 +537,24 @@ def get_track_play_counts(track_ids):

# Create and query identity service endpoint
identity_tracks_endpoint = urljoin(identity_url, 'tracks/listens')
resp = requests.get(identity_tracks_endpoint, params=querystring)
try:
resp = requests.get(identity_tracks_endpoint, params=querystring)
except Exception as e:
logger.error(f'Error retrieving play count - {identity_tracks_endpoint}, {querystring}')
return track_listen_counts

json_resp = resp.json()
keys = list(resp.json().keys())
if not keys:
return track_listen_counts

# Scenario should never arise, since we don't impose date parameter on initial query
if keys and len(keys) != 1:
# Scenario should never arise, since we don't impose date parameter on initial query
if len(keys) != 1:
raise Exception('Invalid number of keys')

# Parse listen query results into track listen count dictionary
date_key = keys[0]
listen_count_json = json_resp[date_key]
track_listen_counts = {}
if 'listenCounts' in listen_count_json:
for listen_info in listen_count_json['listenCounts']:
current_id = listen_info['trackId']
Expand Down
106 changes: 97 additions & 9 deletions discovery-provider/src/queries/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask import Blueprint, request

from src import api_helpers, exceptions
from src.models import User, Track, RepostType, Playlist, SaveType
from src.models import User, Track, RepostType, Playlist, Save, SaveType, Follow
from src.utils import helpers
from src.utils.config import shared_config
from src.utils.db_session import get_db
Expand Down Expand Up @@ -45,8 +45,8 @@ def search_autocomplete():

@bp.route("/search/tags", methods=("GET",))
def search_tags():
logger.warning('search tags working')
search_str = request.args.get("query", type=str)
current_user_id = get_current_user_id(required=False)
if not search_str:
raise exceptions.ArgumentError("Invalid value for parameter 'query'")

Expand Down Expand Up @@ -108,7 +108,7 @@ def search_tags():
count desc
) as usr
where
usr.count > :user_tag_count;
usr.count >= :user_tag_count;
"""
)
track_ids = session.execute(
Expand All @@ -133,8 +133,6 @@ def search_tags():
# user_ids is list of tuples - simplify to 1-D list
user_ids = [i[1] for i in user_ids]

followee_count_dict = get_followee_count_dict(session, user_ids)

tracks = (
session.query(Track)
.filter(
Expand All @@ -144,6 +142,7 @@ def search_tags():
)
.all()
)

tracks = helpers.query_result_to_list(tracks)
track_play_counts = get_track_play_counts(track_ids)
users = (
Expand All @@ -156,9 +155,10 @@ def search_tags():
.all()
)
users = helpers.query_result_to_list(users)
for user in users:
user_id = user["user_id"]
user[response_name_constants.follower_count] = followee_count_dict.get(user_id, 0)

with db.scoped_session() as session:
tracks = populate_track_metadata(session, track_ids, tracks, current_user_id)
users = populate_user_metadata(session, user_ids, users, current_user_id)

followee_sorted_users = \
sorted(users, key=lambda i: i[response_name_constants.follower_count], reverse=True)
Expand All @@ -177,9 +177,97 @@ def search_tags():
followee_sorted_users = \
followee_sorted_users[slice(offset, offset + limit, 1)]

resp = {}
resp = {
'tracks': [],
'users': [],
'saved_tracks': [],
'followed_users': []
}
resp['tracks'] = play_count_sorted_tracks
resp['users'] = followee_sorted_users

# Add personalized results for a given user
if current_user_id:
# Query saved tracks for the current user that contain this tag
saves_query = (
session.query(Save.save_item_id)
.filter(
Save.is_current == True,
Save.is_delete == False,
Save.save_type == SaveType.track,
Save.user_id == current_user_id,
Save.save_item_id.in_(track_ids)
)
.all()
)
saved_track_ids = [i[0] for i in saves_query]
saved_tracks = (
session.query(Track)
.filter(
Track.is_current == True,
Track.is_delete == False,
Track.track_id.in_(saved_track_ids),
)
.all()
)
saved_tracks = helpers.query_result_to_list(saved_tracks)
for saved_track in saved_tracks:
saved_track_id = saved_track["track_id"]
saved_track[response_name_constants.play_count] = \
track_play_counts.get(saved_track_id, 0)

# Query followed users that have referenced this tag
followed_user_query = (
session.query(Follow.followee_user_id)
.filter(
Follow.is_current == True,
Follow.is_delete == False,
Follow.follower_user_id == current_user_id,
Follow.followee_user_id.in_(user_ids)
)
.all()
)
followed_user_ids = [i[0] for i in followed_user_query]
followed_users = (
session.query(User)
.filter(
User.is_current == True,
User.is_ready == True,
User.user_id.in_(followed_user_ids)
)
.all()
)
followed_users = helpers.query_result_to_list(followed_users)
with db.scoped_session() as session:
saved_tracks = \
populate_track_metadata(session, saved_track_ids, saved_tracks, current_user_id)
followed_users = \
populate_user_metadata(
session,
followed_user_ids,
followed_users,
current_user_id
)

# Sort and paginate
play_count_sorted_saved_tracks = \
sorted(saved_tracks, key=lambda i: i[response_name_constants.play_count], reverse=True)

play_count_sorted_saved_tracks = \
play_count_sorted_saved_tracks[slice(offset, offset + limit, 1)]

followed_users_followee_sorted = \
sorted(
followed_users,
key=lambda i: i[response_name_constants.follower_count],
reverse=True)

followed_users_followee_sorted = \
followed_users_followee_sorted[slice(offset, offset + limit, 1)]

resp['saved_tracks'] = play_count_sorted_saved_tracks
resp['followed_users'] = followed_users_followee_sorted

return api_helpers.success_response(resp)

# SEARCH QUERIES
Expand Down

0 comments on commit 7cfaa39

Please sign in to comment.