-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AUD-741] Add query and endpoint for remixable tracks and update libs…
… to expose the new endpoint (#1633) * Add query and endpoint for remixable tracks and update libs to expose the new endpoint * Update params and remove libs code * Remove null import * Add remixable endpoint to full namespace * Update decayed_score helper function
- Loading branch information
1 parent
852e682
commit e329ed9
Showing
3 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from sqlalchemy import desc | ||
from src.models import Track, Stem | ||
from src.queries.query_helpers import create_save_repost_count_subquery, \ | ||
populate_track_metadata, add_users_to_tracks, decayed_score | ||
from src.utils.db_session import get_db_read_replica | ||
from src.utils import helpers | ||
|
||
def get_remixable_tracks(args): | ||
"""Gets a list of remixable tracks""" | ||
db = get_db_read_replica() | ||
limit = args.get("limit", 25) | ||
current_user_id = args.get("current_user_id", None) | ||
|
||
with db.scoped_session() as session: | ||
# Subquery to get current tracks that have stems | ||
remixable_tracks_subquery = ( | ||
session.query(Track) | ||
.join(Stem, Stem.parent_track_id == Track.track_id) | ||
.filter( | ||
Track.is_current == True, | ||
Track.is_unlisted == False, | ||
Track.is_delete == False | ||
) | ||
.subquery() | ||
) | ||
|
||
count_subquery = create_save_repost_count_subquery(session, "track") | ||
|
||
query = ( | ||
session.query( | ||
remixable_tracks_subquery, | ||
count_subquery.c["count"], | ||
decayed_score( | ||
count_subquery.c["count"], | ||
remixable_tracks_subquery.c.created_at | ||
).label("score") | ||
) | ||
.select_from(remixable_tracks_subquery) | ||
.join( | ||
count_subquery, | ||
count_subquery.c["id"] == remixable_tracks_subquery.c.track_id | ||
) | ||
.order_by( | ||
desc("score"), | ||
desc(remixable_tracks_subquery.c.track_id) | ||
) | ||
.limit(limit) | ||
) | ||
|
||
tracks = helpers.query_result_to_list(query.all()) | ||
track_ids = list(map(lambda track: track["track_id"], tracks)) | ||
|
||
# Get user specific data for tracks | ||
tracks = populate_track_metadata(session, track_ids, tracks, current_user_id) | ||
|
||
if args.get("with_users", False): | ||
add_users_to_tracks(session, tracks, current_user_id) | ||
|
||
return tracks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters